-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathdropdown.spec.ts
56 lines (49 loc) · 2.22 KB
/
dropdown.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
describe('Dropdown demo test', () => {
it('navigate to demo section', () => {
cy.visit('http://localhost:3000/dropdown-demo-nav-link');
});
// mouse interactions
it('opens/closes dropdown menu when clicked', () => {
cy.get('[data-cy="toggle"]').click();
cy.get('[data-cy="toggle"]').should('have.class', 'pf-m-expanded');
cy.get('[data-cy="toggle"]').click();
cy.get('[data-cy="toggle"]').should('not.have.class', 'pf-m-expanded');
});
it('closes dropdown when clicked outside', () => {
cy.get('[data-cy="toggle"]').click();
cy.get('main').click('center');
cy.get('[data-cy="toggle"]').should('not.have.class', 'pf-m-expanded');
});
it('closes dropdown when dropdown item clicked', () => {
cy.get('[data-cy="toggle"]').click();
cy.get('[data-cy=dropdown-item] > .pf-v6-c-menu__item').click({ force: true });
cy.get('[data-cy="toggle"]').should('not.have.class', 'pf-m-expanded');
});
// keyboard interactions
it('closes dropdown on pressing esc, focus stays on the toggle', () => {
cy.get('[data-cy="toggle"]').click();
cy.get('[data-cy="toggle"]').trigger('keydown', { key: 'Escape' });
cy.get('[data-cy="toggle"]').should('be.focused');
cy.get('[data-cy="toggle"]').should('not.have.class', 'pf-m-expanded');
});
it('closes dropdown on pressing tab, focus stays on the toggle', () => {
cy.get('[data-cy="toggle"]').click();
cy.get('[data-cy="toggle"]').trigger('keydown', { key: 'Tab' });
cy.get('[data-cy="toggle"]').should('be.focused');
cy.get('[data-cy="toggle"]').should('not.have.class', 'pf-m-expanded');
});
/*
pressing enter or space key on a button calls a click event internally
so testing for a button click should be sufficient
*/
it('Does not autofocus first item on click by default', () => {
cy.get('[data-cy="toggle"]').click();
cy.get('#first-item').should('not.be.focused');
cy.get('[data-cy="toggle"]').trigger('keydown', { key: 'Escape' });
});
it('Autofocuses first item on click when shouldFocusFirstItemOnOpen is true', () => {
cy.get('[data-cy="autofocus-toggle"]').click();
cy.get('#first-item').should('be.focused');
cy.get('[data-cy="toggle"]').trigger('keydown', { key: 'Escape' });
});
});