|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import {AriaLivePoliteness} from '@angular/cdk/a11y'; |
10 |
| -import {ComponentHarness, HarnessPredicate, parallel} from '@angular/cdk/testing'; |
11 |
| -import {SnackBarHarnessFilters} from './snack-bar-harness-filters'; |
| 9 | +import {HarnessPredicate} from '@angular/cdk/testing'; |
| 10 | +import { |
| 11 | + MatSnackBarHarness as BaseMatSnackBarHarness, |
| 12 | + SnackBarHarnessFilters, |
| 13 | +} from '@angular/material/snack-bar/testing'; |
12 | 14 |
|
13 | 15 | /** Harness for interacting with an MDC-based mat-snack-bar in tests. */
|
14 |
| -export class MatSnackBarHarness extends ComponentHarness { |
15 |
| - // Developers can provide a custom component or template for the |
16 |
| - // snackbar. The canonical snack-bar parent is the "MatSnackBarContainer". |
17 |
| - // We use `:not([mat-exit])` to exclude snack bars that are in the process of being dismissed, |
18 |
| - // because the element only gets removed after the animation is finished and since it runs |
19 |
| - // outside of Angular, we don't have a way of being notified when it's done. |
| 16 | +export class MatSnackBarHarness extends BaseMatSnackBarHarness { |
| 17 | + // Developers can provide a custom component or template for the snackbar. The canonical snack-bar |
| 18 | + // parent is the "MatSnackBarContainer". We use `:not([mat-exit])` to exclude snack bars that |
| 19 | + // are in the process of being dismissed, because the element only gets removed after the |
| 20 | + // animation is finished and since it runs outside of Angular, we don't have a way of being |
| 21 | + // notified when it's done. |
20 | 22 | /** The selector for the host element of a `MatSnackBar` instance. */
|
21 | 23 | static hostSelector = '.mat-mdc-snack-bar-container:not([mat-exit])';
|
22 |
| - |
23 |
| - private _simpleSnackBar = this.locatorForOptional('.mat-mdc-simple-snack-bar'); |
24 |
| - private _simpleSnackBarLiveRegion = this.locatorFor('[aria-live]'); |
25 |
| - private _simpleSnackBarMessage = |
26 |
| - this.locatorFor('.mat-mdc-simple-snack-bar .mat-mdc-snack-bar-label'); |
27 |
| - private _simpleSnackBarActionButton = |
28 |
| - this.locatorForOptional('.mat-mdc-simple-snack-bar .mat-mdc-snack-bar-action'); |
| 24 | + protected _messageSelector = '.mat-mdc-simple-snack-bar .mat-mdc-snack-bar-label'; |
| 25 | + protected _simpleSnackBarSelector = '.mat-mdc-simple-snack-bar'; |
| 26 | + protected _actionButtonSelector = '.mat-mdc-simple-snack-bar .mat-mdc-snack-bar-action'; |
29 | 27 |
|
30 | 28 | /**
|
31 | 29 | * Gets a `HarnessPredicate` that can be used to search for a `MatSnackBarHarness` that meets
|
32 | 30 | * certain criteria.
|
33 | 31 | * @param options Options for filtering which snack bar instances are considered a match.
|
34 | 32 | * @return a `HarnessPredicate` configured with the given options.
|
35 | 33 | */
|
36 |
| - static with(options: SnackBarHarnessFilters = {}): HarnessPredicate<MatSnackBarHarness> { |
37 |
| - return new HarnessPredicate(MatSnackBarHarness, options); |
38 |
| - } |
39 |
| - |
40 |
| - /** |
41 |
| - * Gets the role of the snack-bar. The role of a snack-bar is determined based |
42 |
| - * on the ARIA politeness specified in the snack-bar config. |
43 |
| - * @deprecated Use `getAriaLive` instead. |
44 |
| - * @breaking-change 13.0.0 |
45 |
| - */ |
46 |
| - async getRole(): Promise<'alert'|'status'|null> { |
47 |
| - return (await this.host()).getAttribute('role') as Promise<'alert'|'status'|null>; |
48 |
| - } |
49 |
| - |
50 |
| - /** |
51 |
| - * Gets the aria-live of the snack-bar's live region. The aria-live of a snack-bar is |
52 |
| - * determined based on the ARIA politeness specified in the snack-bar config. |
53 |
| - */ |
54 |
| - async getAriaLive(): Promise<AriaLivePoliteness> { |
55 |
| - return (await this._simpleSnackBarLiveRegion()) |
56 |
| - .getAttribute('aria-live') as Promise<AriaLivePoliteness>; |
57 |
| - } |
58 |
| - |
59 |
| - /** |
60 |
| - * Whether the snack-bar has an action. Method cannot be used for snack-bar's with custom content. |
61 |
| - */ |
62 |
| - async hasAction(): Promise<boolean> { |
63 |
| - await this._assertSimpleSnackBar(); |
64 |
| - return (await this._simpleSnackBarActionButton()) !== null; |
65 |
| - } |
66 |
| - |
67 |
| - /** |
68 |
| - * Gets the description of the snack-bar. Method cannot be used for snack-bar's without action or |
69 |
| - * with custom content. |
70 |
| - */ |
71 |
| - async getActionDescription(): Promise<string> { |
72 |
| - await this._assertSimpleSnackBarWithAction(); |
73 |
| - return (await this._simpleSnackBarActionButton())!.text(); |
74 |
| - } |
75 |
| - |
76 |
| - |
77 |
| - /** |
78 |
| - * Dismisses the snack-bar by clicking the action button. Method cannot be used for snack-bar's |
79 |
| - * without action or with custom content. |
80 |
| - */ |
81 |
| - async dismissWithAction(): Promise<void> { |
82 |
| - await this._assertSimpleSnackBarWithAction(); |
83 |
| - await (await this._simpleSnackBarActionButton())!.click(); |
84 |
| - } |
85 |
| - |
86 |
| - /** |
87 |
| - * Gets the message of the snack-bar. Method cannot be used for snack-bar's with custom content. |
88 |
| - */ |
89 |
| - async getMessage(): Promise<string> { |
90 |
| - await this._assertSimpleSnackBar(); |
91 |
| - return (await this._simpleSnackBarMessage()).text(); |
92 |
| - } |
93 |
| - |
94 |
| - /** Gets whether the snack-bar has been dismissed. */ |
95 |
| - async isDismissed(): Promise<boolean> { |
96 |
| - // We consider the snackbar dismissed if it's not in the DOM. We can assert that the |
97 |
| - // element isn't in the DOM by seeing that its width and height are zero. |
98 |
| - |
99 |
| - const host = await this.host(); |
100 |
| - const [exit, dimensions] = await parallel(() => [ |
101 |
| - // The snackbar container is marked with the "exit" attribute after it has been dismissed |
102 |
| - // but before the animation has finished (after which it's removed from the DOM). |
103 |
| - host.getAttribute('mat-exit'), |
104 |
| - host.getDimensions(), |
105 |
| - ]); |
106 |
| - |
107 |
| - return exit != null || (!!dimensions && dimensions.height === 0 && dimensions.width === 0); |
108 |
| - } |
109 |
| - |
110 |
| - /** |
111 |
| - * Asserts that the current snack-bar does not use custom content. Promise rejects if |
112 |
| - * custom content is used. |
113 |
| - */ |
114 |
| - private async _assertSimpleSnackBar(): Promise<void> { |
115 |
| - if (!await this._isSimpleSnackBar()) { |
116 |
| - throw Error('Method cannot be used for snack-bar with custom content.'); |
117 |
| - } |
118 |
| - } |
119 |
| - |
120 |
| - /** |
121 |
| - * Asserts that the current snack-bar does not use custom content and has |
122 |
| - * an action defined. Otherwise the promise will reject. |
123 |
| - */ |
124 |
| - private async _assertSimpleSnackBarWithAction(): Promise<void> { |
125 |
| - await this._assertSimpleSnackBar(); |
126 |
| - if (!await this.hasAction()) { |
127 |
| - throw Error('Method cannot be used for standard snack-bar without action.'); |
128 |
| - } |
129 |
| - } |
130 |
| - |
131 |
| - /** Whether the snack-bar is using the default content template. */ |
132 |
| - private async _isSimpleSnackBar(): Promise<boolean> { |
133 |
| - return await this._simpleSnackBar() !== null; |
| 34 | + static with(options: SnackBarHarnessFilters = {}): HarnessPredicate<BaseMatSnackBarHarness> { |
| 35 | + return new HarnessPredicate<BaseMatSnackBarHarness>(MatSnackBarHarness, options); |
134 | 36 | }
|
135 | 37 | }
|
0 commit comments