Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed May 21, 2020
1 parent 3889d89 commit 0de1352
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/errors.ts
Expand Up @@ -25,7 +25,10 @@ export const enum I18nErrorCodes {
__EXTEND_POINT__
}

export function createI18nError(code: I18nErrorCodes, ...args: unknown[]): I18nError {
export function createI18nError(
code: I18nErrorCodes,
...args: unknown[]
): I18nError {
return createCompileError(
code,
null,
Expand Down
12 changes: 6 additions & 6 deletions src/warnings.ts
Expand Up @@ -13,12 +13,12 @@ export const enum I18nWarnCodes {

export const warnMessages: { [code: number]: string } = {
[I18nWarnCodes.FALLBACK_TO_ROOT]: `Fall back to {type} '{key}' with root locale.`,
[I18nWarnCodes.NOT_SUPPORTED_PRESERVE]: `not supportted 'preserve'.`,
[I18nWarnCodes.NOT_SUPPORTED_FORMATTER]: `not supportted 'formatter'.`,
[I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE]: `not supportted 'preserveDirectiveContent'.`,
[I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX]: `not supportted 'getChoiceIndex'.`,
[I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE]: `component name legacy compatible: '{name}' -> 'i18n'`,
[I18nWarnCodes.NOT_FOUND_PARENT_COMPOSER]: `not found parent composer. use the global composer`
[I18nWarnCodes.NOT_SUPPORTED_PRESERVE]: `Not supportted 'preserve'.`,
[I18nWarnCodes.NOT_SUPPORTED_FORMATTER]: `Not supportted 'formatter'.`,
[I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE]: `Not supportted 'preserveDirectiveContent'.`,
[I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX]: `Not supportted 'getChoiceIndex'.`,
[I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE]: `Component name legacy compatible: '{name}' -> 'i18n'`,
[I18nWarnCodes.NOT_FOUND_PARENT_COMPOSER]: `Not found parent composer. use the global composer.`
}

export function getWarnMessage(
Expand Down
16 changes: 12 additions & 4 deletions test/diretive.test.ts
Expand Up @@ -88,7 +88,9 @@ test('object literal', async () => {
const name = ref('kazupon')
const t = resolveDirective('t')
return () => {
return withDirectives(h('p'), [[t, { path: 'hello', locale: 'ja', args: { name: name.value } }]])
return withDirectives(h('p'), [
[t, { path: 'hello', locale: 'ja', args: { name: name.value } }]
])
}
}
})
Expand Down Expand Up @@ -146,7 +148,9 @@ test('preserve modifier', async () => {
const wrapper = await mount(App, i18n)

expect(mockWarn).toHaveBeenCalledTimes(1)
expect(mockWarn.mock.calls[0][0]).toEqual(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE))
expect(mockWarn.mock.calls[0][0]).toEqual(
getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE)
)
})

test('legacy mode', async () => {
Expand Down Expand Up @@ -202,7 +206,9 @@ describe('errors', () => {
} catch (e) {
error = e
}
expect(error.message).toEqual(errorMessages[I18nErrorCodes.NOT_FOUND_COMPOSER])
expect(error.message).toEqual(
errorMessages[I18nErrorCodes.NOT_FOUND_COMPOSER]
)
})

test(errorMessages[I18nErrorCodes.REQUIRED_VALUE], async () => {
Expand Down Expand Up @@ -231,7 +237,9 @@ describe('errors', () => {
} catch (e) {
error = e
}
expect(error.message).toEqual(format(errorMessages[I18nErrorCodes.REQUIRED_VALUE], 'path'))
expect(error.message).toEqual(
format(errorMessages[I18nErrorCodes.REQUIRED_VALUE], 'path')
)
})

test(errorMessages[I18nErrorCodes.INVALID_VALUE], async () => {
Expand Down
17 changes: 13 additions & 4 deletions test/legacy.test.ts
Expand Up @@ -9,6 +9,7 @@ import { warn } from '../src/utils'

import { createVueI18n } from '../src/legacy'
import { errorMessages, I18nErrorCodes } from '../src/errors'
import { getWarnMessage, I18nWarnCodes } from '../src/warnings'

test('locale', () => {
const i18n = createVueI18n()
Expand Down Expand Up @@ -55,9 +56,15 @@ test('formatter', () => {
}
}
expect(mockWarn).toHaveBeenCalledTimes(3)
expect(mockWarn.mock.calls[0][0]).toEqual(`not supportted 'formatter'.`)
expect(mockWarn.mock.calls[1][0]).toEqual(`not supportted 'formatter'.`)
expect(mockWarn.mock.calls[2][0]).toEqual(`not supportted 'formatter'.`)
expect(mockWarn.mock.calls[0][0]).toEqual(
getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER)
)
expect(mockWarn.mock.calls[1][0]).toEqual(
getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER)
)
expect(mockWarn.mock.calls[2][0]).toEqual(
getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER)
)
})

test('missing', () => {
Expand Down Expand Up @@ -452,7 +459,9 @@ test('getChoiceIndex', () => {

const i18n = createVueI18n({})
i18n.getChoiceIndex(1, 2)
expect(mockWarn.mock.calls[0][0]).toEqual(`not supportted 'getChoiceIndex'.`)
expect(mockWarn.mock.calls[0][0]).toEqual(
getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX)
)
})

test('warnHtmlInMessage', () => {
Expand Down
5 changes: 4 additions & 1 deletion test/plugin.test.ts
Expand Up @@ -13,6 +13,7 @@ import { warn } from '../src/utils'
import { createApp } from 'vue'
import { I18n, I18nInternal } from '../src/i18n'
import { apply } from '../src/plugin'
import { getWarnMessage, I18nWarnCodes } from '../src/warnings'

describe('useI18nComponentName option', () => {
test('default', () => {
Expand All @@ -36,7 +37,9 @@ describe('useI18nComponentName option', () => {
apply(app, i18n, { useI18nComponentName: true })
expect(mockWarn).toHaveBeenCalled()
expect(mockWarn.mock.calls[0][0]).toEqual(
`component name legacy compatible: 'i18n-t' -> 'i18n'`
getWarnMessage(I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE, {
name: 'i18n-t'
})
)
})
})
Expand Down

0 comments on commit 0de1352

Please sign in to comment.