Skip to content

Commit

Permalink
fix(MockInstance): proper reset on empty config #1046
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Sep 12, 2021
1 parent ed53fd6 commit 7d1642d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
17 changes: 14 additions & 3 deletions docs/articles/api/MockInstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,27 @@ describe('suite', () => {

#### Scope

There is `MockInstance.scope()` to reduce code to one line:
There is `MockInstance.scope()` to reduce the code to one line:

```ts
describe('suite', () => {
// Uses beforeAll and afterAll.
MockInstance.scope('all');
MockInstance.scope('suite');

describe('sub suite', () => {
describe('sub suite #1', () => {
// Uses beforeEach and afterEach.
MockInstance.scope();
// the same
// MockInstance.scope('case');
});

describe('sub suite #2', () => {
// Uses beforeAll, afterAll,
// beforeEach and afterEach.
MockInstance.scope('all');
// the same
// MockInstance.scope('suite');
// MockInstance.scope('case');
});
});
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ngMocksUniverse from '../common/ng-mocks-universe';

export default (checkReset: Array<[any, any]>) => {
export default (checkReset: Array<[any, any, any?]>) => {
const showError: string[] = [];

// istanbul ignore next: because of the installed global scope switcher we cannot test this part
Expand Down
37 changes: 29 additions & 8 deletions libs/ng-mocks/src/lib/mock-instance/mock-instance.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// tslint:disable max-file-line-count

import { InjectionToken, Injector } from '@angular/core';

import { AbstractType, Type } from '../common/core.types';
Expand Down Expand Up @@ -36,7 +38,7 @@ ngMocksStack.subscribePop(() => {
const [declaration, config] = set.pop() || /* istanbul ignore next */ [];
const universeConfig = ngMocksUniverse.configInstance.has(declaration)
? ngMocksUniverse.configInstance.get(declaration)
: {};
: /* istanbul ignore next */ {};
ngMocksUniverse.configInstance.set(declaration, {
...universeConfig,
...config,
Expand Down Expand Up @@ -69,7 +71,7 @@ const parseMockInstanceArgs = (args: any[]): MockInstanceArgs => {
return set;
};

const checkReset: Array<[any, any]> = [];
const checkReset: Array<[any, any, any?]> = [];
let checkCollect = false;

// istanbul ignore else: maybe a different runner is used
Expand Down Expand Up @@ -100,8 +102,15 @@ const mockInstanceConfig = <T>(declaration: Type<T> | AbstractType<T> | Injectio
});
}

if (checkCollect) {
checkReset.push([declaration, ngMocksUniverse.configInstance.get(declaration)]);
if (!config) {
// When we are calling MockInstance without a config we need to reset it from the checks too.
for (let i = checkReset.length - 1; i >= 0; i -= 1) {
if (checkReset[i][0] === declaration && checkReset[i][2] === currentStack) {
checkReset.splice(i, 1);
}
}
} else if (checkCollect) {
checkReset.push([declaration, ngMocksUniverse.configInstance.get(declaration), currentStack]);
}
};

Expand All @@ -123,7 +132,7 @@ const mockInstanceMember = <T>(
currentStack.mockInstance = mockInstances;

if (checkCollect) {
checkReset.push([declaration, ngMocksUniverse.configInstance.get(declaration)]);
checkReset.push([declaration, ngMocksUniverse.configInstance.get(declaration), currentStack]);
}

return stub;
Expand Down Expand Up @@ -229,13 +238,25 @@ export function MockInstance<T>(declaration: Type<T> | AbstractType<T> | Injecti
mockInstanceConfig(declaration, data);
}

/**
* @see https://ng-mocks.sudo.eu/api/MockInstance#remember
*/
MockInstance.remember = () => ngMocksStack.stackPush();

/**
* @see https://ng-mocks.sudo.eu/api/MockInstance#restore
*/
MockInstance.restore = () => ngMocksStack.stackPop();
MockInstance.scope = (scope?: 'all') => {
if (scope === 'all') {

/**
* @see https://ng-mocks.sudo.eu/api/MockInstance#scope
*/
MockInstance.scope = (scope: 'all' | 'suite' | 'case' = 'case') => {
if (scope === 'all' || scope === 'suite') {
beforeAll(MockInstance.remember);
afterAll(MockInstance.restore);
} else {
}
if (scope === 'all' || scope === 'case') {
beforeEach(MockInstance.remember);
afterEach(MockInstance.restore);
}
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"max-file-line-count": [true, 250],
"max-func-body-length": [
true,
30,
50,
{
"ignore-parameters-to-function-regex": "^(([fx]?(describe|it))|beforeEach|fakeAsync|inject)$"
}
Expand Down

0 comments on commit 7d1642d

Please sign in to comment.