Skip to content

Commit 5a2889c

Browse files
committed
Load the sortable engine after its mock
vi.doMock only affects later imports, so the statically imported createSortableRoot would have pulled in the real auto-scroll module first and the mock would have stopped applying silently. A type import plus a lazy binding restores the ordering in both engine specs, and drops vi.hoisted with it. The preview spec also loses the file-split rationale that cited a shared module registry, which isolate:true removed. The split itself stands: the sibling renders previews for real.
1 parent c85a45b commit 5a2889c

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

frontend/src/common/drag-and-drop/sortable-lists-engine.preview.spec.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,37 @@
2929
// The drag preview's `getOffset` decides where the pointer sits on the
3030
// preview, and Pragmatic only hands it to the real `setCustomNativeDragPreview`
3131
// — nothing observable from the outside. So this file mocks that module and
32-
// reads the options back, which the sibling engine spec cannot do: it renders
33-
// previews for real. Separate file rather than a mock added there, because the
34-
// suite runs with `isolate: false` and shares one module registry.
32+
// reads the options back. It stays a separate file from the sibling engine
33+
// spec because that one renders previews for real, and one file cannot both
34+
// stub and exercise the same module.
3535

3636
import { vi } from 'vitest';
3737
import { NativeDragSimulation } from 'core-common/drag-and-drop/testing/native-drag-simulation';
38-
import { createSortableRoot } from './sortable-lists-engine';
38+
import type { createSortableRoot as createSortableRootFn } from './sortable-lists-engine';
3939

40-
const { previewCalls } = vi.hoisted(() => ({
41-
previewCalls: [] as {
42-
getOffset?:(args:{ container:HTMLElement }) => { x:number; y:number };
43-
}[],
44-
}));
40+
// `doMock` is not hoisted, so this initialises before the factory below runs
41+
// and a plain const does the job `vi.hoisted()` used to.
42+
const previewCalls:{
43+
getOffset?:(args:{ container:HTMLElement }) => { x:number; y:number };
44+
}[] = [];
4545

46-
vi.mock('@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview', () => ({
46+
vi.doMock('@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview', () => ({
4747
setCustomNativeDragPreview: (options:{
4848
getOffset?:(args:{ container:HTMLElement }) => { x:number; y:number };
4949
}) => {
5050
previewCalls.push(options);
5151
},
5252
}));
5353

54+
let createSortableRoot:typeof createSortableRootFn;
55+
5456
describe('createSortableRoot drag preview offset', () => {
5557
let cleanupFns:(() => void)[] = [];
5658

59+
beforeAll(async () => {
60+
({ createSortableRoot } = await import('./sortable-lists-engine'));
61+
});
62+
5763
beforeEach(() => { previewCalls.length = 0; });
5864

5965
afterEach(() => {

frontend/src/common/drag-and-drop/sortable-lists-engine.spec.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ import {
3232
centerOf,
3333
towardsEdgeOf,
3434
} from 'core-common/drag-and-drop/testing/native-drag-simulation';
35-
import {
36-
createSortableRoot,
37-
type SortableDropIntent,
38-
type SortableDropTransaction,
39-
type SortableSource,
35+
import type {
36+
createSortableRoot as createSortableRootFn,
37+
SortableDropIntent,
38+
SortableDropTransaction,
39+
SortableSource,
4040
} from './sortable-lists-engine';
4141

42-
const { autoScrollRegistrations } = vi.hoisted(() => ({
43-
autoScrollRegistrations: [] as {
44-
element:Element;
45-
getAllowedAxis:() => string;
46-
cleanup:() => void;
47-
cleaned:boolean;
48-
}[],
49-
}));
42+
// `doMock` is not hoisted, so this initialises before the factory below runs
43+
// and a plain const does the job `vi.hoisted()` used to.
44+
const autoScrollRegistrations:{
45+
element:Element;
46+
getAllowedAxis:() => string;
47+
cleanup:() => void;
48+
cleaned:boolean;
49+
}[] = [];
5050

51-
vi.mock('@atlaskit/pragmatic-drag-and-drop-auto-scroll/element', () => ({
51+
vi.doMock('@atlaskit/pragmatic-drag-and-drop-auto-scroll/element', () => ({
5252
autoScrollForElements: (args:{ element:Element; getAllowedAxis:() => string }) => {
5353
const entry = {
5454
element: args.element,
@@ -62,6 +62,8 @@ vi.mock('@atlaskit/pragmatic-drag-and-drop-auto-scroll/element', () => ({
6262
},
6363
}));
6464

65+
let createSortableRoot:typeof createSortableRootFn;
66+
6567
const liveRegistrations = () => autoScrollRegistrations.filter((r) => !r.cleaned);
6668

6769
function buildList(items:string[]):{ root:HTMLElement; rows:HTMLElement[] } {
@@ -145,6 +147,10 @@ function buildCardGrid(items:string[], columns:number):{ root:HTMLElement; cards
145147
describe('createSortableRoot', () => {
146148
let cleanupFns:(() => void)[] = [];
147149

150+
beforeAll(async () => {
151+
({ createSortableRoot } = await import('./sortable-lists-engine'));
152+
});
153+
148154
beforeEach(() => { autoScrollRegistrations.length = 0; });
149155

150156
afterEach(() => {

0 commit comments

Comments
 (0)