fix(path-map): Refactor pathMigrationMap for new curriculum package
This commit is contained in:
committed by
mrugesh mohapatra
parent
bc9b3b4ddd
commit
d52ba8c6fd
86
tools/scripts/seed/__mocks__/curriculum.json
Normal file
86
tools/scripts/seed/__mocks__/curriculum.json
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
{
|
||||||
|
"super-block-a": {
|
||||||
|
"blocks": {
|
||||||
|
"block-one": {
|
||||||
|
"meta": {},
|
||||||
|
"challenges": [
|
||||||
|
{
|
||||||
|
"title": "Challenge One",
|
||||||
|
"block": "Block One",
|
||||||
|
"superBlock": "super-block-a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Two",
|
||||||
|
"block": "Block One",
|
||||||
|
"superBlock": "super-block-a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Three",
|
||||||
|
"block": "Block One",
|
||||||
|
"superBlock": "super-block-a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Four",
|
||||||
|
"block": "Block One",
|
||||||
|
"superBlock": "super-block-a"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"block-two": {
|
||||||
|
"meta": {},
|
||||||
|
"challenges": [
|
||||||
|
{
|
||||||
|
"title": "Challenge Five",
|
||||||
|
"block": "Block Two",
|
||||||
|
"superBlock": "super-block-a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Six",
|
||||||
|
"block": "Block Two",
|
||||||
|
"superBlock": "super-block-a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Seven",
|
||||||
|
"block": "Block Two",
|
||||||
|
"superBlock": "super-block-a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Eight",
|
||||||
|
"block": "Block Two",
|
||||||
|
"superBlock": "super-block-a"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"super-block-b": {
|
||||||
|
"blocks": {
|
||||||
|
"block-one": {
|
||||||
|
"meta": {},
|
||||||
|
"challenges": [
|
||||||
|
{
|
||||||
|
"title": "Challenge Nine",
|
||||||
|
"block": "Block One",
|
||||||
|
"superBlock": "super-block-b",
|
||||||
|
"isPrivate": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Ten",
|
||||||
|
"block": "Block One",
|
||||||
|
"superBlock": "super-block-b"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Eleven",
|
||||||
|
"block": "Block One",
|
||||||
|
"superBlock": "super-block-b"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Challenge Twelve",
|
||||||
|
"block": "Block One",
|
||||||
|
"superBlock": "super-block-b"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
tools/scripts/seed/createPathMigrationMap.js
Normal file
25
tools/scripts/seed/createPathMigrationMap.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
const { flatten } = require('lodash');
|
||||||
|
|
||||||
|
const { dasherize } = require('../../../api-server/server/utils');
|
||||||
|
|
||||||
|
function createPathMigrationMap(curriculum) {
|
||||||
|
return Object.keys(curriculum)
|
||||||
|
.map(key => curriculum[key].blocks)
|
||||||
|
.reduce((challenges, current) => {
|
||||||
|
const superChallenges = Object.keys(current).map(
|
||||||
|
key => current[key].challenges
|
||||||
|
);
|
||||||
|
return challenges.concat(flatten(superChallenges));
|
||||||
|
}, [])
|
||||||
|
.filter(({ isPrivate }) => !isPrivate)
|
||||||
|
.reduce((map, challenge) => {
|
||||||
|
const { title, block, superBlock } = challenge;
|
||||||
|
const dashedTitle = dasherize(title);
|
||||||
|
map[dashedTitle] = `/learn/${dasherize(superBlock)}/${dasherize(
|
||||||
|
block
|
||||||
|
)}/${dashedTitle}`;
|
||||||
|
return map;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.createPathMigrationMap = createPathMigrationMap;
|
32
tools/scripts/seed/createPathMigrationMap.test.js
Normal file
32
tools/scripts/seed/createPathMigrationMap.test.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/* global describe expect */
|
||||||
|
const { isPlainObject } = require('lodash');
|
||||||
|
|
||||||
|
const { createPathMigrationMap } = require('./createPathMigrationMap');
|
||||||
|
const mockCurriculum = require('./__mocks__/curriculum.json');
|
||||||
|
|
||||||
|
describe('createPathMigrationMap', () => {
|
||||||
|
const pathMap = createPathMigrationMap(mockCurriculum);
|
||||||
|
|
||||||
|
it('is a function', () => {
|
||||||
|
expect(typeof createPathMigrationMap).toEqual('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns an object', () => {
|
||||||
|
expect(isPlainObject(pathMap)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('maps a challenge title to the correct uri slug', () => {
|
||||||
|
expect.assertions(3);
|
||||||
|
const slugOne = '/learn/super-block-b/block-one/challenge-ten';
|
||||||
|
const slugTwo = '/learn/super-block-a/block-two/challenge-five';
|
||||||
|
const slugThree = '/learn/super-block-a/block-one/challenge-three';
|
||||||
|
|
||||||
|
expect(pathMap['challenge-ten']).toEqual(slugOne);
|
||||||
|
expect(pathMap['challenge-five']).toEqual(slugTwo);
|
||||||
|
expect(pathMap['challenge-three']).toEqual(slugThree);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not add uri migrations for private challenges', () => {
|
||||||
|
expect(pathMap['challenge-nine']).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
Reference in New Issue
Block a user