-
Notifications
You must be signed in to change notification settings - Fork 93
feat(testing-library): add an overload for template rendering #206
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
timdeschryver
merged 1 commit into
testing-library:master
from
lacolaco:render-template
Apr 22, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -22,7 +22,7 @@ import { | |
waitForOptions as dtlWaitForOptions, | ||
configure as dtlConfigure, | ||
} from '@testing-library/dom'; | ||
import { RenderComponentOptions, RenderDirectiveOptions, RenderResult } from './models'; | ||
import { RenderComponentOptions, RenderDirectiveOptions, RenderTemplateOptions, RenderResult } from './models'; | ||
import { getConfig } from './config'; | ||
|
||
const mountedFixtures = new Set<ComponentFixture<any>>(); | ||
|
@@ -32,14 +32,24 @@ export async function render<ComponentType>( | |
component: Type<ComponentType>, | ||
renderOptions?: RenderComponentOptions<ComponentType>, | ||
): Promise<RenderResult<ComponentType, ComponentType>>; | ||
/** | ||
* @deprecated Use `render(template, { declarations: [DirectiveType] })` instead. | ||
*/ | ||
export async function render<DirectiveType, WrapperType = WrapperComponent>( | ||
component: Type<DirectiveType>, | ||
renderOptions?: RenderDirectiveOptions<WrapperType>, | ||
): Promise<RenderResult<WrapperType>>; | ||
export async function render<WrapperType = WrapperComponent>( | ||
template: string, | ||
renderOptions?: RenderTemplateOptions<WrapperType>, | ||
): Promise<RenderResult<WrapperType>>; | ||
|
||
export async function render<SutType, WrapperType = SutType>( | ||
sut: Type<SutType>, | ||
renderOptions: RenderComponentOptions<SutType> | RenderDirectiveOptions<WrapperType> = {}, | ||
sut: Type<SutType> | string, | ||
renderOptions: | ||
| RenderComponentOptions<SutType> | ||
| RenderDirectiveOptions<WrapperType> | ||
| RenderTemplateOptions<WrapperType> = {}, | ||
): Promise<RenderResult<SutType>> { | ||
const { dom: domConfig, ...globalConfig } = getConfig(); | ||
const { | ||
|
@@ -69,7 +79,12 @@ export async function render<SutType, WrapperType = SutType>( | |
}); | ||
|
||
TestBed.configureTestingModule({ | ||
declarations: addAutoDeclarations(sut, { declarations, excludeComponentDeclaration, template, wrapper }), | ||
declarations: addAutoDeclarations(sut, { | ||
declarations, | ||
excludeComponentDeclaration, | ||
template, | ||
wrapper, | ||
}), | ||
imports: addAutoImports({ | ||
imports: imports.concat(defaultImports), | ||
routes, | ||
|
@@ -176,7 +191,7 @@ export async function render<SutType, WrapperType = SutType>( | |
detectChanges, | ||
navigate, | ||
rerender, | ||
debugElement: fixture.debugElement.query(By.directive(sut)), | ||
debugElement: typeof sut === 'string' ? fixture.debugElement : fixture.debugElement.query(By.directive(sut)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an only difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this fine. |
||
container: fixture.nativeElement, | ||
debug: (element = fixture.nativeElement, maxLength, options) => | ||
Array.isArray(element) | ||
|
@@ -193,14 +208,18 @@ async function createComponent<SutType>(component: Type<SutType>): Promise<Compo | |
} | ||
|
||
async function createComponentFixture<SutType>( | ||
component: Type<SutType>, | ||
sut: Type<SutType> | string, | ||
{ template, wrapper }: Pick<RenderDirectiveOptions<any>, 'template' | 'wrapper'>, | ||
): Promise<ComponentFixture<SutType>> { | ||
if (typeof sut === 'string') { | ||
TestBed.overrideTemplate(wrapper, sut); | ||
return createComponent(wrapper); | ||
} | ||
if (template) { | ||
TestBed.overrideTemplate(wrapper, template); | ||
return createComponent(wrapper); | ||
} | ||
return createComponent(component); | ||
return createComponent(sut); | ||
} | ||
|
||
function setComponentProperties<SutType>( | ||
|
@@ -248,17 +267,21 @@ function getChangesObj<SutType>(oldProps: Partial<SutType> | null, newProps: Par | |
} | ||
|
||
function addAutoDeclarations<SutType>( | ||
component: Type<SutType>, | ||
sut: Type<SutType> | string, | ||
{ | ||
declarations, | ||
excludeComponentDeclaration, | ||
template, | ||
wrapper, | ||
}: Pick<RenderDirectiveOptions<any>, 'declarations' | 'excludeComponentDeclaration' | 'template' | 'wrapper'>, | ||
) { | ||
if (typeof sut === 'string') { | ||
return [...declarations, wrapper]; | ||
} | ||
|
||
const wrappers = () => (template ? [wrapper] : []); | ||
|
||
const components = () => (excludeComponentDeclaration ? [] : [component]); | ||
const components = () => (excludeComponentDeclaration ? [] : [sut]); | ||
|
||
return [...declarations, ...wrappers(), ...components()]; | ||
} | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an opinion about the naming of
componentProperties
?Should it be better to name this
wrapperComponentProperties
orhostComponentProperties
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like
wrapperComponentProperties
which has consistency withwrapper
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I think it is hard to change the name now. Because
RenderComponentOptions
andRenderDirectiveOptions
still hascomponentProperties
, making overload with co-existing these are complex.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking of two options.
Option 1. Just
properties
orprops
in all type ofrender
optionsVue or other families are adopting that. And it is not relevant directly, Storybook for Angular has also a similar API. This option brings breaking change for the entire package but the result is simple.
https://storybook.js.org/docs/angular/writing-stories/introduction
Option 2. Extending
wrapper
for passing wrapper component properties. Implementation for this is more complex than Option 1.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like option 1, and you're (again) totally right.
Let's leave it with componentProperties, and we might rename that in a next breaking version.
I won't do it know, just because this is a minor difference.