-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ngMocks.defaultConfig): config for MockBuilder #971
- Loading branch information
Showing
17 changed files
with
473 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: ngMocks.defaultConfig | ||
description: Documentation about ngMocks.defaultConfig from ng-mocks library | ||
--- | ||
|
||
Sets default config for mocks in [`MockBuilder`](../MockBuilder.md#config). | ||
|
||
- `ngMocks.defaultConfig( Component, config )` - sets a default config for a component | ||
- `ngMocks.defaultConfig( Directive, config )` - sets a default config for a directive | ||
- `ngMocks.defaultConfig( Component )` - removes config | ||
- `ngMocks.defaultConfig( Directive )` - removes config | ||
|
||
The best place to do that is in `src/test.ts` for jasmine or in `src/setup-jest.ts` / `src/test-setup.ts` for `jest`. | ||
|
||
For example, if you have a simple structural directive which you always want to render it. | ||
Then, you can configure it via `ngMocks.defaultConfig`. | ||
|
||
```ts title="src/test.ts" | ||
// Config for a structural directive. | ||
ngMocks.defaultConfig(StructuralDirective, { | ||
// render the mock of the directive by default | ||
render: true, | ||
}); | ||
|
||
// Config for a component with content views. | ||
ngMocks.defaultConfig(ViewComponent, { | ||
render: { | ||
// render a block by default | ||
block1: true, | ||
|
||
// render a block with context | ||
block2: { | ||
$implicit: { | ||
data: 'MOCK_DATA', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// removing the config. | ||
ngMocks.defaultMock(StructuralDirective); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
import coreDefineProperty from './core.define-property'; | ||
import { AnyType } from './core.types'; | ||
import { ngMocksMockConfig } from './mock'; | ||
import ngMocksUniverse from './ng-mocks-universe'; | ||
|
||
export default function (mock: AnyType<any>, source: AnyType<any>, config: ngMocksMockConfig = {}): void { | ||
export default function (mock: AnyType<any>, source: AnyType<any>, configInput: ngMocksMockConfig = {}): void { | ||
coreDefineProperty(mock, 'mockOf', source); | ||
coreDefineProperty(mock, 'nameConstructor', mock.name); | ||
coreDefineProperty(mock, 'name', `MockOf${source.name}`, true); | ||
const config = ngMocksUniverse.getConfigMock().has(source) | ||
? { | ||
...configInput, | ||
config: { | ||
...ngMocksUniverse.getConfigMock().get(source), | ||
...configInput.config, | ||
}, | ||
} | ||
: configInput; | ||
coreDefineProperty(mock.prototype, '__ngMocksConfig', config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
libs/ng-mocks/src/lib/mock-helper/mock-helper.default-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
import { flatten } from '../common/core.helpers'; | ||
import { AnyType } from '../common/core.types'; | ||
import ngMocksUniverse from '../common/ng-mocks-universe'; | ||
import { IMockBuilderConfig } from '../mock-builder/types'; | ||
|
||
export default <T>( | ||
def: AnyType<T> | InjectionToken<T> | string | Array<AnyType<T> | InjectionToken<T> | string>, | ||
config?: IMockBuilderConfig, | ||
): void => { | ||
const map = ngMocksUniverse.getConfigMock(); | ||
for (const item of flatten(def)) { | ||
if (config) { | ||
map.set(item, config); | ||
} else { | ||
map.delete(item); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.