Skip to content

Commit ee99555

Browse files
committed
test: improve unit test suite
1 parent cea57b9 commit ee99555

6 files changed

Lines changed: 920 additions & 846 deletions

File tree

test/unit/core.test.ts

Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,16 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import {
44
createMockDOM,
55
mockGetBoundingClientRect,
6-
} from "../utils/test-helpers.js";
7-
8-
// Test data constants for better maintainability
9-
const TEST_DIMENSIONS = {
10-
CONTAINER_HEIGHT: 2000,
11-
BOUNDING_HEIGHT: 1000,
12-
TARGET_HEIGHT: 50,
13-
TARGET_WIDTH: 100,
14-
VIEWPORT_HEIGHT: 768,
15-
} as const;
16-
17-
const DEFAULT_RECT: DOMRect = {
18-
top: 100,
19-
bottom: 150,
20-
left: 0,
21-
right: 100,
22-
width: TEST_DIMENSIONS.TARGET_WIDTH,
23-
height: TEST_DIMENSIONS.TARGET_HEIGHT,
24-
x: 0,
25-
y: 100,
26-
toJSON: () => ({}),
27-
};
28-
29-
describe("core", () => {
6+
} from "../utils/core-test-helpers.js";
7+
import {
8+
assertions,
9+
DEFAULT_RECT,
10+
errorTestCases,
11+
simulateScrollToPosition,
12+
TEST_OFFSETS,
13+
} from "../utils/shared-test-helpers.js";
14+
15+
describe("Core", () => {
3016
let mockDOM: ReturnType<typeof createMockDOM>;
3117
let targetElement: HTMLElement;
3218
let boundingElement: HTMLElement;
@@ -37,8 +23,21 @@ describe("core", () => {
3723
targetElement = mockDOM.targetElement;
3824
boundingElement = mockDOM.boundingElement;
3925

40-
// Setup consistent mocking
41-
mockGetBoundingClientRect(DEFAULT_RECT);
26+
// Setup consistent mocking with default rect for target element
27+
mockGetBoundingClientRect({
28+
target: DEFAULT_RECT,
29+
container: {
30+
top: 0,
31+
bottom: 2000,
32+
left: 0,
33+
right: 100,
34+
width: 100,
35+
height: 2000,
36+
x: 0,
37+
y: 0,
38+
toJSON: () => ({}),
39+
},
40+
});
4241
});
4342

4443
afterEach(() => {
@@ -66,9 +65,10 @@ describe("core", () => {
6665
adhesive: Adhesive,
6766
expectedStatus: keyof typeof ADHESIVE_STATUS,
6867
) => {
69-
const state = adhesive.getState();
70-
expect(state.status).toBe(ADHESIVE_STATUS[expectedStatus]);
71-
return state;
68+
return assertions.expectElementToBeInState(
69+
adhesive,
70+
ADHESIVE_STATUS[expectedStatus],
71+
);
7272
};
7373

7474
describe("Constructor & Factory Methods", () => {
@@ -395,25 +395,6 @@ describe("core", () => {
395395
});
396396

397397
describe("Positioning Logic", () => {
398-
// Helper to simulate scroll and wait for RAF
399-
const simulateScrollToPosition = (scrollY: number): Promise<void> => {
400-
Object.defineProperty(window, "scrollY", {
401-
value: scrollY,
402-
writable: true,
403-
});
404-
Object.defineProperty(window, "pageYOffset", {
405-
value: scrollY,
406-
writable: true,
407-
});
408-
409-
window.dispatchEvent(new Event("scroll"));
410-
411-
// Wait for requestAnimationFrame to complete
412-
return new Promise<void>((resolve) => {
413-
requestAnimationFrame(() => resolve());
414-
});
415-
};
416-
417398
describe("top positioning behavior", () => {
418399
let adhesive: Adhesive;
419400

@@ -545,9 +526,7 @@ describe("core", () => {
545526

546527
describe("offset handling", () => {
547528
it("applies offset correctly for different values", () => {
548-
const testOffsets = [0, 10, 50, 100];
549-
550-
testOffsets.forEach((offset) => {
529+
TEST_OFFSETS.forEach((offset) => {
551530
const adhesive = createInitializedAdhesive({
552531
boundingEl: boundingElement,
553532
offset,
@@ -917,13 +896,6 @@ describe("core", () => {
917896

918897
describe("Error Handling & Edge Cases", () => {
919898
describe("comprehensive error scenarios", () => {
920-
const errorTestCases = [
921-
{ selector: "", expectedCode: "TARGET_EL_REQUIRED" },
922-
{ selector: "#nonexistent", expectedCode: "TARGET_EL_NOT_FOUND" },
923-
{ selector: ".missing-class", expectedCode: "TARGET_EL_NOT_FOUND" },
924-
{ selector: "invalid>>selector", expectedCode: "TARGET_EL_NOT_FOUND" },
925-
] as const;
926-
927899
it.each(errorTestCases)(
928900
"throws AdhesiveError with code $expectedCode for selector '$selector'",
929901
({ selector, expectedCode }) => {
@@ -1010,24 +982,6 @@ describe("core", () => {
1010982
});
1011983

1012984
describe("Integration Scenarios", () => {
1013-
// Helper function for integration tests
1014-
const simulateScrollToPosition = (scrollY: number): Promise<void> => {
1015-
Object.defineProperty(window, "scrollY", {
1016-
value: scrollY,
1017-
writable: true,
1018-
});
1019-
Object.defineProperty(window, "pageYOffset", {
1020-
value: scrollY,
1021-
writable: true,
1022-
});
1023-
1024-
window.dispatchEvent(new Event("scroll"));
1025-
1026-
return new Promise<void>((resolve) => {
1027-
requestAnimationFrame(() => resolve());
1028-
});
1029-
};
1030-
1031985
describe("complete user workflows", () => {
1032986
it("handles full scroll lifecycle with state transitions", async () => {
1033987
const adhesive = createInitializedAdhesive({

0 commit comments

Comments
 (0)