Skip to content

fix: allow component declarations in modules #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ export interface RenderOptions<C, Q extends Queries = typeof queries> {
componentProviders?: any[];
queries?: Q;
wrapper?: Type<any>;
/**
* Exclude the component to be automatically be added as a declaration
* This is needed when the component is declared in an imported module
*/
excludeComponentDeclaration?: boolean;
}
20 changes: 19 additions & 1 deletion projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ export async function render<T>(
wrapper = WrapperComponent,
componentProperties = {},
componentProviders = [],
excludeComponentDeclaration = false,
} = renderOptions;

const isTemplate = typeof templateOrComponent === 'string';
const componentDeclarations = isTemplate ? [wrapper] : [templateOrComponent];
const componentDeclarations = declareComponents({
templateOrComponent,
wrapper,
isTemplate,
excludeComponentDeclaration,
});

TestBed.configureTestingModule({
declarations: [...declarations, ...componentDeclarations],
Expand Down Expand Up @@ -144,3 +150,15 @@ function setComponentProperties<T>(
}
return fixture;
}

function declareComponents({ isTemplate, wrapper, excludeComponentDeclaration, templateOrComponent }) {
if (isTemplate) {
return [wrapper];
}

if (excludeComponentDeclaration) {
return [];
}

return [templateOrComponent];
}
20 changes: 20 additions & 0 deletions projects/testing-library/tests/render.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Component, ElementRef, OnInit, NgModule } from '@angular/core';
import { render } from '../src/public_api';

@Component({
selector: 'fixture',
template: ``,
})
class FixtureComponent {}

@NgModule({
declarations: [FixtureComponent],
})
export class FixtureModule {}

test('should not throw if component is declared in an import', async () => {
await render(FixtureComponent, {
imports: [FixtureModule],
excludeComponentDeclaration: true,
});
});
2 changes: 1 addition & 1 deletion projects/testing-library/tests/wrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, ElementRef, OnInit } from '@angular/core';
import { Component, ElementRef, OnInit } from '@angular/core';
import { render } from '../src/public_api';

@Component({
Expand Down