-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathgetQuestionsForPackage.test.ts
More file actions
194 lines (172 loc) · 7.77 KB
/
getQuestionsForPackage.test.ts
File metadata and controls
194 lines (172 loc) · 7.77 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
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
import { describe, expect, it, jest } from '@jest/globals';
import type prompts from 'prompts';
import { getQuestionsForPackage } from '../../changefile/getQuestionsForPackage';
import type { ChangeFilePromptOptions } from '../../types/ChangeFilePrompt';
import { initMockLogs } from '../../__fixtures__/mockLogs';
import { makePackageInfos, type PartialPackageInfo } from '../../__fixtures__/packageInfos';
import type { ChangeType } from '../../types/ChangeInfo';
import { getParsedOptions } from '../../options/getOptions';
import type { PackageGroups } from '../../types/PackageInfo';
type GetQuestionsParams = Parameters<typeof getQuestionsForPackage>[0];
/**
* This covers the first part of `promptForChange`: determining what questions to ask for each package.
*/
describe('getQuestionsForPackage', () => {
/** Package name used in the tests */
const pkg = 'foo';
const logs = initMockLogs();
function getQuestionsWrapper(
params: {
options?: Partial<GetQuestionsParams['options']>;
packageInfo?: PartialPackageInfo;
// slight difference from real scenario: pass groups directly, not derived from options
packageGroups?: PackageGroups;
recentMessages?: string[];
} = {}
) {
const { options: repoOptions = {}, packageInfo = {}, packageGroups = {}, recentMessages = ['message'] } = params;
const packageInfos = makePackageInfos({ [pkg]: packageInfo });
// fill in default options
const { options } = getParsedOptions({ cwd: '', argv: [], env: {}, testRepoOptions: repoOptions });
return getQuestionsForPackage({ pkg, packageInfos, packageGroups, options, recentMessages });
}
it('works in basic case', () => {
const questions = getQuestionsWrapper();
expect(questions).toEqual([
{
choices: [
{ title: expect.stringContaining('Patch'), value: 'patch' },
{ title: expect.stringContaining('Minor'), value: 'minor' },
{ title: expect.stringContaining('None'), value: 'none' },
{ title: expect.stringContaining('Major'), value: 'major' },
{ title: expect.stringContaining('Prepatch'), value: 'prepatch' },
{ title: expect.stringContaining('Preminor'), value: 'preminor' },
{ title: expect.stringContaining('Premajor'), value: 'premajor' },
],
message: 'Change type',
name: 'type',
type: 'select',
},
{
choices: [{ title: 'message' }],
message: 'Describe changes (type or choose one)',
name: 'comment',
onState: expect.any(Function),
suggest: expect.any(Function),
type: 'autocomplete',
},
]);
});
// it's somewhat debatable if this is correct (maybe --type should be the override for disallowedChangeTypes?)
it('errors if options.type is disallowed', () => {
const questions = getQuestionsWrapper({
packageInfo: { beachball: { disallowedChangeTypes: ['major'] } },
options: { type: 'major', message: '' },
});
expect(questions).toBeUndefined();
expect(logs.mocks.error).toHaveBeenCalledWith('Change type "major" is not allowed for package "foo"');
});
it('errors if there are no valid change types for package', () => {
const questions = getQuestionsWrapper({
packageInfo: {
beachball: { disallowedChangeTypes: ['major', 'minor', 'patch', 'none', 'premajor', 'preminor', 'prepatch'] },
},
});
expect(questions).toBeUndefined();
expect(logs.mocks.error).toHaveBeenCalledWith('No valid change types available for package "foo"');
});
it('respects disallowedChangeTypes', () => {
const questions = getQuestionsWrapper({
packageInfo: { beachball: { disallowedChangeTypes: ['major'] } },
});
const choices = (questions![0].choices as prompts.Choice[]).map(c => c.value as ChangeType);
expect(choices).toEqual(['patch', 'minor', 'none', 'prepatch', 'preminor', 'premajor']);
});
it('excludes prerelease bump choices if disallowed', () => {
const questions = getQuestionsWrapper({
packageInfo: { beachball: { disallowedChangeTypes: ['prepatch', 'preminor', 'premajor'] } },
});
const choices = (questions![0].choices as prompts.Choice[]).map(c => c.value as ChangeType);
expect(choices).toEqual(['patch', 'minor', 'none', 'major']);
});
it('allows prerelease change for package with prerelease version', () => {
const questions = getQuestionsWrapper({
packageInfo: { version: '1.0.0-beta.1' },
});
const choices = (questions![0].choices as prompts.Choice[]).map(c => c.value as ChangeType);
expect(choices).toEqual(['prerelease', 'patch', 'minor', 'none', 'major', 'prepatch', 'preminor', 'premajor']);
});
// this is a bit weird as well, but documenting current behavior
it('excludes prerelease if disallowed', () => {
const questions = getQuestionsWrapper({
packageInfo: { version: '1.0.0-beta.1', beachball: { disallowedChangeTypes: ['prerelease'] } },
});
const choices = (questions![0].choices as prompts.Choice[]).map(c => c.value as ChangeType);
expect(choices).toEqual(['patch', 'minor', 'none', 'major', 'prepatch', 'preminor', 'premajor']);
});
it('excludes the change type question when options.type is specified', () => {
const questions = getQuestionsWrapper({
options: { type: 'patch', message: '' },
});
expect(questions).toHaveLength(1);
expect(questions![0].name).toBe('comment');
});
it('excludes the change type question with only one valid option', () => {
const questions = getQuestionsWrapper({
packageInfo: {
beachball: { disallowedChangeTypes: ['major', 'minor', 'none', 'premajor', 'preminor', 'prepatch'] },
},
});
expect(questions).toHaveLength(1);
expect(questions![0].name).toBe('comment');
});
it('excludes the change type question when prerelease is implicitly the only valid option', () => {
const questions = getQuestionsWrapper({
packageInfo: {
version: '1.0.0-beta.1',
beachball: { disallowedChangeTypes: ['major', 'minor', 'patch', 'none', 'premajor', 'preminor', 'prepatch'] },
},
});
expect(questions).toHaveLength(1);
expect(questions![0].name).toBe('comment');
});
it('excludes the comment question when options.message is set', () => {
const questions = getQuestionsWrapper({
options: { message: 'message' },
});
expect(questions).toHaveLength(1);
expect(questions![0].name).toBe('type');
});
it('uses options.changeFilePrompt if set', () => {
const customQuestions: prompts.PromptObject[] = [{ name: 'custom', message: 'custom prompt', type: 'text' }];
const changePrompt: ChangeFilePromptOptions['changePrompt'] = jest.fn(() => customQuestions);
const questions = getQuestionsWrapper({
options: { changeFilePrompt: { changePrompt } },
});
expect(questions).toEqual(customQuestions);
// includes the original prompts as parameters
expect(changePrompt).toHaveBeenLastCalledWith(
{
changeType: expect.objectContaining({ name: 'type' }),
description: expect.objectContaining({ name: 'comment' }),
},
pkg
);
});
it('does case-insensitive filtering on description suggestions', async () => {
const recentMessages = ['Foo', 'Bar', 'Baz'];
const recentMessageChoices = [{ title: 'Foo' }, { title: 'Bar' }, { title: 'Baz' }];
const questions = getQuestionsWrapper({ recentMessages });
expect(questions).toEqual([
expect.anything(),
expect.objectContaining({
name: 'comment',
choices: recentMessageChoices,
suggest: expect.any(Function),
}),
]);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const suggestions = await questions![1].suggest!('ba', recentMessageChoices);
expect(suggestions).toEqual([{ title: 'Bar' }, { title: 'Baz' }]);
});
});