-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.test.js
94 lines (82 loc) · 2.19 KB
/
promise.test.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const Promise = require('./promise')
const { readFile } = require('./test-utils')
describe('core', () => {
test('异步处理', (done) => {
const p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('异步数据')
}, 1000)
})
p.then((data) => {
expect(data).toBe('异步数据') // 验证返回值是否正确
done() // 确保 Jest 知道测试完成
})
})
test('p.then是同步的,then中的回调是异步的', () => {
let syncExecuted = false // 用于追踪同步代码是否已经执行
p = new Promise((resolve, reject) => {
resolve(1)
})
p.then((data) => {
expect(syncExecuted).toBe(true)
})
syncExecuted = true
})
test('then may be called multiple times on the same promise.', () => {})
})
describe('链式调用', () => {
/**
* 返回值透传到下一次链式调用,避免深层嵌套
*/
test('happy path', () => {
const expectedData = {
domain: 'github.io',
path: '/docs',
}
readFile('./config.json')
.then((data) => {
expect(data).toEqual(expectedData)
return 'return'
})
.then((data) => {
expect(data).toBe('return')
done()
})
})
test('throw error', () => {
const expectedData = {
domain: 'github.io',
path: '/docs',
}
readFile('./config.json')
.then((data) => {
expect(data).toEqual(expectedData)
throw new Error('woops!something went wrong')
})
.then(
() => {},
(reason) => {
expect(reason).toBe('woops!something went wrong')
}
)
})
})
describe('Promise Resolution Procedure', () => {
/**
* then callback return value process
*/
test('如果 promise 和 x 引用的同一对象,则以 TypeError 理由拒绝 (避免循环引用)', () => {
const p = new Promise((resolve, reject) => {
resolve(1)
})
p.then((data) => {
return p
}).then(null, (err) => {
expect(err).toBe(
'[TypeError: Chaining cycle detected for promise #<Promise>]'
)
})
})
test('如果 x 是一个 promise ,采用它的状态', () => {})
test('递归解析', () => {})
})