-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathareas.ts
More file actions
211 lines (193 loc) · 7.18 KB
/
areas.ts
File metadata and controls
211 lines (193 loc) · 7.18 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { ApolloServer } from 'apollo-server'
import muuid from 'uuid-mongodb'
import { jest } from '@jest/globals'
import MutableAreaDataSource, { createInstance as createAreaInstance } from '../model/MutableAreaDataSource.js'
import { createInstance as createClimbInstance } from '../model/MutableClimbDataSource.js'
import MutableOrganizationDataSource, { createInstance as createOrgInstance } from '../model/MutableOrganizationDataSource.js'
import { AreaType } from '../db/AreaTypes.js'
import { ClimbChangeInputType } from '../db/ClimbTypes.js'
import { OrgType, OrganizationType, OrganizationEditableFieldsType } from '../db/OrganizationTypes.js'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
import { muuidToString } from '../utils/helpers.js'
jest.setTimeout(60000)
describe('areas API', () => {
let server: ApolloServer
let user: muuid.MUUID
let userUuid: string
let inMemoryDB
// Mongoose models for mocking pre-existing state.
let areas: MutableAreaDataSource
let organizations: MutableOrganizationDataSource
let usa: AreaType
let ca: AreaType
let wa: AreaType
let ak: AreaType
beforeAll(async () => {
({ server, inMemoryDB } = await setUpServer())
// Auth0 serializes uuids in "relaxed" mode, resulting in this hex string format
// "59f1d95a-627d-4b8c-91b9-389c7424cb54" instead of base64 "WfHZWmJ9S4yRuTicdCTLVA==".
user = muuid.mode('relaxed').v4()
userUuid = muuidToString(user)
})
beforeEach(async () => {
await inMemoryDB.clear()
areas = createAreaInstance()
organizations = createOrgInstance()
usa = await areas.addCountry('usa')
ca = await areas.addArea(user, 'CA', usa.metadata.area_id)
wa = await areas.addArea(user, 'WA', usa.metadata.area_id)
ak = await areas.addArea(user, 'AK', usa.metadata.area_id)
})
afterAll(async () => {
await server.stop()
await inMemoryDB.close()
})
describe('mutations', () => {
it('updates sorting order of subareas and queries returns them in order', async () => {
const updateSortingOrderQuery = `
mutation ($input: [AreaSortingInput]) {
updateAreasSortingOrder(input: $input)
}
`
const updateResponse = await queryAPI({
query: updateSortingOrderQuery,
variables: {
input: [
{ areaId: wa.metadata.area_id, leftRightIndex: 3 },
{ areaId: ca.metadata.area_id, leftRightIndex: 0 },
{ areaId: ak.metadata.area_id, leftRightIndex: 10 }
]
},
userUuid
})
expect(updateResponse.statusCode).toBe(200)
const sortingOrderResult = updateResponse.body.data.updateAreasSortingOrder
expect(sortingOrderResult).toHaveLength(3)
const areaChildrenQuery = `
query area($input: ID) {
area(uuid: $input) {
children {
uuid
metadata {
leftRightIndex
}
}
}
}
`
const areaChildrenResponse = await queryAPI({
query: areaChildrenQuery,
variables: { input: usa.metadata.area_id },
userUuid
})
expect(areaChildrenResponse.statusCode).toBe(200)
const areaResult = areaChildrenResponse.body.data.area
// In leftRightIndex order
expect(areaResult.children[0]).toMatchObject({ uuid: muuidToString(ca.metadata.area_id), metadata: { leftRightIndex: 0 } })
expect(areaResult.children[1]).toMatchObject({ uuid: muuidToString(wa.metadata.area_id), metadata: { leftRightIndex: 3 } })
expect(areaResult.children[2]).toMatchObject({ uuid: muuidToString(ak.metadata.area_id), metadata: { leftRightIndex: 10 } })
})
})
describe('queries', () => {
const areaQuery = `
query area($input: ID) {
area(uuid: $input) {
uuid
organizations {
orgId
}
}
}
`
let alphaFields: OrganizationEditableFieldsType
let alphaOrg: OrganizationType
beforeEach(async () => {
alphaFields = {
displayName: 'USA without CA Org',
associatedAreaIds: [usa.metadata.area_id],
excludedAreaIds: [ca.metadata.area_id]
}
alphaOrg = await organizations.addOrganization(user, OrgType.localClimbingOrganization, alphaFields)
.then((res: OrganizationType | null) => {
if (res === null) throw new Error('Failure mocking organization.')
return res
})
})
it('retrieves an area and lists associated organizations', async () => {
const response = await queryAPI({
query: areaQuery,
operationName: 'area',
variables: { input: wa.metadata.area_id },
userUuid
})
expect(response.statusCode).toBe(200)
const areaResult = response.body.data.area
expect(areaResult.uuid).toBe(muuidToString(wa.metadata.area_id))
expect(areaResult.organizations).toHaveLength(1)
expect(areaResult.organizations[0].orgId).toBe(muuidToString(alphaOrg.orgId))
})
it('retrieves an area omitting organizations that exclude it', async () => {
const response = await queryAPI({
query: areaQuery,
operationName: 'area',
variables: { input: ca.metadata.area_id },
userUuid
})
expect(response.statusCode).toBe(200)
const areaResult = response.body.data.area
expect(areaResult.uuid).toBe(muuidToString(ca.metadata.area_id))
// Even though alphaOrg associates with ca's parent, usa, it excludes
// ca and so should not be listed.
expect(areaResult.organizations).toHaveLength(0)
})
it('returns climbs in leftRightIndex order', async () => {
const climbs = createClimbInstance()
const leftRoute: ClimbChangeInputType = {
name: 'left',
disciplines: { sport: true },
description: 'Leftmost route on the wall',
leftRightIndex: 0
}
const middleRoute: ClimbChangeInputType = {
name: 'middle',
disciplines: { sport: true },
description: 'Middle route on the wall',
leftRightIndex: 1
}
const rightRoute: ClimbChangeInputType = {
name: 'right',
disciplines: { sport: true },
description: 'Rightmost route on the wall',
leftRightIndex: 2
}
await climbs.addOrUpdateClimbs(
user,
ca.metadata.area_id,
[middleRoute, leftRoute, rightRoute]
)
const areaClimbsQuery = `
query area($input: ID) {
area(uuid: $input) {
climbs {
name
metadata {
leftRightIndex
}
}
}
}
`
const areaClimbsResponse = await queryAPI({
query: areaClimbsQuery,
variables: { input: ca.metadata.area_id },
userUuid
})
expect(areaClimbsResponse.statusCode).toBe(200)
const areaResult = areaClimbsResponse.body.data.area
// In leftRightIndex order
expect(areaResult.climbs[0]).toMatchObject({ name: 'left', metadata: { leftRightIndex: 0 } })
expect(areaResult.climbs[1]).toMatchObject({ name: 'middle', metadata: { leftRightIndex: 1 } })
expect(areaResult.climbs[2]).toMatchObject({ name: 'right', metadata: { leftRightIndex: 2 } })
})
})
})