@@ -19,7 +19,7 @@ This guide discusses the advantages of using component test harnesses and shows
1919## Benefits of component test harnesses
2020
2121There are two primary benefits to using the Angular Material component harnesses in your tests:
22-
22+
23231 . Harnesses make tests easier to read and understand with straightforward APIs.
24242 . Harnesses make tests more robust and less likely to break when updating Angular Material.
2525
@@ -48,8 +48,7 @@ let loader: HarnessLoader;
4848
4949describe (' my-component' , () => {
5050 beforeEach (async () => {
51- await TestBed .configureTestingModule ({imports: [MyModule ], declarations: [UserProfile ]})
52- .compileComponents ();
51+ TestBed .configureTestingModule ({imports: [MyModule ], declarations: [UserProfile ]});
5352 fixture = TestBed .createComponent (UserProfile );
5453 loader = TestbedHarnessEnvironment .loader (fixture );
5554 });
@@ -59,10 +58,10 @@ describe('my-component', () => {
5958This code creates a fixture for ` UserProfile ` and then creates a ` HarnessLoader ` for that fixture.
6059The ` HarnessLoader ` can then locate Angular Material components inside ` UserProfile ` and create
6160harnesses for them. Note that ` HarnessLoader ` and ` TestbedHarnessEnvironment ` are loaded from
62- different paths.
61+ different paths.
6362
6463- ` @angular / cdk / testing ` contains symbols that are shared regardless of the environment your tests
65- are in.
64+ are in.
6665- ` @angular / cdk / testing / testbed ` contains symbols that are used only in Karma tests.
6766- ` @angular / cdk / testing / selenium - webdriver ` (not shown above) contains symbols that are used only in
6867 Selenium WebDriver tests.
@@ -93,7 +92,7 @@ with your tests.
9392
9493The example above retrieves all button harnesses and uses an array index to get the harness for a
9594specific 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 ` .
9796
9897You can load harnesses for a sub-section of the DOM within ` UserProfile ` with the ` getChildLoader `
9998method 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
111110This method creates a ` HarnessPredicate ` , an object that filters loaded harnesses based on the
112111provided constraints. The particular constraint options vary depending on the harness class, but all
113112harnesses support at least:
114-
113+
115114- ` selector ` - CSS selector that the component must match (in addition to its host selector, such
116115 as ` [mat - button ]` )
117116- ` ancestor ` - CSS selector for a some ancestor element above the component in the DOM
118-
117+
119118In 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+
123122Using this method we could locate buttons as follows in our test:
124-
123+
125124` ` ` ts
126125it (' should work' , async () => {
127126 // Harness for mat-button whose id is 'more-info'.
@@ -164,7 +163,7 @@ which will cause the test to wait for `setTimeout`, `Promise`, etc.
164163## Comparison with and without component harnesses
165164
166165Consider 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
168167test to verify that when the user chooses an issue type the proper report displays. First consider
169168what the test might look like without using component harnesses:
170169
@@ -173,10 +172,10 @@ describe('issue-report-selector', () => {
173172 let fixture: ComponentFixture <IssueReportSelector >;
174173
175174 beforeEach (async () => {
176- await TestBed .configureTestingModule ({
175+ TestBed .configureTestingModule ({
177176 imports: [IssueReportSelectorModule ],
178177 declarations: [IssueReportSelector ],
179- }). compileComponents () ;
178+ });
180179
181180 fixture = TestBed .createComponent (IssueReportSelector );
182181 fixture .detectChanges ();
@@ -205,10 +204,10 @@ describe('issue-report-selector', () => {
205204 let loader: HarnessLoader ;
206205
207206 beforeEach (async () => {
208- await TestBed .configureTestingModule ({
207+ TestBed .configureTestingModule ({
209208 imports: [IssueReportSelectorModule ],
210209 declarations: [IssueReportSelector ],
211- }). compileComponents () ;
210+ });
212211
213212 fixture = TestBed .createComponent (IssueReportSelector );
214213 fixture .detectChanges ();
@@ -247,13 +246,13 @@ Notice that the test without harnesses directly uses CSS selectors to query elem
247246` < mat - select > ` , such as ` .mat - select - trigger ` . If the internal DOM of ` < mat - select > ` changes, these
248247queries may stop working. While the Angular team tries to minimize this type of change, some
249248features 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.
251250
252251In addition to DOM structure, component asynchronicity often offers a challenge when updating
253252components. If a component changes between synchronous and asynchronous, downstream unit tests may
254253break due to expectations around timing. Tests then require the addition or removal of some
255254arcane 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
257256with all asynchronous APIs. When a test uses these harnesses, changes to asynchronicity become
258257far more manageable.
259258
0 commit comments