test-utils: TranslationApi type updates

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-15 18:01:01 +02:00
parent aa53609ecd
commit 3079cc8930
2 changed files with 45 additions and 28 deletions
@@ -50,17 +50,21 @@ describe('MockTranslationApi', () => {
shallow: 'Foo {{ bar }}',
multiple: 'Foo {{ bar }} {{ baz }}',
deep: 'Foo {{ bar.baz }}',
});
} as const);
// @ts-expect-error
expect(snapshot.t('shallow')).toBe('Foo {{ bar }}');
expect(snapshot.t('shallow', { bar: 'Bar' })).toBe('Foo Bar');
// @ts-expect-error
expect(snapshot.t('multiple')).toBe('Foo {{ bar }} {{ baz }}');
// @ts-expect-error
expect(snapshot.t('multiple', { bar: 'Bar' })).toBe('Foo Bar {{ baz }}');
expect(snapshot.t('multiple', { bar: 'Bar', baz: 'Baz' })).toBe(
'Foo Bar Baz',
);
// @ts-expect-error
expect(snapshot.t('deep')).toBe('Foo {{ bar.baz }}');
expect(snapshot.t('deep', { bar: { baz: 'Baz' } })).toBe('Foo Baz');
});
@@ -69,7 +73,7 @@ describe('MockTranslationApi', () => {
it('should not escape by default', () => {
const snapshot = snapshotWithMessages({
foo: 'Foo {{ foo }}',
});
} as const);
expect(snapshot.t('foo', { foo: '<div>' })).toBe('Foo <div>');
expect(
@@ -85,7 +89,7 @@ describe('MockTranslationApi', () => {
foo: 'Foo $t(bar) $t(baz)',
bar: 'Nested',
baz: 'Baz {{ qux }}',
});
} as const);
expect(snapshot.t('foo', { qux: 'Deep' })).toBe('Foo Nested Baz Deep');
});
@@ -100,24 +104,36 @@ describe('MockTranslationApi', () => {
relativeSecondsShort:
'= {{ x, relativeTime(range: second; style: short) }}',
list: '= {{ x, list }}',
});
} as const);
expect(snapshot.t('plain', { x: 5 })).toBe('= 5');
expect(snapshot.t('plain', { x: '5' })).toBe('= 5');
expect(snapshot.t('number', { x: 5 })).toBe('= 5');
expect(snapshot.t('number', { x: 5, minimumFractionDigits: 1 })).toBe(
'= 5.0',
);
expect(
snapshot.t('number', {
x: 5,
formatParams: { x: { minimumFractionDigits: 1 } },
}),
).toBe('= 5.0');
expect(snapshot.t('numberFixed', { x: 5 })).toBe('= 5.00');
expect(snapshot.t('numberFixed', { x: 5, minimumFractionDigits: 3 })).toBe(
'= 5.000',
);
expect(
snapshot.t('numberFixed', {
x: 5,
formatParams: { x: { minimumFractionDigits: 3 } },
}),
).toBe('= 5.000');
expect(snapshot.t('relativeTime', { x: 3 })).toBe('= in 3 days');
expect(snapshot.t('relativeTime', { x: -3 })).toBe('= 3 days ago');
expect(snapshot.t('relativeTime', { x: 15, range: 'weeks' })).toBe(
'= in 15 weeks',
);
expect(
snapshot.t('relativeTime', { x: 15, range: 'weeks', style: 'short' }),
snapshot.t('relativeTime', {
x: 15,
formatParams: { x: { range: 'weeks' } },
}),
).toBe('= in 15 weeks');
expect(
snapshot.t('relativeTime', {
x: 15,
formatParams: { x: { range: 'weeks', style: 'short' } },
}),
).toBe('= in 15 wk.');
expect(snapshot.t('relativeSeconds', { x: 1 })).toBe('= in 1 second');
expect(snapshot.t('relativeSeconds', { x: 2 })).toBe('= in 2 seconds');
@@ -138,14 +154,13 @@ describe('MockTranslationApi', () => {
derp_other: 'derps',
derpWithCount_one: '{{ count }} derp',
derpWithCount_other: '{{ count }} derps',
});
} as const);
// TODO(Rugvip): Support plural keys
expect(snapshot.t('derp' as any, { count: 1 })).toBe('derp');
expect(snapshot.t('derp' as any, { count: 2 })).toBe('derps');
expect(snapshot.t('derp' as any, { count: 0 })).toBe('derps');
expect(snapshot.t('derpWithCount' as any, { count: 1 })).toBe('1 derp');
expect(snapshot.t('derpWithCount' as any, { count: 2 })).toBe('2 derps');
expect(snapshot.t('derpWithCount' as any, { count: 0 })).toBe('0 derps');
expect(snapshot.t('derp', { count: 1 })).toBe('derp');
expect(snapshot.t('derp', { count: 2 })).toBe('derps');
expect(snapshot.t('derp', { count: 0 })).toBe('derps');
expect(snapshot.t('derpWithCount', { count: 1 })).toBe('1 derp');
expect(snapshot.t('derpWithCount', { count: 2 })).toBe('2 derps');
expect(snapshot.t('derpWithCount', { count: 0 })).toBe('0 derps');
});
});
@@ -16,6 +16,7 @@
import {
TranslationApi,
TranslationFunction,
TranslationRef,
TranslationSnapshot,
} from '@backstage/core-plugin-api/alpha';
@@ -66,8 +67,6 @@ export class MockTranslationApi implements TranslationApi {
): TranslationSnapshot<TMessages> {
const internalRef = toInternalTranslationRef(translationRef);
const t = this.#i18n.getFixedT(null, internalRef.id);
if (!this.#registeredRefs.has(internalRef.id)) {
this.#registeredRefs.add(internalRef.id);
this.#i18n.addResourceBundle(
@@ -79,11 +78,14 @@ export class MockTranslationApi implements TranslationApi {
);
}
const t = this.#i18n.getFixedT(
null,
internalRef.id,
) as TranslationFunction<TMessages>;
return {
ready: true,
t: (key, options) => {
return t(key as string, { ...options });
},
t,
};
}