Skip to content

Commit

Permalink
fix(mock-instance): a separate config scope
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 10, 2021
1 parent b00b1aa commit a0c930c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/common/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ const applyOverrides = (instance: any, mockOf: any, injector?: Injector): void =
if (instance.__ngMocksConfig.init) {
callbacks.push(instance.__ngMocksConfig.init);
}
if (ngMocksUniverse.config.get(mockOf)?.init) {
callbacks.push(ngMocksUniverse.config.get(mockOf).init);
if (ngMocksUniverse.configInstance.get(mockOf)?.init) {
callbacks.push(ngMocksUniverse.configInstance.get(mockOf).init);
}

for (const callback of callbacks) {
Expand Down
2 changes: 2 additions & 0 deletions lib/common/ng-mocks-universe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface NgMocksUniverse {
cacheDeclarations: Map<any, any>;
cacheProviders: Map<any, any>;
config: Map<any, any>;
configInstance: Map<any, any>;
flags: Set<string>;
getLocalMocks: () => Array<[any, any]>;
getOverrides: () => Map<any, any>;
Expand All @@ -29,6 +30,7 @@ ngMocksUniverse.builtProviders = new Map();
ngMocksUniverse.cacheDeclarations = new Map();
ngMocksUniverse.cacheProviders = new Map();
ngMocksUniverse.config = new Map();
ngMocksUniverse.configInstance = new Map();
ngMocksUniverse.flags = new Set(coreConfig.flags);
ngMocksUniverse.global = new Map();
ngMocksUniverse.touches = new Set();
Expand Down
2 changes: 2 additions & 0 deletions lib/mock-builder/mock-builder-stash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class MockBuilderStash {
cacheDeclarations: ngMocksUniverse.cacheDeclarations,
cacheProviders: ngMocksUniverse.cacheProviders,
config: ngMocksUniverse.config,
configInstance: ngMocksUniverse.configInstance,
flags: ngMocksUniverse.flags,
touches: ngMocksUniverse.touches,
};
Expand All @@ -20,6 +21,7 @@ export class MockBuilderStash {
ngMocksUniverse.cacheDeclarations = new Map();
ngMocksUniverse.cacheProviders = new Map();
ngMocksUniverse.config = new Map();
ngMocksUniverse.configInstance = new Map();
ngMocksUniverse.flags = new Set(coreConfig.flags);
ngMocksUniverse.touches = new Set();
}
Expand Down
1 change: 1 addition & 0 deletions lib/mock-helper/mock-helper.reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default (): void => {
ngMocksUniverse.cacheDeclarations = new Map();
ngMocksUniverse.cacheProviders = new Map();
ngMocksUniverse.config = new Map();
ngMocksUniverse.configInstance = new Map();
ngMocksUniverse.flags = new Set(coreConfig.flags);
ngMocksUniverse.touches = new Set();
};
16 changes: 10 additions & 6 deletions lib/mock-instance/mock-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ const reporter: jasmine.CustomReporter = {
const set = ngMocksUniverse.getLocalMocks();
while (set.length) {
const [declaration, config] = set.pop() || /* istanbul ignore next */ [];
const universeConfig = ngMocksUniverse.config.has(declaration) ? ngMocksUniverse.config.get(declaration) : {};
ngMocksUniverse.config.set(declaration, {
const universeConfig = ngMocksUniverse.configInstance.has(declaration)
? ngMocksUniverse.configInstance.get(declaration)
: {};
ngMocksUniverse.configInstance.set(declaration, {
...universeConfig,
...config,
});
Expand Down Expand Up @@ -71,22 +73,24 @@ export function MockInstance<T>(

export function MockInstance<T>(declaration: Type<T> | AbstractType<T> | InjectionToken<T>, data?: any) {
const config = typeof data === 'function' ? { init: data } : data;
const universeConfig = ngMocksUniverse.config.has(declaration) ? ngMocksUniverse.config.get(declaration) : {};
const universeConfig = ngMocksUniverse.configInstance.has(declaration)
? ngMocksUniverse.configInstance.get(declaration)
: {};
restore(declaration, universeConfig);

if (config) {
ngMocksUniverse.config.set(declaration, {
ngMocksUniverse.configInstance.set(declaration, {
...universeConfig,
...config,
});
} else {
ngMocksUniverse.config.set(declaration, {
ngMocksUniverse.configInstance.set(declaration, {
...universeConfig,
init: undefined,
});
}
}

export function MockReset() {
ngMocksUniverse.config.clear();
ngMocksUniverse.configInstance.clear();
}
4 changes: 2 additions & 2 deletions lib/mock-service/helper.use-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export default <D, I>(
if (overrides) {
callbacks.push(overrides);
}
if (ngMocksUniverse.config.get(def)?.init) {
callbacks.push(ngMocksUniverse.config.get(def).init);
if (ngMocksUniverse.configInstance.get(def)?.init) {
callbacks.push(ngMocksUniverse.configInstance.get(def).init);
}

return applyCallback(def, instance, callbacks, injector, overrides);
Expand Down

0 comments on commit a0c930c

Please sign in to comment.