|
| 1 | +import {HarnessLoader} from '@angular/cdk-experimental/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed'; |
| 3 | +import {Component, TemplateRef, ViewChild} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {MatSnackBar, MatSnackBarConfig, MatSnackBarModule} from '@angular/material/snack-bar'; |
| 6 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 7 | +import {MatSnackBarHarness} from './snack-bar-harness'; |
| 8 | + |
| 9 | +let fixture: ComponentFixture<SnackbarHarnessTest>; |
| 10 | +let loader: HarnessLoader; |
| 11 | +let snackBarHarness: typeof MatSnackBarHarness; |
| 12 | + |
| 13 | +describe('MatSnackBarHarness', () => { |
| 14 | + describe('non-MDC-based', () => { |
| 15 | + beforeEach(async () => { |
| 16 | + await TestBed |
| 17 | + .configureTestingModule({ |
| 18 | + imports: [MatSnackBarModule, NoopAnimationsModule], |
| 19 | + declarations: [SnackbarHarnessTest], |
| 20 | + }) |
| 21 | + .compileComponents(); |
| 22 | + |
| 23 | + fixture = TestBed.createComponent(SnackbarHarnessTest); |
| 24 | + fixture.detectChanges(); |
| 25 | + // Note: we need to specify "document.body" as root element because |
| 26 | + // the snack-bar's will be added as immediate children of the body. |
| 27 | + loader = new TestbedHarnessEnvironment(document.body, fixture); |
| 28 | + snackBarHarness = MatSnackBarHarness; |
| 29 | + }); |
| 30 | + |
| 31 | + runTests(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe( |
| 35 | + 'MDC-based', |
| 36 | + () => { |
| 37 | + // TODO: run tests for MDC based snack-bar once implemented. |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +/** Shared tests to run on both the original and MDC-based snack-bar's. */ |
| 42 | +function runTests() { |
| 43 | + it('should load harness for simple snack-bar', async () => { |
| 44 | + const snackBarRef = fixture.componentInstance.openSimple('Hello!', ''); |
| 45 | + let snackBars = await loader.getAllHarnesses(snackBarHarness); |
| 46 | + |
| 47 | + expect(snackBars.length).toBe(1); |
| 48 | + |
| 49 | + snackBarRef.dismiss(); |
| 50 | + snackBars = await loader.getAllHarnesses(snackBarHarness); |
| 51 | + expect(snackBars.length).toBe(0); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should load harness for custom snack-bar', async () => { |
| 55 | + const snackBarRef = fixture.componentInstance.openCustom(); |
| 56 | + let snackBars = await loader.getAllHarnesses(snackBarHarness); |
| 57 | + |
| 58 | + expect(snackBars.length).toBe(1); |
| 59 | + |
| 60 | + snackBarRef.dismiss(); |
| 61 | + snackBars = await loader.getAllHarnesses(snackBarHarness); |
| 62 | + expect(snackBars.length).toBe(0); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should be able to get role of snack-bar', async () => { |
| 66 | + fixture.componentInstance.openCustom(); |
| 67 | + let snackBar = await loader.getHarness(snackBarHarness); |
| 68 | + expect(await snackBar.getRole()).toBe('alert'); |
| 69 | + |
| 70 | + fixture.componentInstance.openCustom({politeness: 'polite'}); |
| 71 | + snackBar = await loader.getHarness(snackBarHarness); |
| 72 | + expect(await snackBar.getRole()).toBe('status'); |
| 73 | + |
| 74 | + fixture.componentInstance.openCustom({politeness: 'off'}); |
| 75 | + snackBar = await loader.getHarness(snackBarHarness); |
| 76 | + expect(await snackBar.getRole()).toBe(null); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should be able to get message of simple snack-bar', async () => { |
| 80 | + fixture.componentInstance.openSimple('Subscribed to newsletter.'); |
| 81 | + let snackBar = await loader.getHarness(snackBarHarness); |
| 82 | + expect(await snackBar.getMessage()).toBe('Subscribed to newsletter.'); |
| 83 | + |
| 84 | + // For snack-bar's with custom template, the message cannot be |
| 85 | + // retrieved. We expect an error to be thrown. |
| 86 | + fixture.componentInstance.openCustom(); |
| 87 | + snackBar = await loader.getHarness(snackBarHarness); |
| 88 | + await expectAsyncError(() => snackBar.getMessage(), /custom content/); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should be able to get action description of simple snack-bar', async () => { |
| 92 | + fixture.componentInstance.openSimple('Hello', 'Unsubscribe'); |
| 93 | + let snackBar = await loader.getHarness(snackBarHarness); |
| 94 | + expect(await snackBar.getActionDescription()).toBe('Unsubscribe'); |
| 95 | + |
| 96 | + // For snack-bar's with custom template, the action description |
| 97 | + // cannot be retrieved. We expect an error to be thrown. |
| 98 | + fixture.componentInstance.openCustom(); |
| 99 | + snackBar = await loader.getHarness(snackBarHarness); |
| 100 | + await expectAsyncError(() => snackBar.getActionDescription(), /custom content/); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should be able to check whether simple snack-bar has action', async () => { |
| 104 | + fixture.componentInstance.openSimple('With action', 'Unsubscribe'); |
| 105 | + let snackBar = await loader.getHarness(snackBarHarness); |
| 106 | + expect(await snackBar.hasAction()).toBe(true); |
| 107 | + |
| 108 | + fixture.componentInstance.openSimple('No action'); |
| 109 | + snackBar = await loader.getHarness(snackBarHarness); |
| 110 | + expect(await snackBar.hasAction()).toBe(false); |
| 111 | + |
| 112 | + // For snack-bar's with custom template, the action cannot |
| 113 | + // be found. We expect an error to be thrown. |
| 114 | + fixture.componentInstance.openCustom(); |
| 115 | + snackBar = await loader.getHarness(snackBarHarness); |
| 116 | + await expectAsyncError(() => snackBar.hasAction(), /custom content/); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should be able to dismiss simple snack-bar with action', async () => { |
| 120 | + const snackBarRef = fixture.componentInstance.openSimple('With action', 'Unsubscribe'); |
| 121 | + let snackBar = await loader.getHarness(snackBarHarness); |
| 122 | + let actionCount = 0; |
| 123 | + snackBarRef.onAction().subscribe(() => actionCount++); |
| 124 | + |
| 125 | + await snackBar.dismissWithAction(); |
| 126 | + expect(actionCount).toBe(1); |
| 127 | + |
| 128 | + fixture.componentInstance.openSimple('No action'); |
| 129 | + snackBar = await loader.getHarness(snackBarHarness); |
| 130 | + await expectAsyncError(() => snackBar.dismissWithAction(), /without action/); |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Expects the asynchronous function to throw an error that matches |
| 136 | + * the specified expectation. |
| 137 | + */ |
| 138 | +async function expectAsyncError(fn: () => Promise<any>, expectation: RegExp) { |
| 139 | + let error: string|null = null; |
| 140 | + try { |
| 141 | + await fn(); |
| 142 | + } catch (e) { |
| 143 | + error = e.toString(); |
| 144 | + } |
| 145 | + expect(error).not.toBe(null); |
| 146 | + expect(error!).toMatch(expectation); |
| 147 | +} |
| 148 | + |
| 149 | +@Component({ |
| 150 | + template: ` |
| 151 | + <ng-template> |
| 152 | + My custom snack-bar. |
| 153 | + </ng-template> |
| 154 | + ` |
| 155 | +}) |
| 156 | +class SnackbarHarnessTest { |
| 157 | + @ViewChild(TemplateRef, {static: false}) customTmpl: TemplateRef<any>; |
| 158 | + |
| 159 | + constructor(readonly snackBar: MatSnackBar) {} |
| 160 | + |
| 161 | + openSimple(message: string, action = '', config?: MatSnackBarConfig) { |
| 162 | + return this.snackBar.open(message, action, config); |
| 163 | + } |
| 164 | + |
| 165 | + openCustom(config?: MatSnackBarConfig) { |
| 166 | + return this.snackBar.openFromTemplate(this.customTmpl, config); |
| 167 | + } |
| 168 | +} |
0 commit comments