Skip to content

Commit 737ffe4

Browse files
committed
feat: add rest element example test case
1 parent 3783df3 commit 737ffe4

3 files changed

Lines changed: 78 additions & 41 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const MyComp = ({foo, ...props}) => {
2+
console.log(props.bar)
3+
}
4+
5+
MyComp.defaultProps = { foo: "hello", bar: "bye", test: 2 };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const MyComp = ({foo = "hello", ...props}) => {
2+
props = {
3+
...props,
4+
bar: typeof props.bar === "undefined" ? "bye" : props.bar,
5+
test: typeof props.test === "undefined" ? 2 : props.test
6+
};
7+
8+
console.log(props.bar)
9+
}

codemods/react/19/replace-default-props/test/test.ts

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ const buildApi = (parser: string | undefined): API => ({
1010
jscodeshift: parser ? jscodeshift.withParser(parser) : jscodeshift,
1111
stats: () => {
1212
console.error(
13-
"The stats function was called, which is not supported on purpose",
13+
"The stats function was called, which is not supported on purpose"
1414
);
1515
},
1616
report: () => {
1717
console.error(
18-
"The report function was called, which is not supported on purpose",
18+
"The report function was called, which is not supported on purpose"
1919
);
2020
},
2121
});
@@ -24,24 +24,24 @@ describe("react/19/replace-default-props", () => {
2424
it("should correctly transform single default prop", async () => {
2525
const INPUT = await readFile(
2626
join(__dirname, "..", "__testfixtures__/single-default-prop.input.tsx"),
27-
"utf-8",
27+
"utf-8"
2828
);
2929
const OUTPUT = await readFile(
3030
join(__dirname, "..", "__testfixtures__/single-default-prop.output.tsx"),
31-
"utf-8",
31+
"utf-8"
3232
);
3333

3434
const actualOutput = transform(
3535
{
3636
path: "index.js",
3737
source: INPUT,
3838
},
39-
buildApi("tsx"),
39+
buildApi("tsx")
4040
);
4141

4242
assert.deepEqual(
4343
actualOutput?.replace(/W/gm, ""),
44-
OUTPUT.replace(/W/gm, ""),
44+
OUTPUT.replace(/W/gm, "")
4545
);
4646
});
4747

@@ -50,102 +50,102 @@ describe("react/19/replace-default-props", () => {
5050
join(
5151
__dirname,
5252
"..",
53-
"__testfixtures__/multiple-default-props.input.tsx",
53+
"__testfixtures__/multiple-default-props.input.tsx"
5454
),
55-
"utf-8",
55+
"utf-8"
5656
);
5757
const OUTPUT = await readFile(
5858
join(
5959
__dirname,
6060
"..",
61-
"__testfixtures__/multiple-default-props.output.tsx",
61+
"__testfixtures__/multiple-default-props.output.tsx"
6262
),
63-
"utf-8",
63+
"utf-8"
6464
);
6565

6666
const actualOutput = transform(
6767
{
6868
path: "index.js",
6969
source: INPUT,
7070
},
71-
buildApi("tsx"),
71+
buildApi("tsx")
7272
);
7373

7474
assert.deepEqual(
7575
actualOutput?.replace(/W/gm, ""),
76-
OUTPUT.replace(/W/gm, ""),
76+
OUTPUT.replace(/W/gm, "")
7777
);
7878
});
7979

8080
it("should correctly transform nested default props", async () => {
8181
const INPUT = await readFile(
8282
join(__dirname, "..", "__testfixtures__/nested-destructuring.input.tsx"),
83-
"utf-8",
83+
"utf-8"
8484
);
8585
const OUTPUT = await readFile(
8686
join(__dirname, "..", "__testfixtures__/nested-destructuring.output.tsx"),
87-
"utf-8",
87+
"utf-8"
8888
);
8989

9090
const actualOutput = transform(
9191
{
9292
path: "index.js",
9393
source: INPUT,
9494
},
95-
buildApi("tsx"),
95+
buildApi("tsx")
9696
);
9797

9898
assert.deepEqual(
9999
actualOutput?.replace(/W/gm, ""),
100-
OUTPUT.replace(/W/gm, ""),
100+
OUTPUT.replace(/W/gm, "")
101101
);
102102
});
103103

104104
it("should correctly transform default props with functions", async () => {
105105
const INPUT = await readFile(
106106
join(__dirname, "..", "__testfixtures__/with-functions.input.tsx"),
107-
"utf-8",
107+
"utf-8"
108108
);
109109
const OUTPUT = await readFile(
110110
join(__dirname, "..", "__testfixtures__/with-functions.output.tsx"),
111-
"utf-8",
111+
"utf-8"
112112
);
113113

114114
const actualOutput = transform(
115115
{
116116
path: "index.js",
117117
source: INPUT,
118118
},
119-
buildApi("tsx"),
119+
buildApi("tsx")
120120
);
121121

122122
assert.deepEqual(
123123
actualOutput?.replace(/W/gm, ""),
124-
OUTPUT.replace(/W/gm, ""),
124+
OUTPUT.replace(/W/gm, "")
125125
);
126126
});
127127

128128
it("should correctly transform when props have rest prop", async () => {
129129
const INPUT = await readFile(
130130
join(__dirname, "..", "__testfixtures__/with-rest-props.input.tsx"),
131-
"utf-8",
131+
"utf-8"
132132
);
133133
const OUTPUT = await readFile(
134134
join(__dirname, "..", "__testfixtures__/with-rest-props.output.tsx"),
135-
"utf-8",
135+
"utf-8"
136136
);
137137

138138
const actualOutput = transform(
139139
{
140140
path: "index.js",
141141
source: INPUT,
142142
},
143-
buildApi("tsx"),
143+
buildApi("tsx")
144144
);
145145

146146
assert.deepEqual(
147147
actualOutput?.replace(/W/gm, ""),
148-
OUTPUT.replace(/W/gm, ""),
148+
OUTPUT.replace(/W/gm, "")
149149
);
150150
});
151151

@@ -154,89 +154,112 @@ describe("react/19/replace-default-props", () => {
154154
join(
155155
__dirname,
156156
"..",
157-
"__testfixtures__/props-not-destructured.input.tsx",
157+
"__testfixtures__/props-not-destructured.input.tsx"
158158
),
159-
"utf-8",
159+
"utf-8"
160160
);
161161
const OUTPUT = await readFile(
162162
join(
163163
__dirname,
164164
"..",
165-
"__testfixtures__/props-not-destructured.output.tsx",
165+
"__testfixtures__/props-not-destructured.output.tsx"
166166
),
167-
"utf-8",
167+
"utf-8"
168168
);
169169

170170
const actualOutput = transform(
171171
{
172172
path: "index.js",
173173
source: INPUT,
174174
},
175-
buildApi("tsx"),
175+
buildApi("tsx")
176176
);
177177

178178
assert.deepEqual(
179179
actualOutput?.replace(/W/gm, ""),
180-
OUTPUT.replace(/W/gm, ""),
180+
OUTPUT.replace(/W/gm, "")
181181
);
182182
});
183183

184184
it("should correctly transform when some but not all props are defaulted", async () => {
185185
const INPUT = await readFile(
186186
join(__dirname, "..", "__testfixtures__/partial-default-props.input.tsx"),
187-
"utf-8",
187+
"utf-8"
188188
);
189189
const OUTPUT = await readFile(
190190
join(
191191
__dirname,
192192
"..",
193-
"__testfixtures__/partial-default-props.output.tsx",
193+
"__testfixtures__/partial-default-props.output.tsx"
194194
),
195-
"utf-8",
195+
"utf-8"
196196
);
197197

198198
const actualOutput = transform(
199199
{
200200
path: "index.js",
201201
source: INPUT,
202202
},
203-
buildApi("tsx"),
203+
buildApi("tsx")
204204
);
205205

206206
assert.deepEqual(
207207
actualOutput?.replace(/W/gm, ""),
208-
OUTPUT.replace(/W/gm, ""),
208+
OUTPUT.replace(/W/gm, "")
209209
);
210210
});
211211

212212
it("should correctly transform when props are not destructured", async () => {
213213
const INPUT = await readFile(
214214
join(__dirname, "..", "__testfixtures__/button-jsx-example-input.jsx"),
215-
"utf-8",
215+
"utf-8"
216216
);
217217
const OUTPUT = await readFile(
218218
join(__dirname, "..", "__testfixtures__/button-jsx-example-output.jsx"),
219-
"utf-8",
219+
"utf-8"
220220
);
221221

222222
const actualOutput = transform(
223223
{
224224
path: "index.js",
225225
source: INPUT,
226226
},
227-
buildApi("jsx"),
227+
buildApi("jsx")
228228
);
229229

230230
const fs = require("node:fs");
231231
fs.writeFileSync(
232232
join(__dirname, "..", "__testfixtures__/button-jsx-example-output.jsx"),
233-
actualOutput,
233+
actualOutput
234234
);
235235

236236
assert.deepEqual(
237237
actualOutput?.replace(/W/gm, ""),
238-
OUTPUT.replace(/W/gm, ""),
238+
OUTPUT.replace(/W/gm, "")
239239
);
240240
});
241-
});
242241

242+
it("rest element example", async () => {
243+
const INPUT = await readFile(
244+
join(__dirname, "..", "__testfixtures__/rest-element-example.input.jsx"),
245+
"utf-8"
246+
);
247+
const OUTPUT = await readFile(
248+
join(__dirname, "..", "__testfixtures__/rest-element-example.output.jsx"),
249+
"utf-8"
250+
);
251+
252+
const actualOutput = transform(
253+
{
254+
path: "index.js",
255+
source: INPUT,
256+
},
257+
buildApi("jsx")
258+
);
259+
260+
assert.deepEqual(
261+
actualOutput?.replace(/W/gm, ""),
262+
OUTPUT.replace(/W/gm, "")
263+
);
264+
});
265+
});

0 commit comments

Comments
 (0)