-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcallSessionTransaction.test.js
More file actions
80 lines (67 loc) · 2.39 KB
/
Copy pathcallSessionTransaction.test.js
File metadata and controls
80 lines (67 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Boom from '@hapi/boom'
import { pino } from 'pino'
import * as formMetadata from '~/src/api/forms/repositories/form-metadata-repository.js'
import { formMetadataDocument } from '~/src/api/forms/service/__stubs__/service.js'
import { callSessionTransaction } from '~/src/api/forms/service/callSessionTransaction.js'
import { getAuthor } from '~/src/helpers/get-author.js'
import { prepareDb } from '~/src/mongo.js'
jest.mock('~/src/helpers/get-author.js')
jest.mock('~/src/api/forms/repositories/form-metadata-repository.js')
jest.mock('~/src/mongo.js')
jest.useFakeTimers().setSystemTime(new Date('2020-01-01'))
describe('lists', () => {
beforeAll(async () => {
await prepareDb(pino())
})
beforeEach(() => {
jest.mocked(formMetadata.get).mockResolvedValue(formMetadataDocument)
})
describe('callSessionTransaction', () => {
const id = '661e4ca5039739ef2902b214'
const author = getAuthor()
const dateUsedInFakeTime = new Date('2020-01-01')
const defaultAudit = {
'draft.updatedAt': dateUsedInFakeTime,
'draft.updatedBy': author,
updatedAt: dateUsedInFakeTime,
updatedBy: author
}
beforeAll(async () => {
await prepareDb(pino())
})
it('should update the component', async () => {
const transactionResolved = '265a71fd-f2c2-4028-94aa-7c1e2739730f'
const transactionHandler = jest
.fn()
.mockResolvedValue(transactionResolved)
const dbMetadataSpy = jest.spyOn(formMetadata, 'update')
const result = await callSessionTransaction(
id,
transactionHandler,
author,
{
start: 'started',
end: 'finished',
fail: 'failed'
}
)
expect(transactionHandler).toHaveBeenCalled()
expect(result).toBe(transactionResolved)
const [metaFormId, metaUpdateOperations] = dbMetadataSpy.mock.calls[0]
expect(metaFormId).toBe(id)
expect(metaUpdateOperations.$set).toEqual(defaultAudit)
})
it('should correctly surface the error is the component is not found', async () => {
const transactionHandler = jest
.fn()
.mockRejectedValue(Boom.notFound('Not found'))
await expect(
callSessionTransaction(id, transactionHandler, author, {
start: 'started',
end: 'finished',
fail: 'failed'
})
).rejects.toThrow(Boom.notFound('Not found'))
})
})
})