-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.test.js
More file actions
115 lines (97 loc) · 3.26 KB
/
util.test.js
File metadata and controls
115 lines (97 loc) · 3.26 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
const { expect } = require('chai')
const sinon = require('sinon')
const enquirer = require('enquirer')
const { ask, mergeObj } = require("../lib/util")
describe("ask function", () => {
let promptStub
beforeEach(() => {
promptStub = sinon.stub()
sinon.replaceGetter(enquirer, 'prompt', () => promptStub)
})
afterEach(() => {
sinon.restore()
})
it('should prompt the user with the given questions and suggestions', async () => {
const questions = [
['What is your name?', 'John Doe', true],
['What is your age?', '30', true]
]
promptStub.resolves({ '0': 'Alice', '1': '25' })
const answers = await ask(...questions)
expect(answers).to.deep.equal(['Alice', '25'])
})
it('should use the suggestion if no input is provided and it is not mandatory', async () => {
const questions = [
['What is your name?', 'John Doe', false],
['What is your age?', '30', false]
]
promptStub.resolves({ '0': '', '1': '' })
const answers = await ask(...questions)
expect(answers).to.deep.equal(['John Doe', '30'])
})
it('should pass required flag for mandatory questions without defaults', async () => {
const questions = [
['What is your name?', '', true],
['What is your age?', '30', true]
]
promptStub.resolves({ '0': 'Alice', '1': '25' })
await ask(...questions)
const promptArgs = promptStub.firstCall.args[0]
expect(promptArgs[0].required).to.equal(true)
expect(promptArgs[1].required).to.equal(false) // has default, so not required
})
})
describe("mergeObj function", () => {
it("should merge two objects recursively", () => {
const source = {
prop1: "value1",
prop2: {
nestedProp1: "nestedValue1",
nestedProp2: "nestedValue2"
},
prop3: ["item1", "item2"]
}
const target = {
prop1: "value2",
prop2: {
nestedProp1: "nestedValue3",
nestedProp3: "nestedValue4"
},
prop3: ["item3", "item4"]
}
const expected = {
prop1: "value2",
prop2: {
nestedProp1: "nestedValue3",
nestedProp2: "nestedValue2",
nestedProp3: "nestedValue4"
},
prop3: ["item1", "item2", "item3", "item4"]
}
const result = mergeObj(source, target)
expect(result).to.deep.equal(expected)
})
it("should handle empty objects and arrays", () => {
const source = {}
const target = []
const expected = []
const result = mergeObj(source, target)
expect(result).to.deep.equal(expected)
})
it("should handle null and undefined values", () => {
const source = {
prop1: null,
prop2: undefined
}
const target = {
prop1: "value1",
prop2: "value2"
}
const expected = {
prop1: "value1",
prop2: "value2"
}
const result = mergeObj(source, target)
expect(result).to.deep.equal(expected)
})
})