-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRecorder.spec.ts
More file actions
80 lines (64 loc) · 2.91 KB
/
Recorder.spec.ts
File metadata and controls
80 lines (64 loc) · 2.91 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import test from 'ava'
import { Recorder } from '../src/internals/Recorder'
import { RecordsSet } from '../src/internals/RecordsSet'
import { Substitute } from '../src/api/Substitute'
import { SubstituteNodeBase } from '../src/internals/SubstituteNodeBase'
const nodeFactory = (key: string) => {
const node = Substitute.for<SubstituteNodeBase>()
node.key.returns(key)
return node
}
const node = nodeFactory('node')
const otherNode = nodeFactory('otherNode')
const otherNodeDifferentInstance = nodeFactory('otherNode')
test('adds all records once only', t => {
const recorder = Recorder.withIdentityProperty<SubstituteNodeBase>('key')
recorder.addRecord(node)
recorder.addRecord(node)
recorder.addRecord(otherNode)
recorder.addRecord(otherNode)
recorder.addRecord(otherNodeDifferentInstance)
const allRecords = [...recorder.records]
t.deepEqual(allRecords, [node, otherNode, otherNodeDifferentInstance])
})
test('indexes all records correctly', t => {
const recorder = Recorder.withIdentityProperty<SubstituteNodeBase>('key')
recorder.addIndexedRecord(node)
recorder.addIndexedRecord(node)
recorder.addIndexedRecord(otherNode)
recorder.addIndexedRecord(otherNode)
recorder.addIndexedRecord(otherNodeDifferentInstance)
const allRecords = [...recorder.records]
t.deepEqual(allRecords, [node, otherNode, otherNodeDifferentInstance])
const nodeSet = recorder.indexedRecords.get(node.key)
t.true(nodeSet instanceof RecordsSet)
t.deepEqual([...nodeSet!], [node])
const otherNodeSet = recorder.indexedRecords.get(otherNode.key)
t.true(otherNodeSet instanceof RecordsSet)
t.deepEqual([...otherNodeSet!], [otherNode, otherNodeDifferentInstance])
})
test('returns all sibling nodes', t => {
const recorder = Recorder.withIdentityProperty<SubstituteNodeBase>('key')
recorder.addIndexedRecord(node)
recorder.addIndexedRecord(otherNode)
recorder.addIndexedRecord(otherNodeDifferentInstance)
const nodeSiblings = recorder.getSiblingsOf(node)
t.deepEqual([...nodeSiblings], [])
const otherNodeSiblings = recorder.getSiblingsOf(otherNode)
t.deepEqual([...otherNodeSiblings], [otherNodeDifferentInstance])
const otherNodeDifferentInstanceSiblings = recorder.getSiblingsOf(otherNodeDifferentInstance)
t.deepEqual([...otherNodeDifferentInstanceSiblings], [otherNode])
})
test('clears recorded nodes by a given filter function', t => {
const recorder = Recorder.withIdentityProperty<SubstituteNodeBase>('key')
recorder.addIndexedRecord(node)
recorder.addIndexedRecord(otherNode)
recorder.addIndexedRecord(otherNodeDifferentInstance)
recorder.clearRecords(n => n.key === otherNode.key)
t.deepEqual([...recorder.records], [node])
t.deepEqual([...recorder.indexedRecords.get(node.key)!], [node])
t.is(recorder.indexedRecords.get(otherNode.key), undefined)
recorder.clearRecords(_ => true)
t.deepEqual([...recorder.records], [])
t.deepEqual(recorder.indexedRecords.get(node.key), undefined)
})