-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathGraph.upsert.spec.ts
More file actions
59 lines (56 loc) · 1.89 KB
/
Graph.upsert.spec.ts
File metadata and controls
59 lines (56 loc) · 1.89 KB
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
import { expect } from '@jest/globals';
import { scenario } from '@testduet/given-when-then';
import Graph from './Graph';
import './schemas/private/expectExtendValibot';
import './schemas/private/expectIsFrozen';
import type { Identifier } from './schemas/Identifier';
scenario('Graph.upsert()', bdd => {
bdd
.given(
'a Graph object',
() => new Graph<{ readonly '@id': Identifier; readonly name: string }>(() => () => request => request)
)
.when('act().upsert() is called', graph => graph.act(graph => graph.upsert({ '@id': '_:b1', name: 'John Doe' })))
.then('getState() should return the new node', graph =>
expect(graph.getState()).toEqual(
new Map(
Object.entries({
'_:b1': { '@id': '_:b1', name: 'John Doe' }
})
)
)
)
.when('act().upsert() is called with another node', graph =>
graph.act(graph => graph.upsert({ '@id': '_:b2', name: 'Mary Doe' }))
)
.then('getState() should return both nodes', graph =>
expect(graph.getState()).toEqual(
new Map(
Object.entries({
'_:b1': { '@id': '_:b1', name: 'John Doe' },
'_:b2': { '@id': '_:b2', name: 'Mary Doe' }
})
)
)
);
bdd
.given(
'a Graph object',
() => new Graph<{ readonly '@id': Identifier; readonly name: string }>(() => () => request => request)
)
.when('act().upsert() is called twice with node of same @id', graph => {
try {
graph.act(graph => graph.upsert({ '@id': '_:b1', name: 'John Doe' }, { '@id': '_:b1', name: 'Mary Doe' }));
} catch (error) {
return error;
}
return undefined;
})
.then('should throw', (_, error) => {
expect(() => {
if (error) {
throw error;
}
}).toThrow('Cannot upsert a node multiple times in a single transaction (@id = "_:b1")');
});
});