|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { mount } from './helper' |
| 6 | +import { defineComponent, nextTick } from 'vue' |
| 7 | +import { createI18n } from '../src/i18n' |
| 8 | +import { errorMessages, I18nErrorCodes } from '../src/errors' |
| 9 | + |
| 10 | +describe('beforeCreate', () => { |
| 11 | + test('i18n option', async () => { |
| 12 | + const i18n = createI18n({ |
| 13 | + legacy: true, |
| 14 | + locale: 'en', |
| 15 | + messages: { |
| 16 | + en: { |
| 17 | + hello: 'hello!' |
| 18 | + } |
| 19 | + } |
| 20 | + }) |
| 21 | + |
| 22 | + const App = defineComponent({ |
| 23 | + template: `<p>{{ $t('bye') }}</p>`, |
| 24 | + i18n: { |
| 25 | + locale: 'ja', |
| 26 | + messages: { |
| 27 | + ja: { |
| 28 | + bye: 'さようなら' |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + }) |
| 33 | + const { html } = await mount(App, i18n) |
| 34 | + |
| 35 | + expect(html()).toEqual('<p>さようなら</p>') |
| 36 | + }) |
| 37 | + |
| 38 | + test('__i18n option', async () => { |
| 39 | + const i18n = createI18n({ |
| 40 | + legacy: true, |
| 41 | + locale: 'en', |
| 42 | + messages: { |
| 43 | + en: { |
| 44 | + hello: 'hello!' |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + const messages = { |
| 50 | + 'en-US': { |
| 51 | + bye: 'good bye!' |
| 52 | + } |
| 53 | + } |
| 54 | + const App = defineComponent({ |
| 55 | + template: `<p>{{ $t('bye') }}</p>`, |
| 56 | + __i18n: [JSON.stringify(messages)] |
| 57 | + }) |
| 58 | + const { html } = await mount(App, i18n) |
| 59 | + |
| 60 | + expect(html()).toEqual('<p>good bye!</p>') |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +test('$t', async () => { |
| 65 | + const i18n = createI18n({ |
| 66 | + legacy: true, |
| 67 | + locale: 'en', |
| 68 | + messages: { |
| 69 | + en: { |
| 70 | + hello: 'hello!' |
| 71 | + } |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + const App = defineComponent({ template: '<br/>' }) |
| 76 | + const { vm } = await mount(App, i18n) |
| 77 | + |
| 78 | + expect(vm.$t('hello')).toEqual('hello!') |
| 79 | +}) |
| 80 | + |
| 81 | +test('$tc', async () => { |
| 82 | + const i18n = createI18n({ |
| 83 | + legacy: true, |
| 84 | + locale: 'en', |
| 85 | + messages: { |
| 86 | + en: { |
| 87 | + banana: 'no bananas | {n} banana | {n} bananas' |
| 88 | + } |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + const App = defineComponent({ template: '<br/>' }) |
| 93 | + const { vm } = await mount(App, i18n) |
| 94 | + |
| 95 | + expect(vm.$tc('banana', 2)).toEqual('2 bananas') |
| 96 | +}) |
| 97 | + |
| 98 | +test('$te', async () => { |
| 99 | + const i18n = createI18n({ |
| 100 | + legacy: true, |
| 101 | + locale: 'en', |
| 102 | + messages: { |
| 103 | + en: { |
| 104 | + hello: 'hello!' |
| 105 | + } |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + const App = defineComponent({ template: '<br/>' }) |
| 110 | + const { vm } = await mount(App, i18n) |
| 111 | + |
| 112 | + expect(vm.$te('hello')).toBe(true) |
| 113 | + expect(vm.$te('foo')).toBe(false) |
| 114 | +}) |
| 115 | + |
| 116 | +test('$d', async () => { |
| 117 | + const i18n = createI18n({ |
| 118 | + legacy: true, |
| 119 | + locale: 'en-US', |
| 120 | + datetimeFormats: { |
| 121 | + 'en-US': { |
| 122 | + short: { |
| 123 | + year: 'numeric', |
| 124 | + month: '2-digit', |
| 125 | + day: '2-digit', |
| 126 | + hour: '2-digit', |
| 127 | + minute: '2-digit', |
| 128 | + timeZone: 'America/New_York' |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + const App = defineComponent({ template: '<br/>' }) |
| 135 | + const { vm } = await mount(App, i18n) |
| 136 | + |
| 137 | + const dt = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)) |
| 138 | + expect(vm.$d(dt, 'short')).toEqual('12/19/2012, 10:00 PM') |
| 139 | +}) |
| 140 | + |
| 141 | +test('$n', async () => { |
| 142 | + const i18n = createI18n({ |
| 143 | + legacy: true, |
| 144 | + locale: 'en-US', |
| 145 | + numberFormats: { |
| 146 | + 'en-US': { |
| 147 | + percent: { |
| 148 | + style: 'percent', |
| 149 | + useGrouping: false |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + }) |
| 154 | + |
| 155 | + const App = defineComponent({ template: '<br/>' }) |
| 156 | + const { vm } = await mount(App, i18n) |
| 157 | + |
| 158 | + expect(vm.$n(0.99, 'percent')).toEqual('99%') |
| 159 | +}) |
| 160 | + |
| 161 | +test('$i18n', async () => { |
| 162 | + const i18n = createI18n({ |
| 163 | + legacy: true, |
| 164 | + locale: 'en', |
| 165 | + messages: { |
| 166 | + en: { |
| 167 | + hello: 'hello!' |
| 168 | + } |
| 169 | + } |
| 170 | + }) |
| 171 | + |
| 172 | + const App = defineComponent({ template: '<br/>' }) |
| 173 | + const { vm } = await mount(App, i18n) |
| 174 | + |
| 175 | + expect(vm.$i18n.t('hello')).toEqual('hello!') |
| 176 | +}) |
| 177 | + |
| 178 | +test.skip('beforeDestroy', async () => { |
| 179 | + const i18n = createI18n({ |
| 180 | + legacy: true, |
| 181 | + locale: 'en', |
| 182 | + messages: { |
| 183 | + en: { |
| 184 | + hello: 'hello!' |
| 185 | + } |
| 186 | + } |
| 187 | + }) |
| 188 | + |
| 189 | + const App = defineComponent({ template: '<br/>' }) |
| 190 | + const { app, rootEl, vm } = await mount(App, i18n) |
| 191 | + |
| 192 | + app.unmount(rootEl) |
| 193 | + await nextTick() |
| 194 | + |
| 195 | + expect(vm.$i18n).toBeUndefined() |
| 196 | +}) |
| 197 | + |
| 198 | +describe.skip('errors', () => { |
| 199 | + test(errorMessages[I18nErrorCodes.UNEXPECTED_ERROR], async () => { |
| 200 | + const i18n = createI18n({ |
| 201 | + legacy: true, |
| 202 | + locale: 'en', |
| 203 | + messages: { |
| 204 | + en: { |
| 205 | + hello: 'hello!' |
| 206 | + } |
| 207 | + } |
| 208 | + }) |
| 209 | + |
| 210 | + const App = defineComponent({ |
| 211 | + template: '<p>foo</p>' |
| 212 | + }) |
| 213 | + |
| 214 | + let error: Error | null = null |
| 215 | + try { |
| 216 | + await mount(App, i18n) |
| 217 | + } catch (e) { |
| 218 | + error = e |
| 219 | + } |
| 220 | + expect(error.message).toEqual( |
| 221 | + errorMessages[I18nErrorCodes.UNEXPECTED_ERROR] |
| 222 | + ) |
| 223 | + }) |
| 224 | +}) |
0 commit comments