@@ -40,26 +40,19 @@ describe('drawTabbableOnCanvas', () => {
4040 }
4141
4242 beforeEach ( ( ) => {
43- // Reset DOM
4443 document . body . innerHTML = ''
4544
46- // Mock window properties
4745 Object . defineProperty ( window , 'innerWidth' , { value : 1024 , configurable : true } )
4846 Object . defineProperty ( window , 'innerHeight' , { value : 768 , configurable : true } )
49-
50- // Mock document properties
5147 Object . defineProperty ( document . documentElement , 'clientHeight' , { value : 768 , configurable : true } )
5248 Object . defineProperty ( document . documentElement , 'scrollHeight' , { value : 1000 , configurable : true } )
5349 Object . defineProperty ( document . body , 'scrollHeight' , { value : 1000 , configurable : true } )
54-
55- // Mock window.scrollTo
5650 window . scrollTo = vi . fn ( )
5751
58- // Mock canvas context
5952 const mockGetContext = vi . fn ( ) . mockReturnValue ( mockCanvasContext )
53+
6054 HTMLCanvasElement . prototype . getContext = mockGetContext
6155
62- // Reset all mock functions
6356 vi . clearAllMocks ( )
6457 } )
6558
@@ -77,7 +70,6 @@ describe('drawTabbableOnCanvas', () => {
7770 } )
7871
7972 it ( 'should draw lines and circles for tabbable elements' , ( ) => {
80- // Create some tabbable elements
8173 const button = document . createElement ( 'button' )
8274 button . textContent = 'Test Button'
8375 button . tabIndex = 0
@@ -88,7 +80,6 @@ describe('drawTabbableOnCanvas', () => {
8880 input . tabIndex = 0
8981 document . body . appendChild ( input )
9082
91- // Mock getBoundingClientRect for the elements
9283 const mockRect = {
9384 left : 100 ,
9485 top : 100 ,
@@ -99,11 +90,9 @@ describe('drawTabbableOnCanvas', () => {
9990 }
10091 Element . prototype . getBoundingClientRect = vi . fn ( ) . mockReturnValue ( mockRect )
10192
102- // Mock offsetParent to make elements visible
10393 Object . defineProperty ( button , 'offsetParent' , { value : document . body , configurable : true } )
10494 Object . defineProperty ( input , 'offsetParent' , { value : document . body , configurable : true } )
10595
106- // Create spies for canvas context methods
10796 const beginPathSpy = vi . spyOn ( mockCanvasContext , 'beginPath' )
10897 const globalCompositeOperationSpy = vi . spyOn ( mockCanvasContext , 'globalCompositeOperation' , 'set' )
10998 const fillStyleSpy = vi . spyOn ( mockCanvasContext , 'fillStyle' , 'set' )
@@ -112,27 +101,21 @@ describe('drawTabbableOnCanvas', () => {
112101
113102 drawTabbableOnCanvas ( defaultOptions )
114103
115- // Verify the sequence of operations
116104 expect ( beginPathSpy ) . toHaveBeenCalled ( )
117105
118- // Check line drawing operations (happens first)
119106 expect ( globalCompositeOperationSpy ) . toHaveBeenNthCalledWith ( 1 , 'destination-over' )
120107 expect ( lineWidthSpy ) . toHaveBeenNthCalledWith ( 1 , defaultOptions . line ! . width )
121108 expect ( strokeStyleSpy ) . toHaveBeenNthCalledWith ( 1 , defaultOptions . line ! . color )
122109 expect ( mockCanvasContext . moveTo ) . toHaveBeenCalled ( )
123110 expect ( mockCanvasContext . lineTo ) . toHaveBeenCalled ( )
124111 expect ( mockCanvasContext . stroke ) . toHaveBeenCalled ( )
125-
126- // Check circle drawing operations (happens second)
127112 expect ( globalCompositeOperationSpy ) . toHaveBeenNthCalledWith ( 2 , 'source-over' )
128113 expect ( fillStyleSpy ) . toHaveBeenNthCalledWith ( 1 , defaultOptions . circle ! . backgroundColor )
129114 expect ( lineWidthSpy ) . toHaveBeenNthCalledWith ( 2 , defaultOptions . circle ! . borderWidth )
130115 expect ( strokeStyleSpy ) . toHaveBeenNthCalledWith ( 2 , defaultOptions . circle ! . borderColor )
131116 expect ( mockCanvasContext . arc ) . toHaveBeenCalled ( )
132117 expect ( mockCanvasContext . fill ) . toHaveBeenCalled ( )
133118 expect ( mockCanvasContext . stroke ) . toHaveBeenCalled ( )
134-
135- // Check number drawing operations (happens last)
136119 expect ( fillStyleSpy ) . toHaveBeenNthCalledWith ( 2 , defaultOptions . circle ! . fontColor )
137120 expect ( mockCanvasContext . font ) . toBe ( `${ defaultOptions . circle ! . fontSize } px ${ defaultOptions . circle ! . fontFamily } ` )
138121 expect ( mockCanvasContext . textAlign ) . toBe ( 'center' )
@@ -149,7 +132,6 @@ describe('drawTabbableOnCanvas', () => {
149132 } )
150133
151134 it ( 'should handle hidden elements' , ( ) => {
152- // Create a hidden button
153135 const button = document . createElement ( 'button' )
154136 button . style . visibility = 'hidden'
155137 document . body . appendChild ( button )
@@ -160,7 +142,6 @@ describe('drawTabbableOnCanvas', () => {
160142 } )
161143
162144 it ( 'should handle disabled elements' , ( ) => {
163- // Create a disabled button
164145 const button = document . createElement ( 'button' )
165146 button . disabled = true
166147 document . body . appendChild ( button )
@@ -199,17 +180,18 @@ describe('drawTabbableOnCanvas', () => {
199180 radio1 . type = 'radio'
200181 radio1 . name = 'group1'
201182 document . body . appendChild ( radio1 )
183+
202184 const radio2 = document . createElement ( 'input' )
203185 radio2 . type = 'radio'
204186 radio2 . name = 'group1'
205187 radio2 . checked = true
206188 document . body . appendChild ( radio2 )
207- // Make radios visible
208189 Object . defineProperty ( radio1 , 'offsetParent' , { value : document . body , configurable : true } )
209190 Object . defineProperty ( radio2 , 'offsetParent' , { value : document . body , configurable : true } )
210- // Mock getBoundingClientRect
211191 radio2 . getBoundingClientRect = vi . fn ( ) . mockReturnValue ( { left : 0 , top : 0 , width : 10 , height : 10 , right : 10 , bottom : 10 } )
192+
212193 drawTabbableOnCanvas ( defaultOptions )
194+
213195 expect ( mockCanvasContext . beginPath ) . toHaveBeenCalled ( )
214196 } )
215197
@@ -253,14 +235,14 @@ describe('drawTabbableOnCanvas', () => {
253235 Object . defineProperty ( document . documentElement , 'clientHeight' , { value : 100 , configurable : true } )
254236 Object . defineProperty ( document . documentElement , 'scrollHeight' , { value : 100 , configurable : true } )
255237 Object . defineProperty ( document . body , 'scrollHeight' , { value : 200 , configurable : true } )
256- // Set window.innerHeight to match the others
257238 Object . defineProperty ( window , 'innerHeight' , { value : 100 , configurable : true } )
258- // Add a tabbable element to trigger canvas drawing
239+
259240 const btn = document . createElement ( 'button' )
260241 btn . tabIndex = 0
261242 Object . defineProperty ( btn , 'offsetParent' , { value : document . body , configurable : true } )
262243 btn . getBoundingClientRect = vi . fn ( ) . mockReturnValue ( { left : 0 , top : 0 , width : 10 , height : 10 , right : 10 , bottom : 10 } )
263244 document . body . appendChild ( btn )
245+
264246 drawTabbableOnCanvas ( defaultOptions )
265247
266248 const canvas = document . getElementById ( 'wic-tabbable-canvas' ) as HTMLCanvasElement
@@ -272,52 +254,54 @@ describe('drawTabbableOnCanvas', () => {
272254 Object . defineProperty ( document . documentElement , 'clientHeight' , { value : 100 , configurable : true } )
273255 Object . defineProperty ( document . documentElement , 'scrollHeight' , { value : 100 , configurable : true } )
274256 Object . defineProperty ( document . body , 'scrollHeight' , { value : 100 , configurable : true } )
275- // Add a tall div
257+
276258 const tallDiv = document . createElement ( 'div' )
277259 tallDiv . style . height = '300px'
278260 document . body . appendChild ( tallDiv )
279261 tallDiv . getBoundingClientRect = vi . fn ( ) . mockReturnValue ( { top : 0 } )
262+
280263 drawTabbableOnCanvas ( defaultOptions )
264+
281265 const canvas = document . getElementById ( 'wic-tabbable-canvas' ) as HTMLCanvasElement
266+
282267 expect ( canvas . height ) . toBeGreaterThanOrEqual ( 100 )
283268 } )
284269
285270 it ( 'should not throw or attempt to draw if getContext returns null (drawLine)' , ( ) => {
286- // Mock getContext to return null
287271 const originalGetContext = HTMLCanvasElement . prototype . getContext
288272 HTMLCanvasElement . prototype . getContext = vi . fn ( ) . mockReturnValue ( null )
289- // Add two tabbable elements to trigger drawLine
273+
290274 const btn1 = document . createElement ( 'button' )
291275 btn1 . tabIndex = 0
292276 Object . defineProperty ( btn1 , 'offsetParent' , { value : document . body , configurable : true } )
293277 btn1 . getBoundingClientRect = vi . fn ( ) . mockReturnValue ( { left : 0 , top : 0 , width : 10 , height : 10 , right : 10 , bottom : 10 } )
294278 document . body . appendChild ( btn1 )
279+
295280 const btn2 = document . createElement ( 'button' )
296281 btn2 . tabIndex = 0
297282 Object . defineProperty ( btn2 , 'offsetParent' , { value : document . body , configurable : true } )
298283 btn2 . getBoundingClientRect = vi . fn ( ) . mockReturnValue ( { left : 20 , top : 20 , width : 10 , height : 10 , right : 30 , bottom : 30 } )
299284 document . body . appendChild ( btn2 )
300- // Should not throw or call any drawing methods
285+
301286 expect ( ( ) => drawTabbableOnCanvas ( defaultOptions ) ) . not . toThrow ( )
302287 expect ( mockCanvasContext . beginPath ) . not . toHaveBeenCalled ( )
303- // Restore
288+
304289 HTMLCanvasElement . prototype . getContext = originalGetContext
305290 } )
306291
307292 it ( 'should not throw or attempt to draw if getContext returns null (drawCircleAndNumber)' , ( ) => {
308- // Mock getContext to return null
309293 const originalGetContext = HTMLCanvasElement . prototype . getContext
310294 HTMLCanvasElement . prototype . getContext = vi . fn ( ) . mockReturnValue ( null )
311- // Add one tabbable element to trigger drawCircleAndNumber
295+
312296 const btn = document . createElement ( 'button' )
313297 btn . tabIndex = 0
314298 Object . defineProperty ( btn , 'offsetParent' , { value : document . body , configurable : true } )
315299 btn . getBoundingClientRect = vi . fn ( ) . mockReturnValue ( { left : 0 , top : 0 , width : 10 , height : 10 , right : 10 , bottom : 10 } )
316300 document . body . appendChild ( btn )
317- // Should not throw or call any drawing methods
301+
318302 expect ( ( ) => drawTabbableOnCanvas ( defaultOptions ) ) . not . toThrow ( )
319303 expect ( mockCanvasContext . beginPath ) . not . toHaveBeenCalled ( )
320- // Restore
304+
321305 HTMLCanvasElement . prototype . getContext = originalGetContext
322306 } )
323307} )
0 commit comments