Skip to content

Commit

Permalink
feat: renaming ngMocks.default to ngMocks.global
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 12, 2021
1 parent 7da5fc1 commit d9f46d3
Show file tree
Hide file tree
Showing 34 changed files with 389 additions and 138 deletions.
75 changes: 51 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2501,10 +2501,11 @@ describe('MockInstance', () => {

- [`.guts()`](#ngmocksguts)
- [`.defaultMock()`](#ngmocksdefaultmock)
- [`.defaultExclude()`](#ngmocksdefaultexclude)
- [`.defaultKeep()`](#ngmocksdefaultkeep)
- [`.defaultReplace()`](#ngmocksdefaultreplace)
- [`.defaultWipe()`](#ngmocksdefaultwipe)
- [`.globalExclude()`](#ngmocksglobalexclude)
- [`.globalKeep()`](#ngmocksglobalkeep)
- [`.globalMock()`](#ngmocksglobalmock)
- [`.globalReplace()`](#ngmocksglobalreplace)
- [`.globalWipe()`](#ngmocksglobalwipe)
- [`.get()`](#ngmocksget)
- [`.findInstance()`](#ngmocksfindinstance)
- [`.findInstances()`](#ngmocksfindinstances)
Expand Down Expand Up @@ -2602,9 +2603,9 @@ ngMocks.defaultMock(MyComponent, (_, injector) => ({
ngMocks.defaultMock(MyComponent);
```

#### ngMocks.defaultExclude
#### ngMocks.globalExclude

`ngMocks.defaultExclude` marks declarations, services and tokens to be excluded during creating mock modules.
`ngMocks.globalExclude` marks declarations, services and tokens to be excluded during creating mock modules.

The best place to do that is in `src/test.ts` for jasmine or in `src/setupJest.ts` for jest.

Expand All @@ -2619,7 +2620,7 @@ If we import a module that imports `TranslationModule` in tests,
then this effect of `initTestEnvironment` will be overloaded.

To keep the effect, we need to exclude `TranslationModule` during the mocking process.
That is where `ngMocks.defaultExclude` comes for help.
That is where `ngMocks.globalExclude` comes for help.

```ts
// test.ts
Expand All @@ -2634,24 +2635,24 @@ getTestBed().initTestEnvironment(
platformBrowserDynamicTesting(),
);

ngMocks.defaultExclude(TranslationModule);
ngMocks.globalExclude(TranslationModule);
```

Now, if we call `MockModule(ModuleWithTranslationModule)`,
the `TranslationModule` will be excluded out of the final mock module,
and, consequently, the version from `initTestEnvironment` will be used.

#### ngMocks.defaultKeep
#### ngMocks.globalKeep

`ngMocks.defaultExclude` marks declarations, services and tokens to be avoided from the mocking process during creating mock modules.
`ngMocks.globalExclude` marks declarations, services and tokens to be avoided from the mocking process during creating mock modules.

The best place to do that is in `src/test.ts` for jasmine or in `src/setupJest.ts` for jest.

Let's mark the `APP_URL` token in order to be kept in mock modules.

```ts
// test.ts
ngMocks.defaultKeep(APP_URL);
ngMocks.globalKeep(APP_URL);
```

```ts
Expand All @@ -2665,9 +2666,35 @@ const url = TestBed.inject(APP_URL);

The `url` is the original one.

#### ngMocks.defaultReplace
#### ngMocks.globalMock

`ngMocks.defaultReplace` marks declarations and modules (but not services and tokens) to be replaced during creating mock modules.
`ngMocks.globalMock` marks declarations, services and tokens to be mocked if they are appearing in kept modules during creating mock modules.

The best place to do that is in `src/test.ts` for jasmine or in `src/setupJest.ts` for jest.

Let's mark the `APP_URL` token in order to be mocked in its kept modules.

```ts
// test.ts
ngMocks.globalKeep(AppModule);
ngMocks.globalMock(APP_URL);
ngMocks.defaultMock(APP_URL, () => 'mock');
```

```ts
// test.spec.ts
// ...
MockModule(AppModule);
// ...
const url = TestBed.inject(APP_URL);
// ...
```

The `url` is `mock`.

#### ngMocks.globalReplace

`ngMocks.globalReplace` marks declarations and modules (but not services and tokens) to be replaced during creating mock modules.

The best place to do that is in `src/test.ts` for jasmine or in `src/setupJest.ts` for jest.

Expand All @@ -2676,27 +2703,27 @@ we could do it like that:

```ts
// test.ts
ngMocks.defaultReplace(BrowserAnimationsModule, NoopAnimationsModule);
ngMocks.globalReplace(BrowserAnimationsModule, NoopAnimationsModule);
```

Now, all mock modules which import `BrowserAnimationsModule` have `NoopAnimationsModule` instead.

#### ngMocks.defaultWipe
#### ngMocks.globalWipe

`ngMocks.defaultWipe` resets all customizations which have been done by any `ngMocks.default` function.
`ngMocks.globalWipe` resets all customizations which have been done by any `ngMocks.default` function.

```ts
ngMocks.defaultMock(Service, () => ({
stream$: EMPTY,
}));
ngMocks.defaultExclude(Component);
ngMocks.defaultKeep(Directive);
ngMocks.defaultReplace(Pipe, FakePipe);

ngMocks.defaultWipe(Service);
ngMocks.defaultWipe(Component);
ngMocks.defaultWipe(Directive);
ngMocks.defaultWipe(Pipe);
ngMocks.globalExclude(Component);
ngMocks.globalKeep(Directive);
ngMocks.globalReplace(Pipe, FakePipe);

ngMocks.globalWipe(Service);
ngMocks.globalWipe(Component);
ngMocks.globalWipe(Directive);
ngMocks.globalWipe(Pipe);

// All the things above will be mocked as usual
```
Expand Down
42 changes: 25 additions & 17 deletions lib/common/ng-mocks-universe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface NgMocksUniverse {
configInstance: Map<any, any>;
flags: Set<string>;
getBuildDeclaration: (def: any) => any | undefined;
getDefaults: () => Map<any, any>;
getDefaults: () => Map<any, undefined | ['mock' | 'keep' | 'replace' | 'exclude', any?]>;
getLocalMocks: () => Array<[any, any]>;
getOverrides: () => Map<any, any>;
getResolution: (def: any) => undefined | 'mock' | 'keep' | 'replace' | 'exclude';
Expand Down Expand Up @@ -58,7 +58,7 @@ ngMocksUniverse.getLocalMocks = () => {
ngMocksUniverse.getOverrides = globalMap('overrides');
ngMocksUniverse.getDefaults = globalMap('defaults');

ngMocksUniverse.getResolution = (def: any) => {
ngMocksUniverse.getResolution = (def: any): undefined | 'mock' | 'keep' | 'replace' | 'exclude' => {
const set = ngMocksUniverse.config.get('ngMocksDepsResolution');
if (set?.has(def)) {
return set.get(def);
Expand All @@ -68,35 +68,43 @@ ngMocksUniverse.getResolution = (def: any) => {
return undefined;
}

const value = ngMocksUniverse.getDefaults().get(def);
if (!value) {
return 'exclude';
}
if (def === value) {
return 'keep';
}
const [value] = ngMocksUniverse.getDefaults().get(def);

return 'replace';
return value;
};

ngMocksUniverse.getBuildDeclaration = (def: any) => {
ngMocksUniverse.getBuildDeclaration = (def: any): undefined | null | any => {
if (ngMocksUniverse.builtDeclarations.has(def)) {
return ngMocksUniverse.builtDeclarations.get(def);
}
if (ngMocksUniverse.getDefaults().has(def)) {
return ngMocksUniverse.getDefaults().get(def);
if (!ngMocksUniverse.getDefaults().has(def)) {
return;
}

const [mode, replacement] = ngMocksUniverse.getDefaults().get(def);

if (mode === 'exclude') {
return null;
}
if (mode === 'keep') {
return def;
}
if (mode === 'replace') {
return replacement;
}
};

ngMocksUniverse.hasBuildDeclaration = (def: any) => {
ngMocksUniverse.hasBuildDeclaration = (def: any): boolean => {
if (ngMocksUniverse.builtDeclarations.has(def)) {
return true;
}
if (ngMocksUniverse.getDefaults().has(def)) {
return true;
if (!ngMocksUniverse.getDefaults().has(def)) {
return false;
}

return false;
const [mode] = ngMocksUniverse.getDefaults().get(def);

return mode !== 'mock';
};

const hasBuildDeclaration = (def: any): boolean => ngMocksUniverse.hasBuildDeclaration(def);
Expand Down
6 changes: 6 additions & 0 deletions lib/mock-helper/func.global-prepare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ngMocksUniverse from '../common/ng-mocks-universe';

export default () => {
ngMocksUniverse.cacheDeclarations.clear();
ngMocksUniverse.config.get('ngMocksDepsSkip')?.clear();
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component } from '@angular/core';

import ngMocksUniverse from '../common/ng-mocks-universe';

import mockHelperDefaultExclude from './mock-helper.default-exclude';
import mockHelperGlobalExclude from './mock-helper.global-exclude';

@Component({
selector: 'target',
Expand All @@ -19,15 +19,15 @@ describe('mock-helper.default-exclude', () => {

it('resets cacheDeclarations', () => {
ngMocksUniverse.cacheDeclarations.set(TargetComponent, null);
mockHelperDefaultExclude(TargetComponent);
mockHelperGlobalExclude(TargetComponent);
expect(ngMocksUniverse.cacheDeclarations.size).toEqual(0);
});

it('resets ngMocksDepsSkip', () => {
const config = new Set();
config.add(TargetComponent);
ngMocksUniverse.config.set('ngMocksDepsSkip', config);
mockHelperDefaultExclude(TargetComponent);
mockHelperGlobalExclude(TargetComponent);
expect(
ngMocksUniverse.config.get('ngMocksDepsSkip').size,
).toEqual(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { InjectionToken } from '@angular/core';
import { AnyType } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';

import funcGlobalPrepare from './func.global-prepare';

export default (source: AnyType<any> | InjectionToken<any>): void => {
ngMocksUniverse.cacheDeclarations.clear();
ngMocksUniverse.config.get('ngMocksDepsSkip')?.clear();
ngMocksUniverse.getDefaults().set(source, null);
funcGlobalPrepare();
ngMocksUniverse.getDefaults().set(source, ['exclude']);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component } from '@angular/core';

import ngMocksUniverse from '../common/ng-mocks-universe';

import mockHelperDefaultKeep from './mock-helper.default-keep';
import mockHelperGlobalKeep from './mock-helper.global-keep';

@Component({
selector: 'target',
Expand All @@ -19,15 +19,15 @@ describe('mock-helper.default-keep', () => {

it('resets cacheDeclarations', () => {
ngMocksUniverse.cacheDeclarations.set(TargetComponent, null);
mockHelperDefaultKeep(TargetComponent);
mockHelperGlobalKeep(TargetComponent);
expect(ngMocksUniverse.cacheDeclarations.size).toEqual(0);
});

it('resets ngMocksDepsSkip', () => {
const config = new Set();
config.add(TargetComponent);
ngMocksUniverse.config.set('ngMocksDepsSkip', config);
mockHelperDefaultKeep(TargetComponent);
mockHelperGlobalKeep(TargetComponent);
expect(
ngMocksUniverse.config.get('ngMocksDepsSkip').size,
).toEqual(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { InjectionToken } from '@angular/core';
import { AnyType } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';

import funcGlobalPrepare from './func.global-prepare';

export default (source: AnyType<any> | InjectionToken<any>): void => {
ngMocksUniverse.cacheDeclarations.clear();
ngMocksUniverse.config.get('ngMocksDepsSkip')?.clear();
ngMocksUniverse.getDefaults().set(source, source);
funcGlobalPrepare();
ngMocksUniverse.getDefaults().set(source, ['keep']);
};
35 changes: 35 additions & 0 deletions lib/mock-helper/mock-helper.global-mock.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Component } from '@angular/core';

import ngMocksUniverse from '../common/ng-mocks-universe';

import mockHelperGlobalMock from './mock-helper.global-mock';

@Component({
selector: 'target',
template: '{{ name }}',
})
class TargetComponent {
public readonly name = 'target';
}

describe('mock-helper.default-mock', () => {
afterAll(() => {
ngMocksUniverse.config.delete('ngMocksDepsSkip');
});

it('resets cacheDeclarations', () => {
ngMocksUniverse.cacheDeclarations.set(TargetComponent, null);
mockHelperGlobalMock(TargetComponent);
expect(ngMocksUniverse.cacheDeclarations.size).toEqual(0);
});

it('resets ngMocksDepsSkip', () => {
const config = new Set();
config.add(TargetComponent);
ngMocksUniverse.config.set('ngMocksDepsSkip', config);
mockHelperGlobalMock(TargetComponent);
expect(
ngMocksUniverse.config.get('ngMocksDepsSkip').size,
).toEqual(0);
});
});
11 changes: 11 additions & 0 deletions lib/mock-helper/mock-helper.global-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { InjectionToken } from '@angular/core';

import { AnyType } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';

import funcGlobalPrepare from './func.global-prepare';

export default (source: AnyType<any> | InjectionToken<any>): void => {
funcGlobalPrepare();
ngMocksUniverse.getDefaults().set(source, ['mock']);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component } from '@angular/core';

import ngMocksUniverse from '../common/ng-mocks-universe';

import mockHelperDefaultReplace from './mock-helper.default-replace';
import mockHelperGlobalReplace from './mock-helper.global-replace';

@Component({
selector: 'target',
Expand All @@ -27,15 +27,15 @@ describe('mock-helper.default-replace', () => {

it('resets cacheDeclarations', () => {
ngMocksUniverse.cacheDeclarations.set(TargetComponent, null);
mockHelperDefaultReplace(TargetComponent, FakeComponent);
mockHelperGlobalReplace(TargetComponent, FakeComponent);
expect(ngMocksUniverse.cacheDeclarations.size).toEqual(0);
});

it('resets ngMocksDepsSkip', () => {
const config = new Set();
config.add(TargetComponent);
ngMocksUniverse.config.set('ngMocksDepsSkip', config);
mockHelperDefaultReplace(TargetComponent, FakeComponent);
mockHelperGlobalReplace(TargetComponent, FakeComponent);
expect(
ngMocksUniverse.config.get('ngMocksDepsSkip').size,
).toEqual(0);
Expand Down
Loading

0 comments on commit d9f46d3

Please sign in to comment.