@vida-global/core 1.1.13 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,148 +0,0 @@
1
- const Git = require('../../lib/release/git');
2
- const TestHelpers = require('@vida-global/test-helpers');
3
- const utils = require('../../lib/release/utils');
4
- const readline = require('node:readline/promises');
5
-
6
-
7
- jest.mock('node:readline/promises', () => {
8
- return { createInterface: jest.fn(), question: jest.fn(), close: jest.fn() };
9
- });
10
-
11
- jest.mock('../../lib/release/git', () => {
12
- return {branches: jest.fn()};
13
- });
14
-
15
-
16
- beforeEach(() => {
17
- jest.resetAllMocks();
18
- readline.createInterface.mockImplementation(() => {
19
- return {question: readline.question, close: readline.close};
20
- });
21
- });
22
-
23
-
24
- describe('getCurrentVersion', () => {
25
- const major1 = TestHelpers.Faker.Math.randomNumber();
26
- const minor1 = TestHelpers.Faker.Math.randomNumber();
27
- const point1 = TestHelpers.Faker.Math.randomNumber();
28
- const diff1 = TestHelpers.Faker.Math.randomNumber();
29
- const diff2 = TestHelpers.Faker.Math.randomNumber();
30
-
31
- const b1 = `origin/release/${major1}/${minor1}/${point1}`;
32
-
33
- it ('sorts branches by primary version first', () => {
34
- const b2 = `origin/release/${major1 + diff1 }.${minor1 + diff2}.${point1 + diff1}`;
35
- const b3 = `origin/release/${major1 + diff1 + diff2}.${minor1 }.${point1}`;
36
- Git.branches.mockImplementation(() => [b1, b3, b2]);
37
- expect(utils.getCurrentVersion()).toEqual([major1+diff1+diff2, minor1, point1]);
38
- });
39
-
40
- it ('sorts branches by minor version second', () => {
41
- const b2 = `origin/release/${major1 + diff1}.${minor1 + diff1 + diff2}.${point1}`;
42
- const b3 = `origin/release/${major1 + diff1}.${minor1 + diff1 }.${point1 + diff1}`;
43
- Git.branches.mockImplementation(() => [b1, b3, b2]);
44
- expect(utils.getCurrentVersion()).toEqual([major1+diff1, minor1+diff1+diff2, point1]);
45
- });
46
-
47
- it ('sorts branches by point version last', () => {
48
- const b2 = `origin/release/${major1 + diff1}.${minor1 + diff1}.${point1 + diff1}`;
49
- const b3 = `origin/release/${major1 + diff1}.${minor1 + diff1}.${point1 + diff1 + diff2}`;
50
- Git.branches.mockImplementation(() => [b1, b3, b2]);
51
- expect(utils.getCurrentVersion()).toEqual([major1+diff1, minor1+diff1, point1+diff1+diff2]);
52
- });
53
-
54
- it ('ignores branches that don not fit the pattern', () => {
55
- const b2 = `origin/release/${major1 + diff1}.${minor1}.${point1}`;
56
- const b3 = `origin/release/${TestHelpers.Faker.Text.randomString()}`;
57
- const b4 = TestHelpers.Faker.Text.randomString();
58
- Git.branches.mockImplementation(() => [b1, b3, b2, b4]);
59
- expect(utils.getCurrentVersion()).toEqual([major1+diff1, minor1, point1]);
60
- });
61
-
62
- it ('defaults to 1.0.0', () => {
63
- const b2 = `origin/release/${TestHelpers.Faker.Text.randomString()}`;
64
- const b3 = `origin/release/${TestHelpers.Faker.Text.randomString()}`;
65
- const b4 = TestHelpers.Faker.Text.randomString();
66
- Git.branches.mockImplementation(() => [b3, b2, b4]);
67
- expect(utils.getCurrentVersion()).toEqual([1, 0, 0]);
68
- });
69
- });
70
-
71
-
72
- describe('getInput', () => {
73
- let question;
74
- let numOptions;
75
- let options;
76
- let answer;
77
- beforeEach(() => {
78
- question = TestHelpers.Faker.Text.randomSentence();
79
- numOptions = TestHelpers.Faker.Math.randomNumber(10);
80
- options = [...Array(numOptions)].map(() => TestHelpers.Faker.Text.randomString());
81
- answer = options[Math.floor(Math.random() * numOptions)];
82
- readline.question.mockImplementation(() => answer);
83
- });
84
-
85
- it ('asks the question and appends the options', async () => {
86
- await utils.getInput(question, options)
87
- expect(readline.question).toHaveBeenCalledWith(`${question} [${options.join(',')}] `);
88
- });
89
-
90
- it ('asks the question once if it gets a valid answer', async () => {
91
- const theAnswer = await utils.getInput(question, options)
92
- expect(readline.question).toHaveBeenCalledTimes(1);
93
- expect(theAnswer).toEqual(answer);
94
- });
95
-
96
- it ('closes the input after getting an answer', async () => {
97
- await utils.getInput(question, options)
98
- expect(readline.question).toHaveBeenCalledTimes(1);
99
- expect(readline.close).toHaveBeenCalledTimes(1);
100
- expect(readline.question).toHaveBeenCalledBefore(readline.close);
101
- });
102
-
103
- it ('repeats the question until it gets a valid answer', async () => {
104
- const numAttempts = TestHelpers.Faker.Math.randomNumber(10);
105
- let attemptNumber = 1;
106
-
107
- readline.question.mockImplementation(() => {
108
- if (attemptNumber == numAttempts) return answer;
109
- attemptNumber = attemptNumber + 1;
110
- return TestHelpers.Faker.Text.randomString();
111
- });
112
-
113
- const theAnswer = await utils.getInput(question, options)
114
- expect(readline.question).toHaveBeenCalledTimes(numAttempts);
115
- expect(readline.close).toHaveBeenCalledTimes(numAttempts);
116
- expect(theAnswer).toEqual(answer);
117
-
118
- for(let i=1; i <= numAttempts; i++){
119
- expect(readline.question).toHaveBeenNthCalledWith(i, `${question} [${options.join(',')}] `);
120
- }
121
- });
122
- });
123
-
124
-
125
- describe('confirmContinue', () => {
126
- let question;
127
- beforeEach(() => {
128
- question = TestHelpers.Faker.Text.randomSentence();
129
- });
130
-
131
- it ('asks the question with "y" or "n" as options', async () => {
132
- readline.question.mockImplementation(() => 'y');
133
- await utils.confirmContinue(question);
134
- expect(readline.question).toHaveBeenCalledWith(`${question} [y,n] `);
135
- });
136
-
137
- it ('returns true if the user answers "y"', async () => {
138
- readline.question.mockImplementation(() => 'y');
139
- const confirmed = await utils.confirmContinue(question);
140
- expect(confirmed).toBeTruthy();
141
- });
142
-
143
- it ('returns false if the user answers "n"', async () => {
144
- readline.question.mockImplementation(() => 'n');
145
- const confirmed = await utils.confirmContinue(question);
146
- expect(confirmed).toBeFalsy();
147
- });
148
- });