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