-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathvalidate-generated-project.test.js
More file actions
153 lines (124 loc) · 5.55 KB
/
validate-generated-project.test.js
File metadata and controls
153 lines (124 loc) · 5.55 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
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const fs = require('fs')
const path = require('path')
// Mock the dependencies
jest.mock('fs')
// Mocking the config.js file to allow testing with smaller arrays of expected artifacts
jest.mock('../config.js', () => ({
GENERATED_PROJECTS_DIR: '../generated-projects',
EXPECTED_GENERATED_ARTIFACTS: {
'retail-app-demo': ['package.json', 'node_modules', 'config'],
'retail-app-ext': ['package.json', 'node_modules', 'overrides']
}
}))
jest.mock('./utils.js', () => ({
diffArrays: jest.fn()
}))
// Import the functions to test
const {diffArrays} = require('./utils.js')
const {validateGeneratedArtifacts} = require('./validate-generated-project.js')
describe('validateGeneratedArtifacts', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('resolves when all expected artifacts are present', async () => {
const project = 'retail-app-demo'
const expectedArtifacts = ['package.json', 'node_modules', 'config']
const actualArtifacts = ['package.json', 'node_modules', 'config', 'extra-file']
fs.readdirSync.mockReturnValue(actualArtifacts)
diffArrays.mockReturnValue([])
const result = await validateGeneratedArtifacts(project)
expect(fs.readdirSync).toHaveBeenCalledWith(
// path.sep is used to handle the platform-specific path separator. (Windows uses \ and other platforms use /)
expect.stringContaining(`generated-projects${path.sep}${project}`)
)
expect(diffArrays).toHaveBeenCalledWith(expectedArtifacts, actualArtifacts)
expect(result).toBe(`Successfully validated generated artifacts for: ${project} `)
})
test('rejects when artifacts are missing', async () => {
const project = 'retail-app-demo'
const actualArtifacts = ['package.json', 'node_modules']
const missingArtifacts = ['config']
fs.readdirSync.mockReturnValue(actualArtifacts)
diffArrays.mockReturnValue(missingArtifacts)
await expect(validateGeneratedArtifacts(project)).rejects.toBe(
`Generated project (${project}) is missing one or more artifacts: ${missingArtifacts}`
)
})
test('rejects when project directory does not exist', async () => {
const project = 'non-existent-project'
const error = new Error('ENOENT: no such file or directory')
fs.readdirSync.mockImplementation(() => {
throw error
})
await expect(validateGeneratedArtifacts(project)).rejects.toBe(
`Generated project (${project}) is missing one or more artifacts: ${error}`
)
})
test('handles project with no expected artifacts', async () => {
const project = 'unknown-project'
const actualArtifacts = ['some-file']
fs.readdirSync.mockReturnValue(actualArtifacts)
diffArrays.mockReturnValue([])
const result = await validateGeneratedArtifacts(project)
expect(diffArrays).toHaveBeenCalledWith([], actualArtifacts)
expect(result).toBe(`Successfully validated generated artifacts for: ${project} `)
})
})
// Since it requires files at runtime, we'll test the key validation logic
describe('validateExtensibilityConfig validation logic', () => {
test('validates Object.hasOwn usage for extensibility config', () => {
// Test the core validation logic that was fixed
const validConfig = {
ccExtensibility: {
extends: '@salesforce/retail-react-app',
overridesDir: 'overrides'
}
}
const invalidConfigMissingProperty = {
ccExtensibility: {
extends: '@salesforce/retail-react-app'
// missing overridesDir
}
}
const invalidConfigWrongExtends = {
ccExtensibility: {
extends: '@wrong/package',
overridesDir: 'overrides'
}
}
expect(Object.hasOwn(validConfig, 'ccExtensibility')).toBe(true)
expect(Object.hasOwn(validConfig.ccExtensibility, 'extends')).toBe(true)
expect(Object.hasOwn(validConfig.ccExtensibility, 'overridesDir')).toBe(true)
expect(Object.hasOwn(invalidConfigMissingProperty.ccExtensibility, 'overridesDir')).toBe(
false
)
const isValidConfig = (pkg) => {
return (
Object.hasOwn(pkg, 'ccExtensibility') &&
Object.hasOwn(pkg.ccExtensibility, 'extends') &&
Object.hasOwn(pkg.ccExtensibility, 'overridesDir') &&
pkg.ccExtensibility.extends === '@salesforce/retail-react-app' &&
pkg.ccExtensibility.overridesDir === 'overrides'
)
}
expect(isValidConfig(validConfig)).toBe(true)
expect(isValidConfig(invalidConfigMissingProperty)).toBe(false)
expect(isValidConfig(invalidConfigWrongExtends)).toBe(false)
})
test('validates template version matching logic', () => {
const pkg = {version: '1.0.0'}
const validateVersion = (pkg, templateVersion) => {
return !templateVersion || pkg.version === templateVersion
}
expect(validateVersion(pkg, undefined)).toBe(true)
expect(validateVersion(pkg, null)).toBe(true)
expect(validateVersion(pkg, '1.0.0')).toBe(true)
expect(validateVersion(pkg, '2.0.0')).toBe(false)
})
})