-
Notifications
You must be signed in to change notification settings - Fork 1
feat: ActionButton and DataSource #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2faeda0
feat: ActionButton and DataSource
Suneeh c0c419b
adds harness + tests for ActionButtonComponent
Suneeh e4011cb
Merge remote-tracking branch 'origin/master' into 19.2.3
Suneeh cef3380
review comments
Suneeh 6e76820
extend harness
Suneeh 016cab7
integrate icon and error into button
Suneeh aad8ed4
zv-action-button improvements:
saithis 8865cb6
remove unused css
saithis d3d3b0e
Fix review comments
saithis fad2a02
fix lint error
saithis f8ab8a2
Change version to minor and update release notes
saithis 1667177
fix usage code in demo
Suneeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // export what ./public_api exports so we can import with the lib name like this: | ||
| // import { ModuleA } from 'libname' | ||
| export * from './public_api'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "lib": { | ||
| "entryFile": "index.ts" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export { ZvActionButtonComponent, type IZvActionButton } from './src/action-button.component'; | ||
| export { | ||
| ZvActionButtonDataSource, | ||
| type IZvActionButtonDataSource, | ||
| type IZvActionButtonDataSourceOptions, | ||
| } from './src/action-button-data-source'; |
40 changes: 40 additions & 0 deletions
40
projects/components/action-button/src/action-button-data-source.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { computed, DestroyRef, effect, inject, linkedSignal, Signal } from '@angular/core'; | ||
| import { ZvActionDataSource, ZvActionDataSourceOptions, ZvExceptionMessageExtractor } from '@zvoove/components/core'; | ||
|
|
||
| export interface IZvActionButtonDataSource { | ||
| readonly showSuccess: Signal<boolean>; | ||
| readonly showLoading: Signal<boolean>; | ||
| readonly showError: Signal<boolean>; | ||
| readonly error: Signal<unknown>; | ||
| readonly errorMessage: Signal<string | null>; | ||
| readonly disabled: Signal<boolean>; | ||
| execute(): void; | ||
| } | ||
|
|
||
| export declare type IZvActionButtonDataSourceOptions = ZvActionDataSourceOptions; | ||
|
|
||
| export class ZvActionButtonDataSource extends ZvActionDataSource implements IZvActionButtonDataSource { | ||
| readonly #destroyRef = inject(DestroyRef); | ||
| readonly #errorMessageExtractor = inject(ZvExceptionMessageExtractor); | ||
| #timeoutRef: NodeJS.Timeout | undefined; | ||
|
|
||
| public readonly showLoading = this.isLoading; | ||
| public readonly showError = this.hasError; | ||
| public readonly disabled = this.isLoading; | ||
| public readonly showSuccess = linkedSignal(() => this.succeeded()); | ||
| public readonly errorMessage = computed(() => (this.hasError() ? this.#errorMessageExtractor.extractErrorMessage(this.error()) : null)); | ||
|
|
||
| constructor(options: IZvActionButtonDataSourceOptions) { | ||
| super(options); | ||
|
|
||
| effect(() => { | ||
| if (this.showSuccess()) { | ||
| clearTimeout(this.#timeoutRef); | ||
| this.#timeoutRef = setTimeout(() => { | ||
| this.showSuccess.set(false); | ||
| }, 2000); | ||
| } | ||
| }); | ||
| this.#destroyRef.onDestroy(() => clearTimeout(this.#timeoutRef)); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
projects/components/action-button/src/action-button.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <button | ||
| type="button" | ||
| [color]="color()" | ||
| mat-raised-button | ||
| (click)="dataSource().execute()" | ||
| [disabled]="dataSource().disabled() || disabled()" | ||
| [matTooltip]="dataSource().errorMessage()" | ||
| > | ||
Suneeh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @if (dataSource().showError()) { | ||
| <mat-icon [color]="'warn'">error</mat-icon> | ||
| } @else if (dataSource().showLoading()) { | ||
| <mat-icon><mat-spinner [diameter]="18" /></mat-icon> | ||
| } @else if (dataSource().showSuccess()) { | ||
| <mat-icon [color]="'success'">check_circle</mat-icon> | ||
| } @else if (icon()) { | ||
| <mat-icon>{{ icon() }}</mat-icon> | ||
| } | ||
| <ng-content /> | ||
| </button> | ||
122 changes: 122 additions & 0 deletions
122
projects/components/action-button/src/action-button.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { HarnessLoader } from '@angular/cdk/testing'; | ||
| import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
| import { ChangeDetectionStrategy, Component, signal } from '@angular/core'; | ||
| import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
| import { tap, timer } from 'rxjs'; | ||
| import { ZvActionButtonDataSource } from './action-button-data-source'; | ||
| import { ZvActionButtonComponent } from './action-button.component'; | ||
| import { ZvActionButtonHarness } from './action-button.harness'; | ||
| import { ZvButtonColors } from '@zvoove/components/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'zv-test-component', | ||
| template: ` | ||
| <zv-action-button [dataSource]="dataSource" [color]="color()" [icon]="'home'" [disabled]="isDisabled()"> label </zv-action-button> | ||
| `, | ||
| // eslint-disable-next-line @angular-eslint/prefer-on-push-component-change-detection | ||
| changeDetection: ChangeDetectionStrategy.Default, | ||
| imports: [ZvActionButtonComponent], | ||
| }) | ||
| export class TestComponent { | ||
| public readonly isDisabled = signal<boolean>(false); | ||
| public readonly color = signal<ZvButtonColors | null>('primary'); | ||
| public actionFnCalled = false; | ||
| public throwError: Error | null = null; | ||
| dataSource = new ZvActionButtonDataSource({ | ||
| actionFn: () => | ||
| timer(100).pipe( | ||
| tap(() => { | ||
| this.actionFnCalled = true; | ||
| if (this.throwError) { | ||
| throw this.throwError; | ||
| } | ||
| }) | ||
| ), | ||
| }); | ||
| } | ||
| describe('ZvActionButton', () => { | ||
| let fixture: ComponentFixture<TestComponent>; | ||
| let component: TestComponent; | ||
| let loader: HarnessLoader; | ||
| let harness: ZvActionButtonHarness; | ||
|
|
||
| beforeEach(async () => { | ||
| TestBed.configureTestingModule({ | ||
| imports: [TestComponent], | ||
| }).compileComponents(); | ||
|
|
||
| fixture = TestBed.createComponent(TestComponent); | ||
| component = fixture.componentInstance; | ||
| loader = TestbedHarnessEnvironment.loader(fixture); | ||
| harness = await loader.getHarness(ZvActionButtonHarness); | ||
| }); | ||
|
|
||
| it('should create', () => { | ||
| expect(component).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should call dataSource.execute() on click', async () => { | ||
| expect(component.actionFnCalled).toBeFalse(); | ||
| await harness.click(); | ||
| expect(component.actionFnCalled).toBeTrue(); | ||
| }); | ||
|
|
||
| it('should be blocked while loading', fakeAsync(async () => { | ||
| await harness.click(); | ||
| tick(1); | ||
| expect(component.dataSource.showLoading()).toBeTrue(); | ||
| expect(await harness.isLoading()).toBeTrue(); | ||
| })); | ||
|
|
||
| it('should be disabled while loading', fakeAsync(async () => { | ||
| await harness.click(); | ||
| tick(1); | ||
| expect(component.dataSource.showLoading()).toBeTrue(); | ||
| expect(await harness.isDisabled()).toBeTrue(); | ||
| })); | ||
|
|
||
| it('should respect disabled property', async () => { | ||
| expect(await harness.isDisabled()).toBeFalse(); | ||
| component.isDisabled.set(true); | ||
| expect(await harness.isDisabled()).toBeTrue(); | ||
| component.isDisabled.set(false); | ||
| expect(await harness.isDisabled()).toBeFalse(); | ||
| }); | ||
|
|
||
| it('should respect color property', async () => { | ||
| expect(await harness.hasClass('mat-primary')).toBeTrue(); | ||
| component.color.set('accent'); | ||
| expect(await harness.hasClass('mat-accent')).toBeTrue(); | ||
| component.color.set('warn'); | ||
| expect(await harness.hasClass('mat-warn')).toBeTrue(); | ||
| }); | ||
|
|
||
| it('should show icon', async () => { | ||
| expect(await harness.getIcon()).toBe('home'); | ||
| }); | ||
|
|
||
| it('should show label', async () => { | ||
| const buttonContent = await harness.getLabel(); | ||
| expect(buttonContent).toContain('label'); | ||
| }); | ||
|
|
||
| it('should show success icon for 2 seconds', fakeAsync(async () => { | ||
| await harness.click(); | ||
| tick(100); | ||
| expect(component.dataSource.showLoading()).toBeFalse(); | ||
| expect(component.dataSource.showSuccess()).toBeTrue(); | ||
| expect(await harness.getIcon()).toBe('check_circle'); | ||
|
|
||
| tick(2000); | ||
| expect(component.dataSource.showSuccess()).toBeFalse(); | ||
| expect(await harness.getIcon()).toBe('home'); | ||
| })); | ||
|
|
||
| it('should show error message', fakeAsync(async () => { | ||
| component.throwError = new Error('action failed'); | ||
| await harness.click(); | ||
| tick(100); | ||
| expect(await harness.getIcon()).toBe('error'); | ||
| expect(await harness.getErrorMessage()).toBe(component.throwError.message); | ||
| })); | ||
| }); |
39 changes: 39 additions & 0 deletions
39
projects/components/action-button/src/action-button.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { afterRenderEffect, ChangeDetectionStrategy, Component, input, Signal, viewChild } from '@angular/core'; | ||
| import { MatButtonModule } from '@angular/material/button'; | ||
| import { ThemePalette } from '@angular/material/core'; | ||
| import { MatIcon } from '@angular/material/icon'; | ||
| import { MatProgressSpinner } from '@angular/material/progress-spinner'; | ||
| import { MatTooltip } from '@angular/material/tooltip'; | ||
| import { ZvButtonColors } from '@zvoove/components/core'; | ||
| import { IZvActionButtonDataSource } from './action-button-data-source'; | ||
|
|
||
| export interface IZvActionButton { | ||
| label: string; | ||
| color: ThemePalette | null; | ||
| icon: string; | ||
| dataCy: string; | ||
| isDisabled?: Signal<boolean>; | ||
| } | ||
|
|
||
| @Component({ | ||
| selector: 'zv-action-button', | ||
| templateUrl: './action-button.component.html', | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| imports: [MatIcon, MatButtonModule, MatProgressSpinner, MatTooltip], | ||
| }) | ||
| export class ZvActionButtonComponent { | ||
| public readonly dataSource = input.required<IZvActionButtonDataSource>(); | ||
| public readonly icon = input<string | null>(null); | ||
| public readonly color = input<ZvButtonColors | null | undefined>(null); | ||
| public readonly disabled = input<boolean>(false); | ||
|
|
||
| private readonly _tooltip = viewChild(MatTooltip); | ||
|
|
||
| constructor() { | ||
| afterRenderEffect(() => { | ||
| if (this.dataSource().showError()) { | ||
| this._tooltip()?.show(0); | ||
| } | ||
| }); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
projects/components/action-button/src/action-button.harness.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { ComponentHarness } from '@angular/cdk/testing'; | ||
|
|
||
| export class ZvActionButtonHarness extends ComponentHarness { | ||
| static hostSelector = 'zv-action-button'; | ||
|
|
||
| private _button = this.locatorForOptional('button'); | ||
| private _buttonIcon = this.locatorForOptional('button mat-icon'); | ||
| private _loadingSpinner = this.locatorForOptional('button mat-spinner'); | ||
|
|
||
| public async click(): Promise<void> { | ||
| const button = await this._button(); | ||
| return await button?.click(); | ||
| } | ||
|
|
||
| public async getIcon(): Promise<string | undefined> { | ||
| return await (await this._buttonIcon())?.text(); | ||
| } | ||
|
|
||
| public async getLabel(): Promise<string | null> { | ||
| return (await (await this._button())?.text()) ?? null; | ||
| } | ||
|
|
||
| public async isLoading(): Promise<boolean> { | ||
| return !!(await this._loadingSpinner()); | ||
| } | ||
|
|
||
| public async isDisabled(): Promise<boolean | undefined> { | ||
| return await (await this._button())?.getProperty('disabled'); | ||
| } | ||
|
|
||
| public async hasClass(className: string): Promise<boolean> { | ||
| return (await (await this._button())?.hasClass(className)) ?? false; | ||
| } | ||
|
|
||
| public async getErrorMessage(): Promise<string> { | ||
| return (await (await this._button())?.getAttribute('ng-reflect-message')) ?? ''; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
projects/components/core/src/action-data-source/action-data-source.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
| import { switchMap, throwError, timer } from 'rxjs'; | ||
| import { ZvActionDataSource } from './action-data-source'; | ||
|
|
||
| describe('ActionDataSource', () => { | ||
| beforeAll(() => { | ||
| TestBed.configureTestingModule({}); | ||
| }); | ||
| it('should set properties correctly', fakeAsync(() => { | ||
| TestBed.runInInjectionContext(() => { | ||
| const dataSource = new ZvActionDataSource({ actionFn: () => timer(1) }); | ||
| expect(dataSource.error()).toBe(null); | ||
| expect(dataSource.isLoading()).toBe(false); | ||
| expect(dataSource.hasError()).toBe(false); | ||
| expect(dataSource.succeeded()).toBe(false); | ||
|
|
||
| dataSource.execute(); | ||
| expect(dataSource.error()).toBe(null); | ||
| expect(dataSource.isLoading()).toBe(true); | ||
| expect(dataSource.hasError()).toBe(false); | ||
| expect(dataSource.succeeded()).toBe(false); | ||
|
|
||
| tick(1); | ||
| expect(dataSource.error()).toBe(null); | ||
| expect(dataSource.isLoading()).toBe(false); | ||
| expect(dataSource.hasError()).toBe(false); | ||
| expect(dataSource.succeeded()).toBe(true); | ||
| }); | ||
| })); | ||
|
|
||
| it('should set error correctly', fakeAsync(() => { | ||
| TestBed.runInInjectionContext(() => { | ||
| const error = new Error('action failed'); | ||
| const dataSource = new ZvActionDataSource({ actionFn: () => timer(1).pipe(switchMap(() => throwError(() => error))) }); | ||
| expect(dataSource.error()).toBe(null); | ||
| expect(dataSource.isLoading()).toBe(false); | ||
| expect(dataSource.hasError()).toBe(false); | ||
| expect(dataSource.succeeded()).toBe(false); | ||
|
|
||
| dataSource.execute(); | ||
| expect(dataSource.error()).toBe(null); | ||
| expect(dataSource.isLoading()).toBe(true); | ||
| expect(dataSource.hasError()).toBe(false); | ||
| expect(dataSource.succeeded()).toBe(false); | ||
|
|
||
| tick(1); | ||
| expect(dataSource.error()).toBe(error); | ||
| expect(dataSource.isLoading()).toBe(false); | ||
| expect(dataSource.hasError()).toBe(true); | ||
| expect(dataSource.succeeded()).toBe(false); | ||
| }); | ||
| })); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.