From 3079cc8930684a33e59d5f4933edd280fdeb1b84 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 15 Sep 2023 18:01:01 +0200 Subject: [PATCH] test-utils: TranslationApi type updates Signed-off-by: Patrik Oldsberg --- .../TranslationApi/MockTranslationApi.test.ts | 61 ++++++++++++------- .../apis/TranslationApi/MockTranslationApi.ts | 12 ++-- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/packages/test-utils/src/testUtils/apis/TranslationApi/MockTranslationApi.test.ts b/packages/test-utils/src/testUtils/apis/TranslationApi/MockTranslationApi.test.ts index 6b6ef2eb13..45d23d9db2 100644 --- a/packages/test-utils/src/testUtils/apis/TranslationApi/MockTranslationApi.test.ts +++ b/packages/test-utils/src/testUtils/apis/TranslationApi/MockTranslationApi.test.ts @@ -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: '
' })).toBe('Foo
'); 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'); }); }); diff --git a/packages/test-utils/src/testUtils/apis/TranslationApi/MockTranslationApi.ts b/packages/test-utils/src/testUtils/apis/TranslationApi/MockTranslationApi.ts index 0ffea6a1bd..608b634cc4 100644 --- a/packages/test-utils/src/testUtils/apis/TranslationApi/MockTranslationApi.ts +++ b/packages/test-utils/src/testUtils/apis/TranslationApi/MockTranslationApi.ts @@ -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 { 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; + return { ready: true, - t: (key, options) => { - return t(key as string, { ...options }); - }, + t, }; }