-
Notifications
You must be signed in to change notification settings - Fork 26.3k
feat(core): support bindings in TestBed #62040
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
Conversation
274b4a4
to
9611870
Compare
Adds support for passing in `Binding` objects into `TestBed.createComponent`. This makes it easier to test components by avoiding the need to create a wrapper component. Furthermore, it keeps the behavior consistent between tests and the actual app. For example, given a custom checkbox that looks like this: ```typescript @component({ selector: 'my-checkbox', template: '...', host: {'[class.checked]': 'isChecked()'} }) export class MyCheckbox { isChecked = input(false); } ``` A test for the `isChecked` input would look something like this: ```typescript it('should toggle the checked class', () => { @component({ imports: [MyCheckbox], template: '<my-checkbox [isChecked]="isChecked"/>', }) class Wrapper { isChecked = false; } const fixture = TestBed.createComponent(Wrapper); const checkbox = fixture.nativeElement.querySelector('my-checkbox'); fixture.detectChanges(); expect(checkbox.classList).not.toContain('checked'); fixture.componentInstance.isChecked = true; fixture.detectChanges(); expect(checkbox.classList).toContain('checked'); }); ``` Whereas with the new API, the test would look like this: ```typescript it('should toggle the checked class', () => { const isChecked = signal(false); const fixture = TestBed.createComponent(MyCheckbox, { bindings: [inputBinding('isChecked', isChecked)] }); const checkbox = fixture.nativeElement.querySelector('my-checkbox'); fixture.detectChanges(); expect(checkbox.classList).not.toContain('checked'); isChecked.set(true); fixture.detectChanges(); expect(checkbox.classList).toContain('checked'); }); ```
9611870
to
49a1f83
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
reviewed-for: fw-general, public-api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reviewed-for: public-api
This PR was merged into the repository by commit 2e0c98b. The changes were merged into the following branches: main |
Adds support for passing in
Binding
objects intoTestBed.createComponent
. This makes it easier to test components by avoiding the need to create a wrapper component. Furthermore, it keeps the behavior consistent between tests and the actual app. For example, given a custom checkbox that looks like this:A test for the
isChecked
input would look something like this:Whereas with the new API, the test would look like this: