Skip to content

Commit

Permalink
test: improve change testing and remove timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Feb 7, 2024
1 parent 188ea24 commit bfa9252
Show file tree
Hide file tree
Showing 27 changed files with 671 additions and 591 deletions.
10 changes: 10 additions & 0 deletions .run/Template Jest.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="true" type="JavaScriptTestRunnerJest">
<node-interpreter value="project" />
<node-options value="--experimental-vm-modules" />
<jest-options value="--runInBand" />
<envs />
<scope-kind value="ALL" />
<method v="2" />
</configuration>
</component>
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
"ts-standard": "^12.0.0",
"typescript": "4.9.5"
"typescript": "4.9.5",
"wait-for-expect": "^3.0.2"
},
"dependencies": {
"@babel/runtime": "^7.17.2",
Expand Down
37 changes: 21 additions & 16 deletions src/__tests__/areas.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { ApolloServer } from 'apollo-server-express'
import {ApolloServer} from 'apollo-server-express'
import muuid from 'uuid-mongodb'
import { jest } from '@jest/globals'
import {jest} from '@jest/globals'
import MutableAreaDataSource from '../model/MutableAreaDataSource.js'
import MutableOrganizationDataSource from '../model/MutableOrganizationDataSource.js'
import { AreaType } from '../db/AreaTypes.js'
import { OrgType, OrganizationType, OrganizationEditableFieldsType } from '../db/OrganizationTypes.js'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
import { muuidToString } from '../utils/helpers.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 inMemoryDB
let app: express.Application
let inMemoryDB: InMemoryDB

// Mongoose models for mocking pre-existing state.
let areas: MutableAreaDataSource
Expand All @@ -24,7 +27,7 @@ describe('areas API', () => {
let wa: AreaType

beforeAll(async () => {
({ server, inMemoryDB } = await setUpServer())
({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()
Expand Down Expand Up @@ -66,18 +69,19 @@ describe('areas API', () => {
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
})
.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
variables: {input: wa.metadata.area_id},
userUuid,
app
})

expect(response.statusCode).toBe(200)
Expand All @@ -91,8 +95,9 @@ describe('areas API', () => {
const response = await queryAPI({
query: areaQuery,
operationName: 'area',
variables: { input: ca.metadata.area_id },
userUuid
variables: {input: ca.metadata.area_id},
userUuid,
app
})
expect(response.statusCode).toBe(200)
const areaResult = response.body.data.area
Expand Down
26 changes: 15 additions & 11 deletions src/__tests__/history.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import { ApolloServer } from 'apollo-server-express'
import {ApolloServer} from 'apollo-server-express'
import muuid from 'uuid-mongodb'
import { jest } from '@jest/globals'
import {jest} from '@jest/globals'
import MutableAreaDataSource from '../model/MutableAreaDataSource.js'
import MutableOrganizationDataSource from '../model/MutableOrganizationDataSource.js'
import MutableClimbDataSource from '../model/MutableClimbDataSource.js'
import { AreaType } from '../db/AreaTypes.js'
import { OrgType, OrganizationType } from '../db/OrganizationTypes.js'
import { muuidToString } from '../utils/helpers.js'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
import {AreaType} from '../db/AreaTypes.js'
import {OrganizationType, OrgType} from '../db/OrganizationTypes.js'
import {muuidToString} from '../utils/helpers.js'
import {queryAPI, setUpServer} from '../utils/testUtils.js'
import {InMemoryDB} from "../utils/inMemoryDB.js";
import express from "express";

jest.setTimeout(60000)

describe('history API', () => {
let server: ApolloServer
let user: muuid.MUUID
let userUuid: string
let inMemoryDB
let app: express.Application
let inMemoryDB: InMemoryDB

// Mongoose models for mocking pre-existing state.
let areas: MutableAreaDataSource
let organizations: MutableOrganizationDataSource
let climbs: MutableClimbDataSource

beforeAll(async () => {
({ server, inMemoryDB } = await setUpServer())
({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()
Expand Down Expand Up @@ -101,13 +104,14 @@ describe('history API', () => {
email: '[email protected]'
}
alphaOrg = await organizations.addOrganization(user, OrgType.localClimbingOrganization, alphaFields)
climbIds = await climbs.addOrUpdateClimbs(user, ca.metadata.area_id, [{ name: 'Alpha Climb' }])
climbIds = await climbs.addOrUpdateClimbs(user, ca.metadata.area_id, [{name: 'Alpha Climb'}])

// Query for changes and ensure they are tracked.
const resp = await queryAPI({
query: QUERY_RECENT_CHANGE_HISTORY,
variables: { filter: {} },
userUuid
variables: {filter: {}},
userUuid,
app
})
expect(resp.statusCode).toBe(200)
const histories = resp.body.data.getChangeHistory
Expand Down
93 changes: 51 additions & 42 deletions src/__tests__/organizations.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { ApolloServer } from 'apollo-server-express'
import {ApolloServer} from 'apollo-server-express'
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 { OrgType, OrganizationType, OperationType, OrganizationEditableFieldsType } from '../db/OrganizationTypes.js'
import { changelogDataSource } from '../model/ChangeLogDataSource.js'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
import { muuidToString } from '../utils/helpers.js'
import { validate as validateMuuid } from 'uuid'

jest.setTimeout(110000)
import {AreaType} from '../db/AreaTypes.js'
import {OperationType, OrganizationEditableFieldsType, OrganizationType, OrgType} from '../db/OrganizationTypes.js'
import {changelogDataSource} from '../model/ChangeLogDataSource.js'
import {queryAPI, setUpServer} from '../utils/testUtils.js'
import {muuidToString} from '../utils/helpers.js'
import {validate as validateMuuid} from 'uuid'
import {InMemoryDB} from "../utils/inMemoryDB.js";
import express from "express";

describe('organizations API', () => {
let server: ApolloServer
let user: muuid.MUUID
let userUuid: string
let inMemoryDB
let app: express.Application
let inMemoryDB: InMemoryDB

// Mongoose models for mocking pre-existing state.
let areas: MutableAreaDataSource
Expand All @@ -26,7 +26,7 @@ describe('organizations API', () => {
let wa: AreaType

beforeAll(async () => {
({ server, inMemoryDB } = await setUpServer())
({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()
Expand All @@ -43,8 +43,8 @@ describe('organizations API', () => {
})

afterAll(async () => {
await server.stop()
await inMemoryDB.close()
await server?.stop()
await inMemoryDB?.close()
})

describe('mutations', () => {
Expand Down Expand Up @@ -85,9 +85,10 @@ describe('organizations API', () => {
const createResponse = await queryAPI({
query: createQuery,
operationName: 'addOrganization',
variables: { input: { displayName: 'Friends of Openbeta', orgType: 'LOCAL_CLIMBING_ORGANIZATION' } },
variables: {input: {displayName: 'Friends of Openbeta', orgType: 'LOCAL_CLIMBING_ORGANIZATION'}},
userUuid,
roles: ['user_admin']
roles: ['user_admin'],
app
})

expect(createResponse.statusCode).toBe(200)
Expand Down Expand Up @@ -120,7 +121,8 @@ describe('organizations API', () => {
}
},
userUuid,
roles: ['user_admin']
roles: ['user_admin'],
app
})
expect(updateResponse.statusCode).toBe(200)
expect(updateResponse.body.errors).toBeUndefined()
Expand Down Expand Up @@ -163,9 +165,10 @@ describe('organizations API', () => {
const response = await queryAPI({
query: createQuery,
operationName: 'addOrganization',
variables: { input: { displayName: 'Friends of Openbeta', orgType: 'LOCAL_CLIMBING_ORGANIZATION' } },
variables: {input: {displayName: 'Friends of Openbeta', orgType: 'LOCAL_CLIMBING_ORGANIZATION'}},
userUuid,
roles: ['editor']
roles: ['editor'],
app
})
expect(response.statusCode).toBe(200)
expect(response.body.data.organization).toBeNull()
Expand Down Expand Up @@ -219,39 +222,40 @@ describe('organizations API', () => {
hardwareReportLink: 'https://alphaopenbeta.com/reporthardware'
}
alphaOrg = await organizations.addOrganization(user, OrgType.localClimbingOrganization, alphaFields)
.then((res: OrganizationType | null) => {
if (res === null) throw new Error('Failure mocking organization.')
return res
})
.then((res: OrganizationType | null) => {
if (res === null) throw new Error('Failure mocking organization.')
return res
})

deltaFields = {
displayName: 'Delta OpenBeta Club',
email: '[email protected]'
}
deltaOrg = await organizations.addOrganization(user, OrgType.localClimbingOrganization, deltaFields)
.then((res: OrganizationType | null) => {
if (res === null) throw new Error('Failure mocking organization.')
return res
})
.then((res: OrganizationType | null) => {
if (res === null) throw new Error('Failure mocking organization.')
return res
})

gammaFields = {
displayName: 'Delta Gamma OpenBeta Club',
description: 'We are an offshoot of the delta club.\nSee our website for more details.',
excludedAreaIds: [wa.metadata.area_id]
}
gammaOrg = await organizations.addOrganization(user, OrgType.localClimbingOrganization, gammaFields)
.then((res: OrganizationType | null) => {
if (res === null) throw new Error('Failure mocking organization.')
return res
})
.then((res: OrganizationType | null) => {
if (res === null) throw new Error('Failure mocking organization.')
return res
})
})

it('retrieves an organization with an MUUID', async () => {
const response = await queryAPI({
query: organizationQuery,
operationName: 'organization',
variables: { input: muuidToString(alphaOrg.orgId) },
userUuid
variables: {input: muuidToString(alphaOrg.orgId)},
userUuid,
app
})
expect(response.statusCode).toBe(200)
const orgResult = response.body.data.organization
Expand All @@ -268,8 +272,9 @@ describe('organizations API', () => {
const response = await queryAPI({
query: organizationsQuery,
operationName: 'organizations',
variables: { filter: { displayName: { match: 'Delta OpenBeta Club', exactMatch: true } } },
userUuid
variables: {filter: {displayName: {match: 'Delta OpenBeta Club', exactMatch: true}}},
userUuid,
app
})

expect(response.statusCode).toBe(200)
Expand All @@ -282,8 +287,9 @@ describe('organizations API', () => {
const response = await queryAPI({
query: organizationsQuery,
operationName: 'organizations',
variables: { filter: { displayName: { match: 'delta', exactMatch: false } } },
userUuid
variables: {filter: {displayName: {match: 'delta', exactMatch: false}}},
userUuid,
app
})
expect(response.statusCode).toBe(200)
const dataResult = response.body.data.organizations
Expand All @@ -298,7 +304,8 @@ describe('organizations API', () => {
variables: {
limit: 1
},
userUuid
userUuid,
app
})
expect(response.statusCode).toBe(200)
const dataResult = response.body.data.organizations
Expand All @@ -309,8 +316,9 @@ describe('organizations API', () => {
const response = await queryAPI({
query: organizationsQuery,
operationName: 'organizations',
variables: { filter: { associatedAreaIds: { includes: [muuidToString(ca.metadata.area_id)] } } },
userUuid
variables: {filter: {associatedAreaIds: {includes: [muuidToString(ca.metadata.area_id)]}}},
userUuid,
app
})
// Graphql should convert `includes` from a string[] to MUUID[]
expect(response.statusCode).toBe(200)
Expand All @@ -323,8 +331,9 @@ describe('organizations API', () => {
const response = await queryAPI({
query: organizationsQuery,
operationName: 'organizations',
variables: { filter: { excludedAreaIds: { excludes: [muuidToString(wa.metadata.area_id)] } } },
userUuid
variables: {filter: {excludedAreaIds: {excludes: [muuidToString(wa.metadata.area_id)]}}},
userUuid,
app
})
expect(response.statusCode).toBe(200)
const dataResult = response.body.data.organizations
Expand Down
Loading

0 comments on commit bfa9252

Please sign in to comment.