|
| 1 | +describe('useTagGroup', () => { |
| 2 | + const colors = ['Black', 'Red', 'Green', 'Blue', 'Orange'] |
| 3 | + |
| 4 | + beforeEach(() => { |
| 5 | + cy.visit('/useTagGroup') |
| 6 | + |
| 7 | + // Ensure the listbox exists |
| 8 | + cy.findByRole('listbox', {name: /colors example/i}).should('exist') |
| 9 | + |
| 10 | + // Ensure it has 5 color tags |
| 11 | + cy.findAllByRole('option').should('have.length', 5) |
| 12 | + }) |
| 13 | + |
| 14 | + it('clicks a tag and navigates with circular arrow keys', () => { |
| 15 | + // Click first tag ("Black") |
| 16 | + cy.findByRole('option', {name: /Black/i}).click().should('have.focus') |
| 17 | + |
| 18 | + // Arrow Right navigation through all tags |
| 19 | + for (let index = 0; index < colors.length; index++) { |
| 20 | + const nextIndex = (index + 1) % colors.length |
| 21 | + cy.focused().trigger('keydown', {key: 'ArrowRight'}) |
| 22 | + cy.findByRole('option', {name: colors[nextIndex]}).should('have.focus') |
| 23 | + } |
| 24 | + |
| 25 | + // Arrow Left navigation through all tags (circular) |
| 26 | + for (let index = colors.length - 1; index >= 0; index--) { |
| 27 | + const prevIndex = (index + colors.length) % colors.length |
| 28 | + cy.focused().trigger('keydown', {key: 'ArrowLeft'}) |
| 29 | + cy.findByRole('option', {name: colors[prevIndex]}).should('have.focus') |
| 30 | + } |
| 31 | + |
| 32 | + // Circular on the left. |
| 33 | + cy.focused().trigger('keydown', {key: 'ArrowLeft'}) |
| 34 | + cy.findByRole('option', {name: colors[colors.length - 1]}).should( |
| 35 | + 'have.focus', |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + it('deletes a tag using Delete and Backspace', () => { |
| 40 | + // Focus "Red" |
| 41 | + cy.findByRole('option', {name: /Red/i}).click() |
| 42 | + |
| 43 | + // Delete key |
| 44 | + cy.focused().trigger('keydown', {key: 'Delete'}) |
| 45 | + cy.findAllByRole('option').should('have.length', 4) |
| 46 | + |
| 47 | + // Next tag should be "Green" |
| 48 | + cy.focused().should('contain.text', 'Green') |
| 49 | + |
| 50 | + // Backspace key removes "Green" |
| 51 | + cy.focused().trigger('keydown', {key: 'Backspace'}) |
| 52 | + cy.findAllByRole('option').should('have.length', 3) |
| 53 | + |
| 54 | + // Focus should now be on "Blue" |
| 55 | + cy.focused().should('contain.text', 'Blue') |
| 56 | + }) |
| 57 | + |
| 58 | + it('removes a tag via remove button', () => { |
| 59 | + // Remove "Blue" via its remove button |
| 60 | + cy.findByRole('option', {name: /Blue/i}).within(() => { |
| 61 | + cy.findByRole('button', {name: /remove/i}).click() |
| 62 | + }) |
| 63 | + |
| 64 | + // Verify 4 tags remain |
| 65 | + cy.findAllByRole('option').should('have.length', 4) |
| 66 | + |
| 67 | + // Orange tag should have focus. |
| 68 | + cy.findByRole('option', {name: /Orange/i}).should('have.focus') |
| 69 | + }) |
| 70 | + |
| 71 | + it('adds a tag from the list', () => { |
| 72 | + // Focus "Red" |
| 73 | + cy.findByRole('option', {name: /Red/i}).click() |
| 74 | + |
| 75 | + // Clicks the Lime option from the add tags list. |
| 76 | + cy.findByRole('button', {name: /Lime/i}).click() |
| 77 | + |
| 78 | + // Verify 6 tags are visible |
| 79 | + cy.findAllByRole('option').should('have.length', 6) |
| 80 | + |
| 81 | + cy.findByRole('option', {name: /Lime/i}).should('be.visible') |
| 82 | + // Including the new option |
| 83 | + }) |
| 84 | +}) |
0 commit comments