Skip to content

Commit

Permalink
Add v0-specific change descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 committed Nov 16, 2024
1 parent 48e970b commit 941d1f8
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { describe, expect, it, jest } from '@jest/globals';
import prompts from 'prompts';
import { _getQuestionsForPackage } from '../../changefile/promptForChange';
import { ChangeFilePromptOptions } from '../../types/ChangeFilePrompt';
import { getQuestionsForPackage } from '../../changefile/getQuestionsForPackage';
import { ChangeFilePromptOptions, ChangeTypeDescriptions } from '../../types/ChangeFilePrompt';
import { initMockLogs } from '../../__fixtures__/mockLogs';
import { makePackageInfos } from '../../__fixtures__/packageInfos';

/**
* This covers the first part of `promptForChange`: determining what questions to ask for each package.
*/
describe('promptForChange _getQuestionsForPackage', () => {
describe('getQuestionsForPackage', () => {
/** Package name used in the tests */
const pkg = 'foo';

/** Basic params for `_getQuestionsForPackage`, for a package named `foo` */
const defaultQuestionsParams: Parameters<typeof _getQuestionsForPackage>[0] = {
/** Basic params for `getQuestionsForPackage`, for a package named `foo` */
const defaultQuestionsParams: Parameters<typeof getQuestionsForPackage>[0] = {
pkg,
packageInfos: makePackageInfos({ [pkg]: {} }),
packageGroups: {},
Expand All @@ -24,14 +21,14 @@ describe('promptForChange _getQuestionsForPackage', () => {
const logs = initMockLogs();

it('works in basic case', () => {
const questions = _getQuestionsForPackage(defaultQuestionsParams);
const questions = getQuestionsForPackage(defaultQuestionsParams);
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: ' [1mPatch[22m - bug fixes; no API changes', value: 'patch' },
{ title: ' [1mMinor[22m - new feature; backwards-compatible API changes', value: 'minor' },
{ title: ' [1mNone[22m - this change does not affect the published package in any way', value: 'none' },
{ title: ' [1mMajor[22m - breaking changes; major feature', value: 'major' },
],
message: 'Change type',
name: 'type',
Expand All @@ -48,9 +45,29 @@ describe('promptForChange _getQuestionsForPackage', () => {
]);
});

it('uses different descriptions for v0 package', () => {
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({ [pkg]: { version: '0.1.0' } }),
});
expect(questions![0].choices).toEqual([
{
title:
' Patch - bug fixes; new features; backwards-compatible API changes (ok in patches for v0.x packages)',
value: 'patch',
},
{
title: ' Minor - breaking changes; major feature (ok in minor versions for v0.x packages)',
value: 'minor',
},
{ title: ' None - this change does not affect the published package in any way', value: 'none' },
{ title: ' Major - official release', value: 'major' },
]);
});

// 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 = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({ [pkg]: { combinedOptions: { disallowedChangeTypes: ['major'] } } }),
options: { type: 'major', message: '' },
Expand All @@ -60,7 +77,7 @@ describe('promptForChange _getQuestionsForPackage', () => {
});

it('errors if there are no valid change types for package', () => {
const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({
[pkg]: { combinedOptions: { disallowedChangeTypes: ['major', 'minor', 'patch', 'none'] } },
Expand All @@ -71,7 +88,7 @@ describe('promptForChange _getQuestionsForPackage', () => {
});

it('respects disallowedChangeTypes', () => {
const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({ [pkg]: { combinedOptions: { disallowedChangeTypes: ['major'] } } }),
});
Expand All @@ -80,7 +97,7 @@ describe('promptForChange _getQuestionsForPackage', () => {
});

it('allows prerelease change for package with prerelease version', () => {
const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({ [pkg]: { version: '1.0.0-beta.1' } }),
});
Expand All @@ -90,7 +107,7 @@ describe('promptForChange _getQuestionsForPackage', () => {

// this is a bit weird as well, but documenting current behavior
it('excludes prerelease if disallowed', () => {
const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({
[pkg]: { version: '1.0.0-beta.1', combinedOptions: { disallowedChangeTypes: ['prerelease'] } },
Expand All @@ -101,7 +118,7 @@ describe('promptForChange _getQuestionsForPackage', () => {
});

it('excludes the change type question when options.type is specified', () => {
const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
options: { type: 'patch', message: '' },
});
Expand All @@ -110,7 +127,7 @@ describe('promptForChange _getQuestionsForPackage', () => {
});

it('excludes the change type question with only one valid option', () => {
const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({
[pkg]: { combinedOptions: { disallowedChangeTypes: ['major', 'minor', 'none'] } },
Expand All @@ -121,7 +138,7 @@ describe('promptForChange _getQuestionsForPackage', () => {
});

it('excludes the change type question when prerelease is implicitly the only valid option', () => {
const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({
[pkg]: {
Expand All @@ -135,7 +152,7 @@ describe('promptForChange _getQuestionsForPackage', () => {
});

it('excludes the comment question when options.message is set', () => {
const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
options: { message: 'message' },
});
Expand All @@ -147,7 +164,7 @@ describe('promptForChange _getQuestionsForPackage', () => {
const customQuestions: prompts.PromptObject[] = [{ name: 'custom', message: 'custom prompt', type: 'text' }];
const changePrompt: ChangeFilePromptOptions['changePrompt'] = jest.fn(() => customQuestions);

const questions = _getQuestionsForPackage({
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({ [pkg]: { combinedOptions: { changeFilePrompt: { changePrompt } } } }),
});
Expand All @@ -163,10 +180,50 @@ describe('promptForChange _getQuestionsForPackage', () => {
);
});

it('uses options.changeTypeDescriptions if set', () => {
const changeTypeDescriptions: ChangeTypeDescriptions = {
major: 'exciting',
minor: { v0: 'exciting v0!', general: 'boring' },
premajor: 'almost exciting',
};
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({
[pkg]: { version: '1.0.0', combinedOptions: { changeFilePrompt: { changeTypeDescriptions } } },
}),
});

expect(questions![0].choices).toEqual([
{ title: ' Major - exciting', value: 'major' },
{ title: ' Minor - boring', value: 'minor' },
{ title: ' Premajor - almost exciting', value: 'premajor' },
]);
});

it('uses v0-specific options.changeTypeDescriptions if set', () => {
const changeTypeDescriptions: ChangeTypeDescriptions = {
major: 'exciting',
minor: { v0: 'exciting v0!', general: 'boring' },
premajor: 'almost exciting',
};
const questions = getQuestionsForPackage({
...defaultQuestionsParams,
packageInfos: makePackageInfos({
[pkg]: { version: '0.1.0', combinedOptions: { changeFilePrompt: { changeTypeDescriptions } } },
}),
});

expect(questions![0].choices).toEqual([
{ title: ' Major - exciting', value: 'major' },
{ title: ' Minor - exciting v0!', value: 'minor' },
{ title: ' Premajor - almost exciting', value: 'premajor' },
]);
});

it('does case-insensitive filtering on description suggestions', async () => {
const recentMessages = ['Foo', 'Bar', 'Baz'];
const recentMessageChoices = [{ title: 'Foo' }, { title: 'Bar' }, { title: 'Baz' }];
const questions = _getQuestionsForPackage({ ...defaultQuestionsParams, recentMessages });
const questions = getQuestionsForPackage({ ...defaultQuestionsParams, recentMessages });
expect(questions).toEqual([
expect.anything(),
expect.objectContaining({
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/changefile/promptForChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import prompts from 'prompts';
import {
promptForChange,
_getChangeFileInfoFromResponse,
_getQuestionsForPackage,
_promptForPackageChange,
} from '../../changefile/promptForChange';
import { ChangeFilePromptOptions } from '../../types/ChangeFilePrompt';
Expand Down
Loading

0 comments on commit 941d1f8

Please sign in to comment.