1- import { describe , expect , it } from "@effect/vitest"
1+ import { describe , it } from "@effect/vitest"
22import { Effect } from "effect"
3- import path from "node:path"
43
5- import { transformJsx } from "./babel-test-utils.js"
4+ import { transformAndValidateJsx } from "./babel-test-utils.js"
65
76// CHANGE: extract JSX transformation tests to separate file.
87// WHY: comply with max-lines ESLint rule (300 lines limit).
@@ -20,13 +19,10 @@ describe("babel-plugin JSX transformations", () => {
2019 return <div>Hello</div>
2120 }
2221 `
23- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
22+ const { expectContains } = transformAndValidateJsx ( code , "src/App.tsx" )
2423
25- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
26-
27- expect ( result ) . not . toBeNull ( )
28- expect ( result ?. code ) . toContain ( "data-path=\"src/App.tsx:" )
29- expect ( result ?. code ) . toContain ( "<div" )
24+ expectContains ( "data-path=\"src/App.tsx:" )
25+ expectContains ( "<div" )
3026 } ) )
3127
3228 it . effect ( "transforms multiple JSX elements" , ( ) =>
@@ -41,15 +37,10 @@ describe("babel-plugin JSX transformations", () => {
4137 )
4238 }
4339 `
44- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
45-
46- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
40+ const { expectDataPathMinCount } = transformAndValidateJsx ( code , "src/App.tsx" )
4741
48- expect ( result ) . not . toBeNull ( )
4942 // Should contain data-path attributes for div, header, and main
50- const pathMatches = result ?. code ?. match ( / d a t a - p a t h = " s r c \/ A p p \. t s x : \d + : \d + " / g)
51- expect ( pathMatches ) . toBeDefined ( )
52- expect ( pathMatches ?. length ) . toBeGreaterThanOrEqual ( 3 )
43+ expectDataPathMinCount ( 3 )
5344 } ) )
5445
5546 it . effect ( "does not add duplicate data-path attribute (idempotency)" , ( ) =>
@@ -59,16 +50,12 @@ describe("babel-plugin JSX transformations", () => {
5950 return <div data-path="existing:1:0">Hello</div>
6051 }
6152 `
62- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
63-
64- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
53+ const { expectContains, expectDataPathCount } = transformAndValidateJsx ( code , "src/App.tsx" )
6554
66- expect ( result ) . not . toBeNull ( )
6755 // Should keep the existing data-path attribute
68- expect ( result ?. code ) . toContain ( "data-path=\"existing:1:0\"" )
56+ expectContains ( "data-path=\"existing:1:0\"" )
6957 // Count data-path attributes - should only be one
70- const pathMatches = result ?. code ?. match ( / d a t a - p a t h = " / g)
71- expect ( pathMatches ?. length ) . toBe ( 1 )
58+ expectDataPathCount ( 1 )
7259 } ) )
7360
7461 it . effect ( "does not interfere with other path-like attributes" , ( ) =>
@@ -78,15 +65,12 @@ describe("babel-plugin JSX transformations", () => {
7865 return <img src="/image.png" alt="test" />
7966 }
8067 `
81- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
68+ const { expectContains } = transformAndValidateJsx ( code , "src/App.tsx" )
8269
83- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
84-
85- expect ( result ) . not . toBeNull ( )
8670 // Should preserve src attribute
87- expect ( result ?. code ) . toContain ( "src=\"/image.png\"" )
71+ expectContains ( "src=\"/image.png\"" )
8872 // Should add data-path attribute
89- expect ( result ?. code ) . toContain ( "data-path=\"src/App.tsx:" )
73+ expectContains ( "data-path=\"src/App.tsx:" )
9074 } ) )
9175
9276 it . effect ( "handles JSX with existing attributes" , ( ) =>
@@ -96,17 +80,14 @@ describe("babel-plugin JSX transformations", () => {
9680 return <button className="btn" id="submit" onClick={handleClick}>Click</button>
9781 }
9882 `
99- const testFilename = path . resolve ( "/project" , "src/components/Button.tsx" )
100-
101- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
83+ const { expectContains } = transformAndValidateJsx ( code , "src/components/Button.tsx" )
10284
103- expect ( result ) . not . toBeNull ( )
10485 // Should preserve existing attributes
105- expect ( result ?. code ) . toContain ( "className=\"btn\"" )
106- expect ( result ?. code ) . toContain ( "id=\"submit\"" )
107- expect ( result ?. code ) . toContain ( "onClick={handleClick}" )
86+ expectContains ( "className=\"btn\"" )
87+ expectContains ( "id=\"submit\"" )
88+ expectContains ( "onClick={handleClick}" )
10889 // Should add data-path attribute
109- expect ( result ?. code ) . toContain ( "data-path=\"src/components/Button.tsx:" )
90+ expectContains ( "data-path=\"src/components/Button.tsx:" )
11091 } ) )
11192
11293 it . effect ( "handles self-closing JSX elements" , ( ) =>
@@ -116,13 +97,10 @@ describe("babel-plugin JSX transformations", () => {
11697 return <input type="text" />
11798 }
11899 `
119- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
120-
121- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
100+ const { expectContains } = transformAndValidateJsx ( code , "src/App.tsx" )
122101
123- expect ( result ) . not . toBeNull ( )
124- expect ( result ?. code ) . toContain ( "data-path=\"src/App.tsx:" )
125- expect ( result ?. code ) . toContain ( "type=\"text\"" )
102+ expectContains ( "data-path=\"src/App.tsx:" )
103+ expectContains ( "type=\"text\"" )
126104 } ) )
127105
128106 it . effect ( "handles nested JSX components" , ( ) =>
@@ -142,15 +120,10 @@ describe("babel-plugin JSX transformations", () => {
142120 )
143121 }
144122 `
145- const testFilename = path . resolve ( "/project" , "src/pages/Page.tsx" )
123+ const { expectDataPathMinCount } = transformAndValidateJsx ( code , "src/pages/Page.tsx" )
146124
147- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
148-
149- expect ( result ) . not . toBeNull ( )
150- // All components should be tagged
151- const pathMatches = result ?. code ?. match ( / d a t a - p a t h = " s r c \/ p a g e s \/ P a g e \. t s x : \d + : \d + " / g)
152- expect ( pathMatches ) . toBeDefined ( )
153- expect ( pathMatches ?. length ) . toBeGreaterThanOrEqual ( 6 ) // Layout, Header, Logo, Nav, Content, Article
125+ // All components should be tagged: Layout, Header, Logo, Nav, Content, Article
126+ expectDataPathMinCount ( 6 )
154127 } ) )
155128
156129 it . effect ( "handles JSX fragments" , ( ) =>
@@ -165,14 +138,10 @@ describe("babel-plugin JSX transformations", () => {
165138 )
166139 }
167140 `
168- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
169-
170- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
141+ const { expectDataPathMinCount } = transformAndValidateJsx ( code , "src/App.tsx" )
171142
172- expect ( result ) . not . toBeNull ( )
173- // Fragments don't get tagged, but their children do
174- const pathMatches = result ?. code ?. match ( / d a t a - p a t h = " / g)
175- expect ( pathMatches ?. length ) . toBeGreaterThanOrEqual ( 2 ) // Two div elements
143+ // Fragments don't get tagged, but their children do (two div elements)
144+ expectDataPathMinCount ( 2 )
176145 } ) )
177146
178147 it . effect ( "handles JSX with spread attributes" , ( ) =>
@@ -182,13 +151,10 @@ describe("babel-plugin JSX transformations", () => {
182151 return <div {...props}>Content</div>
183152 }
184153 `
185- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
186-
187- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
154+ const { expectContains } = transformAndValidateJsx ( code , "src/App.tsx" )
188155
189- expect ( result ) . not . toBeNull ( )
190- expect ( result ?. code ) . toContain ( "{...props}" )
191- expect ( result ?. code ) . toContain ( "data-path=\"src/App.tsx:" )
156+ expectContains ( "{...props}" )
157+ expectContains ( "data-path=\"src/App.tsx:" )
192158 } ) )
193159
194160 it . effect ( "handles TypeScript JSX generics" , ( ) =>
@@ -198,26 +164,20 @@ describe("babel-plugin JSX transformations", () => {
198164 return <div>Generic Component</div>
199165 }
200166 `
201- const testFilename = path . resolve ( "/project" , "src/Generic.tsx" )
167+ const { expectContains } = transformAndValidateJsx ( code , "src/Generic.tsx" )
202168
203- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
204-
205- expect ( result ) . not . toBeNull ( )
206- expect ( result ?. code ) . toContain ( "data-path=\"src/Generic.tsx:" )
169+ expectContains ( "data-path=\"src/Generic.tsx:" )
207170 } ) )
208171
209172 it . effect ( "correctly formats data-path with line and column" , ( ) =>
210173 Effect . sync ( ( ) => {
211174 const code = `function App() {
212175 return <div>Test</div>
213176}`
214- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
215-
216- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
177+ const { expectMatch } = transformAndValidateJsx ( code , "src/App.tsx" )
217178
218- expect ( result ) . not . toBeNull ( )
219179 // The data-path should contain line 2 (where <div> is) and column number
220- expect ( result ?. code ) . toMatch ( / d a t a - p a t h = " s r c \/ A p p \. t s x : 2 : \d + " / )
180+ expectMatch ( / d a t a - p a t h = " s r c \/ A p p \. t s x : 2 : \d + " / )
221181 } ) )
222182
223183 it . effect ( "handles components with multiple props on multiple lines" , ( ) =>
@@ -235,13 +195,10 @@ describe("babel-plugin JSX transformations", () => {
235195 )
236196 }
237197 `
238- const testFilename = path . resolve ( "/project" , "src/App.tsx" )
239-
240- const result = transformJsx ( code , testFilename , { rootDir : "/project" } )
198+ const { expectContains } = transformAndValidateJsx ( code , "src/App.tsx" )
241199
242- expect ( result ) . not . toBeNull ( )
243- expect ( result ?. code ) . toContain ( "data-path=\"src/App.tsx:" )
244- expect ( result ?. code ) . toContain ( "className=\"primary\"" )
245- expect ( result ?. code ) . toContain ( "onClick={handleClick}" )
200+ expectContains ( "data-path=\"src/App.tsx:" )
201+ expectContains ( "className=\"primary\"" )
202+ expectContains ( "onClick={handleClick}" )
246203 } ) )
247204} )
0 commit comments