/
utils.test.ts
65 lines (62 loc) · 1.4 KB
/
utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// utils
import * as shared from '@intlify/shared'
vi.mock('@intlify/shared', async () => {
const actual = await vi.importActual<object>('@intlify/shared')
return {
...actual,
warn: vi.fn()
}
})
import { handleFlatJson } from '../src/utils'
import { I18nWarnCodes, getWarnMessage } from '../src/warnings'
test('handleFlatJson', () => {
const mockWarn = vi.spyOn(shared, 'warn')
// eslint-disable-next-line @typescript-eslint/no-empty-function
mockWarn.mockImplementation(() => {})
const obj = {
a: { a1: 'a1.value' },
'a.a2': 'a.a2.value',
'b.x': {
'b1.x': 'b1.x.value',
'b2.x': ['b2.x.value0', 'b2.x.value1'],
'b3.x': { 'b3.x': 'b3.x.value' }
},
c: {
'animal.dog': 'Dog',
animal: 'Animal'
},
d: {
'animal.dog': 'Dog',
animal: {}
}
}
const expectObj = {
a: {
a1: 'a1.value',
a2: 'a.a2.value'
},
b: {
x: {
b1: { x: 'b1.x.value' },
b2: { x: ['b2.x.value0', 'b2.x.value1'] },
b3: { x: { b3: { x: 'b3.x.value' } } }
}
},
c: {
'animal.dog': 'Dog',
animal: 'Animal'
},
d: {
animal: {
dog: 'Dog'
}
}
}
expect(handleFlatJson(obj)).toEqual(expectObj)
expect(mockWarn).toHaveBeenCalled()
expect(mockWarn.mock.calls[0][0]).toEqual(
getWarnMessage(I18nWarnCodes.IGNORE_OBJ_FLATTEN, {
key: 'animal'
})
)
})