fix: Add tests for redirectToLearn

This commit is contained in:
Bouncey
2019-02-23 23:40:10 +00:00
committed by mrugesh mohapatra
parent 953e1b2e11
commit 014c26cd4e
3 changed files with 52 additions and 11 deletions

View File

@ -16,7 +16,7 @@ import { homeLocation } from '../../../config/env';
import { ifNoUserSend } from '../utils/middleware';
import { dasherize } from '../utils';
import pathMigrations from '../resources/pathMigration.json';
import _pathMigrations from '../resources/pathMigration.json';
import { fixCompletedChallengeItem } from '../../common/utils';
const log = debug('fcc:boot:challenges');
@ -25,6 +25,7 @@ export default async function bootChallenge(app, done) {
const send200toNonUser = ifNoUserSend(true);
const api = app.loopback.Router();
const router = app.loopback.Router();
const redirectToLearn = createRedirectToLearn(_pathMigrations);
const challengeUrlResolver = await createChallengeUrlResolver(app);
const redirectToCurrentChallenge = createRedirectToCurrentChallenge(
challengeUrlResolver
@ -352,11 +353,17 @@ export function createRedirectToCurrentChallenge(
};
}
function redirectToLearn(req, res) {
export function createRedirectToLearn(
pathMigrations,
base = homeLocation,
learn = learnURL
) {
return function redirectToLearn(req, res) {
const maybeChallenge = last(req.path.split('/'));
if (maybeChallenge in pathMigrations) {
const redirectPath = pathMigrations[maybeChallenge];
return res.status(302).redirect(`${learnURL}${redirectPath}`);
return res.status(302).redirect(`${base}${redirectPath}`);
}
return res.status(302).redirect(learnURL);
return res.status(302).redirect(learn);
};
}

View File

@ -9,7 +9,8 @@ import {
createChallengeUrlResolver,
createRedirectToCurrentChallenge,
getFirstChallenge,
isValidChallengeCompletion
isValidChallengeCompletion,
createRedirectToLearn
} from '../boot/challenge';
import {
@ -22,7 +23,8 @@ import {
mockGetFirstChallenge,
firstChallengeQuery,
mockCompletedChallenge,
mockCompletedChallenges
mockCompletedChallenges,
mockPathMigrationMap
} from './fixtures';
describe('boot/challenge', () => {
@ -372,5 +374,32 @@ describe('boot/challenge', () => {
});
});
xdescribe('redirectToLearn');
describe('redirectToLearn', () => {
const mockHome = 'https://example.com';
const mockLearn = 'https://example.com/learn';
const redirectToLearn = createRedirectToLearn(
mockPathMigrationMap,
mockHome,
mockLearn
);
it('redirects to learn by default', () => {
const req = mockReq({ path: '/challenges' });
const res = mockRes();
redirectToLearn(req, res);
expect(res.redirect.calledWith(mockLearn)).toBe(true);
});
it('maps to the correct redirect if the path matches a challenge', () => {
const req = mockReq({ path: '/challenges/challenge-two' });
const res = mockRes();
const expectedRedirect =
'https://example.com/learn/superblock/block/challenge-two';
redirectToLearn(req, res);
expect(res.redirect.calledWith(expectedRedirect)).toBe(true);
});
});
});

View File

@ -81,3 +81,8 @@ export const firstChallengeQuery = {
// first challenge of the first block of the first superBlock
where: { challengeOrder: 0, superOrder: 1, order: 0 }
};
export const mockPathMigrationMap = {
'challenge-one': '/learn/superblock/block/challenge-one',
'challenge-two': '/learn/superblock/block/challenge-two'
};