Skip to content

Commit e84ec46

Browse files
committed
fix: Resolve bulk action handler test mocking issue
1 parent a6c5174 commit e84ec46

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

test/integration/bulk-action-handler.test.ts

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,45 @@ import { expect, vi } from "vitest"
22
import { bulkActionHandlers } from "../../src/handlers/bulk-action-handlers.js"
33
import { server } from "../msw-setup.js"
44

5+
// Define constants before vi.mock since it's hoisted to the top
6+
const TEST_ENTRY_ID = "test-entry-id"
7+
const TEST_ASSET_ID = "test-asset-id"
8+
const TEST_BULK_ACTION_ID = "test-bulk-action-id"
9+
10+
// Mock the getContentfulClient function to avoid actual API calls
11+
vi.mock("../../src/config/client.js", () => ({
12+
getContentfulClient: vi.fn().mockResolvedValue({
13+
entry: {
14+
get: vi.fn().mockResolvedValue({
15+
sys: { id: TEST_ENTRY_ID, version: 1 },
16+
}),
17+
},
18+
asset: {
19+
get: vi.fn().mockResolvedValue({
20+
sys: { id: TEST_ASSET_ID, version: 1 },
21+
}),
22+
},
23+
bulkAction: {
24+
publish: vi.fn().mockResolvedValue({
25+
sys: { id: TEST_BULK_ACTION_ID, status: "created" },
26+
}),
27+
unpublish: vi.fn().mockResolvedValue({
28+
sys: { id: TEST_BULK_ACTION_ID, status: "created" },
29+
}),
30+
validate: vi.fn().mockResolvedValue({
31+
sys: { id: TEST_BULK_ACTION_ID, status: "created" },
32+
}),
33+
get: vi.fn().mockResolvedValue({
34+
sys: { id: TEST_BULK_ACTION_ID, status: "succeeded" },
35+
succeeded: [
36+
{ sys: { id: TEST_ENTRY_ID, type: "Entry" } },
37+
{ sys: { id: TEST_ASSET_ID, type: "Asset" } },
38+
],
39+
}),
40+
},
41+
}),
42+
}))
43+
544
describe("Bulk Action Handlers Integration Tests", () => {
645
// Start MSW Server before tests
746
beforeAll(() => server.listen())
@@ -10,42 +49,8 @@ describe("Bulk Action Handlers Integration Tests", () => {
1049

1150
const testSpaceId = "test-space-id"
1251
const testEnvironmentId = "master"
13-
const testEntryId = "test-entry-id"
14-
const testAssetId = "test-asset-id"
15-
16-
// Mock the getContentfulClient function to avoid actual API calls
17-
vi.mock("../../src/config/client.js", () => ({
18-
getContentfulClient: vi.fn().mockResolvedValue({
19-
entry: {
20-
get: vi.fn().mockResolvedValue({
21-
sys: { id: testEntryId, version: 1 },
22-
}),
23-
},
24-
asset: {
25-
get: vi.fn().mockResolvedValue({
26-
sys: { id: testAssetId, version: 1 },
27-
}),
28-
},
29-
bulkAction: {
30-
publish: vi.fn().mockResolvedValue({
31-
sys: { id: "test-bulk-action-id", status: "created" },
32-
}),
33-
unpublish: vi.fn().mockResolvedValue({
34-
sys: { id: "test-bulk-action-id", status: "created" },
35-
}),
36-
validate: vi.fn().mockResolvedValue({
37-
sys: { id: "test-bulk-action-id", status: "created" },
38-
}),
39-
get: vi.fn().mockResolvedValue({
40-
sys: { id: "test-bulk-action-id", status: "succeeded" },
41-
succeeded: [
42-
{ sys: { id: testEntryId, type: "Entry" } },
43-
{ sys: { id: testAssetId, type: "Asset" } },
44-
],
45-
}),
46-
},
47-
}),
48-
}))
52+
const testEntryId = TEST_ENTRY_ID
53+
const testAssetId = TEST_ASSET_ID
4954

5055
describe("bulkPublish", () => {
5156
it("should publish multiple entries and assets", async () => {

0 commit comments

Comments
 (0)