Skip to content

Commit

Permalink
feat: add global excludeComponentDeclaration (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored Mar 29, 2021
1 parent 38e15cc commit 0af6018
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion projects/testing-library/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Config } from './models';

let config: Config = {
defaultImports: [],
dom: {},
defaultImports: [],
};

export function configure(newConfig: Partial<Config> | ((config: Partial<Config>) => Partial<Config>)) {
Expand Down
11 changes: 9 additions & 2 deletions projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,14 @@ export interface RenderDirectiveOptions<WrapperType, Properties extends object =
componentProperties?: Partial<WrapperType & Properties>;
}

export interface Config {
defaultImports: any[];
export interface Config extends Pick<RenderComponentOptions<any>, 'excludeComponentDeclaration'> {
/**
* DOM Testing Library config
* @link https://testing-library.com/docs/dom-testing-library/api-configuration/
*/
dom: Partial<dtlConfig>;
/**
* Imports that are added to the imports
*/
defaultImports: any[];
}
13 changes: 8 additions & 5 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export async function render<SutType, WrapperType = SutType>(
sut: Type<SutType>,
renderOptions: RenderComponentOptions<SutType> | RenderDirectiveOptions<WrapperType> = {},
): Promise<RenderResult<SutType>> {
const { dom: domConfig, ...globalConfig } = getConfig();
const {
detectChanges: detectChangesOnRender = true,
declarations = [],
Expand All @@ -55,22 +56,24 @@ export async function render<SutType, WrapperType = SutType>(
excludeComponentDeclaration = false,
routes,
removeAngularAttributes = false,
} = renderOptions as RenderDirectiveOptions<WrapperType>;

const config = getConfig();
defaultImports = [],
} = { ...globalConfig, ...renderOptions };

dtlConfigure({
eventWrapper: (cb) => {
const result = cb();
detectChangesForMountedFixtures();
return result;
},
...config.dom,
...domConfig,
});

TestBed.configureTestingModule({
declarations: addAutoDeclarations(sut, { declarations, excludeComponentDeclaration, template, wrapper }),
imports: addAutoImports({ imports: imports.concat(config.defaultImports), routes }),
imports: addAutoImports({
imports: imports.concat(defaultImports),
routes,
}),
providers: [...providers],
schemas: [...schemas],
});
Expand Down
2 changes: 1 addition & 1 deletion projects/testing-library/tests/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('animationModule', () => {
})
class FixtureModule {}
describe('excludeComponentDeclaration', () => {
test('will throw if component is declared in an import', async () => {
test('does not throw if component is declared in an imported module', async () => {
await render(FixtureComponent, {
imports: [FixtureModule],
excludeComponentDeclaration: true,
Expand Down

0 comments on commit 0af6018

Please sign in to comment.