@@ -19,7 +19,7 @@ This guide discusses the advantages of using component test harnesses and shows
19
19
## Benefits of component test harnesses
20
20
21
21
There are two primary benefits to using the Angular Material component harnesses in your tests:
22
-
22
+
23
23
1 . Harnesses make tests easier to read and understand with straightforward APIs.
24
24
2 . Harnesses make tests more robust and less likely to break when updating Angular Material.
25
25
@@ -48,8 +48,7 @@ let loader: HarnessLoader;
48
48
49
49
describe (' my-component' , () => {
50
50
beforeEach (async () => {
51
- await TestBed .configureTestingModule ({imports: [MyModule ], declarations: [UserProfile ]})
52
- .compileComponents ();
51
+ TestBed .configureTestingModule ({imports: [MyModule ], declarations: [UserProfile ]});
53
52
fixture = TestBed .createComponent (UserProfile );
54
53
loader = TestbedHarnessEnvironment .loader (fixture );
55
54
});
@@ -59,10 +58,10 @@ describe('my-component', () => {
59
58
This code creates a fixture for ` UserProfile ` and then creates a ` HarnessLoader ` for that fixture.
60
59
The ` HarnessLoader ` can then locate Angular Material components inside ` UserProfile ` and create
61
60
harnesses for them. Note that ` HarnessLoader ` and ` TestbedHarnessEnvironment ` are loaded from
62
- different paths.
61
+ different paths.
63
62
64
63
- ` @angular / cdk / testing ` contains symbols that are shared regardless of the environment your tests
65
- are in.
64
+ are in.
66
65
- ` @angular / cdk / testing / testbed ` contains symbols that are used only in Karma tests.
67
66
- ` @angular / cdk / testing / selenium - webdriver ` (not shown above) contains symbols that are used only in
68
67
Selenium WebDriver tests.
@@ -93,7 +92,7 @@ with your tests.
93
92
94
93
The example above retrieves all button harnesses and uses an array index to get the harness for a
95
94
specific button. However, if the number or order of buttons changes, this test will break. You can
96
- write a less brittle test by instead asking for only a subset of harnesses inside ` UserProfile ` .
95
+ write a less brittle test by instead asking for only a subset of harnesses inside ` UserProfile ` .
97
96
98
97
You can load harnesses for a sub-section of the DOM within ` UserProfile ` with the ` getChildLoader `
99
98
method on ` HarnessLoader ` . For example, say that we know ` UserProfile ` has a div,
@@ -111,17 +110,17 @@ You can also use the static `with` method implemented on all Angular Material co
111
110
This method creates a ` HarnessPredicate ` , an object that filters loaded harnesses based on the
112
111
provided constraints. The particular constraint options vary depending on the harness class, but all
113
112
harnesses support at least:
114
-
113
+
115
114
- ` selector ` - CSS selector that the component must match (in addition to its host selector, such
116
115
as ` [mat - button ]` )
117
116
- ` ancestor ` - CSS selector for a some ancestor element above the component in the DOM
118
-
117
+
119
118
In addition to these standard options, ` MatButtonHarness ` also supports
120
-
121
- - ` text ` - String text or regular expressions that matches the text content of the button
122
-
119
+
120
+ - ` text ` - String text or regular expressions that matches the text content of the button
121
+
123
122
Using this method we could locate buttons as follows in our test:
124
-
123
+
125
124
` ` ` ts
126
125
it (' should work' , async () => {
127
126
// Harness for mat-button whose id is 'more-info'.
@@ -164,7 +163,7 @@ which will cause the test to wait for `setTimeout`, `Promise`, etc.
164
163
## Comparison with and without component harnesses
165
164
166
165
Consider an ` < issue - report - selector > ` component that you want to test. It allows a user to
167
- choose an issue type and display the necessary form create report for that issue type. You need a
166
+ choose an issue type and display the necessary form create report for that issue type. You need a
168
167
test to verify that when the user chooses an issue type the proper report displays. First consider
169
168
what the test might look like without using component harnesses:
170
169
@@ -173,10 +172,10 @@ describe('issue-report-selector', () => {
173
172
let fixture: ComponentFixture <IssueReportSelector >;
174
173
175
174
beforeEach (async () => {
176
- await TestBed .configureTestingModule ({
175
+ TestBed .configureTestingModule ({
177
176
imports: [IssueReportSelectorModule ],
178
177
declarations: [IssueReportSelector ],
179
- }). compileComponents () ;
178
+ });
180
179
181
180
fixture = TestBed .createComponent (IssueReportSelector );
182
181
fixture .detectChanges ();
@@ -205,10 +204,10 @@ describe('issue-report-selector', () => {
205
204
let loader: HarnessLoader ;
206
205
207
206
beforeEach (async () => {
208
- await TestBed .configureTestingModule ({
207
+ TestBed .configureTestingModule ({
209
208
imports: [IssueReportSelectorModule ],
210
209
declarations: [IssueReportSelector ],
211
- }). compileComponents () ;
210
+ });
212
211
213
212
fixture = TestBed .createComponent (IssueReportSelector );
214
213
fixture .detectChanges ();
@@ -247,13 +246,13 @@ Notice that the test without harnesses directly uses CSS selectors to query elem
247
246
` < mat - select > ` , such as ` .mat - select - trigger ` . If the internal DOM of ` < mat - select > ` changes, these
248
247
queries may stop working. While the Angular team tries to minimize this type of change, some
249
248
features and bug fixes ultimately require restructuring the DOM. By using the Angular Material
250
- harnesses, you avoid depending on internal DOM structure directly.
249
+ harnesses, you avoid depending on internal DOM structure directly.
251
250
252
251
In addition to DOM structure, component asynchronicity often offers a challenge when updating
253
252
components. If a component changes between synchronous and asynchronous, downstream unit tests may
254
253
break due to expectations around timing. Tests then require the addition or removal of some
255
254
arcane combination of ` whenStable ` , ` flushMicroTasks ` , ` tick ` , or ` detectChanges ` . Component
256
- harnesses, however, avoid this problem by normalizing the asynchronicity of all component behaviors
255
+ harnesses, however, avoid this problem by normalizing the asynchronicity of all component behaviors
257
256
with all asynchronous APIs. When a test uses these harnesses, changes to asynchronicity become
258
257
far more manageable.
259
258
0 commit comments