-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasNotificationQuery.test.ts
173 lines (140 loc) · 4.77 KB
/
hasNotificationQuery.test.ts
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
import { hasNotificationQuery } from './hasNotificationQuery'
import { Task, User } from '../../dbschema/interfaces'
import e from '../edgedb/builder'
import { client } from '../edgedb/client'
import { Client } from 'edgedb'
import { createTaskQuery } from './createTaskQuery'
import { cleanup } from './helpers/cleanup'
describe('selectHasNotification', () => {
let currentUser: User
let otherUser: User
let currentUserClient: Client
let otherUserClient: Client
beforeAll(async () => {
;[currentUser, otherUser] = await e
.select(e.User, () => ({
...e.User['*'],
limit: 2,
}))
.run(client)
currentUserClient = client.withGlobals({ currentUserId: currentUser.id })
otherUserClient = client.withGlobals({ currentUserId: otherUser.id })
})
afterAll(cleanup)
describe('when currentUser creates task with two assignees', () => {
let task: Pick<Task, 'id'>
beforeAll(async () => {
let result = await createTaskQuery.run(currentUserClient, {
title: 'Task',
dueAt: new Date(),
assignees: [currentUser.id, otherUser.id],
})
task = result.task
})
it.only('should not return notification for currentUser', async () => {
// Can't use TX now as it's unsupported in the TS client
await currentUserClient.transaction(async (tx) => {
const { task } = await createTaskQuery.run(tx, {
title: 'Task',
dueAt: new Date(),
assignees: [currentUser.id, otherUser.id],
})
const result = await hasNotificationQuery.run(tx, {
id: task.id,
})
if (!result) {
throw new Error()
}
expect(result.computedHasNotification).toBe(false)
// This could have better API, e.g. tx.rollback(), as the execute takes in any query https://www.edgedb.com/docs/clients/js/driver#transactions
await tx.execute('rollback') // Throws: DisabledCapabilityError: cannot execute transaction control commands
})
})
it('should not return notification for currentUser', async () => {
const result = await hasNotificationQuery.run(currentUserClient, {
id: task.id,
})
if (!result) {
throw new Error()
}
expect(result.computedHasNotification).toBe(false)
})
it('should return notification for otherUser', async () => {
const result = await hasNotificationQuery.run(otherUserClient, {
id: task.id,
})
if (!result) {
throw new Error()
}
expect(result.computedHasNotification).toBe(true)
})
})
describe('when currentUser creates new task and otherUser sees the task', () => {
let task: Pick<Task, 'id'>
beforeAll(async () => {
let result = await createTaskQuery.run(currentUserClient, {
title: 'Task',
dueAt: new Date(),
assignees: [currentUser.id, otherUser.id],
})
task = result.task
await e
.insert(e.TaskSeenEvent, {
task: e.select(e.Task, () => ({ filter_single: { id: task.id } })),
user: e.global.currentUser,
})
.run(otherUserClient)
})
it('should not return any notification for otherUser', async () => {
const result = await hasNotificationQuery.run(otherUserClient, {
id: task.id,
})
if (!result) {
throw new Error()
}
expect(result.computedHasNotification).toBe(false)
})
})
describe('when currentUser creates new comment in task which was seen by otherUser', () => {
let task: Pick<Task, 'id'>
beforeAll(async () => {
let result = await createTaskQuery.run(currentUserClient, {
title: 'Task',
dueAt: new Date(),
assignees: [currentUser.id, otherUser.id],
})
task = result.task
await e
.insert(e.TaskSeenEvent, {
task: e.select(e.Task, () => ({ filter_single: { id: task.id } })),
user: e.global.currentUser,
})
.run(otherUserClient)
await e
.insert(e.TaskComment, {
task: e.select(e.Task, () => ({ filter_single: { id: task.id } })),
user: e.global.currentUser,
note: 'Comment',
})
.run(currentUserClient)
})
it('should return no notification for currentUser', async () => {
const result = await hasNotificationQuery.run(currentUserClient, {
id: task.id,
})
if (!result) {
throw new Error()
}
expect(result.computedHasNotification).toBe(false)
})
it('should return notification for otherUser', async () => {
const result = await hasNotificationQuery.run(otherUserClient, {
id: task.id,
})
if (!result) {
throw new Error()
}
expect(result.computedHasNotification).toBe(true)
})
})
})