|
| 1 | +import { createComponentFactory, Spectator } from '@ngneat/spectator'; |
| 2 | +import { CommonModule } from '@angular/common'; |
| 3 | + |
| 4 | +import { SimpleChangesComponent } from './simple-changes.component'; |
| 5 | + |
| 6 | +describe('SimpleChangesComponent', () => { |
| 7 | + let spectator: Spectator<SimpleChangesComponent>; |
| 8 | + |
| 9 | + const createComponent = createComponentFactory({ |
| 10 | + component: SimpleChangesComponent, |
| 11 | + imports: [CommonModule] |
| 12 | + }); |
| 13 | + |
| 14 | + it('should be sequence of calls is correct', () => { |
| 15 | + spectator = createComponent({ |
| 16 | + props: { value: '1' } |
| 17 | + }); |
| 18 | + |
| 19 | + const { hooks } = spectator.component; |
| 20 | + |
| 21 | + expect(hooks.length).toBe(2); |
| 22 | + expect(hooks[0]).toBe('ngOnChanges'); |
| 23 | + expect(hooks[1]).toBe('ngOnInit'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should be set first change value and next updates', () => { |
| 27 | + spectator = createComponent({ props: { value: '1' } }); |
| 28 | + |
| 29 | + const { changes } = spectator.component; |
| 30 | + |
| 31 | + expect(changes.length).toBe(1, 'size after compile'); |
| 32 | + expect(changes[0].currentValue).toBe('1', 'first change currentValue'); |
| 33 | + expect(changes[0].previousValue).toBe(undefined, 'first change previousValue'); |
| 34 | + expect(changes[0].firstChange).toBe(true, 'first change firstChange'); |
| 35 | + |
| 36 | + spectator.setInput({ value: '2' }); |
| 37 | + |
| 38 | + expect(changes.length).toBe(2, 'size after update'); |
| 39 | + expect(changes[1].currentValue).toBe('2', 'after update currentValue'); |
| 40 | + expect(changes[1].previousValue).toBe('1', 'after update previousValue'); |
| 41 | + expect(changes[1].firstChange).toBe(false, 'after update firstChange'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should not update when value is equal', () => { |
| 45 | + spectator = createComponent({ props: { value: '1' } }); |
| 46 | + |
| 47 | + const { changes } = spectator.component; |
| 48 | + |
| 49 | + expect(changes.length).toBe(1, 'size after compile'); |
| 50 | + |
| 51 | + spectator.setInput({ value: '1' }); |
| 52 | + expect(changes.length).toBe(1, 'not updated'); |
| 53 | + }); |
| 54 | +}); |
0 commit comments