blob: 5c1f0baf3ee8d55c9a18461a2eaaf717f65d8d4b [file] [log] [blame]
// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Message manager test suite.
*/
import {lib} from '../index.js';
/**
* Run processI18nAttribute through tests.
* Note: This relies on no message ids actually matching.
*/
it('processI18nAttribute', () => {
const mm = new lib.MessageManager([]);
const node = globalThis.document.createElement('span');
node.setAttribute('id', 'spic-and-span');
// Test missing i18n.
mm.processI18nAttribute(node);
// Test empty i18n.
node.setAttribute('i18n', '');
mm.processI18nAttribute(node);
// Test empty i18n object.
node.setAttribute('i18n', '{}');
mm.processI18nAttribute(node);
// Test direct message name.
node.setAttribute('i18n', '{"tattr": "FOO"}');
mm.processI18nAttribute(node);
assert.equal(node.getAttribute('tattr'), 'FOO');
// Test $id handling.
node.setAttribute('i18n', '{"tattr": "$id"}');
mm.processI18nAttribute(node);
assert.equal(node.getAttribute('tattr'), 'SPIC_AND_SPAN_TATTR');
// Test _ handling for the textContent.
node.setAttribute('i18n', '{"_": "THIS_IS_A_TEST"}');
mm.processI18nAttribute(node);
assert.equal(node.textContent, 'THIS_IS_A_TEST');
// Test =attr handling.
node.setAttribute('i18n', '{"tattr": "CONTENT", "tind": "=tattr"}');
mm.processI18nAttribute(node);
assert.equal(node.getAttribute('tattr'), 'CONTENT');
assert.equal(node.getAttribute('tind'), 'CONTENT');
});
/**
* Check addMessages behavior.
*/
it('add-messages', /** @suppress {visibility} */ () => {
const mm = new lib.MessageManager([]);
mm.addMessages({
'SOME_ID': {
'description': 'This is goodness',
'message': 'text',
},
'ID_REPLACE': {
'message': 'foo $1 bar $2',
},
});
assert.equal('text', mm.messages_['SOME_ID']);
assert.equal('foo $1 bar $2', mm.messages_['ID_REPLACE']);
});
/**
* Verify get with registered messages work.
*/
it('get-local', () => {
const mm = new lib.MessageManager([]);
mm.addMessages({
'SOME_ID': {
'description': 'This is goodness',
'message': 'text',
},
'ID_REPLACE': {
'message': 'foo $1 bar $2',
},
});
assert.equal('text', mm.get('SOME_ID'));
assert.equal('text', mm.get('SOME_ID', []));
assert.equal('text', mm.get('SOME_ID', [], 'not used'));
assert.equal('foo', mm.get('UNKNOWN', [], 'foo'));
assert.equal('foo X bar Y', mm.get('ID_REPLACE', ['X', 'Y']));
});
/**
* Check loading works.
*/
it('load', async function() {
const mm = new lib.MessageManager([]);
// Skip if nassh is unavailable.
try {
await mm.loadMessages('../../nassh/_locales/en/messages.json');
} catch (e) {
assert.equal(e?.cause?.status, 404);
this.skip();
return;
}
assert.equal(mm.get('OK_BUTTON_LABEL'), 'OK');
});
/**
* Check finding & loading works.
*/
it('find-and-load', async function() {
const mm = new lib.MessageManager(['en']);
// Skip if nassh is unavailable.
try {
await mm.findAndLoadMessages('../../nassh/_locales/$1//messages.json');
} catch (e) {
assert.equal(e?.cause?.status, 404);
this.skip();
return;
}
assert.equal(mm.get('OK_BUTTON_LABEL'), 'OK');
});