|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { fakeAsync } from '@angular/core/testing'; |
| 3 | +import { createHostComponentFactory, SpectatorWithHost } from '@netbasal/spectator'; |
| 4 | +import { CallPipe } from '../../lib/pipes'; |
| 5 | + |
| 6 | +const TEXT = 'NGX Features Awesome'; |
| 7 | +const TEXT2 = 'Really!'; |
| 8 | + |
| 9 | +@Component({ selector: 'host', template: '' }) |
| 10 | +class Host { |
| 11 | + text: string = TEXT; |
| 12 | + method(value: string) { |
| 13 | + return this.transform(value); |
| 14 | + } |
| 15 | + |
| 16 | + transform(value: string) { |
| 17 | + return value && value.toUpperCase(); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +describe('CallPipe', () => { |
| 22 | + let host: SpectatorWithHost<Host, Host>; |
| 23 | + const create = createHostComponentFactory({ |
| 24 | + host: Host, |
| 25 | + component: Host, |
| 26 | + declarations: [ CallPipe ] |
| 27 | + }); |
| 28 | + |
| 29 | + it('should call component method at once', fakeAsync(() => { |
| 30 | + host = create(`{{ text | call: 'method' }}`, false); |
| 31 | + spyOn(host.hostComponent, 'method').and.callThrough(); |
| 32 | + |
| 33 | + host.detectChanges(); |
| 34 | + expect(host.hostElement).toHaveText(TEXT.toUpperCase()); |
| 35 | + |
| 36 | + host.detectChanges(); |
| 37 | + expect(host.hostComponent.method).toHaveBeenCalledTimes(1); |
| 38 | + })); |
| 39 | + |
| 40 | + it('should call component method after change text', fakeAsync(() => { |
| 41 | + host = create(`{{ text | call: 'method' }}`, false); |
| 42 | + spyOn(host.hostComponent, 'method').and.callThrough(); |
| 43 | + |
| 44 | + host.detectChanges(); |
| 45 | + expect(host.hostElement).toHaveText(TEXT.toUpperCase()); |
| 46 | + |
| 47 | + host.detectChanges(); |
| 48 | + expect(host.hostComponent.method).toHaveBeenCalledTimes(1); |
| 49 | + |
| 50 | + host.setHostInput({ text: TEXT2 }); |
| 51 | + expect(host.hostElement).toHaveText(TEXT2.toUpperCase()); |
| 52 | + |
| 53 | + host.detectChanges(); |
| 54 | + expect(host.hostComponent.method).toHaveBeenCalledTimes(2); |
| 55 | + })); |
| 56 | + |
| 57 | + it('should call method by string', fakeAsync(() => { |
| 58 | + host = create(`{{ text | call: 'method' }}`, false); |
| 59 | + spyOn(host.hostComponent, 'method').and.callThrough(); |
| 60 | + |
| 61 | + host.detectChanges(); |
| 62 | + expect(host.hostComponent.method).toHaveBeenCalledTimes(1); |
| 63 | + })); |
| 64 | + |
| 65 | + it('should call method by function', fakeAsync(() => { |
| 66 | + host = create(`{{ text | call: method }}`, false); |
| 67 | + spyOn(host.hostComponent, 'method').and.callThrough(); |
| 68 | + |
| 69 | + host.detectChanges(); |
| 70 | + expect(host.hostComponent.method).toHaveBeenCalledTimes(1); |
| 71 | + })); |
| 72 | + |
| 73 | + it('should call method with extra arguments', fakeAsync(() => { |
| 74 | + host = create(`{{ text | call: method: 'foo': 'bar' }}`, false); |
| 75 | + spyOn(host.hostComponent, 'method').and.callThrough(); |
| 76 | + |
| 77 | + host.detectChanges(); |
| 78 | + expect(host.hostComponent.method).toHaveBeenCalledWith(TEXT, 'foo', 'bar'); |
| 79 | + })); |
| 80 | + |
| 81 | +}); |
0 commit comments