forked from baizn/translation.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils-spec.ts
54 lines (43 loc) · 1.42 KB
/
utils-spec.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
import { getValue, invert, transformOptions, transformSuperAgentError } from '../src/utils'
import { ISuperAgentResponseError } from '../src/interfaces'
import { ERROR_CODE } from '../src/constant'
describe('utils 中的', () => {
it('invert 方法会反转对象的键和值', () => {
expect(invert({
a: 1,
b: 'x'
})).toEqual({
1: 'a',
x: 'b'
})
})
it('getValue 方法会读取对象上指定路径的值', () => {
const obj = {
a: {
b: 'c'
}
}
expect(getValue(obj, 'a')).toEqual({
b: 'c'
})
expect(getValue(null, 'b')).toBeUndefined()
expect(getValue(obj, 'd', 'Y')).toBe('Y')
expect(getValue(obj, ['a', 'b'])).toBe('c')
expect(getValue(undefined, 'a', 'hi')).toBe('hi')
})
it('transformOptions 方法会转换配置项', () => {
expect(transformOptions('test')).toEqual({
text: 'test'
})
const o = {
text: 'xx',
y: 1
}
expect(transformOptions(o)).toBe(o)
})
it('transformSuperAgentError 方法会判断错误类型', () => {
expect(transformSuperAgentError({ timeout: true } as ISuperAgentResponseError).code).toBe(ERROR_CODE.NETWORK_TIMEOUT)
expect(transformSuperAgentError({} as ISuperAgentResponseError).code).toBe(ERROR_CODE.NETWORK_ERROR)
expect(transformSuperAgentError({ status: 500, response: {} } as ISuperAgentResponseError).code).toBe(ERROR_CODE.API_SERVER_ERROR)
})
})