mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
Adding type inference and makeT helper function
This commit is contained in:
@@ -1,11 +1,31 @@
|
||||
import {t} from 'app/client/lib/localization';
|
||||
import {makeT, t} from 'app/client/lib/localization';
|
||||
import {assert} from 'chai';
|
||||
import i18next, {i18n} from 'i18next';
|
||||
import {dom, observable} from "grainjs";
|
||||
import {Disposable, dom, DomContents, observable} from "grainjs";
|
||||
import {popGlobals, pushGlobals, G} from 'grainjs/dist/cjs/lib/browserGlobals';
|
||||
import {JSDOM} from 'jsdom';
|
||||
|
||||
describe('localization', function() {
|
||||
let instance: i18n;
|
||||
before(() => {
|
||||
instance = i18next.createInstance();
|
||||
instance.init({
|
||||
lng: 'en',
|
||||
resources: {
|
||||
en: {
|
||||
translation: {
|
||||
'Text': 'TranslatedText',
|
||||
'Argument': 'Translated {{arg1}} {{arg2}}{{end}}',
|
||||
'Argument_variant': 'Variant {{arg1}} {{arg2}}{{end}}',
|
||||
'Parent': {
|
||||
'Child': 'Translated child {{arg}}',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
beforeEach(function() {
|
||||
// These grainjs browserGlobals are needed for using dom() in tests.
|
||||
const jsdomDoc = new JSDOM("<!doctype html><html><body></body></html>");
|
||||
@@ -16,34 +36,23 @@ describe('localization', function() {
|
||||
popGlobals();
|
||||
});
|
||||
|
||||
let instance: i18n;
|
||||
before(() => {
|
||||
instance = i18next.createInstance();
|
||||
instance.init({
|
||||
lng: 'en',
|
||||
resources: {
|
||||
en: {
|
||||
translation: {
|
||||
'Text': 'TranslatedText',
|
||||
'Argument': 'Translated {{arg1}} {{arg2}}.',
|
||||
'Argument_variant': 'Variant {{arg1}} {{arg2}}.',
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('supports basic operation for strings', function() {
|
||||
assert.equal(t('Argument', {arg1: '1', arg2: '2'}, instance), 'Translated 1 2.');
|
||||
assert.equal(t('Argument', {arg1: '1', arg2: '2', context: 'variant'}, instance), 'Variant 1 2.');
|
||||
assert.equal(t('Argument', {arg1: '1', arg2: '2', end: '.'}, instance), 'Translated 1 2.');
|
||||
assert.equal(t('Argument', {arg1: '1', arg2: '2', end: '.', context: 'variant'}, instance), 'Variant 1 2.');
|
||||
assert.equal(t('Text', null, instance), 'TranslatedText');
|
||||
});
|
||||
|
||||
it('supports dom content interpolation', function() {
|
||||
class Component extends Disposable {
|
||||
public buildDom() {
|
||||
return dom('span', '.');
|
||||
}
|
||||
}
|
||||
const obs = observable("Second");
|
||||
const result = t('Argument', {
|
||||
arg1: dom('span', 'First'),
|
||||
arg2: dom.domComputed(obs, (value) => dom('span', value))
|
||||
arg2: dom.domComputed(obs, (value) => dom('span', value)),
|
||||
end: dom.create(Component)
|
||||
}, instance) as any;
|
||||
assert.isTrue(Array.isArray(result));
|
||||
assert.equal(result.length, 5);
|
||||
@@ -59,8 +68,11 @@ describe('localization', function() {
|
||||
assert.isTrue(result[3][0] instanceof G.Node);
|
||||
assert.isTrue(result[3][1] instanceof G.Node);
|
||||
assert.isTrue(typeof result[3][2] === 'function');
|
||||
// As last we have "."
|
||||
assert.equal(result[4], '.');
|
||||
// As last we have "." as grainjs component.
|
||||
assert.isTrue(Array.isArray(result[4]));
|
||||
assert.isTrue(result[4][0] instanceof G.Node);
|
||||
assert.isTrue(result[4][1] instanceof G.Node);
|
||||
assert.isTrue(typeof result[4][2] === 'function');
|
||||
|
||||
// Make sure that computed works.
|
||||
const span = dom('span', result);
|
||||
@@ -72,10 +84,41 @@ describe('localization', function() {
|
||||
const variantSpan = dom('span', t('Argument', {
|
||||
arg1: dom('span', 'First'),
|
||||
arg2: dom.domComputed(obs, (value) => dom('span', value)),
|
||||
end: dom.create(Component),
|
||||
context: 'variant'
|
||||
}, instance));
|
||||
assert.equal(variantSpan.textContent, "Variant First Third.");
|
||||
obs.set("Fourth");
|
||||
assert.equal(variantSpan.textContent, "Variant First Fourth.");
|
||||
});
|
||||
|
||||
it('supports scoping through makeT', function() {
|
||||
const scoped = makeT('Parent');
|
||||
assert.equal(scoped('Child', { arg : 'Arg'}, instance), 'Translated child Arg');
|
||||
});
|
||||
|
||||
it('infers result from parameters', function() {
|
||||
class Component extends Disposable {
|
||||
public buildDom() {
|
||||
return dom('span', '.');
|
||||
}
|
||||
}
|
||||
// Here we only test that this "compiles" without errors and types are correct.
|
||||
let typeString: string = ''; void typeString;
|
||||
typeString = t('Argument', {arg1: 'argument 1', arg2: 'argument 2'}, instance);
|
||||
typeString = t('Argument', {arg1: 1, arg2: true}, instance);
|
||||
typeString = t('Argument', undefined, instance);
|
||||
const scoped = makeT('Parent');
|
||||
typeString = scoped('Child', {arg: 'argument 1'}, instance);
|
||||
typeString = scoped('Child', {arg: 1}, instance);
|
||||
typeString = scoped('Child', undefined, instance);
|
||||
|
||||
let domContent: DomContents = null; void domContent;
|
||||
|
||||
domContent = t('Argument', {arg1: 'argument 1', arg2: dom('span')}, instance);
|
||||
domContent = t('Argument', {arg1: 1, arg2: dom.domComputed(observable('test'))}, instance);
|
||||
domContent = t('Argument', undefined, instance);
|
||||
domContent = scoped('Child', {arg: dom.create(Component)}, instance);
|
||||
domContent = scoped('Child', {arg: dom.maybe(observable(true), () => dom('span'))}, instance);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user