Skip to content

Commit

Permalink
feat: pass changed files list to i18next getter, #118
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Aug 16, 2022
1 parent ed603da commit 0253f0a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
16 changes: 14 additions & 2 deletions __tests__/client-hmr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,25 @@ describe('client-hmr', () => {
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en');
});

it('should trigger reload when lng-country combination file changed', async () => {
it('should pass changed filed to the i18next getter', () => {
i18nMock.options = { backend: {}, ns: ['name-space'] };
i18nMock.language = 'en';
const getter = jest.fn().mockImplementation(() => i18nMock);
const changedFiles = ['en/name-space'];

applyClientHMR(getter);
whenHotTriggeredWith(changedFiles);

expect(getter).toHaveBeenCalledWith({ changedFiles });
});

it('should trigger reload when lng-country combination file changed', () => {
i18nMock.options = { backend: {}, ns: ['name-space'] };
i18nMock.language = 'en-US';

applyClientHMR(i18nMock);

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

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
Expand Down
16 changes: 14 additions & 2 deletions __tests__/server-hmr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ describe('server-hmr', () => {
accept: jest.fn(),
},
};

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

it('should reload resources on updated lang, ns', () => {
const update = { lang: 'en', ns: 'name-space' };
applyServerHMR(() => i18nMock);

whenNativeHMRTriggeredWith([`${update.lang}/${update.ns}`]);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
Expand All @@ -415,5 +415,17 @@ describe('server-hmr', () => {
expect.any(Function)
);
});

it('should pass changed filed to the i18next getter', () => {
const update = { lang: 'en', ns: 'name-space' };

const getter = jest.fn().mockImplementation(() => i18nMock);
const changedFiles = [`${update.lang}/${update.ns}`];
applyServerHMR(getter);

whenNativeHMRTriggeredWith(changedFiles);

expect(getter).toHaveBeenCalledWith({ changedFiles });
});
});
});
3 changes: 2 additions & 1 deletion lib/client-hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ module.exports = function applyClientHMR(i18nOrGetter) {

module.hot.accept('./trigger.js', () => {
const { changedFiles } = require('./trigger.js');
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
const i18nInstance =
typeof i18nOrGetter === 'function' ? i18nOrGetter({ changedFiles }) : i18nOrGetter;

const list = extractList(changedFiles, i18nInstance);

Expand Down
3 changes: 2 additions & 1 deletion lib/server-hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = function applyServerHMR(i18nOrGetter) {
const logOnce = createLoggerOnce(log);

function reloadServerTranslation({ changedFiles }) {
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
const i18nInstance =
typeof i18nOrGetter === 'function' ? i18nOrGetter({ changedFiles }) : i18nOrGetter;

const list = extractList(changedFiles, i18nInstance);

Expand Down

0 comments on commit 0253f0a

Please sign in to comment.