Skip to content

Commit 44744d2

Browse files
authored
fix: accordion trigger doesn't close it content when state open (#57)
* fix: accordion trigger doesn't close it content * test: add accordion e2e test
1 parent 84476e9 commit 44744d2

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
describe('accordion', () => {
2+
const verifyAccordionSetup = () => {
3+
cy.get('brn-accordion').should('have.attr', 'data-state', 'closed');
4+
cy.get('brn-accordion-item').should('have.length', 3);
5+
cy.get('brn-accordion-item').first().as('firstItem');
6+
cy.get('@firstItem').next().as('secondItem');
7+
cy.get('@secondItem').next().as('thirdItem');
8+
9+
cy.get('@firstItem').should('have.attr', 'data-state', 'closed');
10+
cy.get('@secondItem').should('have.attr', 'data-state', 'closed');
11+
cy.get('@thirdItem').should('have.attr', 'data-state', 'closed');
12+
13+
cy.get('@firstItem')
14+
.find('brn-accordion-trigger')
15+
.should('have.attr', 'role', 'heading')
16+
.should('have.id', 'brn-accordion-trigger-0')
17+
.should('have.attr', 'data-state', 'closed')
18+
.should('have.attr', 'aria-expanded', 'false');
19+
cy.get('@firstItem')
20+
.find('brn-accordion-content')
21+
.should('have.attr', 'role', 'region')
22+
.should('have.id', 'brn-accordion-content-0')
23+
.should('have.attr', 'data-state', 'closed');
24+
};
25+
26+
const verifyAccordionStateOpen = () => {
27+
cy.get('brn-accordion').should('have.attr', 'data-state', 'open');
28+
};
29+
30+
const verifyAccordionStateClosed = () => {
31+
cy.get('brn-accordion').should('have.attr', 'data-state', 'closed');
32+
};
33+
34+
const verifyAccordionFirstItemStateOpen = () => {
35+
cy.get('@firstItem').should('have.attr', 'data-state', 'open');
36+
cy.get('@firstItem')
37+
.find('brn-accordion-trigger')
38+
.should('have.attr', 'data-state', 'open')
39+
.should('have.attr', 'aria-expanded', 'true');
40+
cy.get('@firstItem').find('brn-accordion-content').should('have.attr', 'data-state', 'open');
41+
};
42+
43+
const verifyAccordionFirstItemStateClosed = () => {
44+
cy.get('@firstItem').should('have.attr', 'data-state', 'closed');
45+
cy.get('@firstItem')
46+
.find('brn-accordion-trigger')
47+
.should('have.attr', 'data-state', 'closed')
48+
.should('have.attr', 'aria-expanded', 'false');
49+
cy.get('@firstItem').find('brn-accordion-content').should('have.attr', 'data-state', 'closed');
50+
};
51+
52+
const verifyAccordionSecondItemStateOpen = () => {
53+
cy.get('@secondItem').should('have.attr', 'data-state', 'open');
54+
cy.get('@secondItem')
55+
.find('brn-accordion-trigger')
56+
.should('have.attr', 'data-state', 'open')
57+
.should('have.attr', 'aria-expanded', 'true');
58+
cy.get('@secondItem').find('brn-accordion-content').should('have.attr', 'data-state', 'open');
59+
};
60+
61+
const verifyAccordionSecondItemStateClosed = () => {
62+
cy.get('@secondItem').should('have.attr', 'data-state', 'closed');
63+
cy.get('@secondItem')
64+
.find('brn-accordion-trigger')
65+
.should('have.attr', 'data-state', 'closed')
66+
.should('have.attr', 'aria-expanded', 'false');
67+
cy.get('@secondItem').find('brn-accordion-content').should('have.attr', 'data-state', 'closed');
68+
};
69+
70+
describe('default', () => {
71+
beforeEach(() => {
72+
cy.visit('/iframe.html?id=accordion--default');
73+
cy.injectAxe();
74+
});
75+
76+
it('click on trigger should open and close it content', () => {
77+
verifyAccordionSetup();
78+
79+
// click trigger first item to open it content
80+
cy.get('@firstItem').find('brn-accordion-trigger').click();
81+
verifyAccordionStateOpen();
82+
verifyAccordionFirstItemStateOpen();
83+
84+
// click trigger first item to close it content
85+
cy.get('@firstItem').find('brn-accordion-trigger').click();
86+
verifyAccordionStateClosed();
87+
verifyAccordionFirstItemStateClosed();
88+
});
89+
90+
it('click on trigger should open it content and click on another trigger should closed previous content and open newly triggered content', () => {
91+
verifyAccordionSetup();
92+
93+
// click trigger first item to open it content
94+
cy.get('@firstItem').find('brn-accordion-trigger').click();
95+
verifyAccordionStateOpen();
96+
verifyAccordionFirstItemStateOpen();
97+
98+
// click trigger second item to open it content and close previous content
99+
cy.get('@secondItem').find('brn-accordion-trigger').click();
100+
verifyAccordionStateOpen();
101+
verifyAccordionSecondItemStateOpen();
102+
verifyAccordionFirstItemStateClosed();
103+
104+
// click trigger second item to close it content
105+
cy.get('@secondItem').find('brn-accordion-trigger').click();
106+
verifyAccordionStateClosed();
107+
verifyAccordionSecondItemStateClosed();
108+
});
109+
});
110+
});

libs/ui/accordion/brain/src/lib/brn-accordion.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class BrnAccordionComponent implements AfterContentInit {
5151

5252
public toggleItem(id: number) {
5353
if (this._openItemIds().includes(id)) {
54-
this._openItemIds.update((ids) => ids.filter((openId) => id == openId));
54+
this._openItemIds.update((ids) => ids.filter((openId) => id !== openId));
5555
return;
5656
} else if (this.type === 'single') {
5757
this._openItemIds.set([]);

0 commit comments

Comments
 (0)