ng-mocks 13.5.0 → 14.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -13
- package/examples/MockComponent/test.spec.ts +14 -6
- package/examples/MockDirective-Attribute/test.spec.ts +13 -6
- package/examples/MockDirective-Structural/test.spec.ts +3 -2
- package/examples/MockForms/test.spec.ts +15 -6
- package/examples/MockInstance/test.spec.ts +9 -1
- package/examples/MockModule/test.spec.ts +11 -4
- package/examples/MockPipe/test.spec.ts +5 -4
- package/examples/MockReactiveForms/test.spec.ts +12 -6
- package/examples/MockRender/test.spec.ts +3 -3
- package/examples/TestHttpInterceptor/test.spec.ts +7 -6
- package/examples/TestHttpRequest/test.spec.ts +5 -6
- package/examples/TestLifecycleHooks/test.spec.ts +1 -1
- package/examples/TestPipe/test.spec.ts +3 -3
- package/examples/TestProviderInComponent/test.spec.ts +4 -4
- package/examples/TestProviderInDirective/test.spec.ts +16 -12
- package/examples/TestRoute/test.spec.ts +13 -7
- package/examples/TestRoutingGuard/test.spec.ts +4 -4
- package/examples/TestRoutingResolver/test.spec.ts +8 -3
- package/examples/TestStandaloneComponent/test.spec.ts +87 -0
- package/examples/TestStandaloneDirective/test.spec.ts +68 -0
- package/examples/TestStandalonePipe/test.spec.ts +85 -0
- package/examples/TestToken/test.spec.ts +4 -6
- package/examples/ngMocksFaster/test.spec.ts +14 -5
- package/examples/readme/classic.spec.ts +1 -9
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/ng-mocks.d.ts +57 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
[
|
|
2
|
-
[
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
1
|
+
[<img src="https://img.shields.io/gitter/room/ike18t/ng-mocks" alt="chat on gitter" width="90" height="20" />](https://gitter.im/ng-mocks/community)
|
|
2
|
+
[<img src="https://img.shields.io/npm/v/ng-mocks" alt="npm version" width="88" height="20" />](https://www.npmjs.com/package/ng-mocks)
|
|
3
|
+
[<img src="https://img.shields.io/circleci/build/github/ike18t/ng-mocks/master" alt="build status" width="88" height="20" />](https://app.circleci.com/pipelines/github/ike18t/ng-mocks?branch=master)
|
|
4
|
+
[<img src="https://img.shields.io/coveralls/github/ike18t/ng-mocks/master" alt="coverage status" width="104" height="20" />](https://coveralls.io/github/ike18t/ng-mocks?branch=master)
|
|
5
|
+
[<img src="https://img.shields.io/lgtm/grade/javascript/g/ike18t/ng-mocks" alt="language grade" width="138" height="20" />](https://lgtm.com/projects/g/ike18t/ng-mocks/context:javascript)
|
|
6
6
|
|
|
7
7
|
# Mock components, services and more out of annoying dependencies for simplification of Angular testing
|
|
8
8
|
|
|
@@ -71,14 +71,6 @@ An example of a spec for a profile edit component.
|
|
|
71
71
|
// In the following test suite, we would like to
|
|
72
72
|
// cover behavior of the component.
|
|
73
73
|
describe('profile:classic', () => {
|
|
74
|
-
// First of all, we would like to reuse the same
|
|
75
|
-
// TestBed in every test.
|
|
76
|
-
// ngMocks.faster suppresses reset of TestBed
|
|
77
|
-
// after each test and allows to use TestBed,
|
|
78
|
-
// MockBuilder and MockRender in beforeAll.
|
|
79
|
-
// https://ng-mocks.sudo.eu/api/ngMocks/faster
|
|
80
|
-
ngMocks.faster();
|
|
81
|
-
|
|
82
74
|
// Helps to reset customizations after each test.
|
|
83
75
|
MockInstance.scope();
|
|
84
76
|
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { CommonModule } from '@angular/common';
|
|
1
2
|
import {
|
|
2
3
|
Component,
|
|
3
4
|
ContentChild,
|
|
4
5
|
EventEmitter,
|
|
5
6
|
Input,
|
|
7
|
+
NgModule,
|
|
6
8
|
Output,
|
|
7
9
|
TemplateRef,
|
|
8
10
|
} from '@angular/core';
|
|
@@ -33,18 +35,24 @@ class DependencyComponent {
|
|
|
33
35
|
></app-child>
|
|
34
36
|
`,
|
|
35
37
|
})
|
|
36
|
-
class
|
|
38
|
+
class MyComponent {
|
|
37
39
|
public value = '';
|
|
38
40
|
public trigger = (obj: any) => obj;
|
|
39
41
|
}
|
|
40
42
|
|
|
43
|
+
@NgModule({
|
|
44
|
+
imports: [CommonModule],
|
|
45
|
+
declarations: [MyComponent, DependencyComponent],
|
|
46
|
+
})
|
|
47
|
+
class ItsModule {}
|
|
48
|
+
|
|
41
49
|
describe('MockComponent', () => {
|
|
42
50
|
beforeEach(() => {
|
|
43
|
-
return MockBuilder(
|
|
51
|
+
return MockBuilder(MyComponent, ItsModule);
|
|
44
52
|
});
|
|
45
53
|
|
|
46
54
|
it('sends the correct value to the child input', () => {
|
|
47
|
-
const fixture = MockRender(
|
|
55
|
+
const fixture = MockRender(MyComponent);
|
|
48
56
|
const component = fixture.point.componentInstance;
|
|
49
57
|
|
|
50
58
|
// The same as
|
|
@@ -58,7 +66,7 @@ describe('MockComponent', () => {
|
|
|
58
66
|
).componentInstance;
|
|
59
67
|
|
|
60
68
|
// Let's pretend that DependencyComponent has 'someInput' as
|
|
61
|
-
// an input.
|
|
69
|
+
// an input. MyComponent sets its value via
|
|
62
70
|
// `[someInput]="value"`. The input's value will be passed into
|
|
63
71
|
// the mock component so we can assert on it.
|
|
64
72
|
component.value = 'foo';
|
|
@@ -69,7 +77,7 @@ describe('MockComponent', () => {
|
|
|
69
77
|
});
|
|
70
78
|
|
|
71
79
|
it('does something on an emit of the child component', () => {
|
|
72
|
-
const fixture = MockRender(
|
|
80
|
+
const fixture = MockRender(MyComponent);
|
|
73
81
|
const component = fixture.point.componentInstance;
|
|
74
82
|
|
|
75
83
|
// The same as
|
|
@@ -80,7 +88,7 @@ describe('MockComponent', () => {
|
|
|
80
88
|
const mockComponent = ngMocks.findInstance(DependencyComponent);
|
|
81
89
|
|
|
82
90
|
// Again, let's pretend DependencyComponent has an output
|
|
83
|
-
// called 'someOutput'.
|
|
91
|
+
// called 'someOutput'. MyComponent listens on the output via
|
|
84
92
|
// `(someOutput)="trigger($event)"`.
|
|
85
93
|
// Let's install a spy and trigger the output.
|
|
86
94
|
ngMocks.stubMember(
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
Directive,
|
|
4
4
|
EventEmitter,
|
|
5
5
|
Input,
|
|
6
|
+
NgModule,
|
|
6
7
|
Output,
|
|
7
8
|
} from '@angular/core';
|
|
8
9
|
|
|
@@ -29,18 +30,24 @@ class DependencyDirective {
|
|
|
29
30
|
></span>
|
|
30
31
|
`,
|
|
31
32
|
})
|
|
32
|
-
class
|
|
33
|
+
class MyComponent {
|
|
33
34
|
public value = '';
|
|
34
35
|
public trigger = () => undefined;
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
@NgModule({
|
|
39
|
+
declarations: [MyComponent, DependencyDirective],
|
|
40
|
+
})
|
|
41
|
+
class ItsModule {}
|
|
42
|
+
|
|
37
43
|
describe('MockDirective:Attribute', () => {
|
|
38
44
|
beforeEach(() => {
|
|
39
|
-
|
|
45
|
+
// DependencyDirective is a declaration in ItsModule.
|
|
46
|
+
return MockBuilder(MyComponent, ItsModule);
|
|
40
47
|
});
|
|
41
48
|
|
|
42
49
|
it('sends the correct value to the input', () => {
|
|
43
|
-
const fixture = MockRender(
|
|
50
|
+
const fixture = MockRender(MyComponent);
|
|
44
51
|
const component = fixture.point.componentInstance;
|
|
45
52
|
|
|
46
53
|
// The same as
|
|
@@ -54,7 +61,7 @@ describe('MockDirective:Attribute', () => {
|
|
|
54
61
|
);
|
|
55
62
|
|
|
56
63
|
// Let's pretend DependencyDirective has 'someInput'
|
|
57
|
-
// as an input.
|
|
64
|
+
// as an input. MyComponent sets its value via
|
|
58
65
|
// `[someInput]="value"`. The input's value will be passed into
|
|
59
66
|
// the mock directive so we can assert on it.
|
|
60
67
|
component.value = 'foo';
|
|
@@ -65,7 +72,7 @@ describe('MockDirective:Attribute', () => {
|
|
|
65
72
|
});
|
|
66
73
|
|
|
67
74
|
it('does something on an emit of the child directive', () => {
|
|
68
|
-
const fixture = MockRender(
|
|
75
|
+
const fixture = MockRender(MyComponent);
|
|
69
76
|
const component = fixture.point.componentInstance;
|
|
70
77
|
|
|
71
78
|
// The same as
|
|
@@ -79,7 +86,7 @@ describe('MockDirective:Attribute', () => {
|
|
|
79
86
|
);
|
|
80
87
|
|
|
81
88
|
// Again, let's pretend DependencyDirective has an output called
|
|
82
|
-
// 'someOutput'.
|
|
89
|
+
// 'someOutput'. MyComponent listens on the output via
|
|
83
90
|
// `(someOutput)="trigger()"`.
|
|
84
91
|
// Let's install a spy and trigger the output.
|
|
85
92
|
ngMocks.stubMember(
|
|
@@ -33,7 +33,7 @@ class TargetComponent {
|
|
|
33
33
|
@NgModule({
|
|
34
34
|
declarations: [TargetComponent, DependencyDirective],
|
|
35
35
|
})
|
|
36
|
-
class
|
|
36
|
+
class ItsModule {}
|
|
37
37
|
|
|
38
38
|
describe('MockDirective:Structural', () => {
|
|
39
39
|
// IMPORTANT: by default structural directives are not rendered.
|
|
@@ -41,7 +41,8 @@ describe('MockDirective:Structural', () => {
|
|
|
41
41
|
// Usually a developer knows the context and can render it
|
|
42
42
|
// manually with proper setup.
|
|
43
43
|
beforeEach(() => {
|
|
44
|
-
|
|
44
|
+
// DependencyDirective is a declaration in ItsModule.
|
|
45
|
+
return MockBuilder(TargetComponent, ItsModule).mock(
|
|
45
46
|
DependencyDirective,
|
|
46
47
|
{
|
|
47
48
|
// render: true, // <-- a flag to render the directive by default
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Component, forwardRef } from '@angular/core';
|
|
1
|
+
import { Component, forwardRef, NgModule } from '@angular/core';
|
|
2
2
|
import {
|
|
3
3
|
ControlValueAccessor,
|
|
4
4
|
FormsModule,
|
|
@@ -33,18 +33,27 @@ class DependencyComponent implements ControlValueAccessor {
|
|
|
33
33
|
selector: 'tested',
|
|
34
34
|
template: ` <app-child [(ngModel)]="value"></app-child> `,
|
|
35
35
|
})
|
|
36
|
-
class
|
|
36
|
+
class MyComponent {
|
|
37
37
|
public value: any;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
@NgModule({
|
|
41
|
+
imports: [FormsModule],
|
|
42
|
+
declarations: [MyComponent, DependencyComponent],
|
|
43
|
+
})
|
|
44
|
+
class ItsModule {}
|
|
45
|
+
|
|
40
46
|
describe('MockForms', () => {
|
|
41
47
|
// Helps to reset customizations after each test.
|
|
42
48
|
MockInstance.scope();
|
|
43
49
|
|
|
44
50
|
beforeEach(() => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
// DependencyComponent is a declaration in ItsModule.
|
|
52
|
+
return (
|
|
53
|
+
MockBuilder(MyComponent, ItsModule)
|
|
54
|
+
// FormsModule is an import in ItsModule.
|
|
55
|
+
.keep(FormsModule)
|
|
56
|
+
);
|
|
48
57
|
});
|
|
49
58
|
|
|
50
59
|
it('sends the correct value to the mock form component', async () => {
|
|
@@ -61,7 +70,7 @@ describe('MockForms', () => {
|
|
|
61
70
|
// the spy via MockInstance before the render.
|
|
62
71
|
MockInstance(DependencyComponent, 'writeValue', writeValue);
|
|
63
72
|
|
|
64
|
-
const fixture = MockRender(
|
|
73
|
+
const fixture = MockRender(MyComponent);
|
|
65
74
|
// FormsModule needs fixture.whenStable()
|
|
66
75
|
// right after MockRender to install all hooks.
|
|
67
76
|
await fixture.whenStable();
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { CommonModule } from '@angular/common';
|
|
1
2
|
import {
|
|
2
3
|
AfterViewInit,
|
|
3
4
|
Component,
|
|
4
5
|
Injector,
|
|
6
|
+
NgModule,
|
|
5
7
|
ViewChild,
|
|
6
8
|
} from '@angular/core';
|
|
7
9
|
import { Observable, Subject } from 'rxjs';
|
|
@@ -39,11 +41,17 @@ class RealComponent implements AfterViewInit {
|
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
@NgModule({
|
|
45
|
+
imports: [CommonModule],
|
|
46
|
+
declarations: [RealComponent, ChildComponent],
|
|
47
|
+
})
|
|
48
|
+
class ItsModule {}
|
|
49
|
+
|
|
42
50
|
describe('MockInstance', () => {
|
|
43
51
|
// A normal setup of the TestBed, TargetComponent will be replaced
|
|
44
52
|
// with its mock object.
|
|
45
53
|
// Do not forget to return the promise of MockBuilder.
|
|
46
|
-
beforeEach(() => MockBuilder(RealComponent)
|
|
54
|
+
beforeEach(() => MockBuilder(RealComponent, ItsModule));
|
|
47
55
|
|
|
48
56
|
beforeEach(() => {
|
|
49
57
|
// Because TargetComponent is replaced with its mock object,
|
|
@@ -42,18 +42,25 @@ class DependencyModule {}
|
|
|
42
42
|
></app-child>
|
|
43
43
|
`,
|
|
44
44
|
})
|
|
45
|
-
class
|
|
45
|
+
class MyComponent {
|
|
46
46
|
public value = '';
|
|
47
47
|
public trigger = () => undefined;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
@NgModule({
|
|
51
|
+
imports: [DependencyModule],
|
|
52
|
+
declarations: [MyComponent],
|
|
53
|
+
})
|
|
54
|
+
class ItsModule {}
|
|
55
|
+
|
|
50
56
|
describe('MockModule', () => {
|
|
51
57
|
beforeEach(() => {
|
|
52
|
-
|
|
58
|
+
// DependencyModule is an import of ItsModule.
|
|
59
|
+
return MockBuilder(MyComponent, ItsModule);
|
|
53
60
|
});
|
|
54
61
|
|
|
55
|
-
it('renders
|
|
56
|
-
const fixture = MockRender(
|
|
62
|
+
it('renders MyComponent with its dependencies', () => {
|
|
63
|
+
const fixture = MockRender(MyComponent);
|
|
57
64
|
const component = fixture.point.componentInstance;
|
|
58
65
|
|
|
59
66
|
expect(component).toBeTruthy();
|
|
@@ -23,7 +23,7 @@ class TargetComponent {}
|
|
|
23
23
|
@NgModule({
|
|
24
24
|
declarations: [TargetComponent, DependencyPipe],
|
|
25
25
|
})
|
|
26
|
-
class
|
|
26
|
+
class ItsModule {}
|
|
27
27
|
|
|
28
28
|
// A fake transform function.
|
|
29
29
|
const fakeTransform = (...args: string[]) => JSON.stringify(args);
|
|
@@ -39,9 +39,10 @@ describe('MockPipe', () => {
|
|
|
39
39
|
// const spy = jest.fn().mockImplementation(fakeTransform);
|
|
40
40
|
|
|
41
41
|
beforeEach(() => {
|
|
42
|
-
return
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
return (
|
|
43
|
+
MockBuilder(TargetComponent, ItsModule)
|
|
44
|
+
// DependencyPipe is a declaration in ItsModule
|
|
45
|
+
.mock(DependencyPipe, spy)
|
|
45
46
|
);
|
|
46
47
|
});
|
|
47
48
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Component, forwardRef } from '@angular/core';
|
|
1
|
+
import { Component, forwardRef, NgModule } from '@angular/core';
|
|
2
2
|
import {
|
|
3
3
|
ControlValueAccessor,
|
|
4
4
|
FormControl,
|
|
@@ -34,18 +34,24 @@ class DependencyComponent implements ControlValueAccessor {
|
|
|
34
34
|
selector: 'tested',
|
|
35
35
|
template: ' <app-child [formControl]="formControl"></app-child> ',
|
|
36
36
|
})
|
|
37
|
-
class
|
|
37
|
+
class MyComponent {
|
|
38
38
|
public readonly formControl = new FormControl();
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
@NgModule({
|
|
42
|
+
imports: [ReactiveFormsModule],
|
|
43
|
+
declarations: [MyComponent, DependencyComponent],
|
|
44
|
+
})
|
|
45
|
+
class ItsModule {}
|
|
46
|
+
|
|
41
47
|
describe('MockReactiveForms', () => {
|
|
42
48
|
// Helps to reset MockInstance customizations after each test.
|
|
43
49
|
MockInstance.scope();
|
|
44
50
|
|
|
45
51
|
beforeEach(() => {
|
|
46
|
-
return MockBuilder(
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
return MockBuilder(MyComponent, ItsModule).keep(
|
|
53
|
+
ReactiveFormsModule,
|
|
54
|
+
);
|
|
49
55
|
});
|
|
50
56
|
|
|
51
57
|
it('sends the correct value to the mock form component', () => {
|
|
@@ -62,7 +68,7 @@ describe('MockReactiveForms', () => {
|
|
|
62
68
|
// the spy via MockInstance before the render.
|
|
63
69
|
MockInstance(DependencyComponent, 'writeValue', writeValue);
|
|
64
70
|
|
|
65
|
-
const fixture = MockRender(
|
|
71
|
+
const fixture = MockRender(MyComponent);
|
|
66
72
|
const component = fixture.point.componentInstance;
|
|
67
73
|
|
|
68
74
|
// During initialization it should be called
|
|
@@ -37,7 +37,7 @@ class DependencyModule {}
|
|
|
37
37
|
></app-child>
|
|
38
38
|
`,
|
|
39
39
|
})
|
|
40
|
-
class
|
|
40
|
+
class MyComponent {
|
|
41
41
|
@Output() public readonly trigger = new EventEmitter();
|
|
42
42
|
@Input() public value1 = 'default1';
|
|
43
43
|
@Input() public value2 = 'default2';
|
|
@@ -45,7 +45,7 @@ class TestedComponent {
|
|
|
45
45
|
|
|
46
46
|
describe('MockRender', () => {
|
|
47
47
|
// Do not forget to return the promise of MockBuilder.
|
|
48
|
-
beforeEach(() => MockBuilder(
|
|
48
|
+
beforeEach(() => MockBuilder(MyComponent, DependencyModule));
|
|
49
49
|
|
|
50
50
|
it('renders template', () => {
|
|
51
51
|
const spy =
|
|
@@ -93,7 +93,7 @@ describe('MockRender', () => {
|
|
|
93
93
|
// Generates a template like:
|
|
94
94
|
// <tested [value1]="value1" [value2]="value2"
|
|
95
95
|
// (trigger)="trigger"></tested>.
|
|
96
|
-
const fixture = MockRender(
|
|
96
|
+
const fixture = MockRender(MyComponent, {
|
|
97
97
|
trigger: spy,
|
|
98
98
|
value1: 'something2',
|
|
99
99
|
});
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
MockBuilder,
|
|
19
19
|
MockRender,
|
|
20
20
|
NG_MOCKS_INTERCEPTORS,
|
|
21
|
+
ngMocks,
|
|
21
22
|
} from 'ng-mocks';
|
|
22
23
|
|
|
23
24
|
// An interceptor we want to test.
|
|
@@ -92,13 +93,13 @@ describe('TestHttpInterceptor', () => {
|
|
|
92
93
|
});
|
|
93
94
|
|
|
94
95
|
it('triggers interceptor', () => {
|
|
95
|
-
|
|
96
|
-
const client: HttpClient =
|
|
97
|
-
fixture.debugElement.injector.get(HttpClient);
|
|
98
|
-
const httpMock: HttpTestingController =
|
|
99
|
-
fixture.debugElement.injector.get(HttpTestingController);
|
|
96
|
+
MockRender();
|
|
100
97
|
|
|
101
|
-
// Let's
|
|
98
|
+
// Let's extract the service and http controller for testing.
|
|
99
|
+
const client = ngMocks.findInstance(HttpClient);
|
|
100
|
+
const httpMock = ngMocks.findInstance(HttpTestingController);
|
|
101
|
+
|
|
102
|
+
// Let's do a simple request.
|
|
102
103
|
client.get('/target').subscribe();
|
|
103
104
|
|
|
104
105
|
// Now we can assert that a header has been added to the request.
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
import { Injectable, NgModule } from '@angular/core';
|
|
7
7
|
import { Observable } from 'rxjs';
|
|
8
8
|
|
|
9
|
-
import { MockBuilder, MockRender } from 'ng-mocks';
|
|
9
|
+
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';
|
|
10
10
|
|
|
11
11
|
// A service that does http requests.
|
|
12
12
|
@Injectable()
|
|
@@ -39,12 +39,11 @@ describe('TestHttpRequest', () => {
|
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
it('sends a request', () => {
|
|
42
|
-
|
|
42
|
+
MockRender();
|
|
43
|
+
|
|
43
44
|
// Let's extract the service and http controller for testing.
|
|
44
|
-
const service
|
|
45
|
-
|
|
46
|
-
const httpMock: HttpTestingController =
|
|
47
|
-
fixture.debugElement.injector.get(HttpTestingController);
|
|
45
|
+
const service = ngMocks.findInstance(TargetService);
|
|
46
|
+
const httpMock = ngMocks.findInstance(HttpTestingController);
|
|
48
47
|
|
|
49
48
|
// A simple subscription to check what the service returns.
|
|
50
49
|
let actual: any;
|
|
@@ -100,7 +100,7 @@ describe('TestLifecycleHooks', () => {
|
|
|
100
100
|
const service: TargetService =
|
|
101
101
|
fixture.debugElement.injector.get(TargetService);
|
|
102
102
|
|
|
103
|
-
// By default nothing should be initialized.
|
|
103
|
+
// By default, nothing should be initialized.
|
|
104
104
|
expect(service.onChanges).toHaveBeenCalledTimes(0);
|
|
105
105
|
|
|
106
106
|
// Now let's render the component.
|
|
@@ -28,15 +28,15 @@ describe('TestPipe', () => {
|
|
|
28
28
|
beforeEach(() => MockBuilder(TargetPipe));
|
|
29
29
|
|
|
30
30
|
it('sorts strings', () => {
|
|
31
|
-
const fixture = MockRender(
|
|
32
|
-
|
|
31
|
+
const fixture = MockRender(TargetPipe, {
|
|
32
|
+
$implicit: ['1', '3', '2'],
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
expect(fixture.nativeElement.innerHTML).toEqual('1, 2, 3');
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
it('reverses strings on param', () => {
|
|
39
|
-
const fixture = MockRender('{{ values | target:flag}}', {
|
|
39
|
+
const fixture = MockRender('{{ values | target:flag }}', {
|
|
40
40
|
flag: false,
|
|
41
41
|
values: ['1', '3', '2'],
|
|
42
42
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Component, Injectable } from '@angular/core';
|
|
2
2
|
|
|
3
|
-
import { MockBuilder, MockRender } from 'ng-mocks';
|
|
3
|
+
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';
|
|
4
4
|
|
|
5
5
|
// A simple service, might have contained more logic,
|
|
6
6
|
// but it is redundant for the test demonstration.
|
|
@@ -31,9 +31,9 @@ describe('TestProviderInComponent', () => {
|
|
|
31
31
|
// to access the service.
|
|
32
32
|
const fixture = MockRender(TargetComponent);
|
|
33
33
|
|
|
34
|
-
// The
|
|
35
|
-
//
|
|
36
|
-
const service = fixture.point
|
|
34
|
+
// The component's element is fixture.point.
|
|
35
|
+
// Now we can use ngMocks.get to extract internal services.
|
|
36
|
+
const service = ngMocks.get(fixture.point, TargetService);
|
|
37
37
|
|
|
38
38
|
// Here we go, now we can assert everything about the service.
|
|
39
39
|
expect(service.value).toEqual('target');
|
|
@@ -49,26 +49,30 @@ describe('TestProviderInDirective', () => {
|
|
|
49
49
|
beforeEach(() => MockBuilder(TargetService, TargetDirective));
|
|
50
50
|
|
|
51
51
|
it('has access to the service via a directive', () => {
|
|
52
|
-
// Let's render a div with the directive.
|
|
53
|
-
|
|
54
|
-
const fixture = MockRender('<div target></div>');
|
|
52
|
+
// Let's render a div with the directive.
|
|
53
|
+
MockRender('<div target></div>');
|
|
55
54
|
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
const
|
|
55
|
+
// Let's find the debugElement with the directive.
|
|
56
|
+
// Please note, that we use ngMocks.find here.
|
|
57
|
+
const el = ngMocks.find(TargetDirective);
|
|
58
|
+
|
|
59
|
+
// Let's extract the service.
|
|
60
|
+
const service = ngMocks.get(el, TargetService);
|
|
59
61
|
|
|
60
62
|
// Here we go, now we can assert everything about the service.
|
|
61
63
|
expect(service.value).toEqual(true);
|
|
62
64
|
});
|
|
63
65
|
|
|
64
66
|
it('has access to the service via a structural directive', () => {
|
|
65
|
-
// Let's render a div with the directive.
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
// Let's render a div with the directive.
|
|
68
|
+
MockRender('<div *target></div>');
|
|
69
|
+
|
|
70
|
+
// Let's find the debugNode with the directive.
|
|
71
|
+
// Please note, that we use ngMocks.reveal here.
|
|
72
|
+
const node = ngMocks.reveal(TargetDirective);
|
|
68
73
|
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
const service = fixture.point.injector.get(TargetService);
|
|
74
|
+
// Let's extract the service.
|
|
75
|
+
const service = ngMocks.get(node, TargetService);
|
|
72
76
|
|
|
73
77
|
// Here we go, now we can assert everything about the service.
|
|
74
78
|
expect(service.value).toEqual(true);
|
|
@@ -60,8 +60,9 @@ class TargetModule {}
|
|
|
60
60
|
|
|
61
61
|
describe('TestRoute:Route', () => {
|
|
62
62
|
beforeEach(() => {
|
|
63
|
-
return MockBuilder(
|
|
64
|
-
RouterTestingModule.withRoutes([]),
|
|
63
|
+
return MockBuilder(
|
|
64
|
+
[RouterModule, RouterTestingModule.withRoutes([])],
|
|
65
|
+
TargetModule,
|
|
65
66
|
);
|
|
66
67
|
});
|
|
67
68
|
|
|
@@ -111,9 +112,14 @@ describe('TestRoute:Component', () => {
|
|
|
111
112
|
// RouterTestingModule.withRoutes([]), yes yes, with empty routes
|
|
112
113
|
// to have tools for testing.
|
|
113
114
|
beforeEach(() => {
|
|
114
|
-
return MockBuilder(
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
return MockBuilder(
|
|
116
|
+
[
|
|
117
|
+
TargetComponent,
|
|
118
|
+
RouterModule,
|
|
119
|
+
RouterTestingModule.withRoutes([]),
|
|
120
|
+
],
|
|
121
|
+
TargetModule,
|
|
122
|
+
);
|
|
117
123
|
});
|
|
118
124
|
|
|
119
125
|
it('navigates between pages', fakeAsync(() => {
|
|
@@ -127,8 +133,8 @@ describe('TestRoute:Component', () => {
|
|
|
127
133
|
tick(); // is needed for rendering of the current route.
|
|
128
134
|
}
|
|
129
135
|
|
|
130
|
-
// By default our routes do not have a component.
|
|
131
|
-
// Therefore
|
|
136
|
+
// By default, our routes do not have a component.
|
|
137
|
+
// Therefore, none of them should be rendered.
|
|
132
138
|
expect(location.path()).toEqual('/');
|
|
133
139
|
expect(() => ngMocks.find(Target1Component)).toThrow();
|
|
134
140
|
expect(() => ngMocks.find(Target2Component)).toThrow();
|
|
@@ -115,10 +115,10 @@ describe('TestRoutingGuard', () => {
|
|
|
115
115
|
// to have tools for testing. And the last thing is to exclude
|
|
116
116
|
// `NG_MOCKS_GUARDS` to remove all other guards.
|
|
117
117
|
beforeEach(() => {
|
|
118
|
-
return MockBuilder(
|
|
119
|
-
.
|
|
120
|
-
|
|
121
|
-
|
|
118
|
+
return MockBuilder(
|
|
119
|
+
[LoginGuard, RouterModule, RouterTestingModule.withRoutes([])],
|
|
120
|
+
TargetModule,
|
|
121
|
+
).exclude(NG_MOCKS_GUARDS);
|
|
122
122
|
});
|
|
123
123
|
|
|
124
124
|
// It is important to run routing tests in fakeAsync.
|
|
@@ -88,9 +88,14 @@ describe('TestRoutingResolver', () => {
|
|
|
88
88
|
// add RouterTestingModule.withRoutes([]), yes yes, with empty
|
|
89
89
|
// routes to have tools for testing.
|
|
90
90
|
beforeEach(() => {
|
|
91
|
-
return MockBuilder(
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
return MockBuilder(
|
|
92
|
+
[
|
|
93
|
+
DataResolver,
|
|
94
|
+
RouterModule,
|
|
95
|
+
RouterTestingModule.withRoutes([]),
|
|
96
|
+
],
|
|
97
|
+
TargetModule,
|
|
98
|
+
);
|
|
94
99
|
});
|
|
95
100
|
|
|
96
101
|
// It is important to run routing tests in fakeAsync.
|