-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛 lookup message not caching correctly
It was only caching lookups of fallback locales. If a message was found in the passed locale, it wouldn't be cached.
- Loading branch information
1 parent
f8b0fe8
commit bb8c68f
Showing
5 changed files
with
34 additions
and
20 deletions.
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
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 |
---|---|---|
@@ -1,48 +1,51 @@ | ||
import { lookupMessage, lookupCache } from '../../../src/client/includes/lookup' | ||
import { lookup, lookupCache } from '../../../src/client/includes/lookup' | ||
import { $dictionary, addMessages } from '../../../src/client/stores/dictionary' | ||
|
||
beforeEach(() => { | ||
$dictionary.set({}) | ||
}) | ||
|
||
test('returns null if no locale was passed', () => { | ||
expect(lookupMessage('message.id', undefined)).toBe(null) | ||
expect(lookupMessage('message.id', null)).toBe(null) | ||
expect(lookup('message.id', undefined)).toBe(null) | ||
expect(lookup('message.id', null)).toBe(null) | ||
}) | ||
|
||
test('gets a shallow message of a locale dictionary', () => { | ||
addMessages('en', { field: 'name' }) | ||
expect(lookupMessage('field', 'en')).toBe('name') | ||
|
||
expect(lookup('field', 'en')).toBe('name') | ||
}) | ||
|
||
test('gets a deep message of a locale dictionary', () => { | ||
addMessages('en', { deep: { field: 'lastname' } }) | ||
expect(lookupMessage('deep.field', 'en')).toBe('lastname') | ||
expect(lookup('deep.field', 'en')).toBe('lastname') | ||
}) | ||
|
||
test('gets a message from the fallback dictionary', () => { | ||
addMessages('en', { field: 'name' }) | ||
expect(lookupMessage('field', 'en-US')).toBe('name') | ||
|
||
expect(lookup('field', 'en-US')).toBe('name') | ||
}) | ||
|
||
test('caches found messages by locale', () => { | ||
addMessages('en', { field: 'name' }) | ||
addMessages('pt', { field: 'nome' }) | ||
lookupMessage('field', 'en-US') | ||
lookupMessage('field', 'pt-BR') | ||
lookup('field', 'en-US') | ||
lookup('field', 'pt') | ||
|
||
expect(lookupCache).toMatchObject({ | ||
'en-US': { field: 'name' }, | ||
'pt-BR': { field: 'nome' }, | ||
pt: { field: 'nome' }, | ||
}) | ||
}) | ||
|
||
test("doesn't cache falsy messages", () => { | ||
addMessages('en', { field: 'name' }) | ||
addMessages('pt', { field: 'nome' }) | ||
lookupMessage('field_2', 'en-US') | ||
lookupMessage('field_2', 'pt-BR') | ||
lookup('field_2', 'en-US') | ||
lookup('field_2', 'pt') | ||
expect(lookupCache).not.toMatchObject({ | ||
'en-US': { field_2: 'name' }, | ||
'pt-BR': { field_2: 'nome' }, | ||
pt: { field_2: 'nome' }, | ||
}) | ||
}) |
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