From d52ba8c6fd2a546b6b862e77c87ababfc20c94e9 Mon Sep 17 00:00:00 2001 From: Bouncey Date: Sun, 7 Oct 2018 08:52:41 +0100 Subject: [PATCH] fix(path-map): Refactor pathMigrationMap for new curriculum package --- tools/scripts/seed/__mocks__/curriculum.json | 86 +++++++++++++++++++ tools/scripts/seed/createPathMigrationMap.js | 25 ++++++ .../seed/createPathMigrationMap.test.js | 32 +++++++ 3 files changed, 143 insertions(+) create mode 100644 tools/scripts/seed/__mocks__/curriculum.json create mode 100644 tools/scripts/seed/createPathMigrationMap.js create mode 100644 tools/scripts/seed/createPathMigrationMap.test.js diff --git a/tools/scripts/seed/__mocks__/curriculum.json b/tools/scripts/seed/__mocks__/curriculum.json new file mode 100644 index 0000000000..d958d35793 --- /dev/null +++ b/tools/scripts/seed/__mocks__/curriculum.json @@ -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" + } + ] + } + } + } +} diff --git a/tools/scripts/seed/createPathMigrationMap.js b/tools/scripts/seed/createPathMigrationMap.js new file mode 100644 index 0000000000..eed327892e --- /dev/null +++ b/tools/scripts/seed/createPathMigrationMap.js @@ -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; diff --git a/tools/scripts/seed/createPathMigrationMap.test.js b/tools/scripts/seed/createPathMigrationMap.test.js new file mode 100644 index 0000000000..835731103b --- /dev/null +++ b/tools/scripts/seed/createPathMigrationMap.test.js @@ -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(); + }); +});