Skip to content

Commit 65da18d

Browse files
authored
Merge pull request #1993 from satanTime/issues/1256
feat(MockInstance): resets root overrides likewise properties #1256
2 parents 90acfa5 + a903556 commit 65da18d

File tree

4 files changed

+58
-77
lines changed

4 files changed

+58
-77
lines changed

libs/ng-mocks/src/lib/common/ng-mocks-universe.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ ngMocksUniverse.global.set('flags', {
5454
onTestBedFlushNeed: coreConfig.onTestBedFlushNeed,
5555
});
5656

57-
ngMocksUniverse.getLocalMocks = () => {
58-
if (!ngMocksUniverse.global.has('local-mocks')) {
59-
ngMocksUniverse.global.set('local-mocks', []);
60-
}
61-
62-
return ngMocksUniverse.global.get('local-mocks');
63-
};
64-
6557
ngMocksUniverse.getOverrides = globalMap('overrides');
6658
ngMocksUniverse.getDefaults = globalMap('defaults');
6759

libs/ng-mocks/src/lib/mock-instance/mock-instance-apply.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ export default (def: any): any[] => {
55
const callbacks = [];
66

77
const config = ngMocksUniverse.configInstance.get(def);
8-
if (config?.init) {
9-
callbacks.push(config.init);
10-
}
118
if (config?.overloads) {
129
for (const [name, stub, encapsulation] of config.overloads) {
13-
callbacks.push((instance: any) => {
14-
mockHelperStubMember(instance, name, stub, encapsulation);
15-
});
10+
if (name) {
11+
callbacks.push((instance: any) => {
12+
mockHelperStubMember(instance, name, stub, encapsulation);
13+
});
14+
} else {
15+
callbacks.push(stub);
16+
}
1617
}
1718
}
1819

libs/ng-mocks/src/lib/mock-instance/mock-instance.ts

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,6 @@ ngMocksStack.subscribePop((state, stack) => {
2626
currentStack = stack[stack.length - 1];
2727
});
2828

29-
ngMocksStack.subscribePush(() => {
30-
// On start we have to flush any caches,
31-
// they are not from this spec.
32-
const set = ngMocksUniverse.getLocalMocks();
33-
set.splice(0, set.length);
34-
});
35-
ngMocksStack.subscribePop(() => {
36-
const set = ngMocksUniverse.getLocalMocks();
37-
while (set.length) {
38-
const [declaration, config] = set.pop() || /* istanbul ignore next */ [];
39-
const universeConfig = ngMocksUniverse.configInstance.has(declaration)
40-
? ngMocksUniverse.configInstance.get(declaration)
41-
: /* istanbul ignore next */ {};
42-
ngMocksUniverse.configInstance.set(declaration, {
43-
...universeConfig,
44-
...config,
45-
});
46-
}
47-
});
48-
49-
const restore = (declaration: any, config: any): void => {
50-
ngMocksUniverse.getLocalMocks().push([declaration, config]);
51-
};
52-
5329
interface MockInstanceArgs {
5430
accessor?: 'get' | 'set';
5531
data?: any;
@@ -65,7 +41,10 @@ const parseMockInstanceArgs = (args: any[]): MockInstanceArgs => {
6541
set.value = args[1];
6642
set.accessor = args[2];
6743
} else {
68-
set.data = args[0];
44+
set.value = args[0];
45+
if (typeof set.value !== 'function') {
46+
set.value = set.value?.init;
47+
}
6948
}
7049

7150
return set;
@@ -82,41 +61,9 @@ if (typeof beforeEach !== 'undefined') {
8261
afterEach(() => (checkCollect = false));
8362
}
8463

85-
const mockInstanceConfig = <T>(declaration: Type<T> | AbstractType<T> | InjectionToken<T>, data?: any): void => {
86-
const config = typeof data === 'function' ? { init: data } : data;
87-
const universeConfig = ngMocksUniverse.configInstance.has(declaration)
88-
? ngMocksUniverse.configInstance.get(declaration)
89-
: {};
90-
restore(declaration, universeConfig);
91-
92-
if (config) {
93-
ngMocksUniverse.configInstance.set(declaration, {
94-
...universeConfig,
95-
...config,
96-
});
97-
} else {
98-
ngMocksUniverse.configInstance.set(declaration, {
99-
...universeConfig,
100-
init: undefined,
101-
overloads: [],
102-
});
103-
}
104-
105-
if (!config) {
106-
// When we are calling MockInstance without a config we need to reset it from the checks too.
107-
for (let i = checkReset.length - 1; i >= 0; i -= 1) {
108-
if (checkReset[i][0] === declaration && checkReset[i][2] === currentStack) {
109-
checkReset.splice(i, 1);
110-
}
111-
}
112-
} else if (checkCollect) {
113-
checkReset.push([declaration, ngMocksUniverse.configInstance.get(declaration), currentStack]);
114-
}
115-
};
116-
117-
const mockInstanceMember = <T>(
64+
const mockInstanceConfig = <T>(
11865
declaration: Type<T> | AbstractType<T> | InjectionToken<T>,
119-
name: string,
66+
name: string | undefined,
12067
stub: any,
12168
encapsulation?: 'get' | 'set',
12269
) => {
@@ -230,12 +177,25 @@ export function MockInstance<T>(
230177
export function MockInstance<T>(declaration: Type<T> | AbstractType<T> | InjectionToken<T>, ...args: any[]) {
231178
funcImportExists(declaration, 'MockInstance');
232179

233-
const { key, value, accessor, data } = parseMockInstanceArgs(args);
234-
if (key) {
235-
return mockInstanceMember(declaration, key, value, accessor);
180+
const { key, value, accessor } = parseMockInstanceArgs(args);
181+
182+
if (value) {
183+
return mockInstanceConfig(declaration, key, value, accessor);
236184
}
237185

238-
mockInstanceConfig(declaration, data);
186+
const config = ngMocksUniverse.configInstance.get(declaration) || /* istanbul ignore next */ {};
187+
188+
ngMocksUniverse.configInstance.set(declaration, {
189+
...config,
190+
overloads: [],
191+
});
192+
193+
// When we are calling MockInstance without a config we need to reset it from the checks too.
194+
for (let i = checkReset.length - 1; i >= 0; i -= 1) {
195+
if (checkReset[i][0] === declaration && checkReset[i][2] === currentStack) {
196+
checkReset.splice(i, 1);
197+
}
198+
}
239199
}
240200

241201
/**

tests/issue-1256/test.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { InjectionToken } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
3+
import { MockInstance, MockProvider, MockReset } from 'ng-mocks';
4+
5+
const myToken = new InjectionToken('MY_TOKEN');
6+
7+
// @see https://github.com/ike18t/ng-mocks/issues/1256
8+
// global MockInstance doesn't reset own customizations
9+
describe('issue-1256', () => {
10+
MockInstance.scope();
11+
12+
beforeEach(() =>
13+
TestBed.configureTestingModule({
14+
providers: [MockProvider(myToken, { test: 1 })],
15+
}).compileComponents(),
16+
);
17+
18+
it('changes value #1', () => {
19+
MockInstance(myToken, () => ({ test: 2 }));
20+
const value: any = TestBed.get(myToken);
21+
expect(value.test).toEqual(2);
22+
});
23+
24+
it('uses the default value', () => {
25+
const value: any = TestBed.get(myToken);
26+
expect(value.test).toEqual(1);
27+
});
28+
});

0 commit comments

Comments
 (0)