Skip to content
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

signalInputs #432

Closed
timdeschryver opened this issue Jan 22, 2024 · 13 comments
Closed

signalInputs #432

timdeschryver opened this issue Jan 22, 2024 · 13 comments

Comments

@timdeschryver
Copy link
Member

Add test cases for signal inputs.
Do we need to do something to cover these, or does this just work? 👀

@FabienDehopre
Copy link

FabienDehopre commented Jan 28, 2024

it seems that something needs to be adapted because when I run some tests on components with signal inputs, I get the following type of error:
NG0303: Can't set value of the 'tags' input on the 'TagListComponent' component. Make sure that the 'tags' property is annotated with @Input() or a mapped @Input('tags') exists.
The "tags" input is declared as follow:
readonly tags = input.required<SearchTag[]>();

@FabienDehopre
Copy link

could it be related to this issue? angular/angular#54013

@timdeschryver
Copy link
Member Author

@FabienDehopre thanks for the information.
It does seem like the referenced issue is related, internally ATL also uses fixture.setInput().

@tutkli
Copy link

tutkli commented Feb 26, 2024

I'm facing the same issue. Seems like it's fixed in thymikee/jest-preset-angular#2246

@timdeschryver
Copy link
Member Author

@tutkli the referenced PR allows us to set signal inputs via other components, but not directly using fixture.componentRef.setInput().

@timdeschryver
Copy link
Member Author

@johnwalshuk
Copy link

johnwalshuk commented Mar 22, 2024

How do we handle model changes? We can have two-way binding now but need a way to test any changes.

Example:

@Component({
	selector: 'app-search',
	standalone: true,
	template: `<input [(ngModel)]="keywords()" />`,
})
export class SearchComponent {
	public keywords = model('');
}

Anyone who consumes this component can split the i/o like

<app-search [keywords]="keywords" (keywordsChange)="handleKeywordsChange($event)" />

I'd like to write a test like so but it doesnt appear to be handled currently:

test('it outputs form values per vale change.', async () => {
	const keywordsChange = jest.fn();
	const { getByLabelText } = await renderComponent({
		componentOutputs: {
			keywordsChange,
		}
	});

	await userEvent.type(getByLabelText('Search'), 'a');
	expect(keywordsChange).toHaveBeenCalledWith('a');

	await userEvent.type(getByLabelText('Search'), 'b');
	expect(keywordsChange).toHaveBeenLastCalledWith('ab');

	await userEvent.type(getByLabelText('Search'), 'c');
	expect(keywordsChange).toHaveBeenLastCalledWith('abc');

	await userEvent.type(getByLabelText('Search'), '{backspace}');
	expect(keywordsChange).toHaveBeenLastCalledWith('ab');

	await userEvent.type(getByLabelText('Search'), '{backspace}');
	expect(keywordsChange).toHaveBeenLastCalledWith('a');

	await userEvent.type(getByLabelText('Search'), '{backspace}');
	expect(keywordsChange).toHaveBeenLastCalledWith('');
});

@timdeschryver
Copy link
Member Author

We haven't looked into supporting model for now.
since it's "just" a writable signal, have you tried creating a signal instead of a jest mock?

@mnkyjs
Copy link

mnkyjs commented Mar 26, 2024

Update: after also updating Jest this does work

For an example see https://github.com/testing-library/angular-testing-library/blob/main/apps/example-app/src/app/examples/22-signal-inputs.component.spec.ts

This is not working for me 😞. I updated jest-preset, Testing Library and jest, but I get still the error

Similar to this

NG0303: Can't set value of the 'tags' input on the 'TagListComponent' component. Make sure that the 'tags' property is annotated with @Input() or a mapped @Input('tags') exists.

@timdeschryver
Copy link
Member Author

Do you got a reproduction @mnkyjs ?

@tutkli
Copy link

tutkli commented Mar 27, 2024

Update: after also updating Jest this does work

For an example see https://github.com/testing-library/angular-testing-library/blob/main/apps/example-app/src/app/examples/22-signal-inputs.component.spec.ts

This is not working for me 😞. I updated jest-preset, Testing Library and jest, but I get still the error

Similar to this

NG0303: Can't set value of the 'tags' input on the 'TagListComponent' component. Make sure that the 'tags' property is annotated with @Input() or a mapped @Input('tags') exists.

I've seen this too. Some input names do not work. For me it was 'visible'. Changing the input name fixed it.

@timdeschryver
Copy link
Member Author

@johnwalshuk see #443 to test output/models

@mnkyjs
Copy link

mnkyjs commented Apr 19, 2024

Do you got a reproduction @mnkyjs ?

It was my bad 🤦 , after I removed my node_modules folder and rerun npm install everything worked again 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants