test(curriculum): use Mocha for tests
This commit is contained in:
committed by
mrugesh mohapatra
parent
73485119f4
commit
62cc8acb87
27
curriculum/test/utils/challengeTitles.js
Normal file
27
curriculum/test/utils/challengeTitles.js
Normal file
@@ -0,0 +1,27 @@
|
||||
class ChallengeTitles {
|
||||
constructor() {
|
||||
this.knownTitles = [];
|
||||
}
|
||||
check(title) {
|
||||
if (typeof title !== 'string') {
|
||||
throw new Error(
|
||||
`Expected a valid string for ${title}, but got a(n) ${typeof title}`
|
||||
);
|
||||
}
|
||||
const titleToCheck = title.replace(/\s+/g, '').toLowerCase();
|
||||
if (titleToCheck.length === 0) {
|
||||
throw new Error('Expected a title length greater than 0');
|
||||
}
|
||||
const isKnown = this.knownTitles.includes(titleToCheck);
|
||||
if (isKnown) {
|
||||
throw new Error(`
|
||||
All challenges must have a unique title.
|
||||
|
||||
The title ${title} is already assigned
|
||||
`);
|
||||
}
|
||||
this.knownTitles = [ ...this.knownTitles, titleToCheck ];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ChallengeTitles;
|
24
curriculum/test/utils/mongoIds.js
Normal file
24
curriculum/test/utils/mongoIds.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const _ = require('lodash');
|
||||
const { isMongoId } = require('validator');
|
||||
|
||||
class MongoIds {
|
||||
constructor() {
|
||||
this.knownIds = [];
|
||||
}
|
||||
check(id, title) {
|
||||
if (!isMongoId(id)) {
|
||||
throw new Error(`Expected a valid ObjectId for ${title}, but got ${id}`);
|
||||
}
|
||||
const idIndex = _.findIndex(this.knownIds, existing => id === existing);
|
||||
if (idIndex !== -1) {
|
||||
throw new Error(`
|
||||
All challenges must have a unique id.
|
||||
|
||||
The id for ${title} is already assigned
|
||||
`);
|
||||
}
|
||||
this.knownIds = [ ...this.knownIds, id ];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MongoIds;
|
Reference in New Issue
Block a user