|
| 1 | +import React from 'react'; |
| 2 | +import AnnouncementBanner from '~/components/AnnouncementBanner'; |
| 3 | + |
| 4 | +describe('AnnouncementBanner', () => { |
| 5 | + beforeEach(() => { |
| 6 | + cy.mount(<AnnouncementBanner />); |
| 7 | + }); |
| 8 | + |
| 9 | + describe('Initial render', () => { |
| 10 | + it('renders the banner on page load', () => { |
| 11 | + cy.get('[data-testid="announcement-banner"]', { timeout: 1000 }) |
| 12 | + .should('exist') |
| 13 | + .and('be.visible'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('starts invisible then fades in', () => { |
| 17 | + // Re-mount to capture the initial opacity-0 state before the 100ms timeout |
| 18 | + cy.mount(<AnnouncementBanner />); |
| 19 | + |
| 20 | + // Immediately after mount the banner should be in the DOM but opacity-0 |
| 21 | + cy.get('[data-testid="announcement-banner"]') |
| 22 | + .should('exist') |
| 23 | + .and('have.class', 'opacity-0'); |
| 24 | + |
| 25 | + // After the 100ms delay it becomes visible |
| 26 | + cy.get('[data-testid="announcement-banner"]', { timeout: 1000 }).should( |
| 27 | + 'have.class', |
| 28 | + 'opacity-100', |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it('displays the announcement text', () => { |
| 33 | + cy.contains('The JSON Schema Office Hours Now Runs Weekly!').should( |
| 34 | + 'be.visible', |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it('renders a "Join Us!" link with the correct href', () => { |
| 39 | + cy.get('a') |
| 40 | + .contains('Join Us!') |
| 41 | + .should('be.visible') |
| 42 | + .and( |
| 43 | + 'have.attr', |
| 44 | + 'href', |
| 45 | + 'https://github.com/orgs/json-schema-org/discussions/34', |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('renders the dismiss button', () => { |
| 50 | + cy.get('button[aria-label="Dismiss banner"]').should('be.visible'); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('Text slide-in animation', () => { |
| 55 | + it('text starts off-screen then slides into view', () => { |
| 56 | + cy.mount(<AnnouncementBanner />); |
| 57 | + |
| 58 | + // Before 400ms the text span should be translated left and invisible |
| 59 | + cy.get('[data-testid="announcement-text"]') |
| 60 | + .should('have.class', '-translate-x-8') |
| 61 | + .and('have.class', 'opacity-0'); |
| 62 | + |
| 63 | + // After 400ms it should be in its natural position |
| 64 | + cy.get('[data-testid="announcement-text"]', { timeout: 1000 }) |
| 65 | + .should('have.class', 'translate-x-0') |
| 66 | + .and('have.class', 'opacity-100'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('Dismiss behaviour', () => { |
| 71 | + it('hides the banner when the dismiss button is clicked', () => { |
| 72 | + // Wait for banner to be fully visible first |
| 73 | + cy.get('[data-testid="announcement-banner"]', { timeout: 1000 }).should( |
| 74 | + 'have.class', |
| 75 | + 'opacity-100', |
| 76 | + ); |
| 77 | + |
| 78 | + cy.get('button[aria-label="Dismiss banner"]').click(); |
| 79 | + |
| 80 | + // Banner fades out (opacity-0) before being removed |
| 81 | + cy.get('[data-testid="announcement-banner"]').should( |
| 82 | + 'have.class', |
| 83 | + 'opacity-0', |
| 84 | + ); |
| 85 | + |
| 86 | + // After the 500ms transition the component is removed from the DOM |
| 87 | + cy.get('[data-testid="announcement-banner"]', { timeout: 1000 }).should( |
| 88 | + 'not.exist', |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('does not re-appear after being dismissed', () => { |
| 93 | + cy.get('[data-testid="announcement-banner"]', { timeout: 1000 }).should( |
| 94 | + 'have.class', |
| 95 | + 'opacity-100', |
| 96 | + ); |
| 97 | + |
| 98 | + cy.get('button[aria-label="Dismiss banner"]').click(); |
| 99 | + |
| 100 | + cy.get('[data-testid="announcement-banner"]', { timeout: 1000 }).should( |
| 101 | + 'not.exist', |
| 102 | + ); |
| 103 | + |
| 104 | + // Wait an extra moment to confirm it stays gone |
| 105 | + cy.get('[data-testid="announcement-banner"]', { timeout: 1000 }).should( |
| 106 | + 'not.exist', |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('Accessibility', () => { |
| 112 | + it('dismiss button has an accessible aria-label', () => { |
| 113 | + cy.get('button[aria-label="Dismiss banner"]').should('exist'); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('onHeightChange callback', () => { |
| 118 | + it('calls onHeightChange when the banner mounts', () => { |
| 119 | + const onHeightChange = cy.stub().as('onHeightChange'); |
| 120 | + cy.mount(<AnnouncementBanner onHeightChange={onHeightChange} />); |
| 121 | + |
| 122 | + // The callback should be invoked at least once on mount |
| 123 | + cy.get('@onHeightChange').should('have.been.called'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('calls onHeightChange with a numeric height value', () => { |
| 127 | + const onHeightChange = cy.stub().as('onHeightChange'); |
| 128 | + cy.mount(<AnnouncementBanner onHeightChange={onHeightChange} />); |
| 129 | + |
| 130 | + cy.get('@onHeightChange').should( |
| 131 | + 'have.been.calledWithMatch', |
| 132 | + Cypress.sinon.match.number, |
| 133 | + ); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments