diff --git a/packages/table-core/src/features/ColumnSizing.ts b/packages/table-core/src/features/ColumnSizing.ts index 3cce2ddcb2..e6e662820d 100644 --- a/packages/table-core/src/features/ColumnSizing.ts +++ b/packages/table-core/src/features/ColumnSizing.ts @@ -10,6 +10,7 @@ import { } from '../types' import { getMemoOptions, makeStateUpdater, memo } from '../utils' import { ColumnPinningPosition } from './ColumnPinning' +import { safelyAccessDocument } from '../utils/document' // @@ -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), diff --git a/packages/table-core/src/utils/document.ts b/packages/table-core/src/utils/document.ts new file mode 100644 index 0000000000..3d46d95b3b --- /dev/null +++ b/packages/table-core/src/utils/document.ts @@ -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 +} diff --git a/packages/table-core/tests/test-setup.ts b/packages/table-core/tests/test-setup.ts deleted file mode 100644 index a9d0dd31aa..0000000000 --- a/packages/table-core/tests/test-setup.ts +++ /dev/null @@ -1 +0,0 @@ -import '@testing-library/jest-dom/vitest' diff --git a/packages/table-core/tests/utils/document.test.ts b/packages/table-core/tests/utils/document.test.ts new file mode 100644 index 0000000000..442543d7ff --- /dev/null +++ b/packages/table-core/tests/utils/document.test.ts @@ -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) + }) +}) diff --git a/packages/table-core/vitest.config.ts b/packages/table-core/vitest.config.ts index 523b22e583..016a1bf033 100644 --- a/packages/table-core/vitest.config.ts +++ b/packages/table-core/vitest.config.ts @@ -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, }, })