@@ -27,8 +27,8 @@ Or you could use this to mock them out and have the ability to assert on their i
27
27
28
28
``` typescript
29
29
import { ComponentFixture , TestBed } from ' @angular/core/testing' ;
30
- import { By } from ' @angular/platform-browser ' ;
31
- import { MockComponent , MockedComponent , MockRender } from ' ng-mocks ' ;
30
+ import { MockComponent , MockedComponent , MockHelper , MockRender } from ' ng-mocks ' ;
31
+
32
32
import { DependencyComponent } from ' ./dependency.component' ;
33
33
import { TestedComponent } from ' ./tested.component' ;
34
34
@@ -47,8 +47,11 @@ describe('MockComponent', () => {
47
47
});
48
48
49
49
it (' should send the correct value to the dependency component input' , () => {
50
- const mockedComponent = fixture .debugElement .query (By .css (' dependency-component-selector' ))
51
- .componentInstance as DependencyComponent ; // casting to retain type safety
50
+ // the same as fixture.debugElement.query(By.css('dependency-component-selector')).componentInstance
51
+ const mockedComponent = MockHelper .findOrFail <DependencyComponent >(
52
+ fixture .debugElement ,
53
+ ' dependency-component-selector'
54
+ ).componentInstance ;
52
55
53
56
// let's pretend Dependency Component (unmocked) has 'someInput' as an input
54
57
// the input value will be passed into the mocked component so you can assert on it
@@ -61,8 +64,7 @@ describe('MockComponent', () => {
61
64
62
65
it (' should do something when the dependency component emits on its output' , () => {
63
66
spyOn (component , ' trigger' );
64
- const mockedComponent = fixture .debugElement .query (By .directive (DependencyComponent ))
65
- .componentInstance as DependencyComponent ; // casting to retain type safety
67
+ const mockedComponent = MockHelper .findOrFail (fixture .debugElement , DependencyComponent ).componentInstance ;
66
68
67
69
// again, let's pretend DependencyComponent has an output called 'someOutput'
68
70
// emit on the output that MockComponent setup when generating the mock of Dependency Component
@@ -78,36 +80,37 @@ describe('MockComponent', () => {
78
80
});
79
81
80
82
it (' should render something inside of the dependency component' , () => {
81
- const localFixture = MockRender (`
83
+ const localFixture = MockRender < DependencyComponent > (`
82
84
<dependency-component-selector>
83
85
<p>inside content</p>
84
86
</dependency-component-selector>
85
87
` );
88
+
86
89
// because component does not have any @ContentChild we can access html directly.
87
90
// assert on some side effect
88
- const mockedNgContent = localFixture .debugElement . query ( By . directive ( DependencyComponent )) .nativeElement .innerHTML ;
91
+ const mockedNgContent = localFixture .point .nativeElement .innerHTML ;
89
92
expect (mockedNgContent ).toContain (' <p>inside content</p>' );
90
93
});
91
94
92
95
it (' should render something inside of the dependency component' , () => {
93
- const localFixture = MockRender (`
96
+ const localFixture = MockRender < MockedComponent < DependencyComponent >> (`
94
97
<dependency-component-selector>
95
98
<ng-template #something><p>inside template</p></ng-template>
96
99
<p>inside content</p>
97
100
</dependency-component-selector>
98
101
` );
99
102
100
103
// injected ng-content says as it was.
101
- const mockedNgContent = localFixture .debugElement . query ( By . directive ( DependencyComponent )) .nativeElement .innerHTML ;
104
+ const mockedNgContent = localFixture .point .nativeElement .innerHTML ;
102
105
expect (mockedNgContent ).toContain (' <p>inside content</p>' );
103
106
104
107
// because component does have @ContentChild we need to render them first with proper context.
105
- const mockedElement = localFixture .debugElement .query (By .directive (DependencyComponent ));
106
- const mockedComponent: MockedComponent <DependencyComponent > = mockedElement .componentInstance ;
108
+ const mockedComponent = localFixture .point .componentInstance ;
107
109
mockedComponent .__render (' something' );
108
110
localFixture .detectChanges ();
109
111
110
- const mockedNgTemplate = mockedElement .query (By .css (' [data-key="something"]' )).nativeElement .innerHTML ;
112
+ const mockedNgTemplate = MockHelper .findOrFail (localFixture .debugElement , ' [data-key="something"]' ).nativeElement
113
+ .innerHTML ;
111
114
expect (mockedNgTemplate ).toContain (' <p>inside template</p>' );
112
115
});
113
116
});
@@ -124,8 +127,8 @@ describe('MockComponent', () => {
124
127
125
128
``` typescript
126
129
import { ComponentFixture , TestBed } from ' @angular/core/testing' ;
127
- import { By } from ' @angular/platform-browser' ;
128
130
import { MockDirective , MockHelper } from ' ng-mocks' ;
131
+
129
132
import { DependencyDirective } from ' ./dependency.directive' ;
130
133
import { TestedComponent } from ' ./tested.component' ;
131
134
@@ -149,14 +152,12 @@ describe('MockDirective', () => {
149
152
150
153
// let's pretend Dependency Directive (unmocked) has 'someInput' as an input
151
154
// the input value will be passed into the mocked directive so you can assert on it
152
- const mockedDirectiveInstance = MockHelper .getDirective (
153
- fixture . debugElement . query ( By . css ( ' span' ) ),
155
+ const mockedDirectiveInstance = MockHelper .getDirectiveOrFail (
156
+ MockHelper . findOrFail ( fixture . debugElement , ' span' ),
154
157
DependencyDirective
155
158
);
156
- expect (mockedDirectiveInstance ).toBeTruthy ();
157
- if (mockedDirectiveInstance ) {
158
- expect (mockedDirectiveInstance .someInput ).toEqual (' foo' );
159
- }
159
+
160
+ expect (mockedDirectiveInstance .someInput ).toEqual (' foo' );
160
161
// assert on some side effect
161
162
});
162
163
@@ -166,16 +167,13 @@ describe('MockDirective', () => {
166
167
167
168
// again, let's pretend DependencyDirective has an output called 'someOutput'
168
169
// emit on the output that MockDirective setup when generating the mock of Dependency Directive
169
- const mockedDirectiveInstance = MockHelper .getDirective (
170
- fixture . debugElement . query ( By . css ( ' span' ) ),
170
+ const mockedDirectiveInstance = MockHelper .getDirectiveOrFail (
171
+ MockHelper . findOrFail ( fixture . debugElement , ' span' ),
171
172
DependencyDirective
172
173
);
173
- expect (mockedDirectiveInstance ).toBeTruthy ();
174
- if (mockedDirectiveInstance ) {
175
- mockedDirectiveInstance .someOutput .emit ({
176
- payload: ' foo' ,
177
- }); // if you casted mockedDirective as the original component type then this is type safe
178
- }
174
+ mockedDirectiveInstance .someOutput .emit ({
175
+ payload: ' foo' ,
176
+ }); // if you casted mockedDirective as the original component type then this is type safe
179
177
// assert on some side effect
180
178
});
181
179
});
@@ -189,6 +187,7 @@ when assertions should be done on its nested elements.
189
187
``` typescript
190
188
import { ComponentFixture , TestBed } from ' @angular/core/testing' ;
191
189
import { MockDirective , MockedDirective , MockHelper } from ' ng-mocks' ;
190
+
192
191
import { DependencyDirective } from ' ./dependency.directive' ;
193
192
import { TestedComponent } from ' ./tested.component' ;
194
193
@@ -213,7 +212,7 @@ describe('MockDirective', () => {
213
212
// IMPORTANT: by default structural directives aren't rendered.
214
213
// Because we can't automatically detect when and with which context they should be rendered.
215
214
// Usually developer knows context and can render it manually with proper setup.
216
- const mockedDirectiveInstance = MockHelper .findDirective (
215
+ const mockedDirectiveInstance = MockHelper .findDirectiveOrFail (
217
216
fixture .debugElement ,
218
217
DependencyDirective
219
218
) as MockedDirective <DependencyDirective >;
@@ -227,10 +226,7 @@ describe('MockDirective', () => {
227
226
228
227
// let's pretend Dependency Directive (unmocked) has 'someInput' as an input
229
228
// the input value will be passed into the mocked directive so you can assert on it
230
- expect (mockedDirectiveInstance ).toBeTruthy ();
231
- if (mockedDirectiveInstance ) {
232
- expect (mockedDirectiveInstance .someInput ).toEqual (' foo' );
233
- }
229
+ expect (mockedDirectiveInstance .someInput ).toEqual (' foo' );
234
230
// assert on some side effect
235
231
});
236
232
});
@@ -248,8 +244,8 @@ Personally, I found the best thing to do for assertions is to override the trans
248
244
249
245
``` typescript
250
246
import { ComponentFixture , TestBed } from ' @angular/core/testing' ;
251
- import { By } from ' @angular/platform-browser ' ;
252
- import { MockPipe } from ' ng-mocks ' ;
247
+ import { MockHelper , MockPipe } from ' ng-mocks ' ;
248
+
253
249
import { DependencyPipe } from ' ./dependency.pipe' ;
254
250
import { TestedComponent } from ' ./tested.component' ;
255
251
@@ -262,7 +258,7 @@ describe('MockPipe', () => {
262
258
TestedComponent ,
263
259
264
260
// alternatively you can use MockPipes to mock multiple but you lose the ability to override
265
- MockPipe (DependencyPipe , (... args ) => JSON .stringify (args )),
261
+ MockPipe (DependencyPipe , (... args : string [] ) => JSON .stringify (args )),
266
262
],
267
263
});
268
264
@@ -272,7 +268,8 @@ describe('MockPipe', () => {
272
268
273
269
describe (' with transform override' , () => {
274
270
it (' should return the result of the provided transform function' , () => {
275
- expect (fixture .debugElement .query (By .css (' span' )).nativeElement .innerHTML ).toEqual (' ["foo"]' );
271
+ const pipeElement = MockHelper .findOrFail (fixture .debugElement , ' span' );
272
+ expect (pipeElement .nativeElement .innerHTML ).toEqual (' ["foo"]' );
276
273
});
277
274
});
278
275
});
@@ -289,8 +286,8 @@ describe('MockPipe', () => {
289
286
``` typescript
290
287
import { ComponentFixture , TestBed } from ' @angular/core/testing' ;
291
288
import { ReactiveFormsModule } from ' @angular/forms' ;
292
- import { By } from ' @angular/platform-browser ' ;
293
- import { MockComponent , MockedComponent } from ' ng-mocks ' ;
289
+ import { MockComponent , MockedComponent , MockHelper } from ' ng-mocks ' ;
290
+
294
291
import { DependencyComponent } from ' ./dependency.component' ;
295
292
import { TestedComponent } from ' ./tested.component' ;
296
293
@@ -310,8 +307,10 @@ describe('MockReactiveForms', () => {
310
307
});
311
308
312
309
it (' should send the correct value to the dependency component input' , () => {
313
- const mockedReactiveFormComponent = fixture .debugElement .query (By .css (' dependency-component-selector' ))
314
- .componentInstance as MockedComponent <DependencyComponent >; // casting to retain type safety
310
+ const mockedReactiveFormComponent = MockHelper .findOrFail <MockedComponent <DependencyComponent >>(
311
+ fixture .debugElement ,
312
+ ' dependency-component-selector'
313
+ ).componentInstance ;
315
314
316
315
mockedReactiveFormComponent .__simulateChange (' foo' );
317
316
expect (component .formControl .value ).toBe (' foo' );
@@ -336,6 +335,7 @@ For providers I typically will use TestBed.get(SomeProvider) and extend it using
336
335
``` typescript
337
336
import { ComponentFixture , TestBed } from ' @angular/core/testing' ;
338
337
import { MockModule } from ' ng-mocks' ;
338
+
339
339
import { DependencyModule } from ' ./dependency.module' ;
340
340
import { TestedComponent } from ' ./tested.component' ;
341
341
@@ -379,7 +379,6 @@ It is useful if you want to mock system tokens / services such as APP_INITIALIZE
379
379
380
380
``` typescript
381
381
import { TestBed } from ' @angular/core/testing' ;
382
- import { By } from ' @angular/platform-browser' ;
383
382
import { MockModule , MockRender } from ' ng-mocks' ;
384
383
385
384
import { DependencyModule } from ' ./dependency.module' ;
@@ -411,8 +410,7 @@ describe('MockRender', () => {
411
410
);
412
411
413
412
// assert on some side effect
414
- const componentInstance = fixture .debugElement .query (By .directive (TestedComponent ))
415
- .componentInstance as TestedComponent ;
413
+ const componentInstance = fixture .point .componentInstance as TestedComponent ;
416
414
componentInstance .trigger .emit (' foo1' );
417
415
expect (componentInstance .value1 ).toEqual (' something1' );
418
416
expect (componentInstance .value2 ).toEqual (' check' );
@@ -504,28 +502,31 @@ const spySet: Spy = MockHelper.mockService(instance, propertyName, 'set');
504
502
505
503
``` typescript
506
504
// The example below uses auto spy.
507
- it (' mocks getters, setters and methods in a way that jasmine can mock them w/o an issue' , () => {
508
- const mock: GetterSetterMethodHuetod = MockService (GetterSetterMethodHuetod );
509
- expect (mock ).toBeDefined ();
510
-
511
- // Creating a mock on the getter.
512
- MockHelper .mockService <Spy >(mock , ' name' , ' get' ).and .returnValue (' mock' );
513
- expect (mock .name ).toEqual (' mock' );
514
-
515
- // Creating a mock on the setter.
516
- MockHelper .mockService (mock , ' name' , ' set' );
517
- mock .name = ' mock' ;
518
- expect (MockHelper .mockService (mock , ' name' , ' set' )).toHaveBeenCalledWith (' mock' );
519
-
520
- // Creating a mock on the method.
521
- MockHelper .mockService <Spy >(mock , ' nameMethod' ).and .returnValue (' mock' );
522
- expect (mock .nameMethod (' mock' )).toEqual (' mock' );
523
- expect (MockHelper .mockService (mock , ' nameMethod' )).toHaveBeenCalledWith (' mock' );
524
-
525
- // Creating a mock on the method that doesn't exist.
526
- MockHelper .mockService <Spy >(mock , ' fakeMethod' ).and .returnValue (' mock' );
527
- expect ((mock as any ).fakeMethod (' mock' )).toEqual (' mock' );
528
- expect (MockHelper .mockService (mock , ' fakeMethod' )).toHaveBeenCalledWith (' mock' );
505
+ describe (' MockService' , () => {
506
+ it (' mocks getters, setters and methods in a way that jasmine can mock them w/o an issue' , () => {
507
+ // please note that auto spy should be enabled for this test.
508
+ const mock: GetterSetterMethodHuetod = MockService (GetterSetterMethodHuetod );
509
+ expect (mock ).toBeDefined ();
510
+
511
+ // Creating a mock on the getter.
512
+ MockHelper .mockService <jasmine .Spy >(mock , ' name' , ' get' ).and .returnValue (' mock' );
513
+ expect (mock .name ).toEqual (' mock' );
514
+
515
+ // Creating a mock on the setter.
516
+ MockHelper .mockService (mock , ' name' , ' set' );
517
+ mock .name = ' mock' ;
518
+ expect (MockHelper .mockService (mock , ' name' , ' set' )).toHaveBeenCalledWith (' mock' );
519
+
520
+ // Creating a mock on the method.
521
+ MockHelper .mockService <jasmine .Spy >(mock , ' nameMethod' ).and .returnValue (' mock' );
522
+ expect (mock .nameMethod (' mock' )).toEqual (' mock' );
523
+ expect (MockHelper .mockService (mock , ' nameMethod' )).toHaveBeenCalledWith (' mock' );
524
+
525
+ // Creating a mock on the method that doesn't exist.
526
+ MockHelper .mockService <jasmine .Spy >(mock , ' fakeMethod' ).and .returnValue (' mock' );
527
+ expect ((mock as any ).fakeMethod (' mock' )).toEqual (' mock' );
528
+ expect (MockHelper .mockService (mock , ' fakeMethod' )).toHaveBeenCalledWith (' mock' );
529
+ });
529
530
});
530
531
```
531
532
0 commit comments