|
| 1 | +import { announceSettingsSaveStatus } from '../../../src/admin/settings/announce-settings-save-status'; |
| 2 | + |
| 3 | +describe( 'announceSettingsSaveStatus', () => { |
| 4 | + let a11y; |
| 5 | + let readyStateSpy; |
| 6 | + |
| 7 | + beforeEach( () => { |
| 8 | + jest.useFakeTimers(); |
| 9 | + document.body.innerHTML = ''; |
| 10 | + a11y = { speak: jest.fn() }; |
| 11 | + readyStateSpy = jest.spyOn( document, 'readyState', 'get' ).mockReturnValue( 'interactive' ); |
| 12 | + } ); |
| 13 | + |
| 14 | + afterEach( () => { |
| 15 | + readyStateSpy.mockRestore(); |
| 16 | + jest.useRealTimers(); |
| 17 | + } ); |
| 18 | + |
| 19 | + test( 'announces a successful save politely after the page finishes loading', () => { |
| 20 | + document.body.innerHTML = ` |
| 21 | + <div id="setting-error-settings_updated" class="notice notice-success settings-error"> |
| 22 | + <p><strong>Settings saved.</strong></p> |
| 23 | + </div> |
| 24 | + `; |
| 25 | + |
| 26 | + announceSettingsSaveStatus( a11y ); |
| 27 | + |
| 28 | + window.dispatchEvent( new Event( 'load' ) ); |
| 29 | + expect( a11y.speak ).not.toHaveBeenCalled(); |
| 30 | + |
| 31 | + jest.advanceTimersByTime( 1000 ); |
| 32 | + expect( a11y.speak ).toHaveBeenCalledWith( 'Settings saved.', 'polite' ); |
| 33 | + } ); |
| 34 | + |
| 35 | + test( 'announces a save error assertively', () => { |
| 36 | + document.body.innerHTML = ` |
| 37 | + <div id="setting-error-settings_updated" class="notice notice-error settings-error"> |
| 38 | + <p><strong>Settings save failed.</strong></p> |
| 39 | + </div> |
| 40 | + `; |
| 41 | + |
| 42 | + announceSettingsSaveStatus( a11y ); |
| 43 | + |
| 44 | + window.dispatchEvent( new Event( 'load' ) ); |
| 45 | + jest.advanceTimersByTime( 1000 ); |
| 46 | + expect( a11y.speak ).toHaveBeenCalledWith( 'Settings save failed.', 'assertive' ); |
| 47 | + } ); |
| 48 | + |
| 49 | + test( 'announces when the page has already finished loading', () => { |
| 50 | + readyStateSpy.mockReturnValue( 'complete' ); |
| 51 | + document.body.innerHTML = ` |
| 52 | + <div id="setting-error-settings_updated" class="notice notice-success settings-error"> |
| 53 | + <p><strong>Settings saved.</strong></p> |
| 54 | + </div> |
| 55 | + `; |
| 56 | + |
| 57 | + announceSettingsSaveStatus( a11y ); |
| 58 | + |
| 59 | + jest.advanceTimersByTime( 1000 ); |
| 60 | + expect( a11y.speak ).toHaveBeenCalledWith( 'Settings saved.', 'polite' ); |
| 61 | + } ); |
| 62 | + |
| 63 | + test( 'does not announce when the settings notice is missing', () => { |
| 64 | + announceSettingsSaveStatus( a11y ); |
| 65 | + |
| 66 | + window.dispatchEvent( new Event( 'load' ) ); |
| 67 | + jest.runAllTimers(); |
| 68 | + expect( a11y.speak ).not.toHaveBeenCalled(); |
| 69 | + } ); |
| 70 | + |
| 71 | + test( 'does not announce an empty settings notice', () => { |
| 72 | + document.body.innerHTML = '<div id="setting-error-settings_updated"></div>'; |
| 73 | + |
| 74 | + announceSettingsSaveStatus( a11y ); |
| 75 | + |
| 76 | + window.dispatchEvent( new Event( 'load' ) ); |
| 77 | + jest.runAllTimers(); |
| 78 | + expect( a11y.speak ).not.toHaveBeenCalled(); |
| 79 | + } ); |
| 80 | + |
| 81 | + test( 'does not announce when the accessibility utility is unavailable', () => { |
| 82 | + document.body.innerHTML = ` |
| 83 | + <div id="setting-error-settings_updated"> |
| 84 | + <p><strong>Settings saved.</strong></p> |
| 85 | + </div> |
| 86 | + `; |
| 87 | + |
| 88 | + announceSettingsSaveStatus(); |
| 89 | + |
| 90 | + window.dispatchEvent( new Event( 'load' ) ); |
| 91 | + jest.runAllTimers(); |
| 92 | + expect( a11y.speak ).not.toHaveBeenCalled(); |
| 93 | + } ); |
| 94 | +} ); |
0 commit comments