|
| 1 | +import { readFile } from 'fs/promises' |
| 2 | +import { resolve } from 'path' |
| 3 | + |
| 4 | +import { getVueMessages, MessagesWithLocale } from '../src' |
| 5 | + |
| 6 | +describe('getVueMessages', () => { |
| 7 | + it('extracts locale messages from SFC', async () => { |
| 8 | + // Arrange |
| 9 | + const source = await readFile(resolve(__dirname, 'fixtures', './Simple.vue')) |
| 10 | + |
| 11 | + // Act |
| 12 | + const messages = getVueMessages(source.toString()) |
| 13 | + |
| 14 | + // Assert |
| 15 | + expect(messages).toHaveLength(1) |
| 16 | + expect(messages).toMatchInlineSnapshot(` |
| 17 | +Array [ |
| 18 | + Object { |
| 19 | + "locale": "en", |
| 20 | + "messages": Object { |
| 21 | + "aria-key": "Aria value", |
| 22 | + "greeting": "Hello, { $name } |
| 23 | + .aria-label = Label value", |
| 24 | + "user-name": "World", |
| 25 | + }, |
| 26 | + }, |
| 27 | +] |
| 28 | +`) |
| 29 | + }) |
| 30 | + |
| 31 | + it('extracts multiple SFC blocks', async () => { |
| 32 | + // Arrange |
| 33 | + const source = await readFile(resolve(__dirname, 'fixtures', './Multiple.vue')) |
| 34 | + |
| 35 | + // Act |
| 36 | + const messages = getVueMessages(source.toString()) |
| 37 | + |
| 38 | + // Assert |
| 39 | + expect(messages).toHaveLength(2) |
| 40 | + expect(messages).toMatchInlineSnapshot(` |
| 41 | +Array [ |
| 42 | + Object { |
| 43 | + "locale": "en", |
| 44 | + "messages": Object { |
| 45 | + "aria-key": "Aria value", |
| 46 | + "greeting": "Hello, { $name } |
| 47 | + .aria-label = Label value", |
| 48 | + "user-name": "World", |
| 49 | + }, |
| 50 | + }, |
| 51 | + Object { |
| 52 | + "locale": "uk", |
| 53 | + "messages": Object { |
| 54 | + "aria-key": "Значення aria", |
| 55 | + "greeting": "Привіт, { $name } |
| 56 | + .aria-label = Значення мітки", |
| 57 | + "user-name": "Світ", |
| 58 | + }, |
| 59 | + }, |
| 60 | +] |
| 61 | +`) |
| 62 | + }) |
| 63 | + |
| 64 | + it('throws if fluent block does not have locale', () => { |
| 65 | + // Arrange |
| 66 | + const source = ` |
| 67 | +<fluent> |
| 68 | +key = value |
| 69 | +</fluent> |
| 70 | +` |
| 71 | + |
| 72 | + // Act |
| 73 | + const func = (): MessagesWithLocale[] => getVueMessages(source) |
| 74 | + |
| 75 | + // Assert |
| 76 | + expect(func).toThrowError('fluent custom block does not have locale specified') |
| 77 | + }) |
| 78 | + |
| 79 | + it('ignores non-fluent blocks', () => { |
| 80 | + // Arrange |
| 81 | + const source = ` |
| 82 | +<fluent locale="en"> |
| 83 | +key = value |
| 84 | +fluent-key = fluent value |
| 85 | +</fluent> |
| 86 | +
|
| 87 | +<i18n> |
| 88 | +{ |
| 89 | + "en": { |
| 90 | + "key": "value", |
| 91 | + "i18n-key": "i18n value" |
| 92 | + } |
| 93 | +} |
| 94 | +</i18n> |
| 95 | +` |
| 96 | + |
| 97 | + // Act |
| 98 | + const messages = getVueMessages(source) |
| 99 | + |
| 100 | + // Assert |
| 101 | + expect(messages).toMatchInlineSnapshot(` |
| 102 | +Array [ |
| 103 | + Object { |
| 104 | + "locale": "en", |
| 105 | + "messages": Object { |
| 106 | + "fluent-key": "fluent value", |
| 107 | + "key": "value", |
| 108 | + }, |
| 109 | + }, |
| 110 | +] |
| 111 | +`) |
| 112 | + }) |
| 113 | +}) |
0 commit comments