|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 5 | +// allow any in error |
| 6 | +/* eslint-disable @typescript-eslint/no-empty-function */ |
| 7 | + |
| 8 | +// utils |
| 9 | +import * as shared from '@intlify/shared' |
| 10 | +vi.mock('@intlify/shared', async () => { |
| 11 | + const actual = await vi.importActual<object>('@intlify/shared') |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + warn: vi.fn() |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +import { mount } from './helper' |
| 19 | +import { defineComponent, ref, h, withDirectives, resolveDirective } from 'vue' |
| 20 | +import { format } from '@intlify/shared' |
| 21 | +import { |
| 22 | + compileToFunction, |
| 23 | + registerMessageCompiler, |
| 24 | + resolveValue, |
| 25 | + registerMessageResolver, |
| 26 | + fallbackWithLocaleChain, |
| 27 | + registerLocaleFallbacker |
| 28 | +} from '@intlify/core-base' |
| 29 | +import { createI18n } from '../src/index' |
| 30 | +import { errorMessages, I18nErrorCodes } from '../src/errors' |
| 31 | +import { getWarnMessage, I18nWarnCodes } from '../src/warnings' |
| 32 | + |
| 33 | +beforeAll(() => { |
| 34 | + registerMessageCompiler(compileToFunction) |
| 35 | + registerMessageResolver(resolveValue) |
| 36 | + registerLocaleFallbacker(fallbackWithLocaleChain) |
| 37 | +}) |
| 38 | + |
| 39 | +describe('basic', () => { |
| 40 | + test('literal', async () => { |
| 41 | + const i18n = createI18n({ |
| 42 | + locale: 'en', |
| 43 | + messages: { |
| 44 | + en: { |
| 45 | + hello: 'hello!' |
| 46 | + } |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + const App = defineComponent({ |
| 51 | + setup() { |
| 52 | + // <p v-t="'hello'"></p> |
| 53 | + const t = resolveDirective('t') |
| 54 | + return () => { |
| 55 | + return withDirectives(h('p'), [[t!, 'hello']]) |
| 56 | + } |
| 57 | + } |
| 58 | + }) |
| 59 | + const wrapper = await mount(App, i18n) |
| 60 | + |
| 61 | + expect(wrapper.html()).toEqual('<p>hello!</p>') |
| 62 | + }) |
| 63 | + |
| 64 | + test('binding', async () => { |
| 65 | + const i18n = createI18n({ |
| 66 | + locale: 'en', |
| 67 | + messages: { |
| 68 | + en: { |
| 69 | + hello: 'hello!' |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + const App = defineComponent({ |
| 75 | + setup() { |
| 76 | + // <p v-t="msg"></p> |
| 77 | + const msg = ref('hello') |
| 78 | + const t = resolveDirective('t') |
| 79 | + return () => { |
| 80 | + return withDirectives(h('p'), [[t!, msg.value]]) |
| 81 | + } |
| 82 | + } |
| 83 | + }) |
| 84 | + const wrapper = await mount(App, i18n) |
| 85 | + |
| 86 | + expect(wrapper.html()).toEqual('<p>hello!</p>') |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +test('object literal', async () => { |
| 91 | + const i18n = createI18n({ |
| 92 | + locale: 'en', |
| 93 | + messages: { |
| 94 | + en: { |
| 95 | + hello: 'hello, {name}!' |
| 96 | + }, |
| 97 | + ja: { |
| 98 | + hello: 'こんにちは、{name}!' |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + const App = defineComponent({ |
| 104 | + setup() { |
| 105 | + // <p v-t="{ path: 'hello', locale: 'ja', args: { name: name.value } }"></p> |
| 106 | + const name = ref('kazupon') |
| 107 | + const t = resolveDirective('t') |
| 108 | + return () => { |
| 109 | + return withDirectives(h('p'), [ |
| 110 | + [t!, { path: 'hello', locale: 'ja', args: { name: name.value } }] |
| 111 | + ]) |
| 112 | + } |
| 113 | + } |
| 114 | + }) |
| 115 | + const wrapper = await mount(App, i18n) |
| 116 | + |
| 117 | + expect(wrapper.html()).toEqual('<p>こんにちは、kazupon!</p>') |
| 118 | +}) |
| 119 | + |
| 120 | +test('plural', async () => { |
| 121 | + const i18n = createI18n({ |
| 122 | + locale: 'en', |
| 123 | + messages: { |
| 124 | + en: { |
| 125 | + banana: 'no bananas | {n} banana | {n} bananas' |
| 126 | + } |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + const App = defineComponent({ |
| 131 | + setup() { |
| 132 | + // <p v-t="{ path: 'banana', choice: 2 }"></p> |
| 133 | + const t = resolveDirective('t') |
| 134 | + return () => { |
| 135 | + return withDirectives(h('p'), [[t!, { path: 'banana', choice: 2 }]]) |
| 136 | + } |
| 137 | + } |
| 138 | + }) |
| 139 | + const wrapper = await mount(App, i18n) |
| 140 | + |
| 141 | + expect(wrapper.html()).toEqual('<p>2 bananas</p>') |
| 142 | +}) |
| 143 | + |
| 144 | +test('legacy mode', async () => { |
| 145 | + const i18n = createI18n({ |
| 146 | + legacy: true, |
| 147 | + locale: 'en', |
| 148 | + messages: { |
| 149 | + en: { |
| 150 | + hello: 'hello!' |
| 151 | + } |
| 152 | + } |
| 153 | + }) |
| 154 | + |
| 155 | + const App = defineComponent({ |
| 156 | + setup() { |
| 157 | + // <p v-t="'hello'"></p> |
| 158 | + const t = resolveDirective('t') |
| 159 | + return () => { |
| 160 | + return withDirectives(h('p'), [[t!, 'hello']]) |
| 161 | + } |
| 162 | + } |
| 163 | + }) |
| 164 | + const wrapper = await mount(App, i18n) |
| 165 | + |
| 166 | + expect(wrapper.html()).toEqual('<p>hello!</p>') |
| 167 | +}) |
| 168 | + |
| 169 | +test('using in template', async () => { |
| 170 | + const i18n = createI18n({ |
| 171 | + locale: 'en', |
| 172 | + messages: { |
| 173 | + en: { |
| 174 | + hello: 'hello!' |
| 175 | + } |
| 176 | + } |
| 177 | + }) |
| 178 | + |
| 179 | + const App = defineComponent({ |
| 180 | + template: `<p v-t="'hello'"></p>` |
| 181 | + }) |
| 182 | + const wrapper = await mount(App, i18n) |
| 183 | + |
| 184 | + expect(wrapper.html()).toEqual('<p>hello!</p>') |
| 185 | +}) |
| 186 | + |
| 187 | +describe('errors', () => { |
| 188 | + let org: any // eslint-disable-line @typescript-eslint/no-explicit-any |
| 189 | + let spy: any // eslint-disable-line @typescript-eslint/no-explicit-any |
| 190 | + beforeEach(() => { |
| 191 | + org = console.warn |
| 192 | + spy = vi.fn() |
| 193 | + console.warn = spy |
| 194 | + }) |
| 195 | + afterEach(() => { |
| 196 | + console.warn = org |
| 197 | + }) |
| 198 | + |
| 199 | + test(errorMessages[I18nErrorCodes.REQUIRED_VALUE], async () => { |
| 200 | + const i18n = createI18n({ |
| 201 | + locale: 'en', |
| 202 | + messages: { |
| 203 | + en: { |
| 204 | + hello: 'hello!' |
| 205 | + } |
| 206 | + } |
| 207 | + }) |
| 208 | + |
| 209 | + const App = defineComponent({ |
| 210 | + setup() { |
| 211 | + // <p v-t="{ locale: 'ja' }"></p> |
| 212 | + const t = resolveDirective('t') |
| 213 | + return () => { |
| 214 | + return withDirectives(h('p'), [[t!, { locale: 'ja' }]]) |
| 215 | + } |
| 216 | + } |
| 217 | + }) |
| 218 | + |
| 219 | + let error: Error | null = null |
| 220 | + try { |
| 221 | + await mount(App, i18n) |
| 222 | + } catch (e: any) { |
| 223 | + error = e |
| 224 | + } |
| 225 | + expect(error!.message).toEqual( |
| 226 | + format(errorMessages[I18nErrorCodes.REQUIRED_VALUE], 'path') |
| 227 | + ) |
| 228 | + }) |
| 229 | + |
| 230 | + test(errorMessages[I18nErrorCodes.INVALID_VALUE], async () => { |
| 231 | + const i18n = createI18n({ |
| 232 | + locale: 'en', |
| 233 | + messages: { |
| 234 | + en: { |
| 235 | + hello: 'hello!' |
| 236 | + } |
| 237 | + } |
| 238 | + }) |
| 239 | + |
| 240 | + const App = defineComponent({ |
| 241 | + setup() { |
| 242 | + // <p v-t="1"></p> |
| 243 | + const t = resolveDirective('t') |
| 244 | + return () => withDirectives(h('p'), [[t!, 1]]) |
| 245 | + } |
| 246 | + }) |
| 247 | + |
| 248 | + let error: Error | null = null |
| 249 | + try { |
| 250 | + await mount(App, i18n) |
| 251 | + } catch (e: any) { |
| 252 | + error = e |
| 253 | + } |
| 254 | + expect(error!.message).toEqual(errorMessages[I18nErrorCodes.INVALID_VALUE]) |
| 255 | + }) |
| 256 | +}) |
| 257 | + |
| 258 | +/* eslint-enable @typescript-eslint/no-empty-function */ |
0 commit comments