Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
viet nguyen committed Jan 27, 2024
1 parent e64c66a commit 652b01d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand",
"MediaData"
"history"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand Down
1 change: 1 addition & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
testTimeout: 2 * 60 * 1000,
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1'
},
Expand Down
12 changes: 9 additions & 3 deletions src/__tests__/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ describe('history API', () => {

expect(climbChange.operation).toBe('updateClimb')
expect(climbChange.editedBy).toBe(userUuid)
// Two changes: Insert the climb, update the parent area
// Ordering is non-deterministic.
expect(climbChange.changes.length).toBe(2)

/**
* Four changes (Ordering is non-deterministic)
* 1. Insert the climb
* 2. Update the parent area
* 3. Update aggregate object on crag
* 4. Update the parent area
*/
expect(climbChange.changes.length).toBe(4)
const insertChange = climbChange.changes.filter(c => c.dbOp === 'insert')[0]
const updateChange = climbChange.changes.filter(c => c.dbOp === 'update')[0]
expect(insertChange.fullDocument.uuid).toBe(climbIds[0])
Expand Down
4 changes: 2 additions & 2 deletions src/db/utils/jobs/TreeUpdaters/updateAllAreas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const leafReducer = (node: AreaType): StatsSummary => {
*/
const calculatePolygonFromChildren = (nodes: StatsSummary[]): Feature<Polygon> | null => {
const childAsPolygons = nodes.reduce<Array<Feature<Polygon>>>((acc, curr) => {
if (curr.bbox != null) {
if (Array.isArray(curr.bbox) && curr?.bbox.length === 4) {
acc.push(bbox2Polygon(curr.bbox))
}
return acc
Expand Down Expand Up @@ -179,7 +179,7 @@ export const nodesReducer = async (childResults: StatsSummary[], parent: AreaMon

const polygon = calculatePolygonFromChildren(childResults)
nodeSummary.polygon = polygon?.geometry
nodeSummary.bbox = bboxFromGeojson(polygon)
nodeSummary.bbox = polygon == null ? undefined : bboxFromGeojson(polygon)
nodeSummary.density = areaDensity(nodeSummary.bbox, nodeSummary.totalClimbs)

const { totalClimbs, bbox, density, aggregate } = nodeSummary
Expand Down
2 changes: 1 addition & 1 deletion src/geo-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const bboxFromList = (bboxList: BBoxType[]): any => {
* @returns total climbs per km sq
*/
export const areaDensity = (bbox: BBoxType | undefined, totalClimbs: number): number => {
if (bbox == null) return 0
if (!Array.isArray(bbox) || bbox?.length !== 4) return 0
const areaInKm = area(bboxPolygon(bbox)) / 1000000
const minArea = areaInKm < 5 ? 5 : areaInKm
return totalClimbs / minArea
Expand Down
4 changes: 3 additions & 1 deletion src/model/MutableAreaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ export const newAreaHelper = (areaName: string, parentAncestors: string, parentP
leaf: false,
area_id: uuid,
leftRightIndex: -1,
ext_id: ''
ext_id: '',
bbox: undefined,
polygon: undefined
},
ancestors,
climbs: [],
Expand Down

0 comments on commit 652b01d

Please sign in to comment.