-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathprompts.test.ts
133 lines (119 loc) · 3.47 KB
/
prompts.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
import * as prompts from './prompts';
let getPromptQuestions: typeof prompts.getPromptQuestions;
let getPromptMessages: typeof prompts.getPromptMessages;
let getPromptSettings: typeof prompts.getPromptSettings;
let setPromptConfig: typeof prompts.setPromptConfig;
beforeEach(() => {
jest.resetModules();
getPromptSettings = require('./prompts').getPromptSettings;
getPromptMessages = require('./prompts').getPromptMessages;
getPromptQuestions = require('./prompts').getPromptQuestions;
setPromptConfig = require('./prompts').setPromptConfig;
});
describe('setPromptConfig', () => {
test('should cover questions when prompt config questions is plain object', () => {
const promptConfig = {
questions: {
type: {
description: 'input type',
},
},
};
setPromptConfig(promptConfig);
expect(getPromptQuestions()).toBe(promptConfig.questions);
});
test('should not set questions when prompt config questions is not a plain object', () => {
const initialQuestions = {...getPromptQuestions()};
setPromptConfig({
questions: null,
} as any);
expect(getPromptQuestions()).toEqual(initialQuestions);
setPromptConfig({
questions: 'questions',
} as any);
expect(getPromptQuestions()).toEqual(initialQuestions);
setPromptConfig({
questions: [1, 2, 3],
} as any);
expect(getPromptQuestions()).toEqual(initialQuestions);
});
test('should merge message when prompt config message is string', () => {
const initialMessages = {...getPromptMessages()};
const promptConfig = {
messages: {
emptyWarning: '(%s can not be empty)',
},
};
setPromptConfig(promptConfig);
expect(getPromptMessages()['emptyWarning']).toBe(
promptConfig.messages.emptyWarning
);
expect(getPromptMessages()['lowerLimitWarning']).toBe(
initialMessages['lowerLimitWarning']
);
});
test('should not merge message when prompt config message is not a string', () => {
const initialMessages = {...getPromptMessages()};
const promptConfig = {
messages: {
emptyWarning: '(%s can not be empty)',
min: function () {
return 'min:';
},
},
};
setPromptConfig(promptConfig as any);
expect(getPromptMessages()['emptyWarning']).toBe(
promptConfig.messages.emptyWarning
);
expect(getPromptMessages()['min']).toBe(initialMessages['min']);
});
test('should ignore non-essential message', () => {
const initialMessages = {...getPromptMessages()};
const promptConfig = {
messages: {
more: 'learn more',
},
};
setPromptConfig(promptConfig);
expect(getPromptMessages()).toEqual(initialMessages);
});
test('should fields be independent', () => {
const initialQuestions = {...getPromptQuestions()};
setPromptConfig({
messages: {
emptyWarning: '(%s can not be empty)',
},
});
expect(getPromptQuestions()).toEqual(initialQuestions);
const initialMessages = {...getPromptMessages()};
setPromptConfig({
questions: {
type: {
description: 'input type',
},
},
});
expect(getPromptMessages()).toEqual(initialMessages);
});
test('should settings scopeEnumSeparator be set when value is ",\\/"', () => {
setPromptConfig({
settings: {
scopeEnumSeparator: '/',
},
});
expect(getPromptSettings()).toEqual({
scopeEnumSeparator: '/',
});
const processExit = jest
.spyOn(process, 'exit')
.mockImplementation(() => undefined as never);
setPromptConfig({
settings: {
scopeEnumSeparator: '-',
},
});
expect(processExit).toHaveBeenCalledWith(1);
processExit.mockClear();
});
});