-
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.
feat: 🎸 Support getting deep localized objects/arrays
✅ Closes: Closes #83
- Loading branch information
1 parent
5c3191d
commit ff54136
Showing
12 changed files
with
6,926 additions
and
217 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,51 +1,82 @@ | ||
import { lookup, lookupCache } from '../../../src/runtime/includes/lookup' | ||
import { $dictionary, addMessages } from '../../../src/runtime/stores/dictionary' | ||
import { lookup, lookupCache } from '../../../src/runtime/includes/lookup'; | ||
import { | ||
$dictionary, | ||
addMessages, | ||
} from '../../../src/runtime/stores/dictionary'; | ||
|
||
beforeEach(() => { | ||
$dictionary.set({}) | ||
}) | ||
$dictionary.set({}); | ||
}); | ||
|
||
test('returns null if no locale was passed', () => { | ||
expect(lookup('message.id', undefined)).toBe(null) | ||
expect(lookup('message.id', null)).toBe(null) | ||
}) | ||
expect(lookup('message.id', undefined)).toBeNull(); | ||
expect(lookup('message.id', null)).toBeNull(); | ||
}); | ||
|
||
test('gets a shallow message of a locale dictionary', () => { | ||
addMessages('en', { field: 'name' }) | ||
addMessages('en', { field: 'name' }); | ||
|
||
expect(lookup('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(lookup('deep.field', 'en')).toBe('lastname') | ||
}) | ||
addMessages('en', { deep: { field: 'lastname' } }); | ||
expect(lookup('deep.field', 'en')).toBe('lastname'); | ||
}); | ||
|
||
test('gets a message from the fallback dictionary', () => { | ||
addMessages('en', { field: 'name' }) | ||
addMessages('en', { field: 'name' }); | ||
|
||
expect(lookup('field', 'en-US')).toBe('name') | ||
}) | ||
expect(lookup('field', 'en-US')).toBe('name'); | ||
}); | ||
|
||
test('gets an array ', () => { | ||
addMessages('en', { | ||
careers: [ | ||
{ | ||
role: 'Role 1', | ||
description: 'Description 1', | ||
}, | ||
{ | ||
role: 'Role 2', | ||
description: 'Description 2', | ||
}, | ||
], | ||
}); | ||
|
||
expect(lookup('careers', 'en-US')).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"description": "Description 1", | ||
"role": "Role 1", | ||
}, | ||
Object { | ||
"description": "Description 2", | ||
"role": "Role 2", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test('caches found messages by locale', () => { | ||
addMessages('en', { field: 'name' }) | ||
addMessages('pt', { field: 'nome' }) | ||
lookup('field', 'en-US') | ||
lookup('field', 'pt') | ||
addMessages('en', { field: 'name' }); | ||
addMessages('pt', { field: 'nome' }); | ||
lookup('field', 'en-US'); | ||
lookup('field', 'pt'); | ||
|
||
expect(lookupCache).toMatchObject({ | ||
'en-US': { field: 'name' }, | ||
pt: { field: 'nome' }, | ||
}) | ||
}) | ||
}); | ||
}); | ||
|
||
test("doesn't cache falsy messages", () => { | ||
addMessages('en', { field: 'name' }) | ||
addMessages('pt', { field: 'nome' }) | ||
lookup('field_2', 'en-US') | ||
lookup('field_2', 'pt') | ||
addMessages('en', { field: 'name' }); | ||
addMessages('pt', { field: 'nome' }); | ||
lookup('field_2', 'en-US'); | ||
lookup('field_2', 'pt'); | ||
expect(lookupCache).not.toMatchObject({ | ||
'en-US': { field_2: 'name' }, | ||
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
Oops, something went wrong.