Skip to content

Commit 9666dce

Browse files
committed
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
1 parent 71c623f commit 9666dce

File tree

7 files changed

+36
-264
lines changed

7 files changed

+36
-264
lines changed

apps/example-app-karma/src/app/issues/issue-222.spec.ts

-13
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,3 @@ it('https://github.com/testing-library/angular-testing-library/issues/222 with r
1313

1414
expect(screen.getByText('Hello Mark')).toBeTruthy();
1515
});
16-
17-
it('https://github.com/testing-library/angular-testing-library/issues/222 with change', async () => {
18-
const { change } = await render(`<div>Hello {{ name}}</div>`, {
19-
componentProperties: {
20-
name: 'Sarah',
21-
},
22-
});
23-
24-
expect(screen.getByText('Hello Sarah')).toBeTruthy();
25-
await change({ name: 'Mark' });
26-
27-
expect(screen.getByText('Hello Mark')).toBeTruthy();
28-
});

apps/example-app/src/app/examples/16-input-getter-setter.spec.ts

-14
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ test('should run logic in the input setter and getter', async () => {
1010
expect(getterValueControl).toHaveTextContent('I am value from getter Angular');
1111
});
1212

13-
test('should run logic in the input setter and getter while changing', async () => {
14-
const { change } = await render(InputGetterSetter, { componentProperties: { value: 'Angular' } });
15-
const valueControl = screen.getByTestId('value');
16-
const getterValueControl = screen.getByTestId('value-getter');
17-
18-
expect(valueControl).toHaveTextContent('I am value from setter Angular');
19-
expect(getterValueControl).toHaveTextContent('I am value from getter Angular');
20-
21-
await change({ value: 'React' });
22-
23-
expect(valueControl).toHaveTextContent('I am value from setter React');
24-
expect(getterValueControl).toHaveTextContent('I am value from getter React');
25-
});
26-
2713
test('should run logic in the input setter and getter while re-rendering', async () => {
2814
const { rerender } = await render(InputGetterSetter, { componentProperties: { value: 'Angular' } });
2915

projects/testing-library/src/lib/models.ts

-16
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,6 @@ export interface RenderResult<ComponentType, WrapperType = ComponentType> extend
6363
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'detectChangesOnRender'
6464
>,
6565
) => Promise<void>;
66-
/**
67-
* @deprecated
68-
* Use rerender instead. For more info see {@link https://github.com/testing-library/angular-testing-library/issues/365 GitHub Issue}
69-
*
70-
* @description
71-
* Keeps the current fixture intact and invokes ngOnChanges with the updated properties.
72-
*/
73-
change: (changedProperties: Partial<ComponentType>) => void;
74-
/**
75-
* @deprecated
76-
* Use rerender instead. For more info see {@link https://github.com/testing-library/angular-testing-library/issues/365 GitHub Issue}
77-
*
78-
* @description
79-
* Keeps the current fixture intact, update the @Input properties and invoke ngOnChanges with the updated properties.
80-
*/
81-
changeInput: (changedInputProperties: Partial<ComponentType>) => void;
8266
}
8367

8468
export interface RenderComponentOptions<ComponentType, Q extends Queries = typeof queries> {

projects/testing-library/src/lib/testing-library.ts

-28
Original file line numberDiff line numberDiff line change
@@ -162,32 +162,6 @@ export async function render<SutType, WrapperType = SutType>(
162162
}
163163
};
164164

165-
const changeInput = (changedInputProperties: Partial<SutType>) => {
166-
if (Object.keys(changedInputProperties).length === 0) {
167-
return;
168-
}
169-
170-
setComponentInputs(fixture, changedInputProperties);
171-
172-
fixture.detectChanges();
173-
};
174-
175-
const change = (changedProperties: Partial<SutType>) => {
176-
if (Object.keys(changedProperties).length === 0) {
177-
return;
178-
}
179-
180-
const changes = getChangesObj(fixture.componentInstance as Record<string, any>, changedProperties);
181-
182-
setComponentProperties(fixture, changedProperties);
183-
184-
if (hasOnChangesHook(fixture.componentInstance)) {
185-
fixture.componentInstance.ngOnChanges(changes);
186-
}
187-
188-
fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
189-
};
190-
191165
const navigate = async (elementOrPath: Element | string, basePath = ''): Promise<boolean> => {
192166
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
193167
const [path, params] = (basePath + href).split('?');
@@ -234,8 +208,6 @@ export async function render<SutType, WrapperType = SutType>(
234208
detectChanges: () => detectChanges(),
235209
navigate,
236210
rerender,
237-
change,
238-
changeInput,
239211
// @ts-ignore: fixture assigned
240212
debugElement: fixture.debugElement,
241213
// @ts-ignore: fixture assigned

projects/testing-library/tests/change.spec.ts

-96
This file was deleted.

projects/testing-library/tests/changeInputs.spec.ts

-97
This file was deleted.

projects/testing-library/tests/rerender.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ test('rerenders without props', async () => {
3535
await rerender();
3636

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

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

5051
test('rerenders the component with updated props and resets other props', async () => {
52+
const firstName = 'Mark';
53+
const lastName = 'Peeters';
54+
const { rerender } = await render(FixtureComponent, {
55+
componentInputs: {
56+
firstName,
57+
lastName,
58+
},
59+
});
60+
61+
expect(screen.getByText(`${firstName} ${lastName}`)).toBeInTheDocument();
62+
63+
const firstName2 = 'Chris';
64+
await rerender({ componentInputs: { firstName: firstName2 } });
65+
66+
expect(screen.getByText(firstName2)).toBeInTheDocument();
67+
expect(screen.queryByText(firstName)).not.toBeInTheDocument();
68+
expect(screen.queryByText(lastName)).not.toBeInTheDocument();
69+
70+
expect(ngOnChangesSpy).toHaveBeenCalledTimes(2); // one time initially and one time for rerender
71+
const rerenderedChanges = ngOnChangesSpy.mock.calls[1][0] as SimpleChanges;
72+
expect(rerenderedChanges).toEqual({
73+
lastName: {
74+
previousValue: 'Peeters',
75+
currentValue: undefined,
76+
firstChange: false,
77+
},
78+
firstName: {
79+
previousValue: 'Mark',
80+
currentValue: 'Chris',
81+
firstChange: false,
82+
},
83+
});
84+
});
85+
86+
test('rerenders the component with updated props and resets other props with componentProperties', async () => {
5187
const firstName = 'Mark';
5288
const lastName = 'Peeters';
5389
const { rerender } = await render(FixtureComponent, {

0 commit comments

Comments
 (0)