|
1 |
| -import {createKeyboardEvent, dispatchEvent, dispatchMouseEvent} from '@angular/cdk/testing/private'; |
2 | 1 | import {DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW, UP_ARROW} from '@angular/cdk/keycodes';
|
3 |
| -import {Component, DebugElement, QueryList, ViewChild, ViewChildren} from '@angular/core'; |
| 2 | +import {createKeyboardEvent, dispatchEvent, dispatchMouseEvent} from '@angular/cdk/testing/private'; |
| 3 | +import { |
| 4 | + Component, |
| 5 | + DebugElement, |
| 6 | + QueryList, |
| 7 | + signal, |
| 8 | + viewChild, |
| 9 | + ViewChild, |
| 10 | + ViewChildren, |
| 11 | +} from '@angular/core'; |
4 | 12 | import {
|
5 | 13 | ComponentFixture,
|
6 |
| - TestBed, |
7 | 14 | fakeAsync,
|
8 | 15 | flush,
|
| 16 | + TestBed, |
9 | 17 | tick,
|
10 | 18 | waitForAsync,
|
11 | 19 | } from '@angular/core/testing';
|
12 | 20 | import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
|
| 21 | +import {Control, disabled, form} from '@angular/forms/signals'; |
13 | 22 | import {By} from '@angular/platform-browser';
|
14 | 23 | import {
|
15 | 24 | MAT_BUTTON_TOGGLE_DEFAULT_OPTIONS,
|
@@ -318,6 +327,43 @@ describe('MatButtonToggle with forms', () => {
|
318 | 327 | }));
|
319 | 328 | });
|
320 | 329 |
|
| 330 | +describe('MatButtonToggle with signal forms', () => { |
| 331 | + it('should sync single-select value', () => { |
| 332 | + const fixture = TestBed.createComponent(SignalFormsButtonToggle); |
| 333 | + TestBed.tick(); |
| 334 | + expect(fixture.componentInstance.group().value).toBe('two'); |
| 335 | + fixture.nativeElement.querySelector('.mat-button-toggle-button').click(); |
| 336 | + TestBed.tick(); |
| 337 | + expect(fixture.componentInstance.group().value).toBe('one'); |
| 338 | + }); |
| 339 | + |
| 340 | + it('should sync multi-select value', () => { |
| 341 | + const fixture = TestBed.createComponent(SignalFormsMultiButtonToggle); |
| 342 | + TestBed.tick(); |
| 343 | + expect(fixture.componentInstance.group().value).toEqual(['two']); |
| 344 | + fixture.nativeElement.querySelector('.mat-button-toggle-button').click(); |
| 345 | + TestBed.tick(); |
| 346 | + expect(fixture.componentInstance.group().value).toEqual(['two', 'one']); |
| 347 | + }); |
| 348 | + |
| 349 | + it('should sync disabled state', () => { |
| 350 | + const fixture = TestBed.createComponent(SignalFormsButtonToggle); |
| 351 | + TestBed.tick(); |
| 352 | + expect(fixture.componentInstance.group().disabled).toBe(false); |
| 353 | + fixture.componentInstance.isDisabled.set(true); |
| 354 | + TestBed.tick(); |
| 355 | + expect(fixture.componentInstance.group().disabled).toBe(true); |
| 356 | + }); |
| 357 | + |
| 358 | + it('should sync name', () => { |
| 359 | + const fixture = TestBed.createComponent(SignalFormsButtonToggle); |
| 360 | + TestBed.tick(); |
| 361 | + expect(fixture.componentInstance.group().name).toMatch( |
| 362 | + fixture.componentInstance.field().name(), |
| 363 | + ); |
| 364 | + }); |
| 365 | +}); |
| 366 | + |
321 | 367 | describe('MatButtonToggle without forms', () => {
|
322 | 368 | describe('inside of an exclusive selection group', () => {
|
323 | 369 | let fixture: ComponentFixture<ButtonTogglesInsideButtonToggleGroup>;
|
@@ -1151,6 +1197,41 @@ describe('MatButtonToggle without forms', () => {
|
1151 | 1197 | });
|
1152 | 1198 | });
|
1153 | 1199 |
|
| 1200 | +@Component({ |
| 1201 | + template: ` |
| 1202 | + <mat-button-toggle-group [control]="field"> |
| 1203 | + @for (opt of options; track $index) { |
| 1204 | + <mat-button-toggle [value]="opt">{{opt}}</mat-button-toggle> |
| 1205 | + } |
| 1206 | + </mat-button-toggle-group> |
| 1207 | + `, |
| 1208 | + imports: [MatButtonToggle, MatButtonToggleGroup, Control], |
| 1209 | +}) |
| 1210 | +class SignalFormsButtonToggle { |
| 1211 | + options = ['one', 'two', 'three']; |
| 1212 | + isDisabled = signal(false); |
| 1213 | + field = form(signal('two'), p => { |
| 1214 | + disabled(p, this.isDisabled); |
| 1215 | + }); |
| 1216 | + group = viewChild.required(MatButtonToggleGroup); |
| 1217 | +} |
| 1218 | + |
| 1219 | +@Component({ |
| 1220 | + template: ` |
| 1221 | + <mat-button-toggle-group multiple [control]="field"> |
| 1222 | + @for (opt of options; track $index) { |
| 1223 | + <mat-button-toggle [value]="opt">{{opt}}</mat-button-toggle> |
| 1224 | + } |
| 1225 | + </mat-button-toggle-group> |
| 1226 | + `, |
| 1227 | + imports: [MatButtonToggle, MatButtonToggleGroup, Control], |
| 1228 | +}) |
| 1229 | +class SignalFormsMultiButtonToggle { |
| 1230 | + options = ['one', 'two', 'three']; |
| 1231 | + field = form(signal(['two'])); |
| 1232 | + group = viewChild.required(MatButtonToggleGroup); |
| 1233 | +} |
| 1234 | + |
1154 | 1235 | @Component({
|
1155 | 1236 | template: `
|
1156 | 1237 | <mat-button-toggle-group
|
|
0 commit comments