From 15c65ae5b97a30e797ea7db8cd687530054d3c77 Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Tue, 16 Mar 2021 12:30:51 +0100 Subject: [PATCH] fix: create i18next clone to fix #1063 (#1073) * create i18next clone to fix #1063 * added dedicated test for i18next clone --- src/createClient/node.test.ts | 19 ++++++++++++++++++- src/createClient/node.ts | 5 +++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/createClient/node.test.ts b/src/createClient/node.test.ts index 57df1fdb..c045214a 100644 --- a/src/createClient/node.test.ts +++ b/src/createClient/node.test.ts @@ -7,8 +7,8 @@ const config = { } as any describe('createClientNode', () => { + const client = createClientNode(config) it('returns a node client', () => { - const client = createClientNode(config) expect(typeof client.initPromise.then).toEqual('function') expect(typeof client.i18n.addResource).toEqual('function') expect(typeof (client.i18n as any).translator).toEqual('object') @@ -16,5 +16,22 @@ describe('createClientNode', () => { (client.i18n.options as any).defaultLocale ).toEqual(config.defaultLocale) expect((client.i18n.options as any).locales).toEqual(config.locales) + expect((client.i18n.options as any).isClone).not.toEqual(true) + }) + + describe('createClientNode a second time should return a clone of i18next', () => { + it('returns a node client', () => { + const secondClient = createClientNode(config) + expect(typeof secondClient.initPromise.then).toEqual('function') + expect(typeof secondClient.i18n.addResource).toEqual('function') + expect(typeof (secondClient.i18n as any).translator).toEqual('object') + expect( + (secondClient.i18n.options as any).defaultLocale + ).toEqual(config.defaultLocale) + expect((secondClient.i18n.options as any).locales).toEqual(config.locales) + expect((secondClient.i18n.options as any).isClone).toEqual(true) + expect(secondClient).not.toEqual(client) + expect((secondClient as any).store).toEqual((client as any).store) + }) }) }) diff --git a/src/createClient/node.ts b/src/createClient/node.ts index 52ea77db..933f3a82 100644 --- a/src/createClient/node.ts +++ b/src/createClient/node.ts @@ -8,6 +8,11 @@ let instance: I18n export default (config: InternalConfig): CreateClientReturn => { if (!instance) { instance = i18n.createInstance(config) + } else { + instance = instance.cloneInstance({ + ...config, + initImmediate: false, + }) } let initPromise: InitPromise