-
Notifications
You must be signed in to change notification settings - Fork 93
Change input after initial render #56
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
Comments
Hi, thanks for the feedback! When this lib was created I had the opinion that dynamic input change was a bad practice, but now that I read this issue I'm rethinking it. What do you think the API should look like, something as the following? // rerender has a componentProperties parameter
rerender({
myInput: 'foo'
}) Internally, we'll update the properties and run a CD cycle. |
Also, thinking more about it, although it would be useful when using a host/wrapper component; without that, we would still need to manually call Do you think it would be a bad idea to automatically call |
We'll first do that, and see what we can do about |
Are you interested in creating this feature @Wykks ? |
🎉 This issue has been resolved in version 8.1.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@timdeschryver See this pseudo-code: import { SimpleChange } from '@angular/core';
import { ComponentFixture } from '@angular/core/testing';
import { render } from '@testing-library/angular';
const setupComponent = async () => {
const someProps = {};
const { rerender, fixture } = await render(SomeComponent, { componentProperties: someProps });
const getChangesObj = (
oldProps: Partial<SomeComponent> | null,
newProps: Partial<SomeComponent>,
isFirstChange: boolean
) => {
return Object.keys(newProps).reduce((changes, key) => ({
...changes,
[key]: new SimpleChange(isFirstChange ? null : oldProps[key], newProps[key], false)
}), {});
};
// Run ngOnChanges on initial render
if ('ngOnChanges' in fixture.componentInstance) {
fixture.componentInstance.ngOnChanges(getChangesObj(null, someProps, true));
}
const updatedRerender = (props: Partial<SomeComponent>) => {
const changes = getChangesObj(fixture.componentInstance, props, false);
rerender(props);
if ('ngOnChanges' in fixture.componentInstance) {
fixture.componentInstance.ngOnChanges(changes);
}
}
return {
rerender: updatedRerender
};
} |
@miluoshi do you want to create a PR for this? |
@timdeschryver Sure, here it is 🙂 |
🎉 This issue has been resolved in version 9.4.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Uh oh!
There was an error while loading. Please reload this page.
Hi!
What's the correct way to change input after initial render ?
Because
componentProperties
only set input on the first render, but we cannot test subsequent input changes this way (which leads to subsequent calls tongOnChanges
).For now, this works :
But accessing
fixture
is not recommended (https://testing-library.com/docs/angular-testing-library/api#fixture, and I agree with this), and since I do not consider testing dynamic input change is testing an implementation detail, I believe there's should be another way, right ?Edit: Maybe something like this :
Also, thanks for this library it's really usefull !
The text was updated successfully, but these errors were encountered: