import type { MeasuringConfiguration } from '@dnd-kit/core' import type { Store } from '../../state/createStore' import { createStore } from '../../state/createStore' import { measuringFactory } from './measuring' function makeRect( x: number, y: number, width: number, height: number ): DOMRect { const rect = { y, top: y, bottom: y + height, height, x, left: x, right: x + width, width, } return { ...rect, toJSON: () => rect } } describe('measuring', () => { let store: Store, measuringConfig: MeasuringConfiguration beforeEach(() => { store = createStore() store.dispatch = jest.fn() store.getState = jest.fn() jest.spyOn(store.selectors, 'selectIsTreeGrid') jest.spyOn(store.selectors, 'selectPreviewLeftOffset') jest.spyOn(store.selectors, 'selectDraggingRowIds') jest.spyOn(store.selectors, 'selectRowLevel') jest.spyOn(store.selectors, 'selectTreeIndentSize').mockReturnValue( 'large' ) measuringConfig = measuringFactory(store) }) describe('with a flat grid', () => { beforeEach(() => { jest.mocked(store.selectors.selectIsTreeGrid).mockReturnValue(false) jest.mocked( store.selectors.selectPreviewLeftOffset ).mockReturnValue(36) }) it('should adjust the DOM container by the column offset and extra margin', () => { const node = document.createElement('div') jest.spyOn(node, 'getBoundingClientRect').mockReturnValue( makeRect(100, 50, 150, 46) ) const result = measuringConfig.draggable!.measure!(node) expect(result).toHaveProperty('left', 172) expect(result).toHaveProperty('x', 172) expect(result).toHaveProperty('right', 322) }) }) describe('with a tree grid', () => { describe('large indent', () => { beforeEach(() => { jest.mocked(store.selectors.selectIsTreeGrid).mockReturnValue( true ) jest.mocked( store.selectors.selectPreviewLeftOffset ).mockReturnValue(36) jest.mocked( store.selectors.selectDraggingRowIds ).mockReturnValue(new Set(['1'])) jest.mocked(store.selectors.selectRowLevel).mockReturnValue(3) }) it('should adjust the DOM container by the column offset, level, and extra margin', () => { const node = document.createElement('div') jest.spyOn(node, 'getBoundingClientRect').mockReturnValue( makeRect(100, 50, 150, 46) ) const result = measuringConfig.draggable!.measure!(node) expect(result).toHaveProperty('left', 316) expect(result).toHaveProperty('x', 316) expect(result).toHaveProperty('right', 466) }) }) describe('small indent', () => { beforeEach(() => { jest.mocked( store.selectors.selectTreeIndentSize ).mockReturnValue('small') jest.mocked(store.selectors.selectIsTreeGrid).mockReturnValue( true ) jest.mocked( store.selectors.selectPreviewLeftOffset ).mockReturnValue(36) jest.mocked( store.selectors.selectDraggingRowIds ).mockReturnValue(new Set(['1'])) jest.mocked(store.selectors.selectRowLevel).mockReturnValue(3) }) it('should adjust the DOM container by the column offset, level, and extra margin', () => { const node = document.createElement('div') jest.spyOn(node, 'getBoundingClientRect').mockReturnValue( makeRect(100, 50, 150, 46) ) const result = measuringConfig.draggable!.measure!(node) expect(result).toHaveProperty('left', 228) expect(result).toHaveProperty('x', 228) expect(result).toHaveProperty('right', 378) }) }) }) })