ng-mocks 13.5.2 → 14.0.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 +11 -11
- 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/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/{ng-mocks.d.ts → index.d.ts} +21 -5
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/index.mjs +2 -0
- package/index.mjs.map +1 -0
- package/package.json +13 -9
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Directive,
|
|
3
|
+
Injectable,
|
|
4
|
+
Input,
|
|
5
|
+
OnInit,
|
|
6
|
+
VERSION,
|
|
7
|
+
} from '@angular/core';
|
|
8
|
+
|
|
9
|
+
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';
|
|
10
|
+
|
|
11
|
+
// @TODO remove with A5 support
|
|
12
|
+
const injectableRootServiceArgs = [
|
|
13
|
+
{
|
|
14
|
+
providedIn: 'root',
|
|
15
|
+
} as never,
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
// A root service we want to mock.
|
|
19
|
+
@Injectable(...injectableRootServiceArgs)
|
|
20
|
+
class RootService {
|
|
21
|
+
trigger(name: string | null) {
|
|
22
|
+
// does something very cool
|
|
23
|
+
|
|
24
|
+
return name;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A standalone directive we are going to test.
|
|
29
|
+
@Directive({
|
|
30
|
+
selector: 'standalone',
|
|
31
|
+
standalone: true,
|
|
32
|
+
} as never)
|
|
33
|
+
class StandaloneDirective implements OnInit {
|
|
34
|
+
@Input() public readonly name: string | null = null;
|
|
35
|
+
|
|
36
|
+
constructor(public readonly rootService: RootService) {}
|
|
37
|
+
|
|
38
|
+
ngOnInit(): void {
|
|
39
|
+
this.rootService.trigger(this.name);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('TestStandaloneDirective', () => {
|
|
44
|
+
if (Number.parseInt(VERSION.major, 10) < 14) {
|
|
45
|
+
it('needs a14', () => {
|
|
46
|
+
// pending('Need Angular > 5');
|
|
47
|
+
expect(true).toBeTruthy();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
return MockBuilder(StandaloneDirective);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('renders dependencies', () => {
|
|
58
|
+
// Rendering the directive.
|
|
59
|
+
MockRender(StandaloneDirective, {
|
|
60
|
+
name: 'test',
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Asserting that StandaloneDirective calls RootService.trigger.
|
|
64
|
+
const rootService = ngMocks.findInstance(RootService);
|
|
65
|
+
// it's possible because of autoSpy.
|
|
66
|
+
expect(rootService.trigger).toHaveBeenCalledWith('test');
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Injectable,
|
|
3
|
+
Pipe,
|
|
4
|
+
PipeTransform,
|
|
5
|
+
VERSION,
|
|
6
|
+
} from '@angular/core';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
MockBuilder,
|
|
10
|
+
MockInstance,
|
|
11
|
+
MockRender,
|
|
12
|
+
ngMocks,
|
|
13
|
+
} from 'ng-mocks';
|
|
14
|
+
|
|
15
|
+
// @TODO remove with A5 support
|
|
16
|
+
const injectableRootServiceArgs = [
|
|
17
|
+
{
|
|
18
|
+
providedIn: 'root',
|
|
19
|
+
} as never,
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
// A root service we want to mock.
|
|
23
|
+
@Injectable(...injectableRootServiceArgs)
|
|
24
|
+
class RootService {
|
|
25
|
+
trigger(name: string) {
|
|
26
|
+
// does something very cool
|
|
27
|
+
|
|
28
|
+
return name;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// A standalone pipe we are going to test.
|
|
33
|
+
@Pipe({
|
|
34
|
+
name: 'standalone',
|
|
35
|
+
standalone: true,
|
|
36
|
+
} as never)
|
|
37
|
+
class StandalonePipe implements PipeTransform {
|
|
38
|
+
constructor(public readonly rootService: RootService) {}
|
|
39
|
+
|
|
40
|
+
transform(value: string): string {
|
|
41
|
+
return this.rootService.trigger(value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('TestStandalonePipe', () => {
|
|
46
|
+
if (Number.parseInt(VERSION.major, 10) < 14) {
|
|
47
|
+
it('needs a14', () => {
|
|
48
|
+
// pending('Need Angular > 5');
|
|
49
|
+
expect(true).toBeTruthy();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// It creates a context for mocks which will be reset after each test.
|
|
56
|
+
MockInstance.scope();
|
|
57
|
+
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
return MockBuilder(StandalonePipe);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('renders dependencies', () => {
|
|
63
|
+
// Customizing what RootService does.
|
|
64
|
+
MockInstance(
|
|
65
|
+
RootService,
|
|
66
|
+
'trigger',
|
|
67
|
+
typeof jest === 'undefined'
|
|
68
|
+
? jasmine.createSpy().and.returnValue('mock')
|
|
69
|
+
: jest.fn().mockReturnValue('mock'),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
// Rendering the pipe.
|
|
73
|
+
const fixture = MockRender(StandalonePipe, {
|
|
74
|
+
$implicit: 'test',
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Asserting that StandalonePipe calls RootService.trigger.
|
|
78
|
+
const rootService = ngMocks.findInstance(RootService);
|
|
79
|
+
// It's possible because of autoSpy.
|
|
80
|
+
expect(rootService.trigger).toHaveBeenCalledWith('test');
|
|
81
|
+
|
|
82
|
+
// Asserting that StandalonePipe has rendered the result of the RootService
|
|
83
|
+
expect(ngMocks.formatText(fixture)).toEqual('mock');
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -52,12 +52,10 @@ describe('TestToken', () => {
|
|
|
52
52
|
// initialization we need to pass its module as the second
|
|
53
53
|
// parameter.
|
|
54
54
|
beforeEach(() => {
|
|
55
|
-
return MockBuilder(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
.keep(TOKEN_FACTORY)
|
|
60
|
-
.keep(TOKEN_VALUE);
|
|
55
|
+
return MockBuilder(
|
|
56
|
+
[TOKEN_CLASS, TOKEN_EXISTING, TOKEN_FACTORY, TOKEN_VALUE],
|
|
57
|
+
TargetModule,
|
|
58
|
+
);
|
|
61
59
|
});
|
|
62
60
|
|
|
63
61
|
it('creates TOKEN_CLASS', () => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Component, Injectable } from '@angular/core';
|
|
1
|
+
import { Component, Injectable, NgModule } from '@angular/core';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
MockBuilder,
|
|
@@ -26,15 +26,20 @@ class TargetComponent {
|
|
|
26
26
|
public constructor(public readonly service: TargetService) {}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
@NgModule({
|
|
30
|
+
declarations: [TargetComponent],
|
|
31
|
+
providers: [TargetService],
|
|
32
|
+
})
|
|
33
|
+
class ItsModule {}
|
|
34
|
+
|
|
29
35
|
describe('examples:performance', () => {
|
|
30
36
|
describe('beforeEach:mock-instance', () => {
|
|
31
37
|
ngMocks.faster(); // <-- add it before
|
|
32
38
|
|
|
33
39
|
// A normal setup of the TestBed, TargetService will be replaced
|
|
34
40
|
// with its mock copy.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
});
|
|
41
|
+
// TargetService is a provider in ItsModule.
|
|
42
|
+
beforeEach(() => MockBuilder(TargetComponent, ItsModule));
|
|
38
43
|
|
|
39
44
|
// Configuring behavior of the mock TargetService.
|
|
40
45
|
beforeAll(() => {
|
|
@@ -108,7 +113,11 @@ describe('examples:performance', () => {
|
|
|
108
113
|
// A normal setup of the TestBed, TargetService will be replaced
|
|
109
114
|
// with its mock copy.
|
|
110
115
|
beforeEach(() => {
|
|
111
|
-
return
|
|
116
|
+
return (
|
|
117
|
+
MockBuilder(TargetComponent, ItsModule)
|
|
118
|
+
// TargetService is a provider in ItsModule.
|
|
119
|
+
.mock(TargetService, mock)
|
|
120
|
+
);
|
|
112
121
|
});
|
|
113
122
|
|
|
114
123
|
it('test:1', () => {
|
|
@@ -8,7 +8,7 @@ interface Type<T> extends Function {
|
|
|
8
8
|
new (...args: any[]): T;
|
|
9
9
|
}
|
|
10
10
|
declare type AnyType<T> = Type<T> | AbstractType<T>;
|
|
11
|
-
declare type AnyDeclaration<T> = AnyType<T> | InjectionToken<T
|
|
11
|
+
declare type AnyDeclaration<T> = AnyType<T> | InjectionToken<T> | string;
|
|
12
12
|
declare type DebugNodeSelector = DebugNode | ComponentFixture<any> | string | [
|
|
13
13
|
string
|
|
14
14
|
] | [
|
|
@@ -106,6 +106,10 @@ export interface IMockBuilderConfigAll {
|
|
|
106
106
|
* @see https://ng-mocks.sudo.eu/api/MockBuilder#export-flag
|
|
107
107
|
*/
|
|
108
108
|
export?: boolean;
|
|
109
|
+
/**
|
|
110
|
+
* @see https://ng-mocks.sudo.eu/api/MockBuilder#shallow-flag
|
|
111
|
+
*/
|
|
112
|
+
shallow?: boolean;
|
|
109
113
|
}
|
|
110
114
|
/**
|
|
111
115
|
* The interface with flags which are suitable for modules in MockBuilder chain functions.
|
|
@@ -612,7 +616,7 @@ export declare const isMockControlValueAccessor: <T>(value: T) => value is T & M
|
|
|
612
616
|
* isMockNgDef(ArbitraryClass, 'c'); // returns false
|
|
613
617
|
* ```
|
|
614
618
|
*/
|
|
615
|
-
export declare function isMockNgDef<T>(component:
|
|
619
|
+
export declare function isMockNgDef<T>(component: AnyType<T>, ngType: "c"): component is Type<MockedComponent<T>>;
|
|
616
620
|
/**
|
|
617
621
|
* isMockNgDef verifies whether a class is a mock directive class.
|
|
618
622
|
*
|
|
@@ -624,7 +628,7 @@ export declare function isMockNgDef<T>(component: Type<T>, ngType: "c"): compone
|
|
|
624
628
|
* isMockNgDef(ArbitraryClass, 'd'); // returns false
|
|
625
629
|
* ```
|
|
626
630
|
*/
|
|
627
|
-
export declare function isMockNgDef<T>(directive:
|
|
631
|
+
export declare function isMockNgDef<T>(directive: AnyType<T>, ngType: "d"): directive is Type<MockedDirective<T>>;
|
|
628
632
|
/**
|
|
629
633
|
* isMockNgDef verifies whether a class is a mock pipe class.
|
|
630
634
|
*
|
|
@@ -636,7 +640,7 @@ export declare function isMockNgDef<T>(directive: Type<T>, ngType: "d"): directi
|
|
|
636
640
|
* isMockNgDef(ArbitraryClass, 'p'); // returns false
|
|
637
641
|
* ```
|
|
638
642
|
*/
|
|
639
|
-
export declare function isMockNgDef<T>(pipe:
|
|
643
|
+
export declare function isMockNgDef<T>(pipe: AnyType<T>, ngType: "p"): pipe is Type<MockedPipe<T>>;
|
|
640
644
|
/**
|
|
641
645
|
* isMockNgDef verifies whether a class is a mock module class.
|
|
642
646
|
*
|
|
@@ -648,7 +652,7 @@ export declare function isMockNgDef<T>(pipe: Type<T>, ngType: "p"): pipe is Type
|
|
|
648
652
|
* isMockNgDef(ArbitraryClass, 'm'); // returns false
|
|
649
653
|
* ```
|
|
650
654
|
*/
|
|
651
|
-
export declare function isMockNgDef<T>(module:
|
|
655
|
+
export declare function isMockNgDef<T>(module: AnyType<T>, ngType: "m"): module is Type<MockedModule<T>>;
|
|
652
656
|
/**
|
|
653
657
|
* isMockNgDef verifies whether a class is a mock class.
|
|
654
658
|
*
|
|
@@ -2072,6 +2076,17 @@ export declare const ngMocks: {
|
|
|
2072
2076
|
* ```
|
|
2073
2077
|
*/
|
|
2074
2078
|
get<T, D>(elSelector: DebugNodeSelector, provider: AnyDeclaration<T>, notFoundValue: D): D | T;
|
|
2079
|
+
/**
|
|
2080
|
+
* ngMocks.get tries to get an instance of provider or token for TestBed.
|
|
2081
|
+
*
|
|
2082
|
+
* @see https://ng-mocks.sudo.eu/api/ngMocks/get
|
|
2083
|
+
*
|
|
2084
|
+
* ```ts
|
|
2085
|
+
* const myComponent = ngMocks.get(MyComponent);
|
|
2086
|
+
* const myDirective = ngMocks.get(MyDirective);
|
|
2087
|
+
* ```
|
|
2088
|
+
*/
|
|
2089
|
+
get<T>(provider: AnyDeclaration<T>): T;
|
|
2075
2090
|
/**
|
|
2076
2091
|
* ngMocks.findInstance searches for an instance of declaration, provider or token,
|
|
2077
2092
|
* and returns the first one.
|
|
@@ -2404,6 +2419,7 @@ export declare const ngMocks: {
|
|
|
2404
2419
|
*/
|
|
2405
2420
|
config(config: {
|
|
2406
2421
|
mockRenderCacheSize?: number | null;
|
|
2422
|
+
onMockBuilderMissingDependency?: "throw" | "warn" | "i-know-but-disable" | null;
|
|
2407
2423
|
onMockInstanceRestoreNeed?: "throw" | "warn" | "i-know-but-disable" | null;
|
|
2408
2424
|
onTestBedFlushNeed?: "throw" | "warn" | "i-know-but-disable" | null;
|
|
2409
2425
|
}): void;
|