|
1 |
| -import {MatExpansionModule} from '@angular/material/expansion'; |
2 |
| - |
| 1 | +import {Component} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 3 | +import {ComponentHarness, HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 4 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 5 | +import {MatAccordionTogglePosition, MatExpansionModule} from '@angular/material/expansion'; |
| 6 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
3 | 7 | import {MatAccordionHarness} from './accordion-harness';
|
4 | 8 | import {MatExpansionPanelHarness} from './expansion-harness';
|
5 |
| -import {runHarnessTests} from './shared.spec'; |
6 | 9 |
|
7 |
| -describe('Non-MDC-based expansion harnesses', () => { |
8 |
| - runHarnessTests(MatExpansionModule, MatAccordionHarness, MatExpansionPanelHarness); |
| 10 | +describe('MatExpansionHarness', () => { |
| 11 | + let fixture: ComponentFixture<ExpansionHarnessTestComponent>; |
| 12 | + let loader: HarnessLoader; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + await TestBed.configureTestingModule({ |
| 16 | + imports: [MatExpansionModule, NoopAnimationsModule], |
| 17 | + declarations: [ExpansionHarnessTestComponent], |
| 18 | + }).compileComponents(); |
| 19 | + |
| 20 | + fixture = TestBed.createComponent(ExpansionHarnessTestComponent); |
| 21 | + fixture.detectChanges(); |
| 22 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should be able to load accordion', async () => { |
| 26 | + const accordions = await loader.getAllHarnesses(MatAccordionHarness); |
| 27 | + expect(accordions.length).toBe(2); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should be able to load an accordion by selector', async () => { |
| 31 | + const accordions = await loader.getAllHarnesses( |
| 32 | + MatAccordionHarness.with({selector: '#accordion2'}), |
| 33 | + ); |
| 34 | + expect(accordions.length).toBe(1); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should be able to load expansion panels', async () => { |
| 38 | + const panels = await loader.getAllHarnesses(MatExpansionPanelHarness); |
| 39 | + expect(panels.length).toBe(5); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should be able to load expansion panel by title matching regex', async () => { |
| 43 | + const panels = await loader.getAllHarnesses(MatExpansionPanelHarness.with({title: /Panel#2/})); |
| 44 | + expect(panels.length).toBe(1); |
| 45 | + expect(await panels[0].getTitle()).toBe('Title of Panel#2'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should be able to load expansion panel by title matching exact text', async () => { |
| 49 | + const panels = await loader.getAllHarnesses( |
| 50 | + MatExpansionPanelHarness.with({title: 'Standalone Panel Title'}), |
| 51 | + ); |
| 52 | + expect(panels.length).toBe(1); |
| 53 | + expect(await panels[0].getTitle()).toBe('Standalone Panel Title'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should be able to load expansion panel without title', async () => { |
| 57 | + const panels = await loader.getAllHarnesses(MatExpansionPanelHarness.with({title: null})); |
| 58 | + expect(panels.length).toBe(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should be able to load expansion panel by description matching regex', async () => { |
| 62 | + const panels = await loader.getAllHarnesses( |
| 63 | + MatExpansionPanelHarness.with({description: /Panel#2/}), |
| 64 | + ); |
| 65 | + expect(panels.length).toBe(1); |
| 66 | + expect(await panels[0].getDescription()).toBe('Description of Panel#2'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should be able to load expansion panel by description matching exact text', async () => { |
| 70 | + const panels = await loader.getAllHarnesses( |
| 71 | + MatExpansionPanelHarness.with({description: 'Description of Panel#1'}), |
| 72 | + ); |
| 73 | + expect(panels.length).toBe(1); |
| 74 | + expect(await panels[0].getDescription()).toBe('Description of Panel#1'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should be able to load expansion panel without description', async () => { |
| 78 | + const panels = await loader.getAllHarnesses(MatExpansionPanelHarness.with({description: null})); |
| 79 | + expect(panels.length).toBe(3); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should be able to load expanded panels', async () => { |
| 83 | + const panels = await loader.getAllHarnesses(MatExpansionPanelHarness.with({expanded: true})); |
| 84 | + expect(panels.length).toBe(2); |
| 85 | + expect(await panels[0].getTitle()).toBe('Title of Panel#2'); |
| 86 | + expect(await panels[1].getTitle()).toBe('Standalone Panel Title'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should be able to load collapsed panels', async () => { |
| 90 | + const panels = await loader.getAllHarnesses(MatExpansionPanelHarness.with({expanded: false})); |
| 91 | + expect(panels.length).toBe(3); |
| 92 | + expect(await panels[0].getTitle()).toBe('Title of Panel#1'); |
| 93 | + expect(await panels[1].getTextContent()).toBe('Accordion #2 - Content'); |
| 94 | + expect(await panels[2].getTitle()).toBe('Disabled Panel Title'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should be able to load expansion panel by content matching regex', async () => { |
| 98 | + const panels = await loader.getAllHarnesses( |
| 99 | + MatExpansionPanelHarness.with({content: /Accordion #2/}), |
| 100 | + ); |
| 101 | + expect(panels.length).toBe(1); |
| 102 | + expect(await panels[0].getTextContent()).toBe('Accordion #2 - Content'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should be able to load expansion panel by content matching exact string', async () => { |
| 106 | + const panels = await loader.getAllHarnesses( |
| 107 | + MatExpansionPanelHarness.with({content: 'Content of Panel#2'}), |
| 108 | + ); |
| 109 | + expect(panels.length).toBe(1); |
| 110 | + expect(await panels[0].getTextContent()).toBe('Content of Panel#2'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should be able to load expansion panels based on disabled state', async () => { |
| 114 | + const panels = await loader.getAllHarnesses(MatExpansionPanelHarness.with({disabled: true})); |
| 115 | + expect(panels.length).toBe(1); |
| 116 | + expect(await panels[0].getTitle()).toBe('Disabled Panel Title'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should be able to get expansion state of panel', async () => { |
| 120 | + const panel = await loader.getHarness(MatExpansionPanelHarness.with({title: /Panel#1/})); |
| 121 | + expect(await panel.isExpanded()).toBe(false); |
| 122 | + fixture.componentInstance.panel1Expanded = true; |
| 123 | + expect(await panel.isExpanded()).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should be able to get title of panel', async () => { |
| 127 | + const panel = await loader.getHarness(MatExpansionPanelHarness.with({content: /Panel#1/})); |
| 128 | + expect(await panel.getTitle()).toBe('Title of Panel#1'); |
| 129 | + fixture.componentInstance.panel1Title = 'new title'; |
| 130 | + expect(await panel.getTitle()).toBe('new title'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should be able to get description of panel', async () => { |
| 134 | + const panel = await loader.getHarness(MatExpansionPanelHarness.with({content: /Panel#1/})); |
| 135 | + expect(await panel.getDescription()).toBe('Description of Panel#1'); |
| 136 | + fixture.componentInstance.panel1Description = 'new description'; |
| 137 | + expect(await panel.getDescription()).toBe('new description'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should be able to get disabled state of panel', async () => { |
| 141 | + const panel = await loader.getHarness( |
| 142 | + MatExpansionPanelHarness.with({selector: '#disabledPanel'}), |
| 143 | + ); |
| 144 | + expect(await panel.isDisabled()).toBe(true); |
| 145 | + fixture.componentInstance.isDisabled = false; |
| 146 | + expect(await panel.isDisabled()).toBe(false); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should be able to toggle expansion state of panel', async () => { |
| 150 | + const panel = await loader.getHarness(MatExpansionPanelHarness); |
| 151 | + expect(await panel.isExpanded()).toBe(false); |
| 152 | + await panel.toggle(); |
| 153 | + expect(await panel.isExpanded()).toBe(true); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should be able to expand a panel', async () => { |
| 157 | + const panel = await loader.getHarness(MatExpansionPanelHarness); |
| 158 | + expect(await panel.isExpanded()).toBe(false); |
| 159 | + await panel.expand(); |
| 160 | + expect(await panel.isExpanded()).toBe(true); |
| 161 | + // checking a second time to ensure it does not modify |
| 162 | + // the state if already expanded. |
| 163 | + await panel.expand(); |
| 164 | + expect(await panel.isExpanded()).toBe(true); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should be able to collapse a panel', async () => { |
| 168 | + const panel = await loader.getHarness(MatExpansionPanelHarness); |
| 169 | + expect(await panel.isExpanded()).toBe(false); |
| 170 | + await panel.expand(); |
| 171 | + expect(await panel.isExpanded()).toBe(true); |
| 172 | + await panel.collapse(); |
| 173 | + expect(await panel.isExpanded()).toBe(false); |
| 174 | + // checking a second time to ensure it does not modify |
| 175 | + // the state if already collapsed. |
| 176 | + await panel.collapse(); |
| 177 | + expect(await panel.isExpanded()).toBe(false); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should be able to get text content of expansion panel', async () => { |
| 181 | + const panel = await loader.getHarness(MatExpansionPanelHarness); |
| 182 | + expect(await panel.getTextContent()).toBe('Content of Panel#1'); |
| 183 | + fixture.componentInstance.panel1Content = 'new content'; |
| 184 | + expect(await panel.getTextContent()).toBe('new content'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should be able to get harness loader for content of panel', async () => { |
| 188 | + const panel = await loader.getHarness( |
| 189 | + MatExpansionPanelHarness.with({selector: '#standalonePanel'}), |
| 190 | + ); |
| 191 | + const matchedHarnesses = await panel.getAllHarnesses(TestContentHarness); |
| 192 | + expect(matchedHarnesses.length).toBe(1); |
| 193 | + expect(await matchedHarnesses[0].getText()).toBe('Part of expansion panel'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should be able to focus expansion panel', async () => { |
| 197 | + const panel = await loader.getHarness(MatExpansionPanelHarness); |
| 198 | + expect(getActiveElementTag()).not.toBe('mat-expansion-panel-header'); |
| 199 | + await panel.focus(); |
| 200 | + expect(getActiveElementTag()).toBe('mat-expansion-panel-header'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should be able to blur expansion panel', async () => { |
| 204 | + const panel = await loader.getHarness(MatExpansionPanelHarness); |
| 205 | + await panel.focus(); |
| 206 | + expect(getActiveElementTag()).toBe('mat-expansion-panel-header'); |
| 207 | + await panel.blur(); |
| 208 | + expect(getActiveElementTag()).not.toBe('mat-expansion-panel-header'); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should be able to check if expansion panel has toggle indicator', async () => { |
| 212 | + const accordion = await loader.getHarness(MatAccordionHarness); |
| 213 | + const standalonePanel = await loader.getHarness( |
| 214 | + MatExpansionPanelHarness.with({selector: '#standalonePanel'}), |
| 215 | + ); |
| 216 | + const expansionPanels = [standalonePanel, ...(await accordion.getExpansionPanels())]; |
| 217 | + let toggleIndicatorChecks = await parallel(() => { |
| 218 | + return expansionPanels.map(p => p.hasToggleIndicator()); |
| 219 | + }); |
| 220 | + expect(toggleIndicatorChecks.every(s => s)).toBe(true); |
| 221 | + fixture.componentInstance.hideToggleIndicators = true; |
| 222 | + toggleIndicatorChecks = await parallel(() => expansionPanels.map(p => p.hasToggleIndicator())); |
| 223 | + expect(toggleIndicatorChecks.every(s => !s)).toBe(true); |
| 224 | + }); |
| 225 | + |
| 226 | + it('should be able to get toggle indicator position of panels', async () => { |
| 227 | + const accordion = await loader.getHarness(MatAccordionHarness); |
| 228 | + const standalonePanel = await loader.getHarness( |
| 229 | + MatExpansionPanelHarness.with({selector: '#standalonePanel'}), |
| 230 | + ); |
| 231 | + const expansionPanels = [standalonePanel, ...(await accordion.getExpansionPanels())]; |
| 232 | + let togglePositions = await parallel(() => |
| 233 | + expansionPanels.map(p => p.getToggleIndicatorPosition()), |
| 234 | + ); |
| 235 | + expect(togglePositions.every(p => p === 'after')).toBe(true); |
| 236 | + fixture.componentInstance.toggleIndicatorsPosition = 'before'; |
| 237 | + togglePositions = await parallel(() => { |
| 238 | + return expansionPanels.map(p => p.getToggleIndicatorPosition()); |
| 239 | + }); |
| 240 | + expect(togglePositions.every(p => p === 'before')).toBe(true); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should be able to get expansion panels of accordion', async () => { |
| 244 | + const accordion = await loader.getHarness(MatAccordionHarness); |
| 245 | + const panels = await accordion.getExpansionPanels(); |
| 246 | + expect(panels.length).toBe(2); |
| 247 | + expect(await panels[0].getTitle()).toBe('Title of Panel#1'); |
| 248 | + expect(await panels[1].getTitle()).toBe('Title of Panel#2'); |
| 249 | + }); |
| 250 | + |
| 251 | + it('should be able to get expansion panels of accordion with filter', async () => { |
| 252 | + const accordion = await loader.getHarness(MatAccordionHarness); |
| 253 | + const panels = await accordion.getExpansionPanels({title: /Panel#1/}); |
| 254 | + expect(panels.length).toBe(1); |
| 255 | + expect(await panels[0].getTitle()).toBe('Title of Panel#1'); |
| 256 | + }); |
| 257 | + |
| 258 | + it('should be able to check if accordion has multi panel support enabled', async () => { |
| 259 | + const accordion = await loader.getHarness(MatAccordionHarness); |
| 260 | + expect(await accordion.isMulti()).toBe(false); |
| 261 | + fixture.componentInstance.multiMode = true; |
| 262 | + expect(await accordion.isMulti()).toBe(true); |
| 263 | + }); |
9 | 264 | });
|
| 265 | + |
| 266 | +function getActiveElementTag() { |
| 267 | + return document.activeElement ? document.activeElement.tagName.toLowerCase() : ''; |
| 268 | +} |
| 269 | + |
| 270 | +@Component({ |
| 271 | + template: ` |
| 272 | + <mat-accordion id="accordion1" [hideToggle]="hideToggleIndicators" |
| 273 | + [togglePosition]="toggleIndicatorsPosition" |
| 274 | + [multi]="multiMode"> |
| 275 | + <mat-expansion-panel [expanded]="panel1Expanded" id="panel1"> |
| 276 | + <mat-expansion-panel-header> |
| 277 | + <mat-panel-title>{{panel1Title}}</mat-panel-title> |
| 278 | + <mat-panel-description>{{panel1Description}}</mat-panel-description> |
| 279 | + </mat-expansion-panel-header> |
| 280 | + {{panel1Content}} |
| 281 | + </mat-expansion-panel> |
| 282 | + <mat-expansion-panel expanded> |
| 283 | + <mat-expansion-panel-header> |
| 284 | + <mat-panel-title> |
| 285 | + Title of Panel#2 |
| 286 | + </mat-panel-title> |
| 287 | + <mat-panel-description> |
| 288 | + Description of Panel#2 |
| 289 | + </mat-panel-description> |
| 290 | + </mat-expansion-panel-header> |
| 291 | + Content of Panel#2 |
| 292 | + </mat-expansion-panel> |
| 293 | + </mat-accordion> |
| 294 | +
|
| 295 | + <mat-accordion id="accordion2"> |
| 296 | + <mat-expansion-panel> |
| 297 | + <mat-expansion-panel-header> |
| 298 | + Accordion #2 - Header |
| 299 | + </mat-expansion-panel-header> |
| 300 | + <p>Accordion #2 - Content</p> |
| 301 | + </mat-expansion-panel> |
| 302 | + </mat-accordion> |
| 303 | +
|
| 304 | + <mat-expansion-panel id="standalonePanel" expanded |
| 305 | + [hideToggle]="hideToggleIndicators" |
| 306 | + [togglePosition]="toggleIndicatorsPosition"> |
| 307 | + <mat-expansion-panel-header> |
| 308 | + <mat-panel-title>Standalone Panel Title</mat-panel-title> |
| 309 | + </mat-expansion-panel-header> |
| 310 | + <div> |
| 311 | + <span>Standalone Panel Body</span> |
| 312 | + <div class="test-content-harness">Part of expansion panel</div> |
| 313 | + </div> |
| 314 | + </mat-expansion-panel> |
| 315 | +
|
| 316 | + <mat-expansion-panel id="disabledPanel" [disabled]="isDisabled"> |
| 317 | + <mat-expansion-panel-header> |
| 318 | + <mat-panel-title>Disabled Panel Title</mat-panel-title> |
| 319 | + </mat-expansion-panel-header> |
| 320 | + </mat-expansion-panel> |
| 321 | +
|
| 322 | + <div class="test-content-harness">Outside of expansion panel</div> |
| 323 | + `, |
| 324 | +}) |
| 325 | +class ExpansionHarnessTestComponent { |
| 326 | + panel1Expanded = false; |
| 327 | + panel1Title = 'Title of Panel#1'; |
| 328 | + panel1Description = 'Description of Panel#1'; |
| 329 | + panel1Content = 'Content of Panel#1'; |
| 330 | + hideToggleIndicators = false; |
| 331 | + toggleIndicatorsPosition: MatAccordionTogglePosition; |
| 332 | + isDisabled = true; |
| 333 | + multiMode = false; |
| 334 | +} |
| 335 | + |
| 336 | +class TestContentHarness extends ComponentHarness { |
| 337 | + static hostSelector = '.test-content-harness'; |
| 338 | + |
| 339 | + async getText() { |
| 340 | + return (await this.host()).text(); |
| 341 | + } |
| 342 | +} |
0 commit comments