|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { mount } from '../helper' |
| 6 | +import { defineComponent } from 'vue' |
| 7 | +import { createI18n } from '../../src/i18n' |
| 8 | + |
| 9 | +const numberFormats = { |
| 10 | + 'en-US': { |
| 11 | + currency: { |
| 12 | + style: 'currency', |
| 13 | + currency: 'USD', |
| 14 | + currencyDisplay: 'symbol' |
| 15 | + }, |
| 16 | + decimal: { |
| 17 | + style: 'decimal', |
| 18 | + useGrouping: false |
| 19 | + } |
| 20 | + }, |
| 21 | + 'ja-JP': { |
| 22 | + currency: { |
| 23 | + style: 'currency', |
| 24 | + currency: 'JPY', |
| 25 | + currencyDisplay: 'symbol' |
| 26 | + }, |
| 27 | + numeric: { |
| 28 | + style: 'decimal', |
| 29 | + useGrouping: false |
| 30 | + }, |
| 31 | + percent: { |
| 32 | + style: 'percent', |
| 33 | + useGrouping: false |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +let org, spy |
| 39 | +beforeEach(() => { |
| 40 | + org = console.warn |
| 41 | + spy = jest.fn() |
| 42 | + console.warn = spy |
| 43 | +}) |
| 44 | +afterEach(() => { |
| 45 | + console.warn = org |
| 46 | +}) |
| 47 | + |
| 48 | +test('basic usage', async () => { |
| 49 | + const i18n = createI18n({ |
| 50 | + locale: 'en-US', |
| 51 | + numberFormats |
| 52 | + }) |
| 53 | + |
| 54 | + const App = defineComponent({ |
| 55 | + template: ` |
| 56 | +<i18n-n tag="p" :value="100"></i18n-n> |
| 57 | +<i18n-n tag="p" :value="100" format="currency"></i18n-n> |
| 58 | +<i18n-n tag="p" :value="100" format="currency" locale="ja-JP"></i18n-n> |
| 59 | +` |
| 60 | + }) |
| 61 | + const wrapper = await mount(App, i18n) |
| 62 | + |
| 63 | + expect(wrapper.html()).toEqual(`<p>100</p><p>$100.00</p><p>¥100</p>`) |
| 64 | +}) |
| 65 | + |
| 66 | +test('slots', async () => { |
| 67 | + const i18n = createI18n({ |
| 68 | + locale: 'en-US', |
| 69 | + numberFormats |
| 70 | + }) |
| 71 | + |
| 72 | + const App = defineComponent({ |
| 73 | + template: ` |
| 74 | +<i18n-n :value="1234" :format="{ key: 'currency', currency: 'EUR' }"> |
| 75 | + <template #currency="props" |
| 76 | + ><span style="color: green;">{{ props.currency }}</span></template |
| 77 | + > |
| 78 | + <template #integer="props" |
| 79 | + ><span style="font-weight: bold;">{{ props.integer }}</span></template |
| 80 | + > |
| 81 | + <template #group="props" |
| 82 | + ><span style="font-weight: bold;">{{ props.group }}</span></template |
| 83 | + > |
| 84 | + <template #fraction="props" |
| 85 | + ><span style="font-size: small;">{{ props.fraction }}</span></template |
| 86 | + > |
| 87 | +</i18n-n> |
| 88 | +` |
| 89 | + }) |
| 90 | + const wrapper = await mount(App, i18n) |
| 91 | + |
| 92 | + expect(wrapper.html()).toEqual( |
| 93 | + `<span style=\"color: green;\">€</span><span style=\"font-weight: bold;\">1</span><span style=\"font-weight: bold;\">,</span><span style=\"font-weight: bold;\">234</span>.<span style=\"font-size: small;\">00</span>` |
| 94 | + ) |
| 95 | +}) |
0 commit comments