forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts-list.test.ts
130 lines (114 loc) · 2.67 KB
/
posts-list.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { deepStrictEqual } from 'node:assert'
import { test } from 'node:test'
import { createPostsList, type OriginPost } from '../index.ts'
const POST1: OriginPost = {
full: '1',
media: [],
originId: '1'
}
const POST2: OriginPost = {
full: '2',
media: [],
originId: '2'
}
test('works with cached posts without next page', async () => {
let posts = createPostsList([POST1], undefined)
deepStrictEqual(posts.get(), {
hasNext: false,
isLoading: false,
list: [POST1]
})
let promise = posts.next()
deepStrictEqual(posts.get(), {
hasNext: false,
isLoading: false,
list: [POST1]
})
deepStrictEqual(await promise, [])
})
test('works without posts', async () => {
let posts = createPostsList(undefined, async () => {
return [[POST1], async () => [[POST2], undefined]]
})
deepStrictEqual(posts.get(), {
hasNext: true,
isLoading: true,
list: []
})
let next1 = await posts.loading
deepStrictEqual(posts.get(), {
hasNext: true,
isLoading: false,
list: [POST1]
})
deepStrictEqual(next1, [POST1])
let promise2 = posts.next()
deepStrictEqual(posts.get(), {
hasNext: true,
isLoading: true,
list: [POST1]
})
await posts.loading
deepStrictEqual(posts.get(), {
hasNext: false,
isLoading: false,
list: [POST1, POST2]
})
deepStrictEqual(await promise2, [POST2])
let promise3 = posts.next()
deepStrictEqual(posts.get(), {
hasNext: false,
isLoading: false,
list: [POST1, POST2]
})
deepStrictEqual(await promise3, [])
})
test('is ready for double calls', async () => {
let posts = createPostsList(undefined, async () => {
return [[POST1], async () => [[POST2], undefined]]
})
posts.next()
await posts.loading
deepStrictEqual(posts.get(), {
hasNext: true,
isLoading: false,
list: [POST1]
})
let promise1 = posts.next()
let promise2 = posts.next()
deepStrictEqual(posts.get(), {
hasNext: true,
isLoading: true,
list: [POST1]
})
await posts.loading
deepStrictEqual(posts.get(), {
hasNext: false,
isLoading: false,
list: [POST1, POST2]
})
deepStrictEqual(await promise1, [POST2])
deepStrictEqual(await promise2, [POST2])
})
test('works with cached posts with next page loader', async () => {
let posts = createPostsList([POST1], async () => {
return [[POST2], undefined]
})
deepStrictEqual(posts.get(), {
hasNext: true,
isLoading: false,
list: [POST1]
})
posts.next()
deepStrictEqual(posts.get(), {
hasNext: true,
isLoading: true,
list: [POST1]
})
await posts.loading
deepStrictEqual(posts.get(), {
hasNext: false,
isLoading: false,
list: [POST1, POST2]
})
})