| 1 |
'use strict'; |
| 2 |
const reportIfNightModeWasDisabledOnPage = require( |
| 3 |
'../../../resources/skins.minerva.scripts/reportIfNightModeWasDisabledOnPage.js' |
| 4 |
); |
| 5 |
const nightModeDisabledDoc = document.createElement( 'html' ); |
| 6 |
nightModeDisabledDoc.setAttribute( 'class', 'skin-night-mode-page-disabled' ); |
| 7 |
|
| 8 |
const userOptionsEnabled = new Map(); |
| 9 |
userOptionsEnabled.set( 'minerva-theme', 'night' ); |
| 10 |
const userOptionsDisabled = new Map(); |
| 11 |
userOptionsDisabled.set( 'minerva-theme', 'day' ); |
| 12 |
const userOptionsAutomatic = new Map(); |
| 13 |
userOptionsAutomatic.set( 'minerva-theme', 'os' ); |
| 14 |
|
| 15 |
const notify = () => {}; |
| 16 |
const msg = () => {}; |
| 17 |
|
| 18 |
describe( 'reportIfNightModeWasDisabledOnPage.js', () => { |
| 19 |
it( 'returns false if no skin-night-mode-page-disabled class is on the document element', () => { |
| 20 |
global.mw = {}; |
| 21 |
expect( reportIfNightModeWasDisabledOnPage( document.createElement( 'html' ) ) ).toBe( false ); |
| 22 |
} ); |
| 23 |
|
| 24 |
it( 'shows notification for logged in users with night mode preferred, reading from options', () => { |
| 25 |
global.mw = { |
| 26 |
msg, |
| 27 |
notify, |
| 28 |
user: { |
| 29 |
isNamed: () => true, |
| 30 |
options: userOptionsEnabled |
| 31 |
} |
| 32 |
}; |
| 33 |
expect( reportIfNightModeWasDisabledOnPage( nightModeDisabledDoc ) ).toBe( true ); |
| 34 |
} ); |
| 35 |
|
| 36 |
it( 'shows no notification for logged in users with night mode disabled, reading from options', () => { |
| 37 |
global.mw = { |
| 38 |
msg, |
| 39 |
notify, |
| 40 |
user: { |
| 41 |
isNamed: () => true, |
| 42 |
options: userOptionsDisabled |
| 43 |
} |
| 44 |
}; |
| 45 |
expect( reportIfNightModeWasDisabledOnPage( nightModeDisabledDoc ) ).toBe( false ); |
| 46 |
} ); |
| 47 |
|
| 48 |
it( 'respects automatic mode when displaying a notification for logged in users', () => { |
| 49 |
global.mw = { |
| 50 |
msg, |
| 51 |
notify, |
| 52 |
user: { |
| 53 |
isNamed: () => true, |
| 54 |
options: userOptionsAutomatic |
| 55 |
} |
| 56 |
}; |
| 57 |
window.matchMedia = () => ( { |
| 58 |
matches: true |
| 59 |
} ); |
| 60 |
expect( reportIfNightModeWasDisabledOnPage( nightModeDisabledDoc ) ).toBe( true ); |
| 61 |
window.matchMedia = () => ( { |
| 62 |
matches: false |
| 63 |
} ); |
| 64 |
expect( reportIfNightModeWasDisabledOnPage( nightModeDisabledDoc ) ).toBe( false ); |
| 65 |
} ); |
| 66 |
|
| 67 |
it( 'reads from cookie for anons', () => { |
| 68 |
global.mw = { |
| 69 |
msg, |
| 70 |
notify, |
| 71 |
cookie: { |
| 72 |
get: () => 'skin-theme-clientpref-night' |
| 73 |
}, |
| 74 |
user: { |
| 75 |
isNamed: () => false, |
| 76 |
options: userOptionsDisabled |
| 77 |
} |
| 78 |
}; |
| 79 |
expect( reportIfNightModeWasDisabledOnPage( nightModeDisabledDoc ) ).toBe( true ); |
| 80 |
} ); |
| 81 |
|
| 82 |
it( 'reads from cookie for anons, can handle unset cookie', () => { |
| 83 |
global.mw = { |
| 84 |
msg, |
| 85 |
notify, |
| 86 |
cookie: { |
| 87 |
get: () => null |
| 88 |
}, |
| 89 |
user: { |
| 90 |
isNamed: () => false, |
| 91 |
options: userOptionsDisabled |
| 92 |
} |
| 93 |
}; |
| 94 |
expect( reportIfNightModeWasDisabledOnPage( nightModeDisabledDoc ) ).toBe( false ); |
| 95 |
} ); |
| 96 |
} ); |