| 1 |
const { |
| 2 |
isNightModeGadgetEnabled, |
| 3 |
disableNightModeForGadget, |
| 4 |
alterDisableLink, |
| 5 |
alterExclusionMessage |
| 6 |
} = require( '../../../resources/skins.vector.js/disableNightModeIfGadget.js' ); |
| 7 |
|
| 8 |
describe( 'isNightModeGadgetEnabled', () => { |
| 9 |
beforeEach( () => { |
| 10 |
|
| 11 |
mw.loader.getState = () => null; |
| 12 |
} ); |
| 13 |
|
| 14 |
it( 'should return false if no gadgets are installed', () => { |
| 15 |
expect( isNightModeGadgetEnabled() ).toBeFalsy(); |
| 16 |
} ); |
| 17 |
|
| 18 |
it( 'should return false if the gadgets are installed but not enabled', () => { |
| 19 |
|
| 20 |
mw.loader.getState = () => 'registered'; |
| 21 |
|
| 22 |
expect( isNightModeGadgetEnabled() ).toBeFalsy(); |
| 23 |
} ); |
| 24 |
|
| 25 |
it( 'should return true if the gadgets are enabled', () => { |
| 26 |
|
| 27 |
mw.loader.getState = () => 'ready'; |
| 28 |
|
| 29 |
expect( isNightModeGadgetEnabled() ).toBeTruthy(); |
| 30 |
} ); |
| 31 |
} ); |
| 32 |
|
| 33 |
describe( 'disableNightModeForGadget', () => { |
| 34 |
beforeEach( () => { |
| 35 |
document.documentElement.classList.remove( 'skin-theme-clientpref--excluded' ); |
| 36 |
document.documentElement.classList.remove( 'skin-theme-clientpref-night' ); |
| 37 |
document.documentElement.classList.remove( 'skin-theme-clientpref-os' ); |
| 38 |
} ); |
| 39 |
|
| 40 |
it( 'should disable night mode', () => { |
| 41 |
document.documentElement.classList.add( 'skin-theme-clientpref-night' ); |
| 42 |
|
| 43 |
disableNightModeForGadget(); |
| 44 |
|
| 45 |
expect( document.documentElement.classList.contains( 'skin-theme-clientpref-night' ) ).toBeFalsy(); |
| 46 |
} ); |
| 47 |
|
| 48 |
it( 'should disable automatic mode', () => { |
| 49 |
document.documentElement.classList.add( 'skin-theme-clientpref-os' ); |
| 50 |
|
| 51 |
disableNightModeForGadget(); |
| 52 |
|
| 53 |
expect( document.documentElement.classList.contains( 'skin-theme-clientpref-os' ) ).toBeFalsy(); |
| 54 |
} ); |
| 55 |
|
| 56 |
it( 'should add the excluded class', () => { |
| 57 |
disableNightModeForGadget(); |
| 58 |
|
| 59 |
expect( document.documentElement.classList.contains( 'skin-theme-clientpref--excluded' ) ).toBeTruthy(); |
| 60 |
} ); |
| 61 |
} ); |
| 62 |
|
| 63 |
describe( 'alterDisableLink', () => { |
| 64 |
afterEach( () => { |
| 65 |
jest.restoreAllMocks(); |
| 66 |
} ); |
| 67 |
|
| 68 |
it( 'should exit early if the gadget names are empty', () => { |
| 69 |
jest.spyOn( mw, 'msg' ).mockImplementation( () => '' ); |
| 70 |
|
| 71 |
const p = document.createElement( 'p' ); |
| 72 |
const a = document.createElement( 'a' ); |
| 73 |
p.appendChild( a ); |
| 74 |
|
| 75 |
a.href = 'https://test.com/'; |
| 76 |
a.title = 'test'; |
| 77 |
|
| 78 |
alterDisableLink( p ); |
| 79 |
|
| 80 |
expect( a.href ).toBe( 'https://test.com/' ); |
| 81 |
expect( a.title ).toBe( 'test' ); |
| 82 |
} ); |
| 83 |
|
| 84 |
it( 'should leave the surrounding element unaltered', () => { |
| 85 |
const p = document.createElement( 'p' ); |
| 86 |
const a = document.createElement( 'a' ); |
| 87 |
p.appendChild( a ); |
| 88 |
|
| 89 |
p.textContent = 'test'; |
| 90 |
|
| 91 |
alterDisableLink( p ); |
| 92 |
|
| 93 |
expect( p.textContent ).toBe( 'test' ); |
| 94 |
} ); |
| 95 |
|
| 96 |
it( 'should strip the title and href attributes', () => { |
| 97 |
const p = document.createElement( 'p' ); |
| 98 |
const a = document.createElement( 'a' ); |
| 99 |
p.appendChild( a ); |
| 100 |
|
| 101 |
a.href = 'test.com'; |
| 102 |
a.title = 'test'; |
| 103 |
|
| 104 |
alterDisableLink( p ); |
| 105 |
|
| 106 |
expect( a.href ).toBe( '' ); |
| 107 |
expect( a.title ).toBe( '' ); |
| 108 |
} ); |
| 109 |
|
| 110 |
it( 'should make the link display inline', () => { |
| 111 |
const p = document.createElement( 'p' ); |
| 112 |
const a = document.createElement( 'a' ); |
| 113 |
p.appendChild( a ); |
| 114 |
|
| 115 |
alterDisableLink( p ); |
| 116 |
|
| 117 |
expect( a.style.display ).toBe( 'inline' ); |
| 118 |
} ); |
| 119 |
|
| 120 |
|
| 121 |
} ); |
| 122 |
|
| 123 |
describe( 'alterExclusionMessage', () => { |
| 124 |
beforeEach( () => { |
| 125 |
jest.spyOn( mw.loader, 'using' ).mockImplementation( () => ( { |
| 126 |
then: ( fn ) => fn() |
| 127 |
} ) ); |
| 128 |
|
| 129 |
|
| 130 |
jest.spyOn( mw, 'message' ).mockImplementation( () => ( { |
| 131 |
parseDom: () => ( { |
| 132 |
appendTo: () => {} |
| 133 |
} ) |
| 134 |
} ) ); |
| 135 |
} ); |
| 136 |
|
| 137 |
afterEach( () => { |
| 138 |
jest.restoreAllMocks(); |
| 139 |
} ); |
| 140 |
|
| 141 |
it( 'should remove the existing text from the notice', () => { |
| 142 |
const div = document.createElement( 'div' ); |
| 143 |
const p = document.createElement( 'p' ); |
| 144 |
document.documentElement.appendChild( div ); |
| 145 |
div.appendChild( p ); |
| 146 |
|
| 147 |
div.id = 'skin-client-prefs-skin-theme'; |
| 148 |
p.className = 'exclusion-notice'; |
| 149 |
p.textContent = 'test'; |
| 150 |
|
| 151 |
alterExclusionMessage(); |
| 152 |
|
| 153 |
expect( p.textContent ).toBe( '' ); |
| 154 |
} ); |
| 155 |
|
| 156 |
it( 'should not target other client prefs', () => { |
| 157 |
const div = document.createElement( 'div' ); |
| 158 |
const p = document.createElement( 'p' ); |
| 159 |
document.documentElement.appendChild( div ); |
| 160 |
div.appendChild( p ); |
| 161 |
|
| 162 |
div.id = 'skin-client-prefs-vector-feature-custom-font-size'; |
| 163 |
p.className = 'exclusion-notice'; |
| 164 |
p.textContent = 'test'; |
| 165 |
|
| 166 |
alterExclusionMessage(); |
| 167 |
|
| 168 |
expect( p.textContent ).toBe( 'test' ); |
| 169 |
} ); |
| 170 |
} ); |