-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
189 lines (183 loc) · 4.62 KB
/
test.js
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
import test from 'ava'
import DecisionTree from '.'
const proficiency = {
key: 'proficiency',
title: 'What type of weapon are you most proficient with?',
options: [
{
key: 'swords',
label: 'Swords',
leadsTo: dt => {
if (dt.state.attribute === 'D') {
return 'thief'
} else {
return 'fighter'
}
}
},
{
key: 'bows',
label: 'Bows',
leadsTo: 'ranger'
}
],
children: [
{
key: 'fighter',
title: 'Fighter',
description: `
You are a strong fighter, using your sword to cut down those who
stand in your way!
`
},
{
key: 'thief',
title: 'Thief',
description: `
You are a dexterous thief, piercing enemies before they even
know what hit them.
`
},
{
key: 'ranger',
title: 'Ranger',
description: `
You are a skilled ranger, felling combatants with your arrows.
`
}
]
}
const mage = {
key: 'mage',
title: 'Mage',
description: `
You are a powerful mage, hurling fireballs at your foes!
`
}
const sorcerer = {
key: 'sorcerer',
title: 'Sorcerer',
description: `
You are an amazing sorcerer, dealing with magic through your natural
abilities rather than learning casting rituals.
`
}
const attribute = {
key: 'attribute',
title: 'What is your greatest attribute?',
options: [
{
key: 'S',
label: 'Strength',
leadsTo: 'proficiency'
},
{
key: 'I',
label: 'Intelligence',
leadsTo: 'spells'
},
{
key: 'D',
label: 'Dexterity',
leadsTo: 'proficiency'
},
{
key: 'C',
label: 'Charisma',
leadsTo: 'bard'
}
],
children: [
{
key: 'spells',
title: 'What are your preffered type of spells?',
options: [
{
key: 'damage',
label: 'Damage',
leadsTo: 'mage'
},
{
key: 'healing',
label: 'Healing',
leadsTo: 'cleric'
}
],
leadsTo: dt => {
const damage = dt.state.spells.includes('damage')
const healing = dt.state.spells.includes('healing')
if (damage && healing) {
return 'sorcerer'
}
},
children: [
mage,
{
key: 'cleric',
title: 'Cleric',
description: `
You are a knowledgeable cleric, saving your friends by casting
spells to heal them.
`
},
sorcerer
]
},
proficiency,
{
key: 'bard',
title: 'Bard',
description: `
You are a talented bard, inspiring your party with heroic ballads.
`
}
]
}
const tree = { key: 'start', children: [attribute] }
const fighterPath = ['start', 'attribute', 'proficiency', 'fighter']
test('can store a response and traverse a static lead', t => {
const decisionTree = new DecisionTree(tree)
decisionTree.next()
t.deepEqual(decisionTree.pathKeys(), ['start', 'attribute'])
const bard = tree.children[0].options[3].key
decisionTree.set(tree.children[0].key, bard)
t.is(decisionTree.state[tree.children[0].key], bard)
decisionTree.next()
t.deepEqual(decisionTree.pathKeys(), ['start', 'attribute', 'bard'])
})
test('can store state and traverse a functional lead', t => {
const decisionTree = new DecisionTree(tree)
decisionTree.next()
decisionTree.set('attribute', 'S').next()
decisionTree.set('proficiency', 'swords').next()
t.deepEqual(decisionTree.pathKeys(), fighterPath)
decisionTree.prev()
decisionTree.set('attribute', 'D').next()
const thiefPath = ['start', 'attribute', 'proficiency', 'thief']
t.deepEqual(decisionTree.pathKeys(), thiefPath)
})
test('can be instantiated with path and state', t => {
const path = [tree, attribute, proficiency]
const state = { attribute: 'S', proficiency: 'swords' }
const decisionTree = new DecisionTree(tree, path, state)
decisionTree.next()
t.deepEqual(decisionTree.pathKeys(), fighterPath)
})
test('can support multiple choice options where only one is selected', t => {
const decisionTree = new DecisionTree(tree)
decisionTree.next()
decisionTree.set('attribute', 'I')
decisionTree.next()
decisionTree.set('spells', ['damage'])
decisionTree.next()
t.is(decisionTree.current(), mage)
})
test('can support multiple choice options where multiple are selected', t => {
const decisionTree = new DecisionTree(tree)
decisionTree.next()
decisionTree.set('attribute', 'I')
decisionTree.next()
decisionTree.set('spells', ['damage', 'healing'])
decisionTree.next()
t.is(decisionTree.current(), sorcerer)
})