ng-mocks 13.4.2 → 13.5.2
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 +0 -8
- package/examples/MockForms/test.spec.ts +6 -8
- 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/readme/builder.spec.ts +7 -7
- package/examples/readme/classic.spec.ts +8 -16
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/ng-mocks.d.ts +110 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -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
|
|
|
@@ -31,12 +31,7 @@ class DependencyComponent implements ControlValueAccessor {
|
|
|
31
31
|
|
|
32
32
|
@Component({
|
|
33
33
|
selector: 'tested',
|
|
34
|
-
template: `
|
|
35
|
-
<app-child
|
|
36
|
-
[ngModel]="value"
|
|
37
|
-
(ngModelChange)="value = $event"
|
|
38
|
-
></app-child>
|
|
39
|
-
`,
|
|
34
|
+
template: ` <app-child [(ngModel)]="value"></app-child> `,
|
|
40
35
|
})
|
|
41
36
|
class TestedComponent {
|
|
42
37
|
public value: any;
|
|
@@ -67,9 +62,12 @@ describe('MockForms', () => {
|
|
|
67
62
|
MockInstance(DependencyComponent, 'writeValue', writeValue);
|
|
68
63
|
|
|
69
64
|
const fixture = MockRender(TestedComponent);
|
|
65
|
+
// FormsModule needs fixture.whenStable()
|
|
66
|
+
// right after MockRender to install all hooks.
|
|
67
|
+
await fixture.whenStable();
|
|
70
68
|
const component = fixture.point.componentInstance;
|
|
71
69
|
|
|
72
|
-
// During initialization it should be called
|
|
70
|
+
// During initialization, it should be called
|
|
73
71
|
// with null.
|
|
74
72
|
expect(writeValue).toHaveBeenCalledWith(null);
|
|
75
73
|
|
|
@@ -77,12 +75,12 @@ describe('MockForms', () => {
|
|
|
77
75
|
// and simulate its change, like a user does it.
|
|
78
76
|
const mockControlEl = ngMocks.find(DependencyComponent);
|
|
79
77
|
ngMocks.change(mockControlEl, 'foo');
|
|
80
|
-
await fixture.whenStable();
|
|
81
78
|
expect(component.value).toBe('foo');
|
|
82
79
|
|
|
83
80
|
// Let's check that change on existing value
|
|
84
81
|
// causes calls of `writeValue` on the mock component.
|
|
85
82
|
component.value = 'bar';
|
|
83
|
+
// Both below are needed to trigger writeValue.
|
|
86
84
|
fixture.detectChanges();
|
|
87
85
|
await fixture.whenStable();
|
|
88
86
|
expect(writeValue).toHaveBeenCalledWith('bar');
|
|
@@ -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
|
});
|
|
@@ -26,9 +26,9 @@ export const EMPTY = new Subject<any>();
|
|
|
26
26
|
EMPTY.complete();
|
|
27
27
|
|
|
28
28
|
interface User {
|
|
29
|
-
email
|
|
30
|
-
firstName
|
|
31
|
-
lastName
|
|
29
|
+
email?: string | null;
|
|
30
|
+
firstName?: string | null;
|
|
31
|
+
lastName?: string | null;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
@Injectable()
|
|
@@ -56,9 +56,9 @@ class StorageService {
|
|
|
56
56
|
})
|
|
57
57
|
class ProfileComponent implements OnInit {
|
|
58
58
|
public readonly form = new FormGroup({
|
|
59
|
-
email: new FormControl(
|
|
60
|
-
firstName: new FormControl(
|
|
61
|
-
lastName: new FormControl(
|
|
59
|
+
email: new FormControl('', Validators.required),
|
|
60
|
+
firstName: new FormControl('', Validators.required),
|
|
61
|
+
lastName: new FormControl('', Validators.required),
|
|
62
62
|
});
|
|
63
63
|
|
|
64
64
|
@Input() public readonly profile: User | null = null;
|
|
@@ -67,7 +67,7 @@ class ProfileComponent implements OnInit {
|
|
|
67
67
|
|
|
68
68
|
public ngOnInit(): void {
|
|
69
69
|
if (this.profile) {
|
|
70
|
-
this.form.
|
|
70
|
+
this.form.patchValue(this.profile);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
|
|
@@ -28,9 +28,9 @@ export const EMPTY = new Subject<any>();
|
|
|
28
28
|
EMPTY.complete();
|
|
29
29
|
|
|
30
30
|
interface User {
|
|
31
|
-
email
|
|
32
|
-
firstName
|
|
33
|
-
lastName
|
|
31
|
+
email?: string | null;
|
|
32
|
+
firstName?: string | null;
|
|
33
|
+
lastName?: string | null;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
@Injectable()
|
|
@@ -58,9 +58,9 @@ class StorageService {
|
|
|
58
58
|
})
|
|
59
59
|
class ProfileComponent implements OnInit {
|
|
60
60
|
public readonly form = new FormGroup({
|
|
61
|
-
email: new FormControl(
|
|
62
|
-
firstName: new FormControl(
|
|
63
|
-
lastName: new FormControl(
|
|
61
|
+
email: new FormControl('', Validators.required),
|
|
62
|
+
firstName: new FormControl('', Validators.required),
|
|
63
|
+
lastName: new FormControl('', Validators.required),
|
|
64
64
|
});
|
|
65
65
|
|
|
66
66
|
@Input() public readonly profile: User | null = null;
|
|
@@ -69,7 +69,7 @@ class ProfileComponent implements OnInit {
|
|
|
69
69
|
|
|
70
70
|
public ngOnInit(): void {
|
|
71
71
|
if (this.profile) {
|
|
72
|
-
this.form.
|
|
72
|
+
this.form.patchValue(this.profile);
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
@@ -99,20 +99,12 @@ const assertion: any =
|
|
|
99
99
|
// In the following test suite, we would like to
|
|
100
100
|
// cover behavior of the component.
|
|
101
101
|
describe('profile:classic', () => {
|
|
102
|
-
// First of all, we would like to reuse the same
|
|
103
|
-
// TestBed in every test.
|
|
104
|
-
// ngMocks.faster suppresses reset of TestBed
|
|
105
|
-
// after each test and allows to use TestBed,
|
|
106
|
-
// MockBuilder and MockRender in beforeAll.
|
|
107
|
-
// https://ng-mocks.sudo.eu/api/ngMocks/faster
|
|
108
|
-
ngMocks.faster();
|
|
109
|
-
|
|
110
102
|
// Helps to reset customizations after each test.
|
|
111
103
|
MockInstance.scope();
|
|
112
104
|
|
|
113
105
|
// Let's declare TestBed in beforeAll instead of beforeEach.
|
|
114
106
|
// The code mocks everything in SharedModule and provides a mock AuthService.
|
|
115
|
-
|
|
107
|
+
beforeEach(async () => {
|
|
116
108
|
return TestBed.configureTestingModule({
|
|
117
109
|
imports: [
|
|
118
110
|
MockModule(SharedModule), // mock
|