-
Notifications
You must be signed in to change notification settings - Fork 42
/
documents.e2e.ts
109 lines (92 loc) · 2.97 KB
/
documents.e2e.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
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { article, creativeWork } from '@stencila/schema'
import { Document, DocumentEvent } from '@stencila/stencila'
import { Client, connect, disconnect } from './client'
import {
close,
create,
dump,
load,
open,
subscribe,
unsubscribe,
} from './documents'
jest.setTimeout(10000)
const clientId = 'cl-document-tests'
let client: Client
beforeAll(async () => {
client = await connect(
'pr-document-tests',
clientId,
process.env.SERVER_URL ?? 'ws://127.0.0.1:9000'
)
})
afterAll(() => {
disconnect(client)
})
// Test of the basic document workflow of opening a document, subscribing
// to it, executing it, changing it, receiving events and then unsubscribing from it.
test('basic', async () => {
let document: Document
// Open a document
document = await open(client, 'fixtures/articles/code.md')
expect(document).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^do-[0-9a-zA-Z]{20}/),
name: 'code.md',
})
)
// Subscribe to updates to node values
const events: DocumentEvent[] = []
document = await subscribe(client, document.id, 'patched', (event) => {
expect(event.type).toBe('patched')
events.push(event)
})
expect(document).toEqual(
expect.objectContaining({
subscriptions: { patched: [clientId] },
})
)
// Unsubscribe from document
document = await unsubscribe(client, document.id, 'patched')
expect(document).toEqual(
expect.objectContaining({
subscriptions: {},
})
)
// Close the document
await close(client, document.id)
})
test('create', async () => {
let document = await create(client)
let json = await dump(client, document.id, 'json')
expect(json).toMatch(/^{"type":"Article"/)
document = await create(client, creativeWork({ content: ['Beep!'] }))
let md = await dump(client, document.id, 'md')
expect(md).toMatch(/^Beep!/)
document = await create(client, 'Boop!', 'md')
json = await dump(client, document.id, 'json')
expect(json).toMatch(/"content":\["Boop!"\]/)
})
test('load', async () => {
let document = await create(client)
let ok = await load(client, document.id, article({ content: [] }))
expect(ok).toBeTruthy()
let json = await dump(client, document.id, 'json')
expect(json).toMatch(/^{"type":"Article"/)
ok = await load(client, document.id, 'Hello *world*!', 'md')
expect(ok).toBeTruthy()
json = await dump(client, document.id, 'html')
expect(json).toMatch(
'<em itemtype="http://schema.stenci.la/Emphasis" itemscope><span>world</span>'
)
})
test('dump', async () => {
let document = await open(client, 'fixtures/nodes/creative-work.json')
let json = await dump(client, document.id, 'json')
expect(json).toMatch(/^{"type":"CreativeWork"/)
let md = await dump(client, document.id, 'md')
expect(md).toMatch(/^A fixture that is a creative work/)
let rpng = await dump(client, document.id, 'rpng', 'cc-1')
expect(rpng).toMatch(/^data:image\/png/)
})