Skip to content

Commit 5406188

Browse files
committed
Add coverage for adding climb against existing climbs
Fix issue that popped up from coverage (new should be +1)
1 parent 0fe937f commit 5406188

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/model/MutableClimbDataSource.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,17 @@ export default class MutableClimbDataSource extends ClimbDataSource {
110110
// when adding new climbs, ensure they are added in expected sort order, such that:
111111
// * multiple climbs keep the order they're input in.
112112
// * all newly added climbs come after existing climbs.
113-
let newClimbLeftRightIndex = (await this.climbModel
113+
const maxExisting = (await this.climbModel
114114
.find({ _id: { $in: parent.climbs } })
115115
.select('metadata.left_right_index')
116116
.sort({ 'metadata.left_right_index': -1 })
117-
.limit(1))[0]
118-
?.metadata.left_right_index ?? 1
117+
.limit(1))[0]?.metadata.left_right_index
118+
let newClimbLeftRightIndex = (maxExisting ?? 0) + 1
119+
119120
function resolveLeftRightIndex (i: number): { left_right_index: number } | null {
120121
// user input is always prioritized
121122
if (userInput[i].leftRightIndex !== undefined) {
122-
return { left_right_index: userInput[i].leftRightIndex! }
123+
return { left_right_index: userInput[i].leftRightIndex ?? 0 }
123124
}
124125
// otherwise, auto-order new climbs
125126
if (!idList[i].existed) {

src/model/__tests__/MutableClimbDataSource.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,13 @@ describe('Climb CRUD', () => {
212212
).rejects.toThrowError(/You can only add climbs to a crag/)
213213

214214
// Route-only area should accept new boulder problems
215-
await climbs.addOrUpdateClimbs(testUser, routesArea.metadata.area_id, [newBoulderProblem1])
215+
const [newBoulderID] = await climbs.addOrUpdateClimbs(testUser, routesArea.metadata.area_id, [newBoulderProblem1])
216+
// Should come after existing climbs
217+
expect(await climbs.findOneClimbByMUUID(muid.from(newBoulderID))).toMatchObject({
218+
metadata: {
219+
left_right_index: newClimbsToAdd.length + 1
220+
}
221+
})
216222
})
217223

218224
it('can add new boulder problems', async () => {

0 commit comments

Comments
 (0)