|
1 | 1 | --- |
2 | 2 | id: component-bindings |
3 | | -title: Testing Component Bindings |
4 | | -sidebar_label: Testing Component Bindings |
| 3 | +title: Testing @Input and @Ouput |
| 4 | +sidebar_label: '@Input and @Output' |
5 | 5 | --- |
6 | 6 |
|
7 | | -TODO: Write proper documentation. |
8 | | -- Use `getShallow` instead of `renderComponent` and bind values |
9 | | -- Use page object `setBoundValues` in a fakeAsync context. |
| 7 | +In the previous section, we have seen how to mock services and child components in a component test. |
| 8 | +In this section we will cover how to inject data into the inputs of our components, and how to assert events emitted on its outputs. |
10 | 9 |
|
| 10 | +# Binding input data |
| 11 | + |
| 12 | +Consider the component below: |
| 13 | + |
| 14 | +```ts |
| 15 | +@Component({ |
| 16 | + selector: 'greetings-component', |
| 17 | + templateUrl: ` |
| 18 | + <span test-id="message">Hello {{name}}</span> |
| 19 | + ` |
| 20 | +}) |
| 21 | +export class GreetingsComponent { |
| 22 | + @Input() |
| 23 | + userName: string; |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +We would like to verify that the label gets updated with the user name. |
| 28 | + |
| 29 | +As always, we create a page object to model the interactions with our component: |
| 30 | + |
| 31 | +```ts |
| 32 | +class Page<T> extends BasePage<GreetingsComponent, T> { |
| 33 | + |
| 34 | + get messageText(): string { |
| 35 | + return (this.rendering.find('[test-id=message]').nativeElement as HTMLElement).innerText |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +Then, we use `setInputs` on the page object to update the input bindings and trigger a change detection cycle: |
| 41 | + |
| 42 | +```ts |
| 43 | +it('greets the user', fakeAsync(() => { |
| 44 | + // Render the component with initial input values |
| 45 | + const page = new Page(renderComponent(GreetingsComponent, GreetingsModule, { |
| 46 | + inputs: { |
| 47 | + userName: 'John' |
| 48 | + } |
| 49 | + })); |
| 50 | + expect(page.messageText).toBe('Hello John'); |
| 51 | + |
| 52 | + // Set input values and test again |
| 53 | + page.setInputs({ |
| 54 | + userName: 'Atul' |
| 55 | + }); |
| 56 | + expect(page.messageText).toBe('Hello Atul'); |
| 57 | +})); |
| 58 | +``` |
| 59 | + |
| 60 | +Setting input values after the component has been initialized can be useful when you want to test `onChanges` logic. |
| 61 | + |
| 62 | +:::caution |
| 63 | + |
| 64 | +You can only use `setInputs` for inputs that you have initialized in `renderComponent`. This is what the template type parameter `T` in `Page<T>` is for. |
| 65 | + |
| 66 | +::: |
| 67 | + |
| 68 | +# Test output events |
| 69 | + |
| 70 | +Let's consider a keypad component which lets users type in a numeric code: |
| 71 | + |
| 72 | +```ts |
| 73 | +@Component({ |
| 74 | + selector: 'keypad-component', |
| 75 | + templateUrl: ` |
| 76 | + <button *ngFor="number of numbers" [test-id]="number" (click)="onClick(number)></button> |
| 77 | + ` |
| 78 | +}) |
| 79 | +export class KeypadComponent { |
| 80 | + |
| 81 | + numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 82 | + |
| 83 | + code = ''; |
| 84 | + |
| 85 | + @Output() |
| 86 | + codeChange = new EventEmitter<string>(); |
| 87 | + |
| 88 | + onClick(n: number) { |
| 89 | + this.code += String(n); |
| 90 | + this.codeChange.emit(this.code); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +We would like to test the emitted output. |
| 96 | + |
| 97 | + |
| 98 | +```ts |
| 99 | +class Page<T> extends BasePage<KeypadComponent, T> { |
| 100 | + |
| 101 | + getButton(n: number): HTMLButtonElement { |
| 102 | + return this.rendering.find(`[test-id=${n}]`).nativeElement; |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +The page object exposes outputs utilities on `page.outputs`. Use `capture()` to create an array which receives all future events emitted on this output: |
| 108 | + |
| 109 | +```ts |
| 110 | +it('lets the user type a combination', fakeAsync(() => { |
| 111 | + const page = new Page(renderComponent(KeypadComponent, KeypadModule)); |
| 112 | + |
| 113 | + const emittedEvents = page.outputs.codeChage.capture(); |
| 114 | + |
| 115 | + page.getButton(1).click(); |
| 116 | + page.getButton(3).click(); |
| 117 | + page.getButton(3).click(); |
| 118 | + page.getButton(7).click(); |
| 119 | + |
| 120 | + expect(emittedEvents).toEqual(['1', '13', '133', '1337']) |
| 121 | +})); |
| 122 | +``` |
11 | 123 |
|
12 | | -See example in [fancy-button.component.spec.ts](https://github.com/hmil/ng-vacuum/blob/080e8803df257338497dd7b07f663ce502b03aac/test/ng7/src/app/fancy-button.component.spec.ts) |
|
0 commit comments