|
| 1 | +import { announceSettingsSaveStatus } from '../../../src/admin/settings/announce-settings-save-status'; |
| 2 | + |
| 3 | +describe( 'announceSettingsSaveStatus', () => { |
| 4 | + let a11y; |
| 5 | + |
| 6 | + beforeEach( () => { |
| 7 | + document.body.innerHTML = ''; |
| 8 | + a11y = { speak: jest.fn() }; |
| 9 | + } ); |
| 10 | + |
| 11 | + test( 'announces a successful save politely', () => { |
| 12 | + document.body.innerHTML = ` |
| 13 | + <div id="setting-error-settings_updated" class="notice notice-success settings-error"> |
| 14 | + <p><strong>Settings saved.</strong></p> |
| 15 | + </div> |
| 16 | + `; |
| 17 | + |
| 18 | + announceSettingsSaveStatus( a11y ); |
| 19 | + |
| 20 | + expect( a11y.speak ).toHaveBeenCalledWith( 'Settings saved.', 'polite' ); |
| 21 | + } ); |
| 22 | + |
| 23 | + test( 'announces a save error assertively', () => { |
| 24 | + document.body.innerHTML = ` |
| 25 | + <div id="setting-error-settings_updated" class="notice notice-error settings-error"> |
| 26 | + <p><strong>Settings save failed.</strong></p> |
| 27 | + </div> |
| 28 | + `; |
| 29 | + |
| 30 | + announceSettingsSaveStatus( a11y ); |
| 31 | + |
| 32 | + expect( a11y.speak ).toHaveBeenCalledWith( 'Settings save failed.', 'assertive' ); |
| 33 | + } ); |
| 34 | + |
| 35 | + test( 'does not announce when the settings notice is missing', () => { |
| 36 | + announceSettingsSaveStatus( a11y ); |
| 37 | + |
| 38 | + expect( a11y.speak ).not.toHaveBeenCalled(); |
| 39 | + } ); |
| 40 | + |
| 41 | + test( 'does not announce an empty settings notice', () => { |
| 42 | + document.body.innerHTML = '<div id="setting-error-settings_updated"></div>'; |
| 43 | + |
| 44 | + announceSettingsSaveStatus( a11y ); |
| 45 | + |
| 46 | + expect( a11y.speak ).not.toHaveBeenCalled(); |
| 47 | + } ); |
| 48 | + |
| 49 | + test( 'does not announce when the accessibility utility is unavailable', () => { |
| 50 | + document.body.innerHTML = ` |
| 51 | + <div id="setting-error-settings_updated"> |
| 52 | + <p><strong>Settings saved.</strong></p> |
| 53 | + </div> |
| 54 | + `; |
| 55 | + |
| 56 | + announceSettingsSaveStatus(); |
| 57 | + |
| 58 | + expect( a11y.speak ).not.toHaveBeenCalled(); |
| 59 | + } ); |
| 60 | +} ); |
0 commit comments