Skip to content

Commit 54ce673

Browse files
fix(table-core): use right Document instance on getResizeHandler (column-sizing feature) (#5989)
* fix(table-core): safely access to right document instance in ColumnSizing feature * fix typings * knip
1 parent f7bf6f1 commit 54ce673

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

packages/table-core/src/features/ColumnSizing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '../types'
1111
import { getMemoOptions, makeStateUpdater, memo } from '../utils'
1212
import { ColumnPinningPosition } from './ColumnPinning'
13+
import { safelyAccessDocument } from '../utils/document'
1314

1415
//
1516

@@ -428,8 +429,7 @@ export const ColumnSizing: TableFeature = {
428429
}))
429430
}
430431

431-
const contextDocument =
432-
_contextDocument || typeof document !== 'undefined' ? document : null
432+
const contextDocument = safelyAccessDocument(_contextDocument)
433433

434434
const mouseEvents = {
435435
moveHandler: (e: MouseEvent) => onMove(e.clientX),
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function safelyAccessDocument(_document?: Document): Document | null {
2+
return _document || (typeof document !== 'undefined' ? document : null)
3+
}
4+
5+
export function safelyAccessDocumentEvent(event: Event): Document | null {
6+
return !!event &&
7+
!!event.target &&
8+
typeof event.target === 'object' &&
9+
'ownerDocument' in event.target
10+
? (event.target.ownerDocument as Document | null)
11+
: null
12+
}

packages/table-core/tests/test-setup.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {
2+
safelyAccessDocument,
3+
safelyAccessDocumentEvent,
4+
} from '../../src/utils/document'
5+
import { afterEach, beforeEach, expect, describe, test } from 'vitest'
6+
7+
const originalDocument = globalThis.document
8+
9+
export function getDocumentMock(): Document {
10+
return {} as Document
11+
}
12+
13+
describe('safelyAccessDocument', () => {
14+
describe('global document', () => {
15+
const mockedDocument = getDocumentMock()
16+
const originalDocument = globalThis.document
17+
beforeEach(() => {
18+
if (typeof globalThis.document === 'undefined') {
19+
globalThis.document = mockedDocument
20+
}
21+
})
22+
afterEach(() => {
23+
if (typeof originalDocument === 'undefined') {
24+
// @ts-expect-error Just Typings
25+
delete globalThis.document
26+
}
27+
})
28+
29+
test('get global document when no args are passed', () => {
30+
const contextDocument = safelyAccessDocument()
31+
expect(contextDocument).toEqual(mockedDocument)
32+
})
33+
})
34+
35+
test('get document', () => {
36+
let givenDocument = {} as Document
37+
const contextDocument = safelyAccessDocument(givenDocument)
38+
39+
expect(contextDocument).toEqual(givenDocument)
40+
})
41+
})
42+
43+
describe('safelyAccessDocumentEvent', () => {
44+
test('get document by given event', () => {
45+
const fakeDocument = {}
46+
const event = new Event('mousedown')
47+
48+
class FakeElement extends EventTarget {
49+
ownerDocument = fakeDocument
50+
}
51+
52+
Object.defineProperty(event, 'target', { value: new FakeElement() })
53+
54+
const document = safelyAccessDocumentEvent(event)
55+
expect(fakeDocument).toEqual(document)
56+
})
57+
})

packages/table-core/vitest.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ export default defineConfig({
66
name: packageJson.name,
77
dir: './tests',
88
watch: false,
9-
environment: 'jsdom',
10-
setupFiles: ['./tests/test-setup.ts'],
9+
environment: 'node',
1110
globals: true,
1211
},
1312
})

0 commit comments

Comments
 (0)