Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/table-core/src/features/ColumnSizing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../types'
import { getMemoOptions, makeStateUpdater, memo } from '../utils'
import { ColumnPinningPosition } from './ColumnPinning'
import { safelyAccessDocument } from '../utils/document'

//

Expand Down Expand Up @@ -428,8 +429,7 @@ export const ColumnSizing: TableFeature = {
}))
}

const contextDocument =
_contextDocument || typeof document !== 'undefined' ? document : null
const contextDocument = safelyAccessDocument(_contextDocument)

const mouseEvents = {
moveHandler: (e: MouseEvent) => onMove(e.clientX),
Expand Down
12 changes: 12 additions & 0 deletions packages/table-core/src/utils/document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function safelyAccessDocument(_document?: Document): Document | null {
return _document || (typeof document !== 'undefined' ? document : null)
}

export function safelyAccessDocumentEvent(event: Event): Document | null {
return !!event &&
!!event.target &&
typeof event.target === 'object' &&
'ownerDocument' in event.target
? (event.target.ownerDocument as Document | null)
: null
}
1 change: 0 additions & 1 deletion packages/table-core/tests/test-setup.ts

This file was deleted.

57 changes: 57 additions & 0 deletions packages/table-core/tests/utils/document.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
safelyAccessDocument,
safelyAccessDocumentEvent,
} from '../../src/utils/document'
import { afterEach, beforeEach, expect, describe, test } from 'vitest'

const originalDocument = globalThis.document

export function getDocumentMock(): Document {
return {} as Document
}

describe('safelyAccessDocument', () => {
describe('global document', () => {
const mockedDocument = getDocumentMock()
const originalDocument = globalThis.document
beforeEach(() => {
if (typeof globalThis.document === 'undefined') {
globalThis.document = mockedDocument
}
})
afterEach(() => {
if (typeof originalDocument === 'undefined') {
// @ts-expect-error Just Typings
delete globalThis.document
}
})

test('get global document when no args are passed', () => {
const contextDocument = safelyAccessDocument()
expect(contextDocument).toEqual(mockedDocument)
})
})

test('get document', () => {
let givenDocument = {} as Document
const contextDocument = safelyAccessDocument(givenDocument)

expect(contextDocument).toEqual(givenDocument)
})
})

describe('safelyAccessDocumentEvent', () => {
test('get document by given event', () => {
const fakeDocument = {}
const event = new Event('mousedown')

class FakeElement extends EventTarget {
ownerDocument = fakeDocument
}

Object.defineProperty(event, 'target', { value: new FakeElement() })

const document = safelyAccessDocumentEvent(event)
expect(fakeDocument).toEqual(document)
})
})
3 changes: 1 addition & 2 deletions packages/table-core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export default defineConfig({
name: packageJson.name,
dir: './tests',
watch: false,
environment: 'jsdom',
setupFiles: ['./tests/test-setup.ts'],
environment: 'node',
globals: true,
},
})