/
prompt.test.ts
81 lines (60 loc) · 1.93 KB
/
prompt.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import {KEYS} from '../../constants';
import Prompt from '../Prompt';
it('calls handler on change value', () => {
const options = {max: 10, offset: -1};
const prompt = new Prompt();
const onChange = jest.fn();
prompt.enter(onChange, jest.fn(), jest.fn());
expect(onChange).toHaveBeenLastCalledWith('', options);
prompt.put('t');
expect(onChange).toHaveBeenLastCalledWith('t', options);
prompt.put('e');
expect(onChange).toHaveBeenLastCalledWith('te', options);
prompt.put('s');
expect(onChange).toHaveBeenLastCalledWith('tes', options);
prompt.put('t');
expect(onChange).toHaveBeenLastCalledWith('test', options);
expect(onChange).toHaveBeenCalledTimes(5);
});
it('calls handler on success prompt', () => {
const prompt = new Prompt();
const onSuccess = jest.fn();
prompt.enter(jest.fn(), onSuccess, jest.fn());
prompt.put('t');
prompt.put('e');
prompt.put('s');
prompt.put('t');
prompt.put(KEYS.ENTER);
expect(onSuccess).toHaveBeenCalledWith('test');
});
it('calls handler on cancel prompt', () => {
const prompt = new Prompt();
const onCancel = jest.fn();
prompt.enter(jest.fn(), jest.fn(), onCancel);
prompt.put('t');
prompt.put('e');
prompt.put('s');
prompt.put('t');
prompt.put(KEYS.ESCAPE);
expect(onCancel).toHaveBeenCalled();
});
it('clears the line when CONTROL_U is pressed', () => {
const prompt = new Prompt();
const onChange = jest.fn();
const options = {max: 10, offset: -1};
prompt.enter(onChange, jest.fn(), jest.fn());
prompt.put('t');
prompt.put('e');
prompt.put('s');
prompt.put('t');
expect(onChange).toHaveBeenLastCalledWith('test', options);
prompt.put(KEYS.CONTROL_U);
expect(onChange).toHaveBeenLastCalledWith('', options);
});