-
Notifications
You must be signed in to change notification settings - Fork 42
/
documents.test.ts
192 lines (175 loc) · 4.12 KB
/
documents.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import fs from 'fs'
import tmp from 'tmp'
import {
close,
create,
get,
load,
open,
subscribe,
unsubscribe,
write,
writeAs,
} from './documents'
import { DocumentEvent } from './types'
test('create', async () => {
expect(create()).toEqual(
expect.objectContaining({
temporary: true,
name: 'Unnamed',
})
)
expect(create('md')).toEqual(
expect.objectContaining({
temporary: true,
name: 'Unnamed',
format: expect.objectContaining({
name: 'md',
binary: false
}),
})
)
})
/**
* Test of a workflow involving creating a new document and then
* saving it as other files in same and other formats.
*/
test('workfow-create-write-as', async () => {
const doc = create('json');
expect(doc).toEqual(
expect.objectContaining({
temporary: true,
name: 'Unnamed',
})
)
const path1 = tmp.fileSync({ postfix: '.json' }).name
writeAs(doc.id, path1)
fs.existsSync(path1)
const path2 = tmp.fileSync({ postfix: '.markdown' }).name
writeAs(doc.id, path2, "md")
fs.existsSync(path2)
})
/**
* Test of a workflow involving opening and modifying a document
*
* Uses a JSON document as input so that this test is not dependant
* on having a converter plugin installed.
*/
test('workflow-open-modify', async () => {
const path = tmp.fileSync({ postfix: '.json' }).name
fs.writeFileSync(path, '{"type": "Article"}')
let events: DocumentEvent[] = []
// Open the document
const doc = open(path)
const docId = doc.id
expect(doc).toEqual(
expect.objectContaining({
format: expect.objectContaining({
name: 'json',
binary: false,
}),
temporary: false,
subscriptions: {},
})
)
// Subscribe an editor panel to some of the document's topics
subscribe(docId, ['removed', 'renamed', 'modified'], (_topic, event) =>
events.push(event)
)
expect(get(docId).subscriptions).toEqual({
removed: 1,
renamed: 1,
modified: 1,
})
// Subscribe a preview panel to the the `encoded:json` topic
subscribe(docId, ['encoded:json'], (_topic, event) => events.push(event))
expect(get(docId).subscriptions).toEqual({
removed: 1,
renamed: 1,
modified: 1,
'encoded:json': 1,
})
// Load some new content into the document (and wait a bit for events)
load(
docId,
`{
"type": "Article",
"content": [{
"type": "Paragraph",
"content": ["Some content"]
}]
}`
)
await new Promise((resolve) => setTimeout(resolve, 500))
expect(events).toEqual([
expect.objectContaining({
type: 'encoded',
format: expect.objectContaining({
name: "json",
binary: false
}),
}),
])
// Modify the file on disk (and wait a bit for events)
events = []
fs.writeFileSync(
path,
`{
"type": "Article",
"content": [{
"type": "Paragraph",
"content": ["Some new content"]
}]
}`
)
await new Promise((resolve) => setTimeout(resolve, 500))
expect(events).toEqual(
expect.arrayContaining([
expect.objectContaining({
type: 'modified',
content: expect.stringMatching(/Some new content/),
}),
expect.objectContaining({
type: 'encoded',
format: expect.objectContaining({
name: "json",
binary: false
}),
}),
])
)
// Write the document from here (note there should be no `modified` event)
events = []
write(
docId,
`{
"type": "Article",
"content": [{
"type": "Paragraph",
"content": ["Some newer content"]
}]
}`
)
await new Promise((resolve) => setTimeout(resolve, 500))
expect(events).toEqual(
expect.arrayContaining([
expect.objectContaining({
type: 'encoded',
format: expect.objectContaining({
name: "json",
binary: false
}),
}),
])
)
// Unsubscribe from `encoded:json` because say we closed the preview panel
unsubscribe(docId, ['encoded:json'])
expect(get(docId).subscriptions).toEqual({
removed: 1,
renamed: 1,
modified: 1,
})
// Close the document
close(docId)
fs.unlinkSync(path)
})