| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const { Config } = require( 'mmv.bootstrap' ); |
| 19 |
const { createLocalStorage, getFakeLocalStorage } = require( './mmv.testhelpers.js' ); |
| 20 |
const config0 = mw.config; |
| 21 |
const storage = mw.storage; |
| 22 |
const user = mw.user; |
| 23 |
const saveOption = mw.Api.prototype.saveOption; |
| 24 |
|
| 25 |
( function () { |
| 26 |
QUnit.module( 'mmv.Config', QUnit.newMwEnvironment( { |
| 27 |
afterEach: function () { |
| 28 |
mw.config = config0; |
| 29 |
mw.storage = storage; |
| 30 |
mw.user = user; |
| 31 |
mw.Api.prototype.saveOption = saveOption; |
| 32 |
} |
| 33 |
} ) ); |
| 34 |
|
| 35 |
QUnit.test( 'constructor', ( assert ) => { |
| 36 |
const config = new Config(); |
| 37 |
assert.true( config instanceof Config ); |
| 38 |
} ); |
| 39 |
|
| 40 |
QUnit.test( 'isMediaViewerEnabledOnClick', function ( assert ) { |
| 41 |
mw.storage = createLocalStorage( { getItem: this.sandbox.stub() } ); |
| 42 |
mw.config = { get: this.sandbox.stub() }; |
| 43 |
mw.user = { isNamed: this.sandbox.stub() }; |
| 44 |
|
| 45 |
mw.user.isNamed.returns( true ); |
| 46 |
mw.config.get.withArgs( 'wgMediaViewer' ).returns( true ); |
| 47 |
mw.config.get.withArgs( 'wgMediaViewerOnClick' ).returns( true ); |
| 48 |
assert.strictEqual( Config.isMediaViewerEnabledOnClick(), true, 'Returns true for logged-in with standard settings' ); |
| 49 |
|
| 50 |
mw.user.isNamed.returns( true ); |
| 51 |
mw.config.get.withArgs( 'wgMediaViewer' ).returns( false ); |
| 52 |
mw.config.get.withArgs( 'wgMediaViewerOnClick' ).returns( true ); |
| 53 |
assert.strictEqual( Config.isMediaViewerEnabledOnClick(), false, 'Returns false if opted out via user JS flag' ); |
| 54 |
|
| 55 |
mw.user.isNamed.returns( true ); |
| 56 |
mw.config.get.withArgs( 'wgMediaViewer' ).returns( true ); |
| 57 |
mw.config.get.withArgs( 'wgMediaViewerOnClick' ).returns( false ); |
| 58 |
assert.strictEqual( Config.isMediaViewerEnabledOnClick(), false, 'Returns false if opted out via preferences' ); |
| 59 |
|
| 60 |
mw.user.isNamed.returns( false ); |
| 61 |
mw.config.get.withArgs( 'wgMediaViewer' ).returns( false ); |
| 62 |
mw.config.get.withArgs( 'wgMediaViewerOnClick' ).returns( true ); |
| 63 |
assert.strictEqual( Config.isMediaViewerEnabledOnClick(), false, 'Returns false if anon user opted out via user JS flag' ); |
| 64 |
|
| 65 |
mw.user.isNamed.returns( false ); |
| 66 |
mw.config.get.withArgs( 'wgMediaViewer' ).returns( true ); |
| 67 |
mw.config.get.withArgs( 'wgMediaViewerOnClick' ).returns( false ); |
| 68 |
assert.strictEqual( Config.isMediaViewerEnabledOnClick(), false, 'Returns false if anon user opted out in some weird way' ); |
| 69 |
|
| 70 |
mw.user.isNamed.returns( false ); |
| 71 |
mw.config.get.withArgs( 'wgMediaViewer' ).returns( true ); |
| 72 |
mw.config.get.withArgs( 'wgMediaViewerOnClick' ).returns( true ); |
| 73 |
mw.storage.store.getItem.withArgs( 'wgMediaViewerOnClick' ).returns( null ); |
| 74 |
assert.strictEqual( Config.isMediaViewerEnabledOnClick(), true, 'Returns true for anon with standard settings' ); |
| 75 |
|
| 76 |
mw.user.isNamed.returns( false ); |
| 77 |
mw.config.get.withArgs( 'wgMediaViewer' ).returns( true ); |
| 78 |
mw.config.get.withArgs( 'wgMediaViewerOnClick' ).returns( true ); |
| 79 |
mw.storage.store.getItem.withArgs( 'wgMediaViewerOnClick' ).returns( '0' ); |
| 80 |
assert.strictEqual( Config.isMediaViewerEnabledOnClick(), false, 'Returns true for anon opted out via localSettings' ); |
| 81 |
} ); |
| 82 |
|
| 83 |
QUnit.test( 'setMediaViewerEnabledOnClick sense check', function ( assert ) { |
| 84 |
mw.storage = createLocalStorage( { |
| 85 |
getItem: this.sandbox.stub(), |
| 86 |
setItem: this.sandbox.stub(), |
| 87 |
removeItem: this.sandbox.stub() |
| 88 |
} ); |
| 89 |
mw.user = { isNamed: this.sandbox.stub() }; |
| 90 |
mw.config = new mw.Map(); |
| 91 |
mw.config.set( 'wgMediaViewerEnabledByDefault', false ); |
| 92 |
mw.Api.prototype.saveOption = this.sandbox.stub().returns( $.Deferred().resolve() ); |
| 93 |
|
| 94 |
mw.user.isNamed.returns( true ); |
| 95 |
mw.Api.prototype.saveOption.returns( $.Deferred().resolve() ); |
| 96 |
Config.setMediaViewerEnabledOnClick( false ); |
| 97 |
assert.true( mw.Api.prototype.saveOption.called, 'For logged-in users, pref change is via API' ); |
| 98 |
|
| 99 |
mw.user.isNamed.returns( false ); |
| 100 |
Config.setMediaViewerEnabledOnClick( false ); |
| 101 |
assert.true( mw.storage.store.setItem.called, 'For anons, opt-out is set in localStorage' ); |
| 102 |
|
| 103 |
mw.user.isNamed.returns( false ); |
| 104 |
Config.setMediaViewerEnabledOnClick( true ); |
| 105 |
assert.true( mw.storage.store.removeItem.called, 'For anons, opt-in means clearing localStorage' ); |
| 106 |
} ); |
| 107 |
|
| 108 |
QUnit.test( 'shouldShowStatusInfo', function ( assert ) { |
| 109 |
mw.config = new mw.Map(); |
| 110 |
mw.storage = getFakeLocalStorage(); |
| 111 |
mw.user = { isNamed: this.sandbox.stub() }; |
| 112 |
mw.Api.prototype.saveOption = this.sandbox.stub().returns( $.Deferred().resolve() ); |
| 113 |
|
| 114 |
mw.config.set( { |
| 115 |
wgMediaViewer: true, |
| 116 |
wgMediaViewerOnClick: true, |
| 117 |
wgMediaViewerEnabledByDefault: true |
| 118 |
} ); |
| 119 |
mw.user.isNamed.returns( true ); |
| 120 |
|
| 121 |
assert.strictEqual( Config.shouldShowStatusInfo(), false, 'Status info is not shown by default' ); |
| 122 |
Config.setMediaViewerEnabledOnClick( false ); |
| 123 |
assert.strictEqual( Config.shouldShowStatusInfo(), true, 'Status info is shown after MMV is disabled the first time' ); |
| 124 |
Config.setMediaViewerEnabledOnClick( true ); |
| 125 |
assert.strictEqual( Config.shouldShowStatusInfo(), false, 'Status info is not shown when MMV is enabled' ); |
| 126 |
Config.setMediaViewerEnabledOnClick( false ); |
| 127 |
assert.strictEqual( Config.shouldShowStatusInfo(), true, 'Status info is shown after MMV is disabled the first time #2' ); |
| 128 |
Config.disableStatusInfo(); |
| 129 |
assert.strictEqual( Config.shouldShowStatusInfo(), false, 'Status info is not shown when already displayed once' ); |
| 130 |
Config.setMediaViewerEnabledOnClick( true ); |
| 131 |
assert.strictEqual( Config.shouldShowStatusInfo(), false, 'Further status changes have no effect' ); |
| 132 |
Config.setMediaViewerEnabledOnClick( false ); |
| 133 |
assert.strictEqual( Config.shouldShowStatusInfo(), false, 'Further status changes have no effect #2' ); |
| 134 |
|
| 135 |
|
| 136 |
mw.storage = getFakeLocalStorage(); |
| 137 |
mw.user.isNamed.returns( false ); |
| 138 |
assert.strictEqual( Config.shouldShowStatusInfo(), false, 'Status info is not shown by default for logged-in users' ); |
| 139 |
Config.setMediaViewerEnabledOnClick( false ); |
| 140 |
assert.strictEqual( Config.shouldShowStatusInfo(), true, 'Status info is shown after MMV is disabled the first time for logged-in users' ); |
| 141 |
|
| 142 |
|
| 143 |
mw.storage = getFakeLocalStorage(); |
| 144 |
mw.config.set( 'wgMediaViewerEnabledByDefault', false ); |
| 145 |
assert.strictEqual( Config.shouldShowStatusInfo(), false, 'Status info is not shown by default #2' ); |
| 146 |
Config.setMediaViewerEnabledOnClick( true ); |
| 147 |
assert.strictEqual( Config.shouldShowStatusInfo(), false, 'Status info is not shown when MMV is enabled #2' ); |
| 148 |
Config.setMediaViewerEnabledOnClick( false ); |
| 149 |
assert.strictEqual( Config.shouldShowStatusInfo(), true, 'Status info is shown after MMV is disabled the first time #2' ); |
| 150 |
} ); |
| 151 |
}() ); |