| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.init.sa.Platform', { |
| 8 |
beforeEach: function () { |
| 9 |
|
| 10 |
|
| 11 |
this.originalPlatform = ve.init.platform; |
| 12 |
this.purgeKeys = function () { |
| 13 |
let i = localStorage.length; |
| 14 |
|
| 15 |
while ( i-- ) { |
| 16 |
const key = localStorage.key( i ); |
| 17 |
if ( key.indexOf( 've-test-' ) === 0 ) { |
| 18 |
localStorage.removeItem( key ); |
| 19 |
} |
| 20 |
} |
| 21 |
}; |
| 22 |
this.purgeKeys(); |
| 23 |
}, |
| 24 |
afterEach: function () { |
| 25 |
ve.init.platform = this.originalPlatform; |
| 26 |
this.purgeKeys(); |
| 27 |
} |
| 28 |
} ); |
| 29 |
|
| 30 |
QUnit.test( 'getUserConfig', ( assert ) => { |
| 31 |
const platform = new ve.init.sa.Platform(); |
| 32 |
|
| 33 |
assert.strictEqual( platform.getUserConfig( 'test-1' ), null, 'unknown key' ); |
| 34 |
assert.propEqual( |
| 35 |
platform.getUserConfig( [ 'test-1', 'test-2' ] ), |
| 36 |
{ 'test-1': null, 'test-2': null }, |
| 37 |
'multiple unknown keys' |
| 38 |
); |
| 39 |
|
| 40 |
platform.setUserConfig( { 'test-1': 'a', 'test-2': 'b' } ); |
| 41 |
|
| 42 |
assert.strictEqual( platform.getUserConfig( 'test-1' ), 'a', 'get value' ); |
| 43 |
assert.propEqual( |
| 44 |
platform.getUserConfig( [ 'test-1', 'test-2' ] ), |
| 45 |
{ 'test-1': 'a', 'test-2': 'b' }, |
| 46 |
'get multiple values' |
| 47 |
); |
| 48 |
} ); |
| 49 |
|
| 50 |
QUnit.test( 'setUserConfig', ( assert ) => { |
| 51 |
const platform = new ve.init.sa.Platform(); |
| 52 |
|
| 53 |
assert.strictEqual( platform.setUserConfig( 'test-1', 'one' ), true, 'set key' ); |
| 54 |
assert.strictEqual( platform.getUserConfig( 'test-1' ), 'one', 'value persists' ); |
| 55 |
|
| 56 |
assert.strictEqual( |
| 57 |
platform.setUserConfig( { 'test-1': 'one more', 'test-2': 'two' } ), |
| 58 |
true, |
| 59 |
'set multiple keys' |
| 60 |
); |
| 61 |
assert.propEqual( |
| 62 |
platform.getUserConfig( [ 'test-1', 'test-2' ] ), |
| 63 |
{ 'test-1': 'one more', 'test-2': 'two' }, |
| 64 |
'multiple values persist' |
| 65 |
); |
| 66 |
} ); |
| 67 |
|
| 68 |
QUnit.test( 'messages', ( assert ) => { |
| 69 |
const platform = new ve.init.sa.Platform(); |
| 70 |
|
| 71 |
return platform.getInitializedPromise().then( () => { |
| 72 |
assert.true( |
| 73 |
/^<?platformtest-foo>?$/.test( platform.getMessage( 'platformtest-foo' ) ), |
| 74 |
'return plain key as fallback, possibly wrapped in brackets' |
| 75 |
); |
| 76 |
|
| 77 |
platform.addMessages( { |
| 78 |
'platformtest-foo': 'Foo & Bar <quux action="followed">by</quux>!', |
| 79 |
'platformtest-lorem': 'Lorem <&> Ipsum: $1' |
| 80 |
} ); |
| 81 |
|
| 82 |
assert.strictEqual( |
| 83 |
platform.getMessage( 'platformtest-foo' ), |
| 84 |
'Foo & Bar <quux action="followed">by</quux>!', |
| 85 |
'return plain message' |
| 86 |
); |
| 87 |
|
| 88 |
assert.strictEqual( |
| 89 |
platform.getMessage( 'platformtest-lorem', 10 ), |
| 90 |
'Lorem <&> Ipsum: 10', |
| 91 |
'return plain message with $# replacements' |
| 92 |
); |
| 93 |
|
| 94 |
assert.true( |
| 95 |
/^<?platformtest-quux>?$/.test( platform.getMessage( 'platformtest-quux' ) ), |
| 96 |
'return plain key as fallback, possibly wrapped in brackets (after set up)' |
| 97 |
); |
| 98 |
} ); |
| 99 |
} ); |
| 100 |
|
| 101 |
QUnit.test( 'parsedMessage', ( assert ) => { |
| 102 |
const platform = new ve.init.sa.Platform(); |
| 103 |
|
| 104 |
return platform.getInitializedPromise().then( () => { |
| 105 |
assert.true( |
| 106 |
/^(<)?platformtest-quux(>)?$/.test( platform.getParsedMessage( 'platformtest-quux' ) ), |
| 107 |
'any brackets in fallbacks are HTML-escaped' |
| 108 |
); |
| 109 |
|
| 110 |
platform.addMessages( { |
| 111 |
'platformtest-foo': 'Foo & Bar <quux action="followed">by</quux>!', |
| 112 |
'platformtest-lorem': 'Lorem <&> Ipsum: $1' |
| 113 |
} ); |
| 114 |
|
| 115 |
platform.addParsedMessages( { |
| 116 |
'platformtest-foo': 'Foo <quux><html></quux>' |
| 117 |
} ); |
| 118 |
|
| 119 |
assert.strictEqual( |
| 120 |
platform.getParsedMessage( 'platformtest-foo' ), |
| 121 |
'Foo <quux><html></quux>', |
| 122 |
'prefer value from parsedMessage store' |
| 123 |
); |
| 124 |
|
| 125 |
assert.strictEqual( |
| 126 |
platform.getParsedMessage( 'platformtest-lorem', 10 ), |
| 127 |
'Lorem <&> Ipsum: $1', |
| 128 |
'fall back to html-escaped version of plain message, no $# replacements' |
| 129 |
); |
| 130 |
} ); |
| 131 |
} ); |