forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-https-by-key.test.js
More file actions
194 lines (175 loc) · 5.4 KB
/
load-https-by-key.test.js
File metadata and controls
194 lines (175 loc) · 5.4 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require('dotenv-safe').config({
allowEmptyValues: true,
})
const { DB_PASS: rootPass, DB_URL: url } = process.env
const { ArangoTools, dbNameFromFile } = require('arango-tools')
const { setupI18n } = require('@lingui/core')
const englishMessages = require('../locale/en/messages')
const frenchMessages = require('../locale/fr/messages')
const { makeMigrations } = require('../../migrations')
const { httpsLoaderByKey } = require('../loaders')
describe('given the httpsLoaderByKey function', () => {
let query, drop, truncate, migrate, collections, i18n
const consoleErrorOutput = []
const mockedError = (output) => consoleErrorOutput.push(output)
beforeAll(async () => {
;({ migrate } = await ArangoTools({ rootPass, url }))
;({ query, drop, truncate, collections } = await migrate(
makeMigrations({ databaseName: dbNameFromFile(__filename), rootPass }),
))
console.error = mockedError
i18n = setupI18n({
language: 'en',
locales: ['en', 'fr'],
missing: 'Traduction manquante',
catalogs: {
en: englishMessages,
fr: frenchMessages,
},
})
})
beforeEach(async () => {
consoleErrorOutput.length = 0
await truncate()
await collections.https.save({})
await collections.https.save({})
})
afterAll(async () => {
await drop()
})
describe('given a single id', () => {
it('returns a single https scan', async () => {
const expectedCursor = await query`
FOR httpsScan IN https
SORT httpsScan._key ASC LIMIT 1
RETURN httpsScan
`
const expectedHttps = await expectedCursor.next()
const loader = httpsLoaderByKey(query)
const https = await loader.load(expectedHttps._key)
expect(https).toEqual(expectedHttps)
})
})
describe('given multiple ids', () => {
it('returns multiple https scans', async () => {
const httpsKeys = []
const expectedHttpsScans = []
const expectedCursor = await query`
FOR httpsScan IN https
RETURN httpsScan
`
while (expectedCursor.hasNext()) {
const tempHttps = await expectedCursor.next()
httpsKeys.push(tempHttps._key)
expectedHttpsScans.push(tempHttps)
}
const loader = httpsLoaderByKey(query)
const httpsScans = await loader.loadMany(httpsKeys)
expect(httpsScans).toEqual(expectedHttpsScans)
})
})
describe('language is set to english', () => {
beforeAll(() => {
i18n = setupI18n({
language: 'en',
locales: ['en', 'fr'],
missing: 'Traduction manquante',
catalogs: {
en: englishMessages,
fr: frenchMessages,
},
})
})
describe('given a database error', () => {
it('raises an error', async () => {
query = jest
.fn()
.mockRejectedValue(new Error('Database error occurred.'))
const loader = httpsLoaderByKey(query, i18n)
try {
await loader.load('1')
} catch (err) {
expect(err).toEqual(
new Error('Unable to find https scan. Please try again.'),
)
}
expect(consoleErrorOutput).toEqual([
`Database error occurred when running httpsLoaderByKey: Error: Database error occurred.`,
])
})
})
describe('given a cursor error', () => {
it('raises an error', async () => {
const cursor = {
each() {
throw new Error('Cursor error occurred.')
},
}
query = jest.fn().mockReturnValue(cursor)
const loader = httpsLoaderByKey(query, i18n)
try {
await loader.load('1')
} catch (err) {
expect(err).toEqual(
new Error('Unable to find https scan. Please try again.'),
)
}
expect(consoleErrorOutput).toEqual([
`Cursor error occurred when running httpsLoaderByKey: Error: Cursor error occurred.`,
])
})
})
})
describe('language is set to french', () => {
beforeAll(() => {
i18n = setupI18n({
language: 'fr',
locales: ['en', 'fr'],
missing: 'Traduction manquante',
catalogs: {
en: englishMessages,
fr: frenchMessages,
},
})
})
describe('given a database error', () => {
it('raises an error', async () => {
query = jest
.fn()
.mockRejectedValue(new Error('Database error occurred.'))
const loader = httpsLoaderByKey(query, i18n)
try {
await loader.load('1')
} catch (err) {
expect(err).toEqual(
new Error('todo'),
)
}
expect(consoleErrorOutput).toEqual([
`Database error occurred when running httpsLoaderByKey: Error: Database error occurred.`,
])
})
})
describe('given a cursor error', () => {
it('raises an error', async () => {
const cursor = {
each() {
throw new Error('Cursor error occurred.')
},
}
query = jest.fn().mockReturnValue(cursor)
const loader = httpsLoaderByKey(query, i18n)
try {
await loader.load('1')
} catch (err) {
expect(err).toEqual(
new Error('todo'),
)
}
expect(consoleErrorOutput).toEqual([
`Cursor error occurred when running httpsLoaderByKey: Error: Cursor error occurred.`,
])
})
})
})
})