-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: π component re-render when updating fields in lifecycle hook (#646)
* fix: π component re-render when updating fields in lifecycle hook * test: β fix incorrect assertion after prettier bump
- Loading branch information
Showing
6 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
projects/spectator/jest/test/ngonchanges-input/ngonchanges-input.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest'; | ||
import { NgOnChangesInputComponent } from '../../../test/ngonchanges-input/ngonchanges-input.component'; | ||
|
||
describe('NgOnChangesInputComponent', () => { | ||
describe('with Spectator', () => { | ||
let spectator: Spectator<NgOnChangesInputComponent>; | ||
|
||
const createComponent = createComponentFactory({ | ||
component: NgOnChangesInputComponent, | ||
}); | ||
|
||
beforeEach(() => { | ||
spectator = createComponent(); | ||
}); | ||
|
||
it('should re-render when updating fields in ngOnChanges', () => { | ||
expect(spectator.query('button')).toBeDisabled(); | ||
expect(spectator.query('button')).toHaveText('Button disabled'); | ||
|
||
spectator.setInput({ btnDisabled: false }); | ||
expect(spectator.query('button')).not.toBeDisabled(); | ||
expect(spectator.query('button')).toHaveText('Button enabled'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
projects/spectator/test/ngonchanges-input/ngonchanges-input.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createComponentFactory, Spectator } from '@ngneat/spectator'; | ||
import { NgOnChangesInputComponent } from './ngonchanges-input.component'; | ||
|
||
describe('NgOnChangesInputComponent', () => { | ||
describe('with Spectator', () => { | ||
let spectator: Spectator<NgOnChangesInputComponent>; | ||
|
||
const createComponent = createComponentFactory({ | ||
component: NgOnChangesInputComponent, | ||
}); | ||
|
||
beforeEach(() => { | ||
spectator = createComponent(); | ||
}); | ||
|
||
it('should re-render when updating fields in ngOnChanges', () => { | ||
expect(spectator.query('button')).toBeDisabled(); | ||
expect(spectator.query('button')).toHaveText('Button disabled'); | ||
|
||
spectator.setInput({ btnDisabled: false }); | ||
expect(spectator.query('button')).not.toBeDisabled(); | ||
expect(spectator.query('button')).toHaveText('Button enabled'); | ||
}); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
projects/spectator/test/ngonchanges-input/ngonchanges-input.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
standalone: true, | ||
template: ` | ||
<button [disabled]="btnDisabled"> | ||
{{ btnText }} | ||
</button> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class NgOnChangesInputComponent { | ||
@Input() | ||
btnDisabled = true; | ||
|
||
btnText = 'Button disabled'; | ||
|
||
ngOnChanges() { | ||
this.btnText = this.btnDisabled ? 'Button disabled' : 'Button enabled'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters