generated from bennycode/ts-node-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWizardy.test.ts
201 lines (173 loc) · 6.28 KB
/
Wizardy.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
192
193
194
195
196
197
198
199
200
201
import {Prompt} from './Prompt';
import {Wizardy} from './Wizardy';
describe('Wizardy', () => {
describe('constructor', () => {
it('supports IDs for later identification', () => {
const firstWizard = new Wizardy('Account Creation');
const secondWizard = new Wizardy('Hardware Setup');
expect(firstWizard.id).not.toBe(secondWizard.id);
});
});
describe('TOPIC', () => {
it('publishes all answers when a questionnaire has been completed', done => {
const questionnaire: Prompt<string | number>[] = [
{
answerKey: 'category',
answerValue: (answer: string): string => {
const menu = ['burger', 'pizza', 'veggie burger'];
answer = answer.toLowerCase();
if (menu.includes(answer)) {
return answer;
} else {
throw Error(
`Sorry, we only offer: ${menu
.map(category => category.charAt(0).toUpperCase() + category.substr(1))
.join(', ')}.`
);
}
},
question: 'What kind of food do you want to order?',
response: 'Good choice!',
},
{
answerKey: 'quantity',
answerValue: (answer: string): number => {
try {
return parseInt(answer.match(/(\d+)/)![0], 10);
} catch (error) {
throw Error('Please write down the amount as number.');
}
},
question: `How much do you want?`,
response: 'Thanks. We will prepare your order.',
},
];
const wizard = new Wizardy();
wizard.on(Wizardy.TOPIC.END, answers => {
expect(answers).toEqual({
category: 'pizza',
quantity: 7,
});
done();
});
wizard.addQuestions(questionnaire);
expect(wizard.ask()).toBe('What kind of food do you want to order?');
expect(wizard.answer('Fajitas')).toBe('Sorry, we only offer: Burger, Pizza, Veggie burger.');
expect(wizard.ask()).toBe('What kind of food do you want to order?');
expect(wizard.answer('Pizza')).toBe('Good choice!');
expect(wizard.ask()).toBe('How much do you want?');
expect(wizard.answer('Seven')).toBe('Please write down the amount as number.');
expect(wizard.ask()).toBe('How much do you want?');
expect(wizard.answer('I would like to have 7.')).toBe('Thanks. We will prepare your order.');
});
});
describe('answer', () => {
it('triggers the answer value validation', done => {
const questionnaire: Prompt<string>[] = [
{
answerKey: 'firstName',
answerValue: (firstName: string): string => firstName,
question: `What's your first name?`,
response: 'Ok.',
},
{
answerKey: 'lastName',
answerValue: (lastName: string, answers: Record<string, string>): string => {
expect(lastName).withContext('Check current answer value').toBe('Neugebauer');
expect(answers['firstName']).withContext('Receive all previous answers').toBe('Benny');
done();
return lastName;
},
question: `What's your last name?`,
response: 'Nice to meet you.',
},
];
const wizard = new Wizardy();
wizard.addQuestions(questionnaire);
wizard.ask();
wizard.answer('Benny');
wizard.ask();
wizard.answer('Neugebauer');
});
it('catches unknown errors', () => {
const question = {
answerKey: 'firstName',
answerValue: (): string => {
throw 'not an error instance';
},
question: `What's your first name?`,
response: 'Ok.',
};
const wizard = new Wizardy();
wizard.addQuestions([question]);
const errorMessage = wizard.answer('Benny');
expect(errorMessage).toBe('Unknown error without an error message.');
});
});
describe('step', () => {
it('returns the progress of the questionnaire', () => {
const questionnaire: Prompt<string | number>[] = [
{
answerKey: 'item',
answerValue: input => input.trim(),
question: `What would you like to order?`,
response: () => `Lucky you! We have '${wizard.answers.item}' in stock.`,
},
{
answerKey: 'quantity',
answerValue: input => parseInt(input, 10),
question: () => `How many '${wizard.answers.item}' do you want to buy?`,
response: () =>
`Great, we will prepare your order and deliver ${wizard.answers.quantity} ${wizard.answers.item} as soon as possible.`,
},
];
const wizard = new Wizardy();
wizard.addQuestions(questionnaire);
expect(wizard.step).toBe(0);
wizard.ask();
wizard.answer('iPhones');
expect(wizard.step).toBe(1);
wizard.ask();
wizard.answer('13');
expect(wizard.step).toBe(2);
expect(() => {
wizard.ask();
}).toThrow();
wizard.reset();
expect(wizard.step).toBe(0);
});
});
describe('inConversation', () => {
it('shows if the wizard has a running conversation with a user', done => {
const questionnaire: Prompt<string | number>[] = [
{
answerKey: 'item',
answerValue: input => input.trim(),
question: `What would you like to order?`,
response: () => `Lucky you! We have '${wizard.answers.item}' in stock.`,
},
{
answerKey: 'quantity',
answerValue: input => parseInt(input, 10),
question: () => `How many '${wizard.answers.item}' do you want to buy?`,
response: () =>
`Great, we will prepare your order and deliver ${wizard.answers.quantity} ${wizard.answers.item} as soon as possible.`,
},
];
const wizard = new Wizardy();
wizard.on(Wizardy.TOPIC.END, () => {
expect(wizard.inConversation).toBe(false);
done();
});
wizard.addQuestions(questionnaire);
expect(wizard.inConversation).toBe(false);
wizard.ask();
expect(wizard.inConversation).toBe(true);
wizard.answer('iPhones');
expect(wizard.inConversation).toBe(true);
wizard.ask();
expect(wizard.inConversation).toBe(true);
wizard.answer('13');
});
});
});