Skip to content

Commit 0abfcd1

Browse files
committed
@W-18670301 - added prettier and tuned tests
1 parent 3413187 commit 0abfcd1

File tree

4 files changed

+117
-7
lines changed

4 files changed

+117
-7
lines changed

package-lock.json

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"dependencies": {
4242
"@babel/parser": "^7.27.5",
4343
"@babel/traverse": "^7.27.4",
44-
"node-fetch": "^2.6.9"
44+
"node-fetch": "^2.6.9",
45+
"prettier": "^3.6.2"
4546
}
4647
}

packages/pwa-kit-create-app/scripts/trim-extensions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const traverse = require('@babel/traverse').default
1111
const generate = require('@babel/generator').default
1212
const path = require('path')
1313
const pluginConfig = require('../assets/plugin-config')
14+
const { execSync } = require('child_process')
1415

1516
const removeComponentCandidates = [] // List of files that are candidates for removal, as a result of trimming.
1617
const SEPARATOR = path.sep // Use OS-specific path separator
@@ -218,6 +219,8 @@ function processFile(filePath, plugins) {
218219
}).code
219220
// Replace the original file with the trimmed version
220221
fs.writeFileSync(filePath, output)
222+
// prettify the file
223+
execSync(`npx prettier --write ${filePath}`)
221224
console.log(`Updated file ${filePath}`)
222225
} catch (e) {
223226
console.error(`Error updating file ${filePath}: ${e.message}`)

packages/pwa-kit-create-app/scripts/trim-extensions.test.js

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
/* eslint-disable @typescript-eslint/no-var-requires */
88
const fs = require('fs')
9+
const { execSync } = require('child_process')
910

1011
jest.mock('../assets/plugin-config', () => ({
1112
plugins: {
@@ -19,6 +20,37 @@ jest.mock('../assets/plugin-config', () => ({
1920
}))
2021

2122
jest.mock('fs')
23+
jest.mock('child_process')
24+
25+
// custom matcher to compare strings line by line with trimming
26+
expect.extend({
27+
toEqualTrimmedLines(received, expected) {
28+
const clean = str =>
29+
str
30+
.split('\n')
31+
.map(line => line.trim()) // Trim each line
32+
.filter(line => line.length > 0) // Optional: remove empty lines
33+
34+
const receivedLines = clean(received)
35+
const expectedLines = clean(expected)
36+
37+
const pass = this.equals(receivedLines, expectedLines)
38+
39+
if (pass) {
40+
return {
41+
pass: true,
42+
message: () =>
43+
`✅ Expected strings not to match line by line (but they did).\n\nExpected: ${this.utils.printExpected(expectedLines)}\nReceived: ${this.utils.printReceived(receivedLines)}`,
44+
};
45+
} else {
46+
return {
47+
pass: false,
48+
message: () =>
49+
`❌ Expected strings to match line by line (with trimming).\n\nExpected: ${this.utils.printExpected(expectedLines)}\nReceived: ${this.utils.printReceived(receivedLines)}`,
50+
};
51+
}
52+
},
53+
})
2254

2355
const trimExtensions = require('./trim-extensions')
2456

@@ -47,6 +79,7 @@ describe('trim-extensions', () => {
4779
}
4880
})
4981
fs.unlinkSync.mockReturnValue(true)
82+
execSync.mockReturnValue(true)
5083
})
5184

5285
it('handles OR operator correctly', () => {
@@ -62,8 +95,9 @@ describe('trim-extensions', () => {
6295
trimExtensions('/mock/dir', {SFDC_EXT_featureA: true, SFDC_EXT_featureB: false})
6396
expect(fs.writeFileSync).toHaveBeenCalledWith(
6497
expect.any(String),
65-
"const feature = 'Feature Enabled';"
98+
expect.toEqualTrimmedLines("const feature = 'Feature Enabled';")
6699
)
100+
expect(execSync).toHaveBeenCalledWith('npx prettier --write /mock/dir/src/components/featureComponent.jsx')
67101
})
68102

69103
it('handles variable declarations correctly', () => {
@@ -82,12 +116,13 @@ describe('trim-extensions', () => {
82116

83117
expect(fs.writeFileSync).toHaveBeenCalledWith(
84118
expect.any(String),
85-
"const featureAFunc = () => 'Feature A';"
119+
expect.toEqualTrimmedLines("const featureAFunc = () => 'Feature A';")
86120
)
87121
expect(fs.writeFileSync).toHaveBeenCalledWith(
88122
expect.any(String),
89123
expect.not.stringContaining("const featureBFunc = () => 'Feature B';")
90124
)
125+
expect(execSync).toHaveBeenCalledWith('npx prettier --write /mock/dir/src/components/featureComponent.jsx')
91126
})
92127

93128
it('handles variable with ternary expressions correctly', () => {
@@ -104,12 +139,13 @@ describe('trim-extensions', () => {
104139

105140
expect(fs.writeFileSync).toHaveBeenCalledWith(
106141
expect.any(String),
107-
'const showFeature = Feature_A;'
142+
expect.toEqualTrimmedLines("const showFeature = Feature_A;")
108143
)
109144
expect(fs.writeFileSync).toHaveBeenCalledWith(
110145
expect.any(String),
111146
expect.not.stringContaining('const showFeature = Feature_B')
112147
)
148+
expect(execSync).toHaveBeenCalledWith('npx prettier --write /mock/dir/src/components/featureComponent.jsx')
113149
})
114150

115151
it('handles return with ternary expressions correctly', () => {
@@ -122,10 +158,56 @@ describe('trim-extensions', () => {
122158

123159
trimExtensions('/mock/dir', {SFDC_EXT_featureA: true})
124160

161+
const expected = `
162+
function test() {
163+
return Feature_A;
164+
}
165+
`
125166
expect(fs.writeFileSync).toHaveBeenCalledWith(
126167
expect.any(String),
127-
expect.stringContaining('return Feature_A')
168+
expect.toEqualTrimmedLines(expected)
128169
)
170+
expect(execSync).toHaveBeenCalledWith('npx prettier --write /mock/dir/src/components/featureComponent.jsx')
171+
})
172+
173+
it('handles PropTypes declarations correctly', () => {
174+
const code = `
175+
MyClass.PropTypes = {
176+
name: PropTypes.string,
177+
description: PropTypes.string
178+
};
179+
SFDC_EXT_featureA && (MyClass.PropType = {
180+
...MyClass.PropType,
181+
featureAProp: PropTypes.string
182+
});
183+
SFDC_EXT_featureB && (MyClass.PropType = {
184+
...MyClass.PropType,
185+
featureBProp: PropTypes.string
186+
});
187+
`
188+
fs.readFileSync.mockReturnValue(code)
189+
190+
trimExtensions('/mock/dir', {SFDC_EXT_featureA: true})
191+
192+
const expected = `
193+
MyClass.PropTypes = {
194+
name: PropTypes.string,
195+
description: PropTypes.string
196+
};
197+
MyClass.PropType = {
198+
...MyClass.PropType,
199+
featureAProp: PropTypes.string
200+
};
201+
`
202+
expect(fs.writeFileSync).toHaveBeenCalledWith(
203+
expect.any(String),
204+
expect.toEqualTrimmedLines(expected)
205+
)
206+
expect(fs.writeFileSync).not.toHaveBeenCalledWith(
207+
expect.any(String),
208+
expect.stringContaining('featureBProp: PropTypes.string')
209+
)
210+
expect(execSync).toHaveBeenCalledWith('npx prettier --write /mock/dir/src/components/featureComponent.jsx')
129211
})
130212

131213
it('handles JSX elements in return statements correctly', () => {
@@ -143,14 +225,23 @@ describe('trim-extensions', () => {
143225

144226
trimExtensions('/mock/dir', {SFDC_EXT_featureA: true, SFDC_EXT_featureB: false})
145227

228+
const expected = `
229+
function test() {
230+
return (
231+
<div>
232+
<ComponentA />
233+
</div>);
234+
}
235+
`
146236
expect(fs.writeFileSync).toHaveBeenCalledWith(
147237
expect.any(String),
148-
expect.stringContaining('<ComponentA />')
238+
expect.toEqualTrimmedLines(expected)
149239
)
150240
expect(fs.writeFileSync).not.toHaveBeenCalledWith(
151241
expect.any(String),
152242
expect.stringContaining('<ComponentB />')
153243
)
244+
expect(execSync).toHaveBeenCalledWith('npx prettier --write /mock/dir/src/components/featureComponent.jsx')
154245
})
155246

156247
it('does not remove referenced imports', () => {

0 commit comments

Comments
 (0)