Ghost/test/unit/shared/i18n.test.js
Hannah Wolfe f08a55c21f
Renamed tests to .test.js & updated commands
refs: https://github.com/TryGhost/Team/issues/856
refs: https://github.com/TryGhost/Team/issues/756

- The .test.js extension is better than _spec.js as it's more obvious that it's an extension
- It also meaans we can use the --extension parameter in mocha, which should result in a better default behaviour for `yarn test`
- It also highlights that some of our tests were named incorrectly and were not (and still will not be) run (see https://github.com/TryGhost/Team/issues/856)
- Note: even with this change, `yarn test` is throwing errors, I believe because of this issue https://github.com/TryGhost/Team/issues/756
2021-07-06 20:45:01 +01:00

91 lines
2.8 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const I18n = require('../../../core/shared/i18n').I18n;
const logging = {
warn: sinon.stub(),
error: sinon.stub()
};
describe('I18n Class Behaviour', function () {
it('defaults to en', function () {
const i18n = new I18n({logging});
i18n.locale().should.eql('en');
});
it('can have a different locale set', function () {
const i18n = new I18n({locale: 'fr', logging});
i18n.locale().should.eql('fr');
});
describe('file loading behaviour', function () {
it('will fallback to en file correctly without changing locale', function () {
const i18n = new I18n({locale: 'fr', logging});
let fileSpy = sinon.spy(i18n, '_readTranslationsFile');
i18n.locale().should.eql('fr');
i18n.init();
i18n.locale().should.eql('fr');
fileSpy.calledTwice.should.be.true();
fileSpy.secondCall.args[0].should.eql('en');
});
});
describe('translation key dot notation (default behaviour)', function () {
const fakeStrings = {
test: {string: {path: 'I am correct'}}
};
let i18n;
beforeEach(function initBasicI18n() {
i18n = new I18n({logging});
sinon.stub(i18n, '_loadStrings').returns(fakeStrings);
i18n.init();
});
it('correctly loads strings', function () {
i18n._strings.should.eql(fakeStrings);
});
it('correctly uses dot notation', function () {
i18n.t('test.string.path').should.eql('I am correct');
});
it('uses key fallback correctly', function () {
i18n.t('unknown.string').should.eql('An error occurred');
});
it('errors for invalid strings', function () {
should(function () {
i18n.t('unknown string');
}).throw('i18n.t() called with an invalid key: unknown string');
});
});
describe('translation key fulltext notation (theme behaviour)', function () {
const fakeStrings = {'Full text': 'I am correct'};
let i18n;
beforeEach(function initFulltextI18n() {
i18n = new I18n({stringMode: 'fulltext', logging});
sinon.stub(i18n, '_loadStrings').returns(fakeStrings);
i18n.init();
});
it('correctly loads strings', function () {
i18n._strings.should.eql(fakeStrings);
});
it('correctly uses fulltext with bracket notation', function () {
i18n.t('Full text').should.eql('I am correct');
});
it('uses key fallback correctly', function () {
i18n.t('unknown string').should.eql('unknown string');
});
});
});