Skip to content

Commit

Permalink
feat(tests): Add test cases for loadTranslations
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jan 26, 2023
1 parent 5e66b38 commit 949ab2c
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"gettext-parser": "^6.0.0",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"mock-xmlhttprequest": "^8.1.0",
"rollup": "^3.9.1",
"ts-jest": "^29.0.3",
"tslib": "^2.4.1",
Expand Down
166 changes: 166 additions & 0 deletions tests/loadTranslations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { MockXhrServer, newServer } from 'mock-xmlhttprequest'

import { loadTranslations, register, translate, _unregister } from '../lib/translation'

const setLocale = (locale) => document.documentElement.setAttribute('data-locale', locale)

describe('loadTranslations', () => {
let server: MockXhrServer

beforeEach(() => {
setLocale('de')
server = newServer()
.addHandler('GET', '/myapp/l10n/de.json', {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
translations: {
'Hello world!': 'Hallo Welt!',
},
}),
})
.addHandler('GET', '/invalid/l10n/de.json', {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
strings: {
'Hello world!': 'Hallo Welt!',
},
}),
})
.addHandler('GET', '/empty/l10n/de.json', {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: '',
})
.addHandler('GET', '/404/l10n/de.json', {
status: 404,
statusText: 'Not Found',
})
.addHandler('GET', '/500/l10n/de.json', {
status: 500,
statusText: 'Internal Server Error',
})
.addHandler('GET', '/networkissue/l10n/de.json', (req) => req.setNetworkError())
.setDefault404()
server
.disableTimeout()
server
.install()
})

afterEach(() => {
server.remove()
jest.clearAllMocks()
})

it('calls callback if app already exists', async () => {
register('myapp', {
Bye: 'Tschüss',
})

const callback = jest.fn()
try {
await loadTranslations('myapp', callback)
// Callback called
expect(callback).toBeCalledTimes(1)
// No requests done
expect(server.getRequestLog().length).toBe(0)
// Old translations work
expect(translate('myapp', 'Bye')).toBe('Tschüss')
// does not override translations
expect(translate('myapp', 'Hello world!')).toBe('Hello world!')
} catch (e) {
expect(e).toBe('Unexpected error')
} finally {
_unregister('myapp')
}
})

it('calls callback if locale is English', async () => {
setLocale('en')
const callback = jest.fn()

try {
await loadTranslations('myapp', callback)
// Callback called
expect(callback).toBeCalledTimes(1)
// No requests done
expect(server.getRequestLog().length).toBe(0)
} catch (e) {
expect(e).toBe('Unexpected error')
}
})

it('registers new translations', async () => {
const callback = jest.fn()
try {
await loadTranslations('myapp', callback)
// Callback called
expect(callback).toBeCalledTimes(1)
// No requests done
expect(server.getRequestLog().length).toBe(1)
// New translations work
expect(translate('myapp', 'Hello world!')).toBe('Hallo Welt!')
} catch (e) {
expect(e).toBe('Unexpected error')
} finally {
console.warn(server.getRequestLog()[0])
}
})

it('does reject on network error', async () => {
const callback = jest.fn()
try {
await loadTranslations('networkissue', callback)
expect('').toBe('Unexpected pass')
} catch (e) {
expect(e instanceof Error).toBe(true)
expect((<Error>e).message).toBe('Network error')
}
})

it('does reject on server error', async () => {
const callback = jest.fn()
try {
await loadTranslations('500', callback)
expect('').toBe('Unexpected pass')
} catch (e) {
expect(e instanceof Error).toBe(true)
expect((<Error>e).message).toBe('Internal Server Error')
}
})

it('does reject on unavailable bundle', async () => {
const callback = jest.fn()
try {
await loadTranslations('404', callback)
expect('').toBe('Unexpected pass')
} catch (e) {
expect(e instanceof Error).toBe(true)
expect((<Error>e).message).toBe('Not Found')
}
})

it('does reject on invalid bundle', async () => {
const callback = jest.fn()
try {
await loadTranslations('invalid', callback)
expect('').toBe('Unexpected pass')
} catch (e) {
expect(e instanceof Error).toBe(true)
expect((<Error>e).message).toBe('Invalid content of translation bundle')
}
})

it('does reject on empty bundle', async () => {
const callback = jest.fn()
try {
await loadTranslations('invalid', callback)
expect('').toBe('Unexpected pass')
} catch (e) {
expect(e instanceof Error).toBe(true)
expect((<Error>e).message).toBe('Invalid content of translation bundle')
}
})
})

0 comments on commit 949ab2c

Please sign in to comment.