@@ -6,6 +6,65 @@ import { render, fireEvent, screen } from '@testing-library/react';
66// import '@testing-library/jest-dom';
77import CodeMirror , { ReactCodeMirrorRef } from '..' ;
88
9+ // Setup JSDOM mocks for CodeMirror
10+ beforeAll ( ( ) => {
11+ // Mock Range.getClientRects
12+ Object . defineProperty ( global . Range . prototype , 'getClientRects' , {
13+ writable : true ,
14+ value : jest . fn ( ( ) => ( {
15+ length : 1 ,
16+ item : ( ) => ( { bottom : 16 , height : 16 , left : 0 , right : 100 , top : 0 , width : 100 } ) ,
17+ [ Symbol . iterator ] : function * ( ) {
18+ yield { bottom : 16 , height : 16 , left : 0 , right : 100 , top : 0 , width : 100 } ;
19+ } ,
20+ } ) ) ,
21+ } ) ;
22+
23+ Object . defineProperty ( global . Range . prototype , 'getBoundingClientRect' , {
24+ writable : true ,
25+ value : jest . fn ( ( ) => ( { bottom : 16 , height : 16 , left : 0 , right : 100 , top : 0 , width : 100 } ) ) ,
26+ } ) ;
27+
28+ // Mock observers used by CodeMirror internals in JSDOM.
29+ class MockIntersectionObserver {
30+ observe = jest . fn ( ) ;
31+ unobserve = jest . fn ( ) ;
32+ disconnect = jest . fn ( ) ;
33+ takeRecords = jest . fn ( ( ) => [ ] ) ;
34+ }
35+ class MockResizeObserver {
36+ observe = jest . fn ( ) ;
37+ unobserve = jest . fn ( ) ;
38+ disconnect = jest . fn ( ) ;
39+ }
40+ class MockMutationObserver {
41+ observe = jest . fn ( ) ;
42+ disconnect = jest . fn ( ) ;
43+ takeRecords = jest . fn ( ( ) => [ ] ) ;
44+ }
45+ global . IntersectionObserver = MockIntersectionObserver as unknown as typeof IntersectionObserver ;
46+ global . ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver ;
47+ global . MutationObserver = MockMutationObserver as unknown as typeof MutationObserver ;
48+ window . IntersectionObserver = global . IntersectionObserver ;
49+ window . ResizeObserver = global . ResizeObserver ;
50+ window . MutationObserver = global . MutationObserver ;
51+
52+ // Mock element properties
53+ Object . defineProperty ( global . HTMLElement . prototype , 'clientHeight' , { get : ( ) => 100 } ) ;
54+ Object . defineProperty ( global . HTMLElement . prototype , 'clientWidth' , { get : ( ) => 500 } ) ;
55+ Object . defineProperty ( global . HTMLElement . prototype , 'offsetHeight' , { get : ( ) => 100 } ) ;
56+ Object . defineProperty ( global . HTMLElement . prototype , 'offsetWidth' , { get : ( ) => 500 } ) ;
57+ Object . defineProperty ( global . Element . prototype , 'scrollIntoView' , { writable : true , value : jest . fn ( ) } ) ;
58+
59+ // Suppress CodeMirror JSDOM warnings
60+ const originalError = console . error ;
61+ console . error = ( ...args ) => {
62+ if ( args [ 0 ] ?. toString ?.( ) . includes ( 'getClientRects' ) || args [ 0 ] ?. toString ?.( ) . includes ( 'observe is not a function' ) )
63+ return ;
64+ originalError . apply ( console , args ) ;
65+ } ;
66+ } ) ;
67+
968it ( 'CodeMirror' , async ( ) => {
1069 const component = renderer . create ( < CodeMirror /> ) ;
1170 let tree = component . toJSON ( ) ;
0 commit comments