-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/11.x.x' into sync-automated-tran…
…slations-with-lts-branch * origin/11.x.x: feat(components/toast): add toast and toaster harness (#3141)
- Loading branch information
Showing
14 changed files
with
397 additions
and
8 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
apps/code-examples/src/app/code-examples/toast/toast/basic/demo.component.spec.ts
This file contains 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,58 @@ | ||
import { HarnessLoader } from '@angular/cdk/testing'; | ||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { SkyToastType } from '@skyux/toast'; | ||
import { SkyToasterHarness } from '@skyux/toast/testing'; | ||
|
||
import { DemoComponent } from './demo.component'; | ||
|
||
describe('Basic toast demo', () => { | ||
let fixture: ComponentFixture<DemoComponent>; | ||
let loader: HarnessLoader; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [DemoComponent, NoopAnimationsModule], | ||
}); | ||
|
||
fixture = TestBed.createComponent(DemoComponent); | ||
loader = TestbedHarnessEnvironment.documentRootLoader(fixture); | ||
}); | ||
|
||
async function setupTest(): Promise<{ | ||
toasterHarness: SkyToasterHarness; | ||
}> { | ||
fixture.componentInstance.openToast(); | ||
fixture.detectChanges(); | ||
|
||
const toasterHarness: SkyToasterHarness = | ||
await loader.getHarness(SkyToasterHarness); | ||
|
||
return { toasterHarness }; | ||
} | ||
|
||
it('should open success toasts', async () => { | ||
const { toasterHarness } = await setupTest(); | ||
|
||
fixture.componentInstance.openToast(); | ||
fixture.detectChanges(); | ||
|
||
await expectAsync(toasterHarness.getNumberOfToasts()).toBeResolvedTo(2); | ||
|
||
const toast = await toasterHarness.getToastByMessage( | ||
'This is a sample toast message.', | ||
); | ||
|
||
await expectAsync(toast.getType()).toBeResolvedTo(SkyToastType.Success); | ||
}); | ||
|
||
it('should close all toasts', async () => { | ||
fixture.componentInstance.openToast(); | ||
fixture.detectChanges(); | ||
fixture.componentInstance.closeAll(); | ||
fixture.detectChanges(); | ||
|
||
await expectAsync(loader.getHarness(SkyToasterHarness)).toBeRejected(); | ||
}); | ||
}); |
This file contains 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
9 changes: 9 additions & 0 deletions
9
.../code-examples/src/app/code-examples/toast/toast/custom-component/custom-toast-harness.ts
This file contains 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,9 @@ | ||
import { ComponentHarness } from '@angular/cdk/testing'; | ||
|
||
export class CustomToastHarness extends ComponentHarness { | ||
public static hostSelector = 'app-toast-content-demo'; | ||
|
||
public async getText(): Promise<string> { | ||
return await (await this.host()).text(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
apps/code-examples/src/app/code-examples/toast/toast/custom-component/demo.component.spec.ts
This file contains 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,56 @@ | ||
import { HarnessLoader } from '@angular/cdk/testing'; | ||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { SkyToastType } from '@skyux/toast'; | ||
import { SkyToasterHarness } from '@skyux/toast/testing'; | ||
|
||
import { CustomToastHarness } from './custom-toast-harness'; | ||
import { DemoComponent } from './demo.component'; | ||
|
||
describe('Custom component toast demo', () => { | ||
let fixture: ComponentFixture<DemoComponent>; | ||
let loader: HarnessLoader; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [DemoComponent, NoopAnimationsModule], | ||
}); | ||
|
||
fixture = TestBed.createComponent(DemoComponent); | ||
loader = TestbedHarnessEnvironment.documentRootLoader(fixture); | ||
}); | ||
|
||
async function setupTest(): Promise<{ | ||
toasterHarness: SkyToasterHarness; | ||
}> { | ||
fixture.componentInstance.openToast(); | ||
fixture.detectChanges(); | ||
|
||
const toasterHarness: SkyToasterHarness = | ||
await loader.getHarness(SkyToasterHarness); | ||
|
||
return { toasterHarness }; | ||
} | ||
|
||
it('should open custom component in toast', async () => { | ||
const { toasterHarness } = await setupTest(); | ||
|
||
const toasts = await toasterHarness.getToasts(); | ||
const customHarness = await toasts[0].queryHarness(CustomToastHarness); | ||
|
||
await expectAsync(toasts[0].getType()).toBeResolvedTo(SkyToastType.Success); | ||
await expectAsync(customHarness.getText()).toBeResolvedTo( | ||
'Custom message: This toast has embedded a custom component for its content.', | ||
); | ||
}); | ||
|
||
it('should close all toasts', async () => { | ||
fixture.componentInstance.openToast(); | ||
fixture.detectChanges(); | ||
fixture.componentInstance.closeAll(); | ||
fixture.detectChanges(); | ||
|
||
await expectAsync(loader.getHarness(SkyToasterHarness)).toBeRejected(); | ||
}); | ||
}); |
This file contains 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
4 changes: 2 additions & 2 deletions
4
libs/components/core/testing/src/modules/overlay/overlay-harness-filters.ts
This file contains 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { SkyHarnessFilters } from '../../shared/harness-filters'; | ||
import { BaseHarnessFilters } from '@angular/cdk/testing'; | ||
|
||
/** | ||
* A set of criteria that can be used to filter a list of SkyOverlayHarness instances. | ||
* @internal | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-empty-object-type | ||
export interface SkyOverlayHarnessFilters extends SkyHarnessFilters {} | ||
export interface SkyOverlayHarnessFilters extends BaseHarnessFilters {} |
This file contains 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 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 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
12 changes: 12 additions & 0 deletions
12
libs/components/toast/testing/src/modules/toast-harness-filters.ts
This file contains 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,12 @@ | ||
import { BaseHarnessFilters } from '@angular/cdk/testing'; | ||
|
||
/** | ||
* A set of criteria that can be used to filter a list of `SkyToastHarness` instances. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-empty-object-type | ||
export interface SkyToastHarnessFilters extends BaseHarnessFilters { | ||
/** | ||
* Finds toasts with the matching text. | ||
*/ | ||
message?: string; | ||
} |
72 changes: 72 additions & 0 deletions
72
libs/components/toast/testing/src/modules/toast-harness.ts
This file contains 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,72 @@ | ||
import { HarnessPredicate } from '@angular/cdk/testing'; | ||
import { SkyQueryableComponentHarness } from '@skyux/core/testing'; | ||
import { SkyToastType } from '@skyux/toast'; | ||
|
||
import { SkyToastHarnessFilters } from './toast-harness-filters'; | ||
|
||
/** | ||
* Harness for interacting with the toast component in tests. | ||
*/ | ||
export class SkyToastHarness extends SkyQueryableComponentHarness { | ||
/** | ||
* @internal | ||
*/ | ||
public static hostSelector = 'sky-toast'; | ||
|
||
#getToast = this.locatorFor('.sky-toast'); | ||
|
||
/** | ||
* Gets a `HarnessPredicate` that can be used to search for a | ||
* `SkyToastHarness` that meets certain criteria. | ||
*/ | ||
public static with( | ||
filters: SkyToastHarnessFilters, | ||
): HarnessPredicate<SkyToastHarness> { | ||
return new HarnessPredicate(SkyToastHarness, filters).addOption( | ||
'message', | ||
filters.message, | ||
async (harness, message) => { | ||
const harnessMessage = await harness.getMessage(); | ||
return await HarnessPredicate.stringMatches(message, harnessMessage); | ||
}, | ||
); | ||
} | ||
|
||
/** | ||
* Clicks the toast close button. | ||
*/ | ||
public async close(): Promise<void> { | ||
const button = await this.locatorFor('.sky-toast-btn-close')(); | ||
return await button.click(); | ||
} | ||
|
||
/** | ||
* Gets the toast message. | ||
*/ | ||
public async getMessage(): Promise<string> { | ||
const toastBody = await this.locatorForOptional('.sky-toast-body')(); | ||
|
||
if (toastBody) { | ||
return (await toastBody.text()).trim(); | ||
} else { | ||
throw new Error( | ||
'No toast message found. This method cannot be used to query toasts with custom components.', | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the toast type. | ||
*/ | ||
public async getType(): Promise<SkyToastType> { | ||
const toast = await this.#getToast(); | ||
if (await toast.hasClass('sky-toast-danger')) { | ||
return SkyToastType.Danger; | ||
} else if (await toast.hasClass('sky-toast-warning')) { | ||
return SkyToastType.Warning; | ||
} else if (await toast.hasClass('sky-toast-success')) { | ||
return SkyToastType.Success; | ||
} | ||
return SkyToastType.Info; | ||
} | ||
} |
Oops, something went wrong.