Skip to content

Commit

Permalink
Fix tests & add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Jul 19, 2022
1 parent c2c920f commit 8e35aa5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
26 changes: 24 additions & 2 deletions __tests__/client-hmr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ describe('client-hmr', () => {
jest.restoreAllMocks();
});

it('should warn regarding missing backend options', () => {
it('should warn regarding missing backend options once', () => {
jest.spyOn(global.console, 'warn');
i18nMock.options = { ns: ['name-space'] };

applyClientHMR(i18nMock);
whenHotTriggeredWith(['en/name-space']);
applyClientHMR(i18nMock);

expect(global.console.warn).toHaveBeenCalledTimes(1);
expect(global.console.warn).toHaveBeenCalledWith(
expect.stringContaining('i18next-backend not found'),
expect.stringContaining('i18next-http-backend not found'),
expect.any(String),
expect.any(String)
);
Expand Down Expand Up @@ -96,6 +102,22 @@ describe('client-hmr', () => {
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en');
});

it('should trigger reload when i18n given as a getter function', async () => {
i18nMock.options = { backend: {}, ns: ['name-space'] };
i18nMock.language = 'en';

applyClientHMR(() => i18nMock);

await whenHotTriggeredWith(['en/name-space']);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en'],
['name-space'],
expect.any(Function)
);
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en');
});

it('should trigger reload when lng-country combination file changed', async () => {
i18nMock.options = { backend: {}, ns: ['name-space'] };
i18nMock.language = 'en-US';
Expand Down
46 changes: 46 additions & 0 deletions __tests__/server-hmr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ describe('server-hmr', () => {
);
});

it('should notify that server HMR started HMR mode once', async () => {
jest.spyOn(global.console, 'log');

applyServerHMR(i18nMock); // second call

expect(global.console.log).toHaveBeenCalledTimes(1);
expect(global.console.log).toHaveBeenCalledWith(
expect.stringContaining('Server HMR has started')
);
expect(global.console.log).not.toHaveBeenCalledWith(expect.stringContaining('callback mode'));
});

it('should reload resources on updated lang, ns', () => {
const update = { lang: 'en', ns: 'name-space' };
whenNativeHMRTriggeredWith([`${update.lang}/${update.ns}`]);
Expand Down Expand Up @@ -154,6 +166,17 @@ describe('server-hmr', () => {
expect(plugin.addListener).toHaveBeenCalled();
});

it('should notify that server HMR started as callback mode once', async () => {
jest.spyOn(global.console, 'log');

applyServerHMR(i18nMock); // second call

expect(global.console.log).toHaveBeenCalledTimes(1);
expect(global.console.log).toHaveBeenCalledWith(
expect.stringContaining('Server HMR has started - callback mode')
);
});

it('should reload resources on updated lang, ns', () => {
const update = { lang: 'en', ns: 'name-space' };
plugin.callbacks[0]({ changedFiles: [`${update.lang}/${update.ns}`] });
Expand Down Expand Up @@ -230,4 +253,27 @@ describe('server-hmr', () => {
);
});
});

describe('i18n as a getter', () => {
beforeEach(() => {
global.mockModule = {
hot: {
accept: jest.fn(),
},
};

applyServerHMR(() => i18nMock);
});

it('should reload resources on updated lang, ns', () => {
const update = { lang: 'en', ns: 'name-space' };
whenNativeHMRTriggeredWith([`${update.lang}/${update.ns}`]);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
[update.lang],
[update.ns],
expect.any(Function)
);
});
});
});
13 changes: 5 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,16 @@ function makeUniqueList(list) {
return [...new Set(list)];
}

function createLoggerOnce(log) {
function createLoggerOnce(logger) {
const msgCount = new Map();
return (msg, type = 'log') => {
if (!msgCount.has(msg)) {
msgCount.set(msg, 0);
}

if (msgCount.get(msg) >= 1) {
const count = msgCount.has(msg) ? msgCount.get(msg) : 0;
if (count > 0) {
return;
}

log(msg, type);
msgCount.set(msg, 1);
logger(msg, type);
msgCount.set(msg, count + 1);
};
}

Expand Down

0 comments on commit 8e35aa5

Please sign in to comment.