Skip to content

Commit

Permalink
test: fix language unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
orestbida committed Aug 12, 2024
1 parent 45f5118 commit 23fc0b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
20 changes: 14 additions & 6 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,27 @@ describe("API tests", () => {
expect(globalObj._state._currentLanguageCode).toBe('it');
})

it('Should fail when language does not exists the language to "it"', async () => {
it('Should throw error when desired language is not defined', async () => {
fetch.mockReturnValueOnce(false);
const set = await api.setLanguage('en-IT', true);
expect(set).toBe(false);
try {
await api.setLanguage('en-IT', true);
} catch (ex) {
expect(ex).toBe(`Could not load translation for the 'en-IT' language`);
}
expect(globalObj._state._currentLanguageCode).not.toBe('en-IT');
})

it('Should fail when fetch fails', async () => {
it('Should throw error when fetch fails', async () => {
fetch.mockImplementationOnce(() => Promise.reject("json file not found"));
api.getConfig('language').translations.it = './it.json';
await api.setLanguage('en');
const set = await api.setLanguage('it');
expect(set).toBe(false);

try {
await api.setLanguage('it');
} catch (ex) {
expect(ex).toBe(`Could not load translation for the 'it' language`);
}

expect(globalObj._state._currentLanguageCode).not.toBe('it');
})

Expand Down
10 changes: 6 additions & 4 deletions tests/language.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ describe('Test language utils', () => {
default: 'it'
}
};

const validTranslationData = await loadTranslationData('it');

expect(validTranslationData).toBe(false)

try {
await loadTranslationData('it');
} catch (ex) {
expect(ex).toBe(`Could not load translation for the 'it' language`);
}
});

it('Should return the default language if the detection method is not supported', () => {
Expand Down

0 comments on commit 23fc0b6

Please sign in to comment.