1
- import { Component } from '@angular/core' ;
1
+ import { Component , signal } from '@angular/core' ;
2
2
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
3
3
import { Platform } from '@angular/cdk/platform' ;
4
4
import { HarnessLoader , parallel } from '@angular/cdk/testing' ;
@@ -7,22 +7,21 @@ import {MatButtonModule} from '../module';
7
7
import { MatIconModule } from '../../icon' ;
8
8
import { MatIconHarness } from '../../icon/testing' ;
9
9
import { MatButtonHarness } from './button-harness' ;
10
+ import { MatButtonAppearance } from '../button-base' ;
10
11
11
12
describe ( 'MatButtonHarness' , ( ) => {
12
13
let fixture : ComponentFixture < ButtonHarnessTest > ;
13
14
let loader : HarnessLoader ;
14
- let platform : Platform ;
15
15
16
16
beforeEach ( ( ) => {
17
17
fixture = TestBed . createComponent ( ButtonHarnessTest ) ;
18
18
fixture . detectChanges ( ) ;
19
19
loader = TestbedHarnessEnvironment . loader ( fixture ) ;
20
- platform = TestBed . inject ( Platform ) ;
21
20
} ) ;
22
21
23
22
it ( 'should load all button harnesses' , async ( ) => {
24
23
const buttons = await loader . getAllHarnesses ( MatButtonHarness ) ;
25
- expect ( buttons . length ) . toBe ( 16 ) ;
24
+ expect ( buttons . length ) . toBe ( 17 ) ;
26
25
} ) ;
27
26
28
27
it ( 'should load button with exact text' , async ( ) => {
@@ -41,7 +40,7 @@ describe('MatButtonHarness', () => {
41
40
it ( 'should filter by whether a button is disabled' , async ( ) => {
42
41
const enabledButtons = await loader . getAllHarnesses ( MatButtonHarness . with ( { disabled : false } ) ) ;
43
42
const disabledButtons = await loader . getAllHarnesses ( MatButtonHarness . with ( { disabled : true } ) ) ;
44
- expect ( enabledButtons . length ) . toBe ( 14 ) ;
43
+ expect ( enabledButtons . length ) . toBe ( 15 ) ;
45
44
expect ( disabledButtons . length ) . toBe ( 2 ) ;
46
45
} ) ;
47
46
@@ -79,7 +78,7 @@ describe('MatButtonHarness', () => {
79
78
const button = await loader . getHarness ( MatButtonHarness . with ( { text : 'Basic button' } ) ) ;
80
79
await button . click ( ) ;
81
80
82
- expect ( fixture . componentInstance . clicked ) . toBe ( true ) ;
81
+ expect ( fixture . componentInstance . clicked ( ) ) . toBe ( true ) ;
83
82
} ) ;
84
83
85
84
it ( 'should not click a disabled button' , async ( ) => {
@@ -88,14 +87,14 @@ describe('MatButtonHarness', () => {
88
87
// cancel dispatched click events on disabled buttons. We skip this check on Edge and Firefox.
89
88
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=1582570 and:
90
89
// https://stackoverflow.com/questions/32377026/disabled-button-is-clickable-on-edge-browser
91
- if ( platform . FIREFOX ) {
90
+ if ( TestBed . inject ( Platform ) . FIREFOX ) {
92
91
return ;
93
92
}
94
93
95
94
const button = await loader . getHarness ( MatButtonHarness . with ( { text : 'Filled button' } ) ) ;
96
95
await button . click ( ) ;
97
96
98
- expect ( fixture . componentInstance . clicked ) . toBe ( false ) ;
97
+ expect ( fixture . componentInstance . clicked ( ) ) . toBe ( false ) ;
99
98
} ) ;
100
99
101
100
it ( 'should be able to handle nested harnesses' , async ( ) => {
@@ -127,6 +126,7 @@ describe('MatButtonHarness', () => {
127
126
'basic' ,
128
127
'basic' ,
129
128
'basic' ,
129
+ 'basic' ,
130
130
'icon' ,
131
131
'fab' ,
132
132
'mini-fab' ,
@@ -151,6 +151,7 @@ describe('MatButtonHarness', () => {
151
151
'filled' ,
152
152
'elevated' ,
153
153
'outlined' ,
154
+ 'tonal' ,
154
155
null ,
155
156
null ,
156
157
null ,
@@ -166,16 +167,25 @@ describe('MatButtonHarness', () => {
166
167
const button = await loader . getHarness ( MatButtonHarness . with ( { appearance : 'filled' } ) ) ;
167
168
expect ( await button . getText ( ) ) . toBe ( 'Filled button' ) ;
168
169
} ) ;
170
+
171
+ it ( 'should get the appearance of a button with a dynamic appearance' , async ( ) => {
172
+ const button = await loader . getHarness (
173
+ MatButtonHarness . with ( { selector : '#dynamic-appearance' } ) ,
174
+ ) ;
175
+ expect ( await button . getAppearance ( ) ) . toBe ( 'tonal' ) ;
176
+ fixture . componentInstance . dynamicAppearance . set ( 'filled' ) ;
177
+ expect ( await button . getAppearance ( ) ) . toBe ( 'filled' ) ;
178
+ } ) ;
169
179
} ) ;
170
180
171
181
@Component ( {
172
182
// Include one of each type of button selector to ensure that they're all captured by
173
183
// the harness's selector.
174
184
template : `
175
- <button id="basic" type="button" matButton (click)="clicked = true">
185
+ <button id="basic" type="button" matButton (click)="clicked.set( true) ">
176
186
Basic button
177
187
</button>
178
- <button id="flat" type="button" matButton="filled" disabled (click)="clicked = true">
188
+ <button id="flat" type="button" matButton="filled" disabled (click)="clicked.set( true) ">
179
189
Filled button
180
190
</button>
181
191
<button id="raised" type="button" matButton="elevated">Elevated button</button>
@@ -194,13 +204,14 @@ describe('MatButtonHarness', () => {
194
204
<a id="anchor-flat" matButton="filled">Filled anchor</a>
195
205
<a id="anchor-raised" matButton="elevated" disabled>Elevated anchor</a>
196
206
<a id="anchor-stroked" matButton="outlined">Stroked anchor</a>
207
+ <a id="dynamic-appearance" [matButton]="dynamicAppearance()">Stroked anchor</a>
197
208
<a id="anchor-icon" matIconButton>Icon anchor</a>
198
209
<a id="anchor-fab" matFab>Fab anchor</a>
199
210
<a id="anchor-mini-fab" matMiniFab>Mini Fab anchor</a>
200
211
` ,
201
212
imports : [ MatButtonModule , MatIconModule ] ,
202
213
} )
203
214
class ButtonHarnessTest {
204
- disabled = true ;
205
- clicked = false ;
215
+ clicked = signal ( false ) ;
216
+ dynamicAppearance = signal < MatButtonAppearance > ( 'tonal' ) ;
206
217
}
0 commit comments