Skip to content

feat: remove change and changeInput in favor of rerender #378

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 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: remove change and changeInput in favor of rerender
BREAKING CHANGE:

Use 
erender instead of change.
Use 
erender instead of changechangeInput

For more info see #365
  • Loading branch information
timdeschryver committed Mar 27, 2023
commit 9666dcefcbe72e0bcabd5bce549680abc1faa22b
13 changes: 0 additions & 13 deletions apps/example-app-karma/src/app/issues/issue-222.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,3 @@ it('https://github.com/testing-library/angular-testing-library/issues/222 with r

expect(screen.getByText('Hello Mark')).toBeTruthy();
});

it('https://github.com/testing-library/angular-testing-library/issues/222 with change', async () => {
const { change } = await render(`<div>Hello {{ name}}</div>`, {
componentProperties: {
name: 'Sarah',
},
});

expect(screen.getByText('Hello Sarah')).toBeTruthy();
await change({ name: 'Mark' });

expect(screen.getByText('Hello Mark')).toBeTruthy();
});
14 changes: 0 additions & 14 deletions apps/example-app/src/app/examples/16-input-getter-setter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ test('should run logic in the input setter and getter', async () => {
expect(getterValueControl).toHaveTextContent('I am value from getter Angular');
});

test('should run logic in the input setter and getter while changing', async () => {
const { change } = await render(InputGetterSetter, { componentProperties: { value: 'Angular' } });
const valueControl = screen.getByTestId('value');
const getterValueControl = screen.getByTestId('value-getter');

expect(valueControl).toHaveTextContent('I am value from setter Angular');
expect(getterValueControl).toHaveTextContent('I am value from getter Angular');

await change({ value: 'React' });

expect(valueControl).toHaveTextContent('I am value from setter React');
expect(getterValueControl).toHaveTextContent('I am value from getter React');
});

test('should run logic in the input setter and getter while re-rendering', async () => {
const { rerender } = await render(InputGetterSetter, { componentProperties: { value: 'Angular' } });

Expand Down
16 changes: 0 additions & 16 deletions projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,6 @@ export interface RenderResult<ComponentType, WrapperType = ComponentType> extend
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'detectChangesOnRender'
>,
) => Promise<void>;
/**
* @deprecated
* Use rerender instead. For more info see {@link https://github.com/testing-library/angular-testing-library/issues/365 GitHub Issue}
*
* @description
* Keeps the current fixture intact and invokes ngOnChanges with the updated properties.
*/
change: (changedProperties: Partial<ComponentType>) => void;
/**
* @deprecated
* Use rerender instead. For more info see {@link https://github.com/testing-library/angular-testing-library/issues/365 GitHub Issue}
*
* @description
* Keeps the current fixture intact, update the @Input properties and invoke ngOnChanges with the updated properties.
*/
changeInput: (changedInputProperties: Partial<ComponentType>) => void;
}

export interface RenderComponentOptions<ComponentType, Q extends Queries = typeof queries> {
Expand Down
28 changes: 0 additions & 28 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,32 +162,6 @@ export async function render<SutType, WrapperType = SutType>(
}
};

const changeInput = (changedInputProperties: Partial<SutType>) => {
if (Object.keys(changedInputProperties).length === 0) {
return;
}

setComponentInputs(fixture, changedInputProperties);

fixture.detectChanges();
};

const change = (changedProperties: Partial<SutType>) => {
if (Object.keys(changedProperties).length === 0) {
return;
}

const changes = getChangesObj(fixture.componentInstance as Record<string, any>, changedProperties);

setComponentProperties(fixture, changedProperties);

if (hasOnChangesHook(fixture.componentInstance)) {
fixture.componentInstance.ngOnChanges(changes);
}

fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
};

const navigate = async (elementOrPath: Element | string, basePath = ''): Promise<boolean> => {
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
const [path, params] = (basePath + href).split('?');
Expand Down Expand Up @@ -234,8 +208,6 @@ export async function render<SutType, WrapperType = SutType>(
detectChanges: () => detectChanges(),
navigate,
rerender,
change,
changeInput,
// @ts-ignore: fixture assigned
debugElement: fixture.debugElement,
// @ts-ignore: fixture assigned
Expand Down
96 changes: 0 additions & 96 deletions projects/testing-library/tests/change.spec.ts

This file was deleted.

97 changes: 0 additions & 97 deletions projects/testing-library/tests/changeInputs.spec.ts

This file was deleted.

36 changes: 36 additions & 0 deletions projects/testing-library/tests/rerender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test('rerenders without props', async () => {
await rerender();

expect(screen.getByText('Sarah')).toBeInTheDocument();
expect(ngOnChangesSpy).toHaveBeenCalledTimes(1); // one time initially and one time for rerender
});

test('rerenders the component with updated inputs', async () => {
Expand All @@ -48,6 +49,41 @@ test('rerenders the component with updated inputs', async () => {
});

test('rerenders the component with updated props and resets other props', async () => {
const firstName = 'Mark';
const lastName = 'Peeters';
const { rerender } = await render(FixtureComponent, {
componentInputs: {
firstName,
lastName,
},
});

expect(screen.getByText(`${firstName} ${lastName}`)).toBeInTheDocument();

const firstName2 = 'Chris';
await rerender({ componentInputs: { firstName: firstName2 } });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this test fails while using componentInputs instead of componentProperties.
Do you think this is the expected behavior @shaman-apprentice ?
Changes is {} here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test should pass. On a first glance I don't know why it doesn't. Unfortunately, I am busy at the moment. When you want to create an issue for it, I promise to dig into it within the next 2-3 weeks. A quick look if you find a solution faster is of course very welcome.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a few minutes to deal with it. #380 will fix it. Good catch and thanks a lot for adding this test!


expect(screen.getByText(firstName2)).toBeInTheDocument();
expect(screen.queryByText(firstName)).not.toBeInTheDocument();
expect(screen.queryByText(lastName)).not.toBeInTheDocument();

expect(ngOnChangesSpy).toHaveBeenCalledTimes(2); // one time initially and one time for rerender
const rerenderedChanges = ngOnChangesSpy.mock.calls[1][0] as SimpleChanges;
expect(rerenderedChanges).toEqual({
lastName: {
previousValue: 'Peeters',
currentValue: undefined,
firstChange: false,
},
firstName: {
previousValue: 'Mark',
currentValue: 'Chris',
firstChange: false,
},
});
});

test('rerenders the component with updated props and resets other props with componentProperties', async () => {
const firstName = 'Mark';
const lastName = 'Peeters';
const { rerender } = await render(FixtureComponent, {
Expand Down