|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Component} from '@angular/core'; |
| 10 | +import {TestBed} from '@angular/core/testing'; |
| 11 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 12 | +import {ComponentHarness} from '@angular/cdk/testing'; |
| 13 | +import {AccordionHarness, AccordionGroupHarness} from './accordion-harness'; |
| 14 | +import {AccordionGroup, AccordionPanel, AccordionTrigger} from '../index'; |
| 15 | + |
| 16 | +/** Lightweight test harness to test querying inside the accordion body panel. */ |
| 17 | +class TestButtonHarness extends ComponentHarness { |
| 18 | + static hostSelector = 'button.test-button'; |
| 19 | + |
| 20 | + async getText(): Promise<string> { |
| 21 | + return (await this.host()).text(); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe('Accordion Harnesses', () => { |
| 26 | + let fixture: any; |
| 27 | + let loader: any; |
| 28 | + |
| 29 | + @Component({ |
| 30 | + imports: [AccordionGroup, AccordionPanel, AccordionTrigger], |
| 31 | + template: ` |
| 32 | + <div ngAccordionGroup> |
| 33 | + <div #panel1="ngAccordionPanel" ngAccordionPanel> |
| 34 | + <button class="test-button">Inside Content 1</button> |
| 35 | + </div> |
| 36 | + <button ngAccordionTrigger [panel]="panel1">Section 1</button> |
| 37 | +
|
| 38 | + <div #panel2="ngAccordionPanel" ngAccordionPanel>Content 2</div> |
| 39 | + <button ngAccordionTrigger [panel]="panel2" disabled>Section 2</button> |
| 40 | + </div> |
| 41 | + `, |
| 42 | + }) |
| 43 | + class AccordionHarnessTestComponent {} |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + TestBed.configureTestingModule({ |
| 47 | + imports: [AccordionHarnessTestComponent], |
| 48 | + }); |
| 49 | + fixture = TestBed.createComponent(AccordionHarnessTestComponent); |
| 50 | + fixture.detectChanges(); |
| 51 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should find accordion group and list all scoped accordions using getAccordions', async () => { |
| 55 | + const group = await loader.getHarness(AccordionGroupHarness); |
| 56 | + const accordions = await group.getAccordions(); |
| 57 | + |
| 58 | + expect(accordions.length).toBe(2); |
| 59 | + expect(await accordions[0].getTitle()).toBe('Section 1'); |
| 60 | + expect(await accordions[1].getTitle()).toBe('Section 2'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should find all individual accordions via standard root loader', async () => { |
| 64 | + const accordions = await loader.getAllHarnesses(AccordionHarness); |
| 65 | + expect(accordions.length).toBe(2); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should filter accordions by title', async () => { |
| 69 | + const accordions = await loader.getAllHarnesses(AccordionHarness.with({title: 'Section 1'})); |
| 70 | + expect(accordions.length).toBe(1); |
| 71 | + expect(await accordions[0].getTitle()).toBe('Section 1'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should filter accordions by expanded state', async () => { |
| 75 | + const accordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 76 | + await accordion.expand(); |
| 77 | + |
| 78 | + const expandedAccordions = await loader.getAllHarnesses( |
| 79 | + AccordionHarness.with({expanded: true}), |
| 80 | + ); |
| 81 | + expect(expandedAccordions.length).toBe(1); |
| 82 | + expect(await expandedAccordions[0].getTitle()).toBe('Section 1'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should filter accordions by disabled state', async () => { |
| 86 | + const disabledAccordions = await loader.getAllHarnesses( |
| 87 | + AccordionHarness.with({disabled: true}), |
| 88 | + ); |
| 89 | + expect(disabledAccordions.length).toBe(1); |
| 90 | + expect(await disabledAccordions[0].getTitle()).toBe('Section 2'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should get the title of the accordion', async () => { |
| 94 | + const accordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 95 | + expect(await accordion.getTitle()).toBe('Section 1'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should correctly report the expanded state of an accordion', async () => { |
| 99 | + const accordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 100 | + expect(await accordion.isExpanded()).toBeFalse(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should correctly report the disabled state of an accordion', async () => { |
| 104 | + const activeAccordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 105 | + const disabledAccordion = await loader.getHarness(AccordionHarness.with({title: 'Section 2'})); |
| 106 | + |
| 107 | + expect(await activeAccordion.isDisabled()).toBeFalse(); |
| 108 | + expect(await disabledAccordion.isDisabled()).toBeTrue(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('expands a collapsed accordion using the expand method', async () => { |
| 112 | + const accordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 113 | + |
| 114 | + await accordion.expand(); |
| 115 | + |
| 116 | + expect(await accordion.isExpanded()).toBeTrue(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('collapses an expanded accordion using the collapse method', async () => { |
| 120 | + const accordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 121 | + await accordion.expand(); |
| 122 | + |
| 123 | + await accordion.collapse(); |
| 124 | + |
| 125 | + expect(await accordion.isExpanded()).toBeFalse(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('toggles the expanded state of an accordion using the toggle method', async () => { |
| 129 | + const accordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 130 | + |
| 131 | + await accordion.toggle(); |
| 132 | + |
| 133 | + expect(await accordion.isExpanded()).toBeTrue(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should support focusing and blurring accordion triggers', async () => { |
| 137 | + const accordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 138 | + await accordion.focus(); |
| 139 | + expect(await accordion.isFocused()).toBeTrue(); |
| 140 | + |
| 141 | + await accordion.blur(); |
| 142 | + expect(await accordion.isFocused()).toBeFalse(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should query components inside the accordion panel using ContentContainerComponentHarness', async () => { |
| 146 | + const accordion = await loader.getHarness(AccordionHarness.with({title: 'Section 1'})); |
| 147 | + const button = await accordion.getHarness(TestButtonHarness); |
| 148 | + expect(await button.getText()).toBe('Inside Content 1'); |
| 149 | + }); |
| 150 | +}); |
0 commit comments