Skip to content

Commit 8a9b9c3

Browse files
committed
fixed another failing test suite
1 parent 9a1d513 commit 8a9b9c3

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

packages/event-processor/__tests__/pendingEventsDispatcher.spec.ts

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('LocalStoragePendingEventsDispatcher', () => {
5151
localStorage.clear()
5252
})
5353

54-
it('should properly send the events to the passed in eventDispatcher, when callback statusCode=200', () => {
54+
it('should properly send the events to the passed in eventDispatcher, when callback statusCode=200', (done) => {
5555
const callback = jest.fn()
5656
const eventV1Request: EventV1Request = {
5757
url: 'http://cdn.com',
@@ -61,22 +61,25 @@ describe('LocalStoragePendingEventsDispatcher', () => {
6161

6262
pendingEventsDispatcher.dispatchEvent(eventV1Request, callback)
6363

64-
expect(callback).not.toHaveBeenCalled()
65-
// manually invoke original eventDispatcher callback
66-
const internalDispatchCall = ((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock)
67-
.mock.calls[0]
68-
internalDispatchCall[1]({ statusCode: 200 })
69-
70-
// assert that the original dispatch function was called with the request
71-
expect((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock).toBeCalledTimes(1)
72-
expect(internalDispatchCall[0]).toEqual(eventV1Request)
73-
74-
// assert that the passed in callback to pendingEventsDispatcher was called
75-
expect(callback).toHaveBeenCalledTimes(1)
76-
expect(callback).toHaveBeenCalledWith({ statusCode: 200 })
64+
setTimeout(() => {
65+
expect(callback).not.toHaveBeenCalled()
66+
// manually invoke original eventDispatcher callback
67+
const internalDispatchCall = ((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock)
68+
.mock.calls[0]
69+
internalDispatchCall[1]({ statusCode: 200 })
70+
71+
// assert that the original dispatch function was called with the request
72+
expect((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock).toBeCalledTimes(1)
73+
expect(internalDispatchCall[0]).toEqual(eventV1Request)
74+
75+
// assert that the passed in callback to pendingEventsDispatcher was called
76+
expect(callback).toHaveBeenCalledTimes(1)
77+
expect(callback).toHaveBeenCalledWith({ statusCode: 200 })
78+
done()
79+
})
7780
})
7881

79-
it('should properly send the events to the passed in eventDispatcher, when callback statusCode=400', () => {
82+
it('should properly send the events to the passed in eventDispatcher, when callback statusCode=400', (done) => {
8083
const callback = jest.fn()
8184
const eventV1Request: EventV1Request = {
8285
url: 'http://cdn.com',
@@ -86,19 +89,22 @@ describe('LocalStoragePendingEventsDispatcher', () => {
8689

8790
pendingEventsDispatcher.dispatchEvent(eventV1Request, callback)
8891

89-
expect(callback).not.toHaveBeenCalled()
90-
// manually invoke original eventDispatcher callback
91-
const internalDispatchCall = ((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock)
92-
.mock.calls[0]
93-
internalDispatchCall[1]({ statusCode: 400 })
94-
95-
// assert that the original dispatch function was called with the request
96-
expect((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock).toBeCalledTimes(1)
97-
expect(internalDispatchCall[0]).toEqual(eventV1Request)
98-
99-
// assert that the passed in callback to pendingEventsDispatcher was called
100-
expect(callback).toHaveBeenCalledTimes(1)
101-
expect(callback).toHaveBeenCalledWith({ statusCode: 400})
92+
setTimeout(() => {
93+
expect(callback).not.toHaveBeenCalled()
94+
// manually invoke original eventDispatcher callback
95+
const internalDispatchCall = ((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock)
96+
.mock.calls[0]
97+
internalDispatchCall[1]({ statusCode: 400 })
98+
99+
// assert that the original dispatch function was called with the request
100+
expect((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock).toBeCalledTimes(1)
101+
expect(internalDispatchCall[0]).toEqual(eventV1Request)
102+
103+
// assert that the passed in callback to pendingEventsDispatcher was called
104+
expect(callback).toHaveBeenCalledTimes(1)
105+
expect(callback).toHaveBeenCalledWith({ statusCode: 400})
106+
done()
107+
})
102108
})
103109
})
104110

@@ -129,7 +135,7 @@ describe('PendingEventsDispatcher', () => {
129135

130136
describe('dispatch', () => {
131137
describe('when the dispatch is successful', () => {
132-
it('should save the pendingEvent to the store and remove it once dispatch is completed', () => {
138+
it('should save the pendingEvent to the store and remove it once dispatch is completed', async () => {
133139
const callback = jest.fn()
134140
const eventV1Request: EventV1Request = {
135141
url: 'http://cdn.com',
@@ -139,8 +145,8 @@ describe('PendingEventsDispatcher', () => {
139145

140146
pendingEventsDispatcher.dispatchEvent(eventV1Request, callback)
141147

142-
expect(store.values()).toHaveLength(1)
143-
expect(store.get('uuid')).toEqual({
148+
expect(await store.values()).toHaveLength(1)
149+
expect(await store.get('uuid')).toEqual({
144150
uuid: 'uuid',
145151
timestamp: 1,
146152
request: eventV1Request,
@@ -162,12 +168,12 @@ describe('PendingEventsDispatcher', () => {
162168
expect(callback).toHaveBeenCalledTimes(1)
163169
expect(callback).toHaveBeenCalledWith({ statusCode: 200 })
164170

165-
expect(store.values()).toHaveLength(0)
171+
expect(await store.values()).toHaveLength(0)
166172
})
167173
})
168174

169175
describe('when the dispatch is unsuccessful', () => {
170-
it('should save the pendingEvent to the store and remove it once dispatch is completed', () => {
176+
it('should save the pendingEvent to the store and remove it once dispatch is completed', async () => {
171177
const callback = jest.fn()
172178
const eventV1Request: EventV1Request = {
173179
url: 'http://cdn.com',
@@ -177,8 +183,8 @@ describe('PendingEventsDispatcher', () => {
177183

178184
pendingEventsDispatcher.dispatchEvent(eventV1Request, callback)
179185

180-
expect(store.values()).toHaveLength(1)
181-
expect(store.get('uuid')).toEqual({
186+
expect(await store.values()).toHaveLength(1)
187+
expect(await store.get('uuid')).toEqual({
182188
uuid: 'uuid',
183189
timestamp: 1,
184190
request: eventV1Request,
@@ -200,24 +206,24 @@ describe('PendingEventsDispatcher', () => {
200206
expect(callback).toHaveBeenCalledTimes(1)
201207
expect(callback).toHaveBeenCalledWith({ statusCode: 400 })
202208

203-
expect(store.values()).toHaveLength(0)
209+
expect(await store.values()).toHaveLength(0)
204210
})
205211
})
206212
})
207213

208214
describe('sendPendingEvents', () => {
209215
describe('when no pending events are in the store', () => {
210-
it('should not invoked dispatch', () => {
211-
expect(store.values()).toHaveLength(0)
216+
it('should not invoked dispatch', async () => {
217+
expect(await store.values()).toHaveLength(0)
212218

213219
pendingEventsDispatcher.sendPendingEvents()
214220
expect(originalEventDispatcher.dispatchEvent).not.toHaveBeenCalled()
215221
})
216222
})
217223

218224
describe('when there are multiple pending events in the store', () => {
219-
it('should dispatch all of the pending events, and remove them from store', () => {
220-
expect(store.values()).toHaveLength(0)
225+
it('should dispatch all of the pending events, and remove them from store', async (done) => {
226+
expect(await store.values()).toHaveLength(0)
221227

222228
const callback = jest.fn()
223229
const eventV1Request1: EventV1Request = {
@@ -232,29 +238,33 @@ describe('PendingEventsDispatcher', () => {
232238
params: ({ id: 'event2' } as unknown) as EventV1,
233239
}
234240

235-
store.set('uuid1', {
241+
await store.set('uuid1', {
236242
uuid: 'uuid1',
237243
timestamp: 1,
238244
request: eventV1Request1,
239245
})
240-
store.set('uuid2', {
246+
await store.set('uuid2', {
241247
uuid: 'uuid2',
242248
timestamp: 2,
243249
request: eventV1Request2,
244250
})
245251

246-
expect(store.values()).toHaveLength(2)
252+
expect(await store.values()).toHaveLength(2)
247253

248254
pendingEventsDispatcher.sendPendingEvents()
249-
expect(originalEventDispatcher.dispatchEvent).toHaveBeenCalledTimes(2)
250255

251-
// manually invoke original eventDispatcher callback
252-
const internalDispatchCalls = ((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock)
253-
.mock.calls
254-
internalDispatchCalls[0][1]({ statusCode: 200 })
255-
internalDispatchCalls[1][1]({ statusCode: 200 })
256+
setTimeout(async () => {
257+
expect(originalEventDispatcher.dispatchEvent).toHaveBeenCalledTimes(2)
256258

257-
expect(store.values()).toHaveLength(0)
259+
// manually invoke original eventDispatcher callback
260+
const internalDispatchCalls = ((originalEventDispatcher.dispatchEvent as unknown) as jest.Mock)
261+
.mock.calls
262+
internalDispatchCalls[0][1]({ statusCode: 200 })
263+
internalDispatchCalls[1][1]({ statusCode: 200 })
264+
265+
expect(await store.values()).toHaveLength(0)
266+
done()
267+
})
258268
})
259269
})
260270
})

0 commit comments

Comments
 (0)