Skip to content

Commit

Permalink
Rewriting common tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zendranm committed Sep 13, 2024
1 parent 6f3982a commit e4e6dac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 45 deletions.
5 changes: 2 additions & 3 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ Package.onTest(function (api) {
'tests/data/pl-pl/x.i18n.yml',
'tests/data/z.i18n.yml',
]);
api.addFiles(['tests/client.ts'], 'client');
// api.addFiles(['tests/server.ts', 'tests/common.ts'], 'server');
api.addFiles(['tests/server.ts'], 'server');
api.addFiles(['tests/client.ts', 'tests/common.ts'], 'client');
api.addFiles(['tests/server.ts', 'tests/common.ts'], 'server');
});
65 changes: 23 additions & 42 deletions tests/common.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
/// <reference path="meteor_practicalmeteor_sinon.d.ts"/>

import { sinon } from 'meteor/practicalmeteor:sinon';

import { i18n } from '../source/common';

const expect = chai.expect;
import assert from 'assert';

describe('universe-i18n', () => {
it('should support YAML files', async () => {
await i18n.setLocale('fr-FR');
expect(i18n.__('common.name')).to.equal('yml-fr');
assert.equal(i18n.__('common.name'), 'yml-fr');
});

it('should support JSON files', async () => {
await i18n.setLocale('es-ES');
expect(i18n.__('common.name')).to.equal('json-es-es');
assert.equal(i18n.__('common.name'), 'json-es-es');
});

it('should support all kinds of translations files', async () => {
Expand All @@ -28,76 +23,62 @@ describe('universe-i18n', () => {
['pl-PL', 'yml-pl-pl'],
['tr', 'yml-tr'],
];

for (const [locale, name] of cases) {
await i18n.setLocale(locale);
expect(i18n.__('common.name')).to.equal(name);
assert.equal(i18n.__('common.name'), name);
}
});

it('should be able to set locale', async () => {
await i18n.setLocale('de-DE');
expect(i18n.getLocale()).to.equal('de-DE');
expect(i18n.setLocale('pl-PL')).to.be.ok;
expect(i18n.getLocale()).to.equal('pl-PL');
assert.equal(i18n.getLocale(), 'de-DE');
assert.ok(i18n.setLocale('pl-PL'));
assert.equal(i18n.getLocale(), 'pl-PL');
});

it('should be able to set/get translations', async () => {
await i18n.setLocale('en-US');
expect(i18n.addTranslation('en-US', 'common', 'yes', 'Yes')).to.be.ok;
expect(i18n.__('common.yes')).to.equal('Yes');

expect(i18n.addTranslation('en-US', 'common.no', 'No')).to.be.ok;
expect(i18n.__('common.no')).to.equal('No');

expect(i18n.addTranslation('en-US', 'common.ok', 'Ok')).to.be.ok;
expect(i18n.__('common.ok')).to.equal('Ok');

assert.ok(i18n.addTranslation('en-US', 'common', 'yes', 'Yes'));
assert.equal(i18n.__('common.yes'), 'Yes');
assert.ok(i18n.addTranslation('en-US', 'common.no', 'No'));
assert.equal(i18n.__('common.no'), 'No');
assert.ok(i18n.addTranslation('en-US', 'common.ok', 'Ok'));
assert.equal(i18n.__('common.ok'), 'Ok');
const params = {
common: {
hello: 'Hello {$name}',
},
};
expect(i18n.addTranslations('en-US', params)).to.be.ok;
expect(i18n.getTranslation('common', 'hello', { name: 'World' })).to.equal(
assert.ok(i18n.addTranslations('en-US', params));
assert.equal(
i18n.getTranslation('common', 'hello', { name: 'World' }),
'Hello World',
);

expect(
assert.ok(
i18n.addTranslation(
'en-US',
'common',
'firstAndThird',
'First: {$0}, Third: {$2}',
),
).to.be.ok;
expect(i18n.__('common', 'firstAndThird', ['a', 'b', 'c'])).to.equal(
);
assert.equal(
i18n.__('common', 'firstAndThird', ['a', 'b', 'c']),
'First: a, Third: c',
);
});

it('should be able to listen on locale change', async () => {
const callback = sinon.spy();

expect(i18n.onceChangeLocale(callback)).to.equal(undefined);

await i18n.setLocale('pl-PL');

expect(callback).to.have.been.calledOnce;
expect(callback).to.have.been.calledWith('pl-PL');
});

it('should be able to set options', async () => {
await i18n.setLocale('en-US');
i18n.setOptions({ open: '{{', close: '}}' });

const params = {
common: {
hello: 'Hello {{name}}',
},
};
expect(i18n.addTranslations('en-US', params)).to.be.ok;
expect(i18n.getTranslation('common', 'hello', { name: 'World' })).to.equal(
assert.ok(i18n.addTranslations('en-US', params));
assert.equal(
i18n.getTranslation('common', 'hello', { name: 'World' }),
'Hello World',
);
i18n.setOptions({ open: '{$', close: '}' });
Expand Down

0 comments on commit e4e6dac

Please sign in to comment.