-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathviewer_notification_state.test.js
More file actions
65 lines (53 loc) · 1.8 KB
/
Copy pathviewer_notification_state.test.js
File metadata and controls
65 lines (53 loc) · 1.8 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
const db = require('../server/db')
const {
getNotifications,
getRecentNotifications,
setLastLogin
} = require('../server/viewer/viewer_notification_state')
jest.mock('../server/db')
describe('db', () => {
beforeEach(() => {
jest.resetAllMocks()
jest.resetModules()
})
test('returns notifications when passed ids', () => {
const fakeNotifications = [
{ title: 'Notification 1', text: 'This is notification 1' },
{ title: 'Notification 2', text: 'This is notification 2' }
]
db.manyOrNone.mockResolvedValue(fakeNotifications)
return getNotifications([1, 2]).then(result => {
expect(result).toEqual(fakeNotifications)
expect(db.manyOrNone).toHaveBeenCalledWith(expect.any(String), { ids: [1, 2] })
})
})
test('returns undefined when passed ids as 0', () => {
return expect(getNotifications(0)).toBeUndefined()
})
test('returns notifications created after a given date', () => {
const fakeNotifications = [{ id: 1 }, { id: 2 }]
db.manyOrNone.mockResolvedValue(fakeNotifications)
return getRecentNotifications('2022-01-01').then(result => {
expect(result).toEqual(fakeNotifications)
expect(db.manyOrNone).toHaveBeenCalledWith(expect.any(String), { date: '2022-01-01' })
})
})
test('should insert a new record if the user does not exist', () => {
db.none.mockResolvedValue()
const userId = 1
const today = '2023-09-13'
return setLastLogin(userId, today).then(() => {
expect(db.none).toHaveBeenCalledWith(expect.stringContaining('INSERT INTO users'), {
userId,
today
})
})
})
test('should handle other errors from db.none', () => {
const errorMessage = 'Database error'
db.none.mockRejectedValue(new Error(errorMessage))
const userId = 1
const today = '2023-09-13'
return expect(setLastLogin(userId, today)).rejects.toThrow(errorMessage)
})
})