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

inputBox.dispatchEvent(new Event('input', { bubbles: true })) doesn't work after importing NgxCurrencyModule in Unit Test #201

Open
JoeyWu123 opened this issue Jul 29, 2024 · 2 comments

Comments

@JoeyWu123
Copy link

JoeyWu123 commented Jul 29, 2024

Hi,
I integrated V3.0.0 of ngx-currency with our company project (the latest version is not compatible with the Angular version we are using). I am trying to write unit tests to prove that out input box won't take the invalid value after having currencyMask added. The Input box is 2-way binding to variable "costInfo" using ngModel. I tried the manual tests, and everything looks good.
However, in unit test,
I tried

inputBox.value = '100.00';  //(this is a valid input value)
inputBox.dispatchEvent(new Event('input', { bubbles: true }));

the costInfo doesn't change. I already add

 await fixture.whenStable();
 fixture.detectChanges();

the issue is still same

However, if I remove NgxCurrencyModule from

await TestBed.configureTestingModule({
imports: []})

costInfo is successfully changed.

May I ask how to write the correct unit tests for input element with currencyMask? Thank you

@JoeyWu123 JoeyWu123 changed the title inputBox.dispatchEvent(new Event('input', { bubbles: true })) doesn't work after inputting NgxCurrencyModule in Unit Test inputBox.dispatchEvent(new Event('input', { bubbles: true })) doesn't work after importing NgxCurrencyModule in Unit Test Jul 29, 2024
@ArtemDintecom
Copy link
Contributor

I think you should change the value of ngModel instead of working with input.value directly. Or you can simulate keyboard input by sending KeyDown and KeyUp events.

@JoeyWu123
Copy link
Author

JoeyWu123 commented Jul 30, 2024

Thanks for suggestion. It seems in Angular unit test,

inputBox.dispatchEvent(new KeyboardEvent('keydown', { key: '3' }))

doesn't directly update the value in input box, therefore the jobCost isn't updated, unless I write the function for "keydown" in component class

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

2 participants