-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: π Enable testing localized code (#44)
* fix: π Enable testing localized code Currently it's not possible to test code using `$Dictionary` (e.g. components with calls to `this.localize()`), as the dictionary is generated during build by `gulp-tasks`. The new function `_generateDictionary` generates this dictionary during init. * refactor: π‘ Move code to its own file Move `generateDictionary()` to its own file to make it testable. * test: π Test `generateDictionary()` * docs: βοΈ Add new config value to README * fix: π Don't fail on undefined languages * test: π Improve tests Add a check of language values to app test.
- Loading branch information
Showing
7 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/plugin-testing-integration/src/__tests__/localizationSpec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { generateDictionary } from '../localization'; | ||
import mockGlobby from 'globby'; | ||
import { requireFromProject as mockRequireFromProject } from '../helpers'; | ||
|
||
const mockDictFiles = { | ||
'thisComponentCS.json': { | ||
keyOne: 'foo', | ||
keyTwo: 'bar' | ||
}, | ||
'that_componentCS.JSON': { | ||
keyA: 'baz', | ||
keyB: 'quux' | ||
} | ||
}; | ||
|
||
jest.mock('globby', () => ({ | ||
sync: jest.fn(() => Object.keys(mockDictFiles)) | ||
})); | ||
|
||
jest.mock('../helpers', () => ({ | ||
requireFromProject: jest.fn(filename => { | ||
return mockDictFiles[filename]; | ||
}) | ||
})); | ||
|
||
const expectedDictionary = { | ||
thisComponent: { | ||
keyOne: expect.anything(), | ||
keyTwo: expect.anything() | ||
}, | ||
that_component: { | ||
keyA: expect.anything(), | ||
keyB: expect.anything() | ||
} | ||
}; | ||
|
||
describe('Localization', () => { | ||
describe('generateDictionary', () => { | ||
it('can generate a dictionary from dictionary JSONs, given glob patterns', () => { | ||
const languages = { | ||
cs: ['./some/path/*CS.JSON'], | ||
en: ['./some/path/*EN.JSON'] | ||
}; | ||
const locale = 'cs'; | ||
const dict = generateDictionary(languages, locale); | ||
|
||
expect(mockGlobby.sync).toHaveBeenCalledWith(['./some/path/*CS.JSON']); | ||
expect(mockRequireFromProject).toHaveBeenCalledTimes(2); | ||
expect(mockRequireFromProject).toHaveBeenNthCalledWith( | ||
1, | ||
'thisComponentCS.json' | ||
); | ||
expect(mockRequireFromProject).toHaveBeenNthCalledWith( | ||
2, | ||
'that_componentCS.JSON' | ||
); | ||
expect(dict).toMatchObject(expectedDictionary); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import MessageFormat from 'messageformat'; | ||
import globby from 'globby'; | ||
import path from 'path'; | ||
import { requireFromProject } from './helpers'; | ||
|
||
function generateDictionary(languages, locale = 'en') { | ||
if (!languages) { | ||
return {}; | ||
} | ||
const mf = new MessageFormat(locale); | ||
const dictionaries = {}; | ||
const langFileGlobs = languages[locale]; | ||
globby.sync(langFileGlobs).forEach(file => { | ||
try { | ||
const filename = path | ||
.basename(file) | ||
.replace(locale.toUpperCase() + path.extname(file), ''); | ||
|
||
const dictJson = requireFromProject(file); | ||
const dict = {}; | ||
Object.keys(dictJson).forEach(key => { | ||
const message = dictJson[key]; | ||
dict[key] = mf.compile(message); | ||
}); | ||
|
||
dictionaries[filename] = dict; | ||
} catch (e) { | ||
console.error( | ||
`Tried to load dictionary JSON at path "${file}", but recieved following error.` | ||
); | ||
console.error(e); | ||
} | ||
}); | ||
return dictionaries; | ||
} | ||
|
||
export { generateDictionary }; |