Skip to content

Commit

Permalink
feat(devicepreferences): expand i18n support
Browse files Browse the repository at this point in the history
- list preference localizations
- update a preference localization for a locale
  • Loading branch information
john-u committed Jan 6, 2022
1 parent 8da99e1 commit e5b4d15
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/endpoint/devicepreferences.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Endpoint } from '../endpoint'
import { EndpointClient, EndpointClientConfig } from '../endpoint-client'
import { LocaleTag } from '../types'
import { LocaleReference, LocaleTag } from '../types'
import { PreferenceType } from './devices'


Expand Down Expand Up @@ -196,4 +196,12 @@ export class DevicePreferencesEndpoint extends Endpoint {
public getTranslations(preferenceId: PreferenceId, locale: LocaleTag): Promise<PreferenceLocalization> {
return this.client.get(`${preferenceId}/i18n/${locale}`)
}

public listTranslations(preferenceId: PreferenceId): Promise<LocaleReference[]> {
return this.client.get(`${preferenceId}/i18n`)
}

public updateTranslations(preferenceId: PreferenceId, localization: PreferenceLocalization): Promise<PreferenceLocalization> {
return this.client.put(`${preferenceId}/i18n/${localization.tag}`, localization)
}
}
46 changes: 41 additions & 5 deletions test/unit/devicepreferences.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { NoOpAuthenticator } from '../../src/authenticator'
import { EndpointClient } from '../../src/endpoint-client'
import { DevicePreference, DevicePreferenceCreate, DevicePreferencesEndpoint, PreferenceLocalization } from '../../src/endpoint/devicepreferences'
import { LocaleReference } from '../../src/types'


jest.mock('../../src/endpoint-client')

const MOCK_PREFERENCE_L10N = {} as PreferenceLocalization
const MOCK_PREFERENCE_LIST = [] as DevicePreference[]
const MOCK_PREFERENCE = {} as DevicePreference
const MOCK_PREFERENCE_CREATE = {} as DevicePreferenceCreate
const MOCK_PREFERENCE_L10N = { tag: 'localeTag' } as PreferenceLocalization
const MOCK_PREFERENCE = { preferenceId: 'preferenceId' } as DevicePreference
const MOCK_PREFERENCE_LIST = [MOCK_PREFERENCE] as DevicePreference[]
const MOCK_PREFERENCE_CREATE = { preferenceType: 'type' } as unknown as DevicePreferenceCreate
const MOCK_LOCALE_LIST = [{ tag: 'tag' }] as LocaleReference[]

describe('devicepreferences', () => {
const authenticator = new NoOpAuthenticator()
Expand Down Expand Up @@ -112,7 +114,7 @@ describe('devicepreferences', () => {
const response = await devicepreferences.createTranslations(preferenceId, MOCK_PREFERENCE_L10N)

expect(postSpy).toBeCalledWith(`${preferenceId}/i18n`, MOCK_PREFERENCE_L10N)
expect(response).toStrictEqual(MOCK_PREFERENCE)
expect(response).toStrictEqual(MOCK_PREFERENCE_L10N)
})

test('create failure', async () => {
Expand All @@ -139,5 +141,39 @@ describe('devicepreferences', () => {

await expect(promise).rejects.toThrow(error)
})

test('list', async () => {
getSpy.mockResolvedValueOnce(MOCK_LOCALE_LIST)

const response = await devicepreferences.listTranslations(preferenceId)

expect(getSpy).toBeCalledWith(`${preferenceId}/i18n`)
expect(response).toStrictEqual(MOCK_LOCALE_LIST)
})

test('list failure', async () => {
getSpy.mockRejectedValueOnce(error)

const promise = devicepreferences.listTranslations(preferenceId)

await expect(promise).rejects.toThrow(error)
})

test('update', async () => {
putSpy.mockResolvedValueOnce(MOCK_PREFERENCE_L10N)

const response = await devicepreferences.updateTranslations(preferenceId, MOCK_PREFERENCE_L10N)

expect(putSpy).toBeCalledWith(`${preferenceId}/i18n/${MOCK_PREFERENCE_L10N.tag}`, MOCK_PREFERENCE_L10N)
expect(response).toStrictEqual(MOCK_PREFERENCE_L10N)
})

test('update failure', async () => {
putSpy.mockRejectedValueOnce(error)

const promise = devicepreferences.updateTranslations(preferenceId, MOCK_PREFERENCE_L10N)

await expect(promise).rejects.toThrow(error)
})
})
})

0 comments on commit e5b4d15

Please sign in to comment.