|
| 1 | +import { ReactiveFormsModule, FormsModule, FormControl } from '@angular/forms'; |
| 2 | +import { render, RenderResult } from '../../src/public_api'; |
| 3 | +import { Component, ViewChild, Input } from '@angular/core'; |
| 4 | + |
| 5 | +describe('selectOption: single', () => { |
| 6 | + test('with a template-driven form', async () => { |
| 7 | + @Component({ |
| 8 | + selector: 'fixture', |
| 9 | + template: ` |
| 10 | + <select data-testid="select" [(ngModel)]="value"> |
| 11 | + <option value="1" data-testid="apples">Apples</option> |
| 12 | + <option value="2" data-testid="oranges">Oranges</option> |
| 13 | + <option value="3" data-testid="lemons">Lemons</option> |
| 14 | + </select> |
| 15 | +
|
| 16 | + <p data-testid="text">{{ value }}</p> |
| 17 | + `, |
| 18 | + }) |
| 19 | + class FixtureComponent { |
| 20 | + value: string; |
| 21 | + } |
| 22 | + |
| 23 | + const component = await render(FixtureComponent, { |
| 24 | + imports: [FormsModule], |
| 25 | + }); |
| 26 | + |
| 27 | + assertSelectOptions(component, () => component.fixture.componentInstance.value); |
| 28 | + }); |
| 29 | + |
| 30 | + test('with a reactive form', async () => { |
| 31 | + @Component({ |
| 32 | + selector: 'fixture', |
| 33 | + template: ` |
| 34 | + <select data-testid="select" [formControl]="value"> |
| 35 | + <option value="1" data-testid="apples">Apples</option> |
| 36 | + <option value="2" data-testid="oranges">Oranges</option> |
| 37 | + <option value="3" data-testid="lemons">Lemons</option> |
| 38 | + </select> |
| 39 | +
|
| 40 | + <p data-testid="text">{{ value.value }}</p> |
| 41 | + `, |
| 42 | + }) |
| 43 | + class FixtureComponent { |
| 44 | + value = new FormControl(''); |
| 45 | + } |
| 46 | + |
| 47 | + const component = await render(FixtureComponent, { |
| 48 | + imports: [ReactiveFormsModule], |
| 49 | + }); |
| 50 | + |
| 51 | + assertSelectOptions(component, () => component.fixture.componentInstance.value.value); |
| 52 | + }); |
| 53 | + |
| 54 | + test('with change event', async () => { |
| 55 | + @Component({ |
| 56 | + selector: 'fixture', |
| 57 | + template: ` |
| 58 | + <select data-testid="select" (change)="onChange($event)"> |
| 59 | + <option value="1" data-testid="apples">Apples</option> |
| 60 | + <option value="2" data-testid="oranges">Oranges</option> |
| 61 | + <option value="3" data-testid="lemons">Lemons</option> |
| 62 | + </select> |
| 63 | +
|
| 64 | + <p data-testid="text">{{ value }}</p> |
| 65 | + `, |
| 66 | + }) |
| 67 | + class FixtureComponent { |
| 68 | + value = ''; |
| 69 | + |
| 70 | + onChange(event: KeyboardEvent) { |
| 71 | + this.value = (<HTMLInputElement>event.target).value; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const component = await render(FixtureComponent); |
| 76 | + |
| 77 | + assertSelectOptions(component, () => component.fixture.componentInstance.value); |
| 78 | + }); |
| 79 | + |
| 80 | + test('by reference', async () => { |
| 81 | + @Component({ |
| 82 | + selector: 'fixture', |
| 83 | + template: ` |
| 84 | + <select data-testid="select" #input> |
| 85 | + <option value="1" data-testid="apples">Apples</option> |
| 86 | + <option value="2" data-testid="oranges">Oranges</option> |
| 87 | + <option value="3" data-testid="lemons">Lemons</option> |
| 88 | + </select> |
| 89 | +
|
| 90 | + <p data-testid="text">{{ input.value }}</p> |
| 91 | + `, |
| 92 | + }) |
| 93 | + class FixtureComponent { |
| 94 | + @ViewChild('input', { static: false }) value; |
| 95 | + } |
| 96 | + |
| 97 | + const component = await render(FixtureComponent); |
| 98 | + |
| 99 | + assertSelectOptions(component, () => component.fixture.componentInstance.value.nativeElement.value); |
| 100 | + }); |
| 101 | + |
| 102 | + function assertSelectOptions(component: RenderResult, value: () => string) { |
| 103 | + const inputControl = component.getByTestId('select') as HTMLSelectElement; |
| 104 | + component.selectOptions(inputControl, /apples/i); |
| 105 | + component.selectOptions(inputControl, 'Oranges'); |
| 106 | + |
| 107 | + expect(value()).toBe('2'); |
| 108 | + expect(component.getByTestId('text').textContent).toBe('2'); |
| 109 | + expect(inputControl.value).toBe('2'); |
| 110 | + |
| 111 | + expect((component.getByTestId('apples') as HTMLOptionElement).selected).toBe(false); |
| 112 | + expect((component.getByTestId('oranges') as HTMLOptionElement).selected).toBe(true); |
| 113 | + expect((component.getByTestId('lemons') as HTMLOptionElement).selected).toBe(false); |
| 114 | + } |
| 115 | +}); |
| 116 | + |
| 117 | +describe('selectOption: multiple', () => { |
| 118 | + test('with a template-driven form', async () => { |
| 119 | + @Component({ |
| 120 | + selector: 'fixture', |
| 121 | + template: ` |
| 122 | + <select data-testid="select" multiple [(ngModel)]="value"> |
| 123 | + <option value="1" data-testid="apples">Apples</option> |
| 124 | + <option value="2" data-testid="oranges">Oranges</option> |
| 125 | + <option value="3" data-testid="lemons">Lemons</option> |
| 126 | + </select> |
| 127 | +
|
| 128 | + <p data-testid="text">{{ value }}</p> |
| 129 | + `, |
| 130 | + }) |
| 131 | + class FixtureComponent { |
| 132 | + value: string; |
| 133 | + } |
| 134 | + |
| 135 | + const component = await render(FixtureComponent, { |
| 136 | + imports: [FormsModule], |
| 137 | + }); |
| 138 | + assertSelectOptions(component, () => component.fixture.componentInstance.value); |
| 139 | + }); |
| 140 | + |
| 141 | + test('with a reactive form', async () => { |
| 142 | + @Component({ |
| 143 | + selector: 'fixture', |
| 144 | + template: ` |
| 145 | + <select data-testid="select" multiple [formControl]="value"> |
| 146 | + <option value="1" data-testid="apples">Apples</option> |
| 147 | + <option value="2" data-testid="oranges">Oranges</option> |
| 148 | + <option value="3" data-testid="lemons">Lemons</option> |
| 149 | + </select> |
| 150 | +
|
| 151 | + <p data-testid="text">{{ value.value }}</p> |
| 152 | + `, |
| 153 | + }) |
| 154 | + class FixtureComponent { |
| 155 | + value = new FormControl(''); |
| 156 | + } |
| 157 | + |
| 158 | + const component = await render(FixtureComponent, { |
| 159 | + imports: [ReactiveFormsModule], |
| 160 | + }); |
| 161 | + |
| 162 | + assertSelectOptions(component, () => component.fixture.componentInstance.value.value); |
| 163 | + }); |
| 164 | + |
| 165 | + test('with change event', async () => { |
| 166 | + @Component({ |
| 167 | + selector: 'fixture', |
| 168 | + template: ` |
| 169 | + <select data-testid="select" multiple (change)="onChange($event)"> |
| 170 | + <option value="1" data-testid="apples">Apples</option> |
| 171 | + <option value="2" data-testid="oranges">Oranges</option> |
| 172 | + <option value="3" data-testid="lemons">Lemons</option> |
| 173 | + </select> |
| 174 | +
|
| 175 | + <p data-testid="text">{{ value }}</p> |
| 176 | + `, |
| 177 | + }) |
| 178 | + class FixtureComponent { |
| 179 | + value = []; |
| 180 | + |
| 181 | + onChange(event: KeyboardEvent) { |
| 182 | + this.value = Array.from((<HTMLSelectElement>event.target).selectedOptions).map(o => o.value); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + const component = await render(FixtureComponent); |
| 187 | + |
| 188 | + assertSelectOptions(component, () => component.fixture.componentInstance.value); |
| 189 | + }); |
| 190 | + |
| 191 | + test('by reference', async () => { |
| 192 | + @Component({ |
| 193 | + selector: 'fixture', |
| 194 | + template: ` |
| 195 | + <select data-testid="select" multiple #input> |
| 196 | + <option value="1" data-testid="apples">Apples</option> |
| 197 | + <option value="2" data-testid="oranges">Oranges</option> |
| 198 | + <option value="3" data-testid="lemons">Lemons</option> |
| 199 | + </select> |
| 200 | +
|
| 201 | + <p data-testid="text">{{ input.value }}</p> |
| 202 | + `, |
| 203 | + }) |
| 204 | + class FixtureComponent { |
| 205 | + @ViewChild('input', { static: false }) value; |
| 206 | + } |
| 207 | + |
| 208 | + const component = await render(FixtureComponent); |
| 209 | + |
| 210 | + const inputControl = component.getByTestId('select') as HTMLSelectElement; |
| 211 | + component.selectOptions(inputControl, /apples/i); |
| 212 | + component.selectOptions(inputControl, ['Oranges', 'Lemons']); |
| 213 | + |
| 214 | + const options = component.fixture.componentInstance.value.nativeElement.selectedOptions; |
| 215 | + const value = Array.from(options).map((o: any) => o.value); |
| 216 | + |
| 217 | + expect(value).toEqual(['2', '3']); |
| 218 | + // shouldn't this be an empty string? - https://stackblitz.com/edit/angular-pdvm9n |
| 219 | + expect(component.getByTestId('text').textContent).toBe('2'); |
| 220 | + expect((component.getByTestId('apples') as HTMLOptionElement).selected).toBe(false); |
| 221 | + expect((component.getByTestId('oranges') as HTMLOptionElement).selected).toBe(true); |
| 222 | + expect((component.getByTestId('lemons') as HTMLOptionElement).selected).toBe(true); |
| 223 | + }); |
| 224 | + |
| 225 | + function assertSelectOptions(component: RenderResult, value: () => string) { |
| 226 | + const inputControl = component.getByTestId('select') as HTMLSelectElement; |
| 227 | + component.selectOptions(inputControl, /apples/i); |
| 228 | + component.selectOptions(inputControl, ['Oranges', 'Lemons']); |
| 229 | + |
| 230 | + expect(value()).toEqual(['2', '3']); |
| 231 | + expect(component.getByTestId('text').textContent).toBe('2,3'); |
| 232 | + expect((component.getByTestId('apples') as HTMLOptionElement).selected).toBe(false); |
| 233 | + expect((component.getByTestId('oranges') as HTMLOptionElement).selected).toBe(true); |
| 234 | + expect((component.getByTestId('lemons') as HTMLOptionElement).selected).toBe(true); |
| 235 | + } |
| 236 | +}); |
0 commit comments