-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathareas.ts
More file actions
110 lines (99 loc) · 3.69 KB
/
areas.ts
File metadata and controls
110 lines (99 loc) · 3.69 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
import { ApolloServer } from '@apollo/server'
import muuid from 'uuid-mongodb'
import { jest } from '@jest/globals'
import MutableAreaDataSource from '../model/MutableAreaDataSource.js'
import MutableOrganizationDataSource from '../model/MutableOrganizationDataSource.js'
import { AreaType } from '../db/AreaTypes.js'
import { OrganizationEditableFieldsType, OrganizationType, OrgType } from '../db/OrganizationTypes.js'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
import { muuidToString } from '../utils/helpers.js'
import { InMemoryDB } from '../utils/inMemoryDB.js'
import express from 'express'
jest.setTimeout(60000)
describe('areas API', () => {
let server: ApolloServer
let user: muuid.MUUID
let userUuid: string
let app: express.Application
let inMemoryDB: InMemoryDB
// Mongoose models for mocking pre-existing state.
let areas: MutableAreaDataSource
let organizations: MutableOrganizationDataSource
let usa: AreaType
let ca: AreaType
let wa: AreaType
beforeAll(async () => {
({ server, inMemoryDB, app } = 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 = MutableAreaDataSource.getInstance()
organizations = MutableOrganizationDataSource.getInstance()
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)
})
afterAll(async () => {
await server.stop()
await inMemoryDB.close()
})
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 omitting organizations that exclude it', async () => {
const response = await queryAPI({
query: areaQuery,
operationName: 'area',
variables: { input: ca.metadata.area_id },
userUuid,
app
})
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.each([userUuid, undefined])('retrieves an area and lists associated organizations', async (userId) => {
const response = await queryAPI({
query: areaQuery,
operationName: 'area',
variables: { input: wa.metadata.area_id },
userUuid: userId,
app
})
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))
})
})
})