Skip to content

Commit c047bf5

Browse files
committed
fix(api-headless-cms-scheduler): tests
1 parent b1f7dfc commit c047bf5

File tree

15 files changed

+91
-42
lines changed

15 files changed

+91
-42
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ describe("graphql/schema", () => {
5151
expect(result.data).toEqual(input);
5252
}
5353
});
54-
it("listScheduleSchema: rejects missing modelId or where", async () => {
55-
const result1 = await listScheduleSchema.safeParseAsync({});
56-
const result2 = await listScheduleSchema.safeParseAsync({ modelId: "model" });
57-
expect(result1.success).toBe(false);
58-
expect(result2.success).toBe(false);
54+
it("listScheduleSchema: rejects missing modelId", async () => {
55+
const result = await listScheduleSchema.safeParseAsync({});
56+
expect(result.success).toBe(false);
5957
});
6058
it("listScheduleSchema: rejects invalid sort values", async () => {
6159
const result = await listScheduleSchema.safeParseAsync({

packages/api-headless-cms-scheduler/__tests__/handler/Handler.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import type { ScheduleContext } from "~/types.js";
66
import { NotFoundError } from "@webiny/handler-graphql";
77
import { type IScheduleEntryValues, ScheduleType } from "~/scheduler/types.js";
88
import type { CmsEntry } from "@webiny/api-headless-cms/types/index.js";
9-
import { createScheduleRecordId } from "~/scheduler/createScheduleRecordId.js";
9+
import {
10+
createScheduleRecordId,
11+
createScheduleRecordIdWithVersion
12+
} from "~/scheduler/createScheduleRecordId.js";
1013
import { MOCK_TARGET_MODEL_ID } from "~tests/mocks/targetModel.js";
1114
import { dateToISOString } from "~/scheduler/dates.js";
1215
import { UnpublishHandlerAction } from "~/handler/actions/UnpublishHandlerAction.js";
1316
import { PublishHandlerAction } from "~/handler/actions/PublishHandlerAction.js";
1417

1518
const createEventScheduleRecordId = (targetId: string): string => {
16-
return `${createScheduleRecordId(targetId)}#0001`;
19+
return `${createScheduleRecordIdWithVersion(targetId)}`;
1720
};
1821

1922
describe("Handler", () => {

packages/api-headless-cms-scheduler/__tests__/mocks/cms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const createMockCms = (
55
cms?: Partial<ScheduleExecutorCms & ScheduleFetcherCms>
66
): ScheduleExecutorCms & ScheduleFetcherCms => {
77
return {
8-
listEntries: jest.fn(),
8+
listLatestEntries: jest.fn(),
99
publishEntry: jest.fn(),
1010
unpublishEntry: jest.fn(),
1111
updateEntry: jest.fn(),

packages/api-headless-cms-scheduler/__tests__/scheduler/ScheduleFetcher.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("ScheduleFetcher", () => {
1111
function createMockCms(overrides: Partial<HeadlessCms> = {}) {
1212
return {
1313
getEntryById: jest.fn(),
14-
listEntries: jest.fn(),
14+
listLatestEntries: jest.fn(),
1515
...overrides
1616
} as unknown as HeadlessCms;
1717
}
@@ -69,7 +69,7 @@ describe("ScheduleFetcher", () => {
6969
savedBy: { id: "user-1" }
7070
};
7171
const cms = createMockCms({
72-
listEntries: jest.fn().mockResolvedValue([[entry], entryMeta])
72+
listLatestEntries: jest.fn().mockResolvedValue([[entry], entryMeta])
7373
});
7474
const fetcher = new ScheduleFetcher({ cms, targetModel, scheduleModel });
7575
const params: ISchedulerListParams = {

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import { PublishScheduleAction } from "~/scheduler/actions/PublishScheduleAction
77
import type { CmsEntry, CmsEntryValues } from "@webiny/api-headless-cms/types/index.js";
88
import { ScheduleRecord } from "~/scheduler/ScheduleRecord.js";
99
import { type IScheduleEntryValues, ScheduleType } from "~/scheduler/types.js";
10-
import { createScheduleRecordId } from "~/scheduler/createScheduleRecordId.js";
10+
import {
11+
createScheduleRecordId,
12+
createScheduleRecordIdWithVersion
13+
} from "~/scheduler/createScheduleRecordId.js";
1114
import { dateToISOString } from "~/scheduler/dates.js";
1215
import { SchedulerService } from "~/service/SchedulerService.js";
1316
import { CreateScheduleCommand, SchedulerClient } from "@webiny/aws-sdk/client-scheduler/index.js";
1417
import { mockClient } from "aws-sdk-client-mock";
18+
import { createMockFetcher } from "~tests/mocks/fetcher";
1519

1620
describe("PublishScheduleAction", () => {
1721
const service = createMockService();
22+
const fetcher = createMockFetcher();
1823
const getIdentity = createMockGetIdentity();
1924
const scheduleModel = createMockScheduleModel();
2025
const targetModel = createMockTargetModel();
@@ -42,7 +47,8 @@ describe("PublishScheduleAction", () => {
4247
getIdentity,
4348
targetModel,
4449
scheduleModel,
45-
cms
50+
cms,
51+
fetcher
4652
});
4753

4854
const result = await action.schedule({
@@ -51,12 +57,12 @@ describe("PublishScheduleAction", () => {
5157
type: ScheduleType.publish
5258
},
5359
targetId: "target-id#0002",
54-
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
60+
scheduleRecordId: createScheduleRecordIdWithVersion(`target-id#0002`)
5561
});
5662

5763
expect(result).toBeInstanceOf(ScheduleRecord);
5864
expect(result).toEqual({
59-
id: createScheduleRecordId(`target-id#0002`),
65+
id: createScheduleRecordIdWithVersion(`target-id#0002`),
6066
targetId: "target-id#0002",
6167
model: targetModel,
6268
scheduledBy: getIdentity(),
@@ -95,7 +101,8 @@ describe("PublishScheduleAction", () => {
95101
getIdentity,
96102
targetModel,
97103
scheduleModel,
98-
cms
104+
cms,
105+
fetcher
99106
});
100107

101108
const scheduleOn = new Date(Date.now() - 1000000);
@@ -105,12 +112,12 @@ describe("PublishScheduleAction", () => {
105112
type: ScheduleType.publish
106113
},
107114
targetId: "target-id#0002",
108-
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
115+
scheduleRecordId: createScheduleRecordIdWithVersion(`target-id#0002`)
109116
});
110117

111118
expect(result).toBeInstanceOf(ScheduleRecord);
112119
expect(result).toEqual({
113-
id: createScheduleRecordId(`target-id#0002`),
120+
id: createScheduleRecordIdWithVersion(`target-id#0002`),
114121
targetId: "target-id#0002",
115122
model: targetModel,
116123
scheduledBy: getIdentity(),
@@ -148,7 +155,7 @@ describe("PublishScheduleAction", () => {
148155

149156
const crateEntryMock = jest.fn(async () => {
150157
const entry: Pick<CmsEntry<IScheduleEntryValues>, "id" | "values" | "savedBy"> = {
151-
id: createScheduleRecordId(`target-id#0002`),
158+
id: createScheduleRecordIdWithVersion(`target-id#0002`),
152159
values: {
153160
targetId: "target-id#0002",
154161
type: ScheduleType.publish,
@@ -186,7 +193,8 @@ describe("PublishScheduleAction", () => {
186193
getIdentity,
187194
targetModel,
188195
scheduleModel,
189-
cms
196+
cms,
197+
fetcher
190198
});
191199

192200
const result = await action.schedule({
@@ -195,12 +203,12 @@ describe("PublishScheduleAction", () => {
195203
type: ScheduleType.publish
196204
},
197205
targetId: "target-id#0002",
198-
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
206+
scheduleRecordId: createScheduleRecordIdWithVersion(`target-id#0002`)
199207
});
200208

201209
expect(result).toBeInstanceOf(ScheduleRecord);
202210
expect(result).toEqual({
203-
id: createScheduleRecordId(`target-id#0002`),
211+
id: createScheduleRecordIdWithVersion(`target-id#0002`),
204212
targetId: "target-id#0002",
205213
model: targetModel,
206214
scheduledBy: getIdentity(),

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import { UnpublishScheduleAction } from "~/scheduler/actions/UnpublishScheduleAc
77
import type { CmsEntry } from "@webiny/api-headless-cms/types/index.js";
88
import { ScheduleRecord } from "~/scheduler/ScheduleRecord.js";
99
import { type IScheduleEntryValues, ScheduleType } from "~/scheduler/types.js";
10-
import { createScheduleRecordId } from "~/scheduler/createScheduleRecordId.js";
10+
import {
11+
createScheduleRecordId,
12+
createScheduleRecordIdWithVersion
13+
} from "~/scheduler/createScheduleRecordId.js";
1114
import { mockClient } from "aws-sdk-client-mock";
1215
import { dateToISOString } from "~/scheduler/dates.js";
1316
import { CreateScheduleCommand, SchedulerClient } from "@webiny/aws-sdk/client-scheduler/index.js";
1417
import { SchedulerService } from "~/service/SchedulerService.js";
18+
import { createMockFetcher } from "~tests/mocks/fetcher.js";
1519

1620
describe("UnpublishScheduleAction", () => {
1721
const service = createMockService();
22+
const fetcher = createMockFetcher();
1823
const getIdentity = createMockGetIdentity();
1924
const scheduleModel = createMockScheduleModel();
2025
const targetModel = createMockTargetModel();
@@ -42,7 +47,8 @@ describe("UnpublishScheduleAction", () => {
4247
getIdentity,
4348
targetModel,
4449
scheduleModel,
45-
cms
50+
cms,
51+
fetcher
4652
});
4753

4854
const result = await action.schedule({
@@ -51,12 +57,12 @@ describe("UnpublishScheduleAction", () => {
5157
type: ScheduleType.unpublish
5258
},
5359
targetId: "target-id#0002",
54-
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
60+
scheduleRecordId: createScheduleRecordIdWithVersion(`target-id#0002`)
5561
});
5662

5763
expect(result).toBeInstanceOf(ScheduleRecord);
5864
expect(result).toEqual({
59-
id: createScheduleRecordId(`target-id#0002`),
65+
id: createScheduleRecordIdWithVersion(`target-id#0002`),
6066
targetId: "target-id#0002",
6167
model: targetModel,
6268
scheduledBy: getIdentity(),
@@ -91,7 +97,8 @@ describe("UnpublishScheduleAction", () => {
9197
getIdentity,
9298
targetModel,
9399
scheduleModel,
94-
cms
100+
cms,
101+
fetcher
95102
});
96103

97104
const scheduleOn = new Date(Date.now() - 1000000);
@@ -101,12 +108,12 @@ describe("UnpublishScheduleAction", () => {
101108
type: ScheduleType.unpublish
102109
},
103110
targetId: "target-id#0002",
104-
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
111+
scheduleRecordId: createScheduleRecordIdWithVersion(`target-id#0002`)
105112
});
106113

107114
expect(result).toBeInstanceOf(ScheduleRecord);
108115
expect(result).toEqual({
109-
id: createScheduleRecordId(`target-id#0002`),
116+
id: createScheduleRecordIdWithVersion(`target-id#0002`),
110117
targetId: "target-id#0002",
111118
model: targetModel,
112119
scheduledBy: getIdentity(),
@@ -138,7 +145,7 @@ describe("UnpublishScheduleAction", () => {
138145

139146
const createEntryMock = jest.fn(async () => {
140147
const entry: Pick<CmsEntry<IScheduleEntryValues>, "id" | "values" | "savedBy"> = {
141-
id: createScheduleRecordId(`target-id#0002`),
148+
id: createScheduleRecordIdWithVersion(`target-id#0002`),
142149
values: {
143150
targetId: "target-id#0002",
144151
type: ScheduleType.unpublish,
@@ -176,7 +183,8 @@ describe("UnpublishScheduleAction", () => {
176183
getIdentity,
177184
targetModel,
178185
scheduleModel,
179-
cms
186+
cms,
187+
fetcher
180188
});
181189

182190
const result = await action.schedule({
@@ -185,12 +193,12 @@ describe("UnpublishScheduleAction", () => {
185193
type: ScheduleType.unpublish
186194
},
187195
targetId: "target-id#0002",
188-
scheduleRecordId: createScheduleRecordId(`target-id#0002`)
196+
scheduleRecordId: createScheduleRecordIdWithVersion(`target-id#0002`)
189197
});
190198

191199
expect(result).toBeInstanceOf(ScheduleRecord);
192200
expect(result).toEqual({
193-
id: createScheduleRecordId(`target-id#0002`),
201+
id: createScheduleRecordIdWithVersion(`target-id#0002`),
194202
targetId: "target-id#0002",
195203
model: targetModel,
196204
scheduledBy: getIdentity(),

packages/api-headless-cms-scheduler/__tests__/scheduler/createScheduleRecordId.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1-
import { createScheduleRecordId } from "~/scheduler/createScheduleRecordId.js";
1+
import {
2+
createScheduleRecordId,
3+
createScheduleRecordIdWithVersion
4+
} from "~/scheduler/createScheduleRecordId.js";
25
import { SCHEDULE_ID_PREFIX } from "~/constants.js";
36

47
describe("createScheduleRecordId", () => {
5-
it("should create a valid schedule record ID", () => {
8+
it("should create a valid schedule record ID with version", () => {
9+
const result = createScheduleRecordIdWithVersion("target-id#0001");
10+
11+
expect(result).toEqual(`${SCHEDULE_ID_PREFIX}target-id-0001#0001`);
12+
});
13+
14+
it("should create a valid schedule record ID from already created record ID with version", () => {
15+
const result = createScheduleRecordIdWithVersion("target-id#0001");
16+
17+
expect(result).toEqual(`${SCHEDULE_ID_PREFIX}target-id-0001#0001`);
18+
19+
const rerunResult = createScheduleRecordIdWithVersion(result);
20+
21+
expect(rerunResult).toEqual(`${SCHEDULE_ID_PREFIX}target-id-0001#0001`);
22+
});
23+
24+
it("should create a valid schedule record ID without version", () => {
625
const result = createScheduleRecordId("target-id#0001");
726

827
expect(result).toEqual(`${SCHEDULE_ID_PREFIX}target-id-0001`);
928
});
1029

11-
it("should create a valid schedule record ID from already created record ID", () => {
30+
it("should create a valid schedule record ID from already created record ID without version", () => {
1231
const result = createScheduleRecordId("target-id#0001");
1332

1433
expect(result).toEqual(`${SCHEDULE_ID_PREFIX}target-id-0001`);

packages/api-headless-cms-scheduler/src/scheduler/ScheduleExecutor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type ISchedulerInput,
77
ScheduleType
88
} from "~/scheduler/types.js";
9-
import { createScheduleRecordId } from "~/scheduler/createScheduleRecordId.js";
9+
import { createScheduleRecordIdWithVersion } from "~/scheduler/createScheduleRecordId.js";
1010
import type { PublishScheduleActionCms } from "~/scheduler/actions/PublishScheduleAction.js";
1111
import type { UnpublishScheduleActionCms } from "~/scheduler/actions/UnpublishScheduleAction.js";
1212
import { WebinyError } from "@webiny/error";
@@ -28,7 +28,7 @@ export class ScheduleExecutor implements IScheduleExecutor {
2828
}
2929

3030
public async schedule(targetId: string, input: ISchedulerInput): Promise<IScheduleRecord> {
31-
const scheduleRecordId = createScheduleRecordId(targetId);
31+
const scheduleRecordId = createScheduleRecordIdWithVersion(targetId);
3232
const original = await this.fetcher.getScheduled(targetId);
3333

3434
const action = this.getAction(input.type);
@@ -45,7 +45,7 @@ export class ScheduleExecutor implements IScheduleExecutor {
4545
}
4646

4747
public async cancel(initialId: string): Promise<void> {
48-
const id = createScheduleRecordId(initialId);
48+
const id = createScheduleRecordIdWithVersion(initialId);
4949
const original = await this.fetcher.getScheduled(id);
5050
if (!original) {
5151
throw new WebinyError(

packages/api-headless-cms-scheduler/src/scheduler/ScheduleFetcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
ISchedulerListResponse
88
} from "~/scheduler/types.js";
99
import { NotFoundError } from "@webiny/handler-graphql";
10-
import { createScheduleRecordId } from "~/scheduler/createScheduleRecordId.js";
10+
import { createScheduleRecordIdWithVersion } from "~/scheduler/createScheduleRecordId.js";
1111
import { transformScheduleEntry } from "~/scheduler/ScheduleRecord.js";
1212
import { convertException } from "@webiny/utils";
1313

@@ -31,7 +31,7 @@ export class ScheduleFetcher implements IScheduleFetcher {
3131
}
3232

3333
public async getScheduled(targetId: string): Promise<IScheduleRecord | null> {
34-
const scheduleRecordId = createScheduleRecordId(targetId);
34+
const scheduleRecordId = createScheduleRecordIdWithVersion(targetId);
3535
try {
3636
const entry = await this.cms.getEntryById<IScheduleEntryValues>(
3737
this.scheduleModel,

packages/api-headless-cms-scheduler/src/scheduler/createScheduleRecordId.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { parseIdentifier } from "@webiny/utils/parseIdentifier.js";
22
import { SCHEDULE_ID_PREFIX } from "~/constants.js";
33
import { zeroPad } from "@webiny/utils/zeroPad.js";
44

5+
export const createScheduleRecordIdWithVersion = (input: string): string => {
6+
const recordId = createScheduleRecordId(input);
7+
const { id } = parseIdentifier(recordId);
8+
9+
return `${id}#0001`;
10+
};
11+
512
export const createScheduleRecordId = (input: string): string => {
613
/**
714
* A possibility that the input is already a schedule record ID?
@@ -11,5 +18,5 @@ export const createScheduleRecordId = (input: string): string => {
1118
}
1219

1320
const { id, version } = parseIdentifier(input);
14-
return `${SCHEDULE_ID_PREFIX}${id}-${zeroPad(version || 1)}#0001`;
21+
return `${SCHEDULE_ID_PREFIX}${id}-${zeroPad(version || 1)}`;
1522
};

0 commit comments

Comments
 (0)