Skip to content

Commit 2e35600

Browse files
committed
fix(api-headless-cms-scheduler): tests which broke after refactor
1 parent 211bbe0 commit 2e35600

File tree

18 files changed

+813
-309
lines changed

18 files changed

+813
-309
lines changed

.github/workflows/wac/utils/listPackagesWithJestTests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ const CUSTOM_HANDLERS: Record<string, () => Array<PackageWithTests>> = {
355355
},
356356
"api-headless-cms-scheduler": () => {
357357
return [
358-
{cmd: "packages/headless-cms-scheduler --storage=ddb-es,ddb", storage: "ddb-es"},
359-
{cmd: "packages/headless-cms-scheduler --storage=ddb-os,ddb", storage: "ddb-os"},
360-
{cmd: "packages/headless-cms-scheduler --storage=ddb", storage: "ddb"}
358+
{ cmd: "packages/headless-cms-scheduler --storage=ddb-es,ddb", storage: "ddb-es" },
359+
{ cmd: "packages/headless-cms-scheduler --storage=ddb-os,ddb", storage: "ddb-os" },
360+
{ cmd: "packages/headless-cms-scheduler --storage=ddb", storage: "ddb" }
361361
];
362362
}
363363
};

packages/api-headless-cms-scheduler/__tests__/graphql/schema.test.ts

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@ import {
55
listScheduleSchema,
66
updateScheduleSchema
77
} from "~/graphql/schema.js";
8+
import { type ISchedulerInput, ScheduleType } from "~/scheduler/types.js";
9+
10+
interface IExpectedSchemaInput {
11+
id: string;
12+
modelId: string;
13+
input: ISchedulerInput;
14+
}
15+
16+
interface IExpectedCancelSchemaInput {
17+
id: string;
18+
modelId: string;
19+
}
820

921
describe("graphql/schema", () => {
1022
it("getScheduleSchema: accepts valid input and returns expected data", async () => {
11-
const input = { modelId: "model", id: "123" };
23+
const input = {
24+
modelId: "model",
25+
id: "123"
26+
};
1227
const result = await getScheduleSchema.safeParseAsync(input);
1328
expect(result.success).toBe(true);
1429
if (result.success) {
@@ -67,7 +82,10 @@ describe("graphql/schema", () => {
6782
const input = {
6883
modelId: "model",
6984
id: "123",
70-
input: { immediately: true, type: "publish" }
85+
input: {
86+
immediately: true,
87+
type: "publish"
88+
}
7189
};
7290
const result = await createScheduleSchema.safeParseAsync(input);
7391
expect(result.success).toBe(true);
@@ -77,31 +95,34 @@ describe("graphql/schema", () => {
7795
});
7896
it("createScheduleSchema: accepts dateOn input and returns expected data", async () => {
7997
const date = new Date();
80-
const input = {
98+
const input: IExpectedSchemaInput = {
8199
modelId: "model",
82100
id: "123",
83-
input: { dateOn: date, type: "unpublish" }
101+
input: {
102+
scheduleOn: date,
103+
type: ScheduleType.unpublish
104+
}
84105
};
85106
const result = await createScheduleSchema.safeParseAsync(input);
86107
expect(result.success).toBe(true);
87-
if (result.success) {
88-
expect(result.data).toEqual({
89-
...input,
90-
input: {
91-
...input.input,
92-
// @ts-expect-error
93-
dateOn: result.data.input.dateOn
94-
}
95-
});
96-
// @ts-expect-error
97-
expect(result.data.input.dateOn instanceof Date).toBe(true);
98-
}
108+
109+
expect(result.data).toEqual({
110+
...input,
111+
input: {
112+
...input.input,
113+
scheduleOn: date
114+
}
115+
});
116+
// @ts-expect-error
117+
expect(result.data.input.scheduleOn).toBeInstanceOf(Date);
99118
});
100119
it("createScheduleSchema: rejects missing input fields", async () => {
101120
const result = await createScheduleSchema.safeParseAsync({
102121
modelId: "model",
103122
id: "123",
104-
input: { type: "publish" }
123+
input: {
124+
type: "publish"
125+
}
105126
});
106127
expect(result.success).toBe(false);
107128
});
@@ -110,7 +131,10 @@ describe("graphql/schema", () => {
110131
const input = {
111132
modelId: "model",
112133
id: "123",
113-
input: { immediately: true, type: "publish" }
134+
input: {
135+
immediately: true,
136+
type: "publish"
137+
}
114138
};
115139
const result = await updateScheduleSchema.safeParseAsync(input);
116140
expect(result.success).toBe(true);
@@ -120,10 +144,13 @@ describe("graphql/schema", () => {
120144
});
121145
it("updateScheduleSchema: accepts dateOn input and returns expected data", async () => {
122146
const date = new Date();
123-
const input = {
147+
const input: IExpectedSchemaInput = {
124148
modelId: "model",
125149
id: "123",
126-
input: { dateOn: date, type: "unpublish" }
150+
input: {
151+
scheduleOn: date,
152+
type: ScheduleType.unpublish
153+
}
127154
};
128155
const result = await updateScheduleSchema.safeParseAsync(input);
129156
expect(result.success).toBe(true);
@@ -132,25 +159,28 @@ describe("graphql/schema", () => {
132159
...input,
133160
input: {
134161
...input.input,
135-
// @ts-expect-error
136-
dateOn: result.data.input.dateOn
162+
scheduleOn: date
137163
}
138164
});
139-
// @ts-expect-error
140-
expect(result.data.input.dateOn instanceof Date).toBe(true);
165+
expect(result.data.input.scheduleOn).toBeInstanceOf(Date);
141166
}
142167
});
143168
it("updateScheduleSchema: rejects missing input fields", async () => {
144169
const result = await updateScheduleSchema.safeParseAsync({
145170
modelId: "model",
146171
id: "123",
147-
input: { type: "publish" }
172+
input: {
173+
type: "publish"
174+
}
148175
});
149176
expect(result.success).toBe(false);
150177
});
151178

152179
it("cancelScheduleSchema: accepts valid input and returns expected data", async () => {
153-
const input = { modelId: "model", id: "123" };
180+
const input: IExpectedCancelSchemaInput = {
181+
modelId: "model",
182+
id: "123"
183+
};
154184
const result = await cancelScheduleSchema.safeParseAsync(input);
155185
expect(result.success).toBe(true);
156186
if (result.success) {

packages/api-headless-cms-scheduler/__tests__/scheduler/actions/PublishScheduleAction.test.ts

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import { createMockTargetModel } from "~tests/mocks/targetModel.js";
66
import { PublishScheduleAction } from "~/scheduler/actions/PublishScheduleAction.js";
77
import type { CmsEntry, CmsEntryValues } from "@webiny/api-headless-cms/types/index.js";
88
import { ScheduleRecord } from "~/scheduler/ScheduleRecord.js";
9-
import type { IScheduleEntryValues } from "~/scheduler/types.js";
9+
import { type IScheduleEntryValues, ScheduleType } from "~/scheduler/types.js";
1010
import { createScheduleRecordId } from "~/scheduler/createScheduleRecordId.js";
11+
import { dateToISOString } from "~/scheduler/dates.js";
12+
import { SchedulerService } from "~/service/SchedulerService.js";
13+
import { CreateScheduleCommand, SchedulerClient } from "@webiny/aws-sdk/client-scheduler/index.js";
14+
import { mockClient } from "aws-sdk-client-mock";
1115

1216
describe("PublishScheduleAction", () => {
1317
const service = createMockService();
@@ -44,7 +48,7 @@ describe("PublishScheduleAction", () => {
4448
const result = await action.schedule({
4549
input: {
4650
immediately: true,
47-
type: "publish"
51+
type: ScheduleType.publish
4852
},
4953
targetId: "target-id#0002",
5054
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
@@ -58,12 +62,13 @@ describe("PublishScheduleAction", () => {
5862
scheduledBy: getIdentity(),
5963
publishOn: expect.any(Date),
6064
unpublishOn: undefined,
61-
type: "publish",
65+
dateOn: expect.any(Date),
66+
type: ScheduleType.publish,
6267
title: "Test Entry"
6368
});
6469
});
6570

66-
it("should publish an entry immediately if the dateOn is in the past", async () => {
71+
it("should publish an entry immediately if the scheduleOn is in the past", async () => {
6772
const updateEntryMock = jest.fn(async <T = CmsEntryValues>() => {
6873
return {} as CmsEntry<T>;
6974
});
@@ -93,11 +98,11 @@ describe("PublishScheduleAction", () => {
9398
cms
9499
});
95100

96-
const dateOn = new Date(Date.now() - 1000000);
101+
const scheduleOn = new Date(Date.now() - 1000000);
97102
const result = await action.schedule({
98103
input: {
99-
dateOn,
100-
type: "publish"
104+
scheduleOn,
105+
type: ScheduleType.publish
101106
},
102107
targetId: "target-id#0002",
103108
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
@@ -109,43 +114,53 @@ describe("PublishScheduleAction", () => {
109114
targetId: "target-id#0002",
110115
model: targetModel,
111116
scheduledBy: getIdentity(),
112-
publishOn: dateOn,
117+
publishOn: expect.any(Date),
113118
unpublishOn: undefined,
114-
type: "publish",
119+
dateOn: undefined,
120+
type: ScheduleType.publish,
115121
title: "Test Entry"
116122
});
117123

118124
expect(updateEntryMock).toHaveBeenCalledTimes(1);
119125
expect(updateEntryMock).toHaveBeenCalledWith(targetModel, "target-id#0002", {
120126
firstPublishedBy: getIdentity(),
121-
firstPublishedOn: dateOn.toISOString(),
127+
firstPublishedOn: scheduleOn.toISOString(),
122128
lastPublishedBy: getIdentity(),
123-
lastPublishedOn: dateOn.toISOString()
129+
lastPublishedOn: scheduleOn.toISOString()
124130
});
125131
});
126132

127133
it("should schedule a publish action for a future date", async () => {
128-
const serviceCreate = jest.fn(async () => {});
129-
const service = createMockService({
130-
create: serviceCreate
134+
const client = mockClient(SchedulerClient);
135+
client.on(CreateScheduleCommand).resolves({
136+
$metadata: {
137+
httpStatusCode: 200
138+
}
131139
});
132-
const dateOn = new Date(Date.now() + 1000000);
140+
const service = new SchedulerService({
141+
getClient: () => client,
142+
config: {
143+
lambdaArn: "arn:aws:lambda:us-east-1:123456789012:function:my-function",
144+
roleArn: "arn:aws:iam::123456789012:role/my-role"
145+
}
146+
});
147+
const scheduleOn = new Date(Date.now() + 1000000);
133148

134149
const crateEntryMock = jest.fn(async () => {
135-
// @ts-expect-error
136-
const entry: CmsEntry<IScheduleEntryValues> = {
150+
const entry: Pick<CmsEntry<IScheduleEntryValues>, "id" | "values" | "savedBy"> = {
137151
id: createScheduleRecordId(`target-id#0002`),
138152
values: {
139153
targetId: "target-id#0002",
140-
type: "publish",
141-
dateOn: dateOn.toISOString(),
154+
type: ScheduleType.publish,
155+
scheduledOn: dateToISOString(scheduleOn),
156+
dateOn: dateToISOString(scheduleOn),
142157
title: "Test Entry",
143158
targetModelId: targetModel.modelId,
144159
scheduledBy: getIdentity()
145160
},
146161
savedBy: getIdentity()
147162
};
148-
return entry;
163+
return entry as CmsEntry<IScheduleEntryValues>;
149164
});
150165
const cms = createMockCms({
151166
// @ts-expect-error
@@ -176,8 +191,8 @@ describe("PublishScheduleAction", () => {
176191

177192
const result = await action.schedule({
178193
input: {
179-
dateOn,
180-
type: "publish"
194+
scheduleOn,
195+
type: ScheduleType.publish
181196
},
182197
targetId: "target-id#0002",
183198
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
@@ -189,21 +204,23 @@ describe("PublishScheduleAction", () => {
189204
targetId: "target-id#0002",
190205
model: targetModel,
191206
scheduledBy: getIdentity(),
192-
publishOn: dateOn,
207+
publishOn: scheduleOn,
193208
unpublishOn: undefined,
194-
type: "publish",
209+
dateOn: scheduleOn,
210+
type: ScheduleType.publish,
195211
title: "Test Entry"
196212
});
197213

198214
expect(crateEntryMock).toHaveBeenCalledTimes(1);
199215
expect(crateEntryMock).toHaveBeenCalledWith(scheduleModel, {
200216
id: createScheduleRecordId(`target-id#0002`),
201-
dateOn: dateOn.toISOString(),
217+
dateOn: undefined,
202218
scheduledBy: getIdentity(),
219+
scheduledOn: dateToISOString(scheduleOn),
203220
targetId: "target-id#0002",
204221
targetModelId: "targetModel",
205222
title: "Test Entry",
206-
type: "publish"
223+
type: ScheduleType.publish
207224
});
208225
});
209226
});

0 commit comments

Comments
 (0)