@@ -6,8 +6,12 @@ import { createMockTargetModel } from "~tests/mocks/targetModel.js";
6
6
import { PublishScheduleAction } from "~/scheduler/actions/PublishScheduleAction.js" ;
7
7
import type { CmsEntry , CmsEntryValues } from "@webiny/api-headless-cms/types/index.js" ;
8
8
import { ScheduleRecord } from "~/scheduler/ScheduleRecord.js" ;
9
- import type { IScheduleEntryValues } from "~/scheduler/types.js" ;
9
+ import { type IScheduleEntryValues , ScheduleType } from "~/scheduler/types.js" ;
10
10
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" ;
11
15
12
16
describe ( "PublishScheduleAction" , ( ) => {
13
17
const service = createMockService ( ) ;
@@ -44,7 +48,7 @@ describe("PublishScheduleAction", () => {
44
48
const result = await action . schedule ( {
45
49
input : {
46
50
immediately : true ,
47
- type : " publish"
51
+ type : ScheduleType . publish
48
52
} ,
49
53
targetId : "target-id#0002" ,
50
54
scheduleRecordId : createScheduleRecordId ( `target-id#0002` )
@@ -58,12 +62,13 @@ describe("PublishScheduleAction", () => {
58
62
scheduledBy : getIdentity ( ) ,
59
63
publishOn : expect . any ( Date ) ,
60
64
unpublishOn : undefined ,
61
- type : "publish" ,
65
+ dateOn : expect . any ( Date ) ,
66
+ type : ScheduleType . publish ,
62
67
title : "Test Entry"
63
68
} ) ;
64
69
} ) ;
65
70
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 ( ) => {
67
72
const updateEntryMock = jest . fn ( async < T = CmsEntryValues > ( ) => {
68
73
return { } as CmsEntry < T > ;
69
74
} ) ;
@@ -93,11 +98,11 @@ describe("PublishScheduleAction", () => {
93
98
cms
94
99
} ) ;
95
100
96
- const dateOn = new Date ( Date . now ( ) - 1000000 ) ;
101
+ const scheduleOn = new Date ( Date . now ( ) - 1000000 ) ;
97
102
const result = await action . schedule ( {
98
103
input : {
99
- dateOn ,
100
- type : " publish"
104
+ scheduleOn ,
105
+ type : ScheduleType . publish
101
106
} ,
102
107
targetId : "target-id#0002" ,
103
108
scheduleRecordId : createScheduleRecordId ( `target-id#0002` )
@@ -109,43 +114,53 @@ describe("PublishScheduleAction", () => {
109
114
targetId : "target-id#0002" ,
110
115
model : targetModel ,
111
116
scheduledBy : getIdentity ( ) ,
112
- publishOn : dateOn ,
117
+ publishOn : expect . any ( Date ) ,
113
118
unpublishOn : undefined ,
114
- type : "publish" ,
119
+ dateOn : undefined ,
120
+ type : ScheduleType . publish ,
115
121
title : "Test Entry"
116
122
} ) ;
117
123
118
124
expect ( updateEntryMock ) . toHaveBeenCalledTimes ( 1 ) ;
119
125
expect ( updateEntryMock ) . toHaveBeenCalledWith ( targetModel , "target-id#0002" , {
120
126
firstPublishedBy : getIdentity ( ) ,
121
- firstPublishedOn : dateOn . toISOString ( ) ,
127
+ firstPublishedOn : scheduleOn . toISOString ( ) ,
122
128
lastPublishedBy : getIdentity ( ) ,
123
- lastPublishedOn : dateOn . toISOString ( )
129
+ lastPublishedOn : scheduleOn . toISOString ( )
124
130
} ) ;
125
131
} ) ;
126
132
127
133
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
+ }
131
139
} ) ;
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 ) ;
133
148
134
149
const crateEntryMock = jest . fn ( async ( ) => {
135
- // @ts -expect-error
136
- const entry : CmsEntry < IScheduleEntryValues > = {
150
+ const entry : Pick < CmsEntry < IScheduleEntryValues > , "id" | "values" | "savedBy" > = {
137
151
id : createScheduleRecordId ( `target-id#0002` ) ,
138
152
values : {
139
153
targetId : "target-id#0002" ,
140
- type : "publish" ,
141
- dateOn : dateOn . toISOString ( ) ,
154
+ type : ScheduleType . publish ,
155
+ scheduledOn : dateToISOString ( scheduleOn ) ,
156
+ dateOn : dateToISOString ( scheduleOn ) ,
142
157
title : "Test Entry" ,
143
158
targetModelId : targetModel . modelId ,
144
159
scheduledBy : getIdentity ( )
145
160
} ,
146
161
savedBy : getIdentity ( )
147
162
} ;
148
- return entry ;
163
+ return entry as CmsEntry < IScheduleEntryValues > ;
149
164
} ) ;
150
165
const cms = createMockCms ( {
151
166
// @ts -expect-error
@@ -176,8 +191,8 @@ describe("PublishScheduleAction", () => {
176
191
177
192
const result = await action . schedule ( {
178
193
input : {
179
- dateOn ,
180
- type : " publish"
194
+ scheduleOn ,
195
+ type : ScheduleType . publish
181
196
} ,
182
197
targetId : "target-id#0002" ,
183
198
scheduleRecordId : createScheduleRecordId ( `target-id#0002` )
@@ -189,21 +204,23 @@ describe("PublishScheduleAction", () => {
189
204
targetId : "target-id#0002" ,
190
205
model : targetModel ,
191
206
scheduledBy : getIdentity ( ) ,
192
- publishOn : dateOn ,
207
+ publishOn : scheduleOn ,
193
208
unpublishOn : undefined ,
194
- type : "publish" ,
209
+ dateOn : scheduleOn ,
210
+ type : ScheduleType . publish ,
195
211
title : "Test Entry"
196
212
} ) ;
197
213
198
214
expect ( crateEntryMock ) . toHaveBeenCalledTimes ( 1 ) ;
199
215
expect ( crateEntryMock ) . toHaveBeenCalledWith ( scheduleModel , {
200
216
id : createScheduleRecordId ( `target-id#0002` ) ,
201
- dateOn : dateOn . toISOString ( ) ,
217
+ dateOn : undefined ,
202
218
scheduledBy : getIdentity ( ) ,
219
+ scheduledOn : dateToISOString ( scheduleOn ) ,
203
220
targetId : "target-id#0002" ,
204
221
targetModelId : "targetModel" ,
205
222
title : "Test Entry" ,
206
- type : " publish"
223
+ type : ScheduleType . publish
207
224
} ) ;
208
225
} ) ;
209
226
} ) ;
0 commit comments