From 4fc1d6a950747410be71fe702ec5b2abc9d35eec Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Fri, 1 Jul 2016 18:44:34 -0700 Subject: [PATCH] Add(challenges): Add dev mode exception to next challenge logic --- common/app/routes/challenges/utils.js | 67 ++++++---- common/app/routes/challenges/utils.test.js | 141 ++++++++++++++++++++- 2 files changed, 183 insertions(+), 25 deletions(-) diff --git a/common/app/routes/challenges/utils.js b/common/app/routes/challenges/utils.js index 8bcbd2c8cf..6ee92636fe 100644 --- a/common/app/routes/challenges/utils.js +++ b/common/app/routes/challenges/utils.js @@ -91,7 +91,14 @@ export function getFirstChallenge( ]; } -export function getNextChallenge(current, entities, skip = 0) { +export function getNextChallenge( + current, + entities, + { + isDev = false, + skip = 0 + } = {} +) { const { challenge: challengeMap, block: blockMap } = entities; // find current challenge // find current block @@ -108,15 +115,22 @@ export function getNextChallenge(current, entities, skip = 0) { // skip is used to skip isComingSoon challenges block.challenges[ index + 1 + skip ] ]; - if (nextChallenge && nextChallenge.isComingSoon) { + if (!isDev && nextChallenge && nextChallenge.isComingSoon) { // if we find a next challenge and it is a coming soon // recur with plus one to skip this challenge - return getNextChallenge(current, entities, skip + 1); + return getNextChallenge(current, entities, { isDev, skip: skip + 1 }); } return nextChallenge; } -export function getFirstChallengeOfNextBlock(current, entities, skip = 0) { +export function getFirstChallengeOfNextBlock( + current, + entities, + { + isDev = false, + skip = 0 + } = {} +) { const { challenge: challengeMap, block: blockMap, @@ -147,28 +161,35 @@ export function getFirstChallengeOfNextBlock(current, entities, skip = 0) { } // grab first challenge from next block const nextChallenge = challengeMap[newBlock.challenges[0]]; - if (nextChallenge && nextChallenge.isComingSoon) { - // if first challenge is coming soon, find next challenge here - const nextChallenge2 = getNextChallenge(nextChallenge.dashedName, entities); - if (!nextChallenge2) { - // whole block is coming soon - // skip this block - return getFirstChallengeOfNextBlock( - current, - entities, - skip + 1 - ); - } + if (isDev || !nextChallenge || !nextChallenge.isComingSoon) { + return nextChallenge; + } + // if first challenge is coming soon, find next challenge here + const nextChallenge2 = getNextChallenge( + nextChallenge.dashedName, + entities, + { isDev } + ); + if (nextChallenge2) { return nextChallenge2; } - return nextChallenge; + // whole block is coming soon + // skip this block + return getFirstChallengeOfNextBlock( + current, + entities, + { isDev, skip: skip + 1 } + ); } export function getFirstChallengeOfNextSuperBlock( current, entities, superBlocks, - skip = 0 + { + isDev = false, + skip = 0 + } = {} ) { const { challenge: challengeMap, @@ -199,14 +220,15 @@ export function getFirstChallengeOfNextSuperBlock( return null; } const nextChallenge = challengeMap[newBlock.challenges[0]]; - if (!nextChallenge || !nextChallenge.isComingSoon) { + if (isDev || !nextChallenge || !nextChallenge.isComingSoon) { return nextChallenge; } // coming soon challenge, grab next // non coming soon challenge in same block instead const nextChallengeInBlock = getNextChallenge( nextChallenge.dashedName, - entities + entities, + { isDev } ); if (nextChallengeInBlock) { return nextChallengeInBlock; @@ -215,7 +237,8 @@ export function getFirstChallengeOfNextSuperBlock( // grab first challenge in next block in newSuperBlock instead const challengeInNextBlock = getFirstChallengeOfNextBlock( nextChallenge.dashedName, - entities + entities, + { isDev } ); if (challengeInNextBlock) { @@ -227,7 +250,7 @@ export function getFirstChallengeOfNextSuperBlock( current, entities, superBlocks, - skip + 1 + { isDev, skip: skip + 1 } ); } diff --git a/common/app/routes/challenges/utils.test.js b/common/app/routes/challenges/utils.test.js index 685c95f192..7a472e89a5 100644 --- a/common/app/routes/challenges/utils.test.js +++ b/common/app/routes/challenges/utils.test.js @@ -8,7 +8,7 @@ import { test('common/app/routes/challenges/utils', function(t) { t.test('getNextChallenge', t => { - t.plan(4); + t.plan(5); t.test('should return falsey when current challenge is not found', t => { t.plan(1); const entities = { @@ -120,10 +120,46 @@ test('common/app/routes/challenges/utils', function(t) { ); t.isEqual(shouldBeNext, nextChallenge); }); + t.test('should not skip isComingSoon challenge in dev', t => { + t.plan(1); + const currentChallenge = { + dashedName: 'current-challenge', + block: 'current-block' + }; + const comingSoon = { + dashedName: 'coming-soon', + isComingSoon: true, + block: 'current-block' + }; + const nextChallenge = { + dashedName: 'next-challenge', + block: 'current-block' + }; + const entities = { + challenge: { + 'current-challenge': currentChallenge, + 'next-challenge': nextChallenge, + 'coming-soon': comingSoon + }, + block: { + 'current-block': { + challenges: [ + 'current-challenge', + 'coming-soon', + 'next-challenge' + ] + } + } + }; + t.isEqual( + getNextChallenge('current-challenge', entities, { isDev: true }), + comingSoon + ); + }); }); t.test('getFirstChallengeOfNextBlock', t => { - t.plan(7); + t.plan(8); t.test('should return falsey when current challenge is not found', t => { t.plan(1); const entities = { @@ -302,6 +338,58 @@ test('common/app/routes/challenges/utils', function(t) { 'getFirstChallengeOfNextBlock did not return the correct challenge' ); }); + t.test('should not skip coming soon in dev mode', t => { + t.plan(1); + const currentChallenge = { + dashedName: 'current-challenge', + block: 'current-block' + }; + const firstChallenge = { + dashedName: 'first-challenge', + block: 'next-block' + }; + const comingSoon = { + dashedName: 'coming-soon', + block: 'next-block', + isComingSoon: true + }; + const entities = { + challenge: { + [currentChallenge.dashedName]: currentChallenge, + [firstChallenge.dashedName]: firstChallenge, + 'coming-soon': comingSoon + }, + block: { + 'current-block': { + dashedName: 'current-block', + superBlock: 'current-super-block' + }, + 'next-block': { + dashedName: 'next-block', + superBlock: 'current-super-block', + challenges: [ + 'coming-soon', + 'first-challenge' + ] + } + }, + superBlock: { + 'current-super-block': { + dashedName: 'current-super-block', + blocks: [ 'current-block', 'next-block' ] + } + } + }; + t.equal( + getFirstChallengeOfNextBlock( + currentChallenge.dashedName, + entities, + { isDev: true } + ), + comingSoon, + 'getFirstChallengeOfNextBlock returned isComingSoon challenge' + ); + }); t.test('should skip block if all challenges are coming soon', t => { t.plan(2); const currentChallenge = { @@ -375,7 +463,7 @@ test('common/app/routes/challenges/utils', function(t) { }); t.test('getFirstChallengeOfNextBlock', t => { - t.plan(9); + t.plan(10); t.test('should return falsey if current challenge not found', t => { t.plan(1); const entities = { @@ -529,6 +617,53 @@ test('common/app/routes/challenges/utils', function(t) { firstChallenge ); }); + t.test('should not skip coming soon in dev mode', t => { + t.plan(1); + const firstChallenge = { + dashedName: 'first-challenge', + block: 'next-block' + }; + const comingSoon = { + dashedName: 'coming-soon', + block: 'next-block', + isComingSoon: true + }; + const entities = { + challenge: { + 'current-challenge': { block: 'current-block' }, + [firstChallenge.dashedName]: firstChallenge, + 'coming-soon': comingSoon + }, + block: { + 'current-block': { superBlock: 'current-super-block' }, + 'next-block': { + dashedName: 'next-block', + superBlock: 'next-super-block', + challenges: [ 'coming-soon', 'first-challenge' ] + } + }, + superBlock: { + 'current-super-block': { dashedName: 'current-super-block' }, + 'next-super-block': { + dashedName: 'next-super-block', + blocks: [ 'next-block' ] + } + } + }; + const superBlocks = [ + 'current-super-block', + 'next-super-block' + ]; + t.isEqual( + getFirstChallengeOfNextSuperBlock( + 'current-challenge', + entities, + superBlocks, + { isDev: true } + ), + comingSoon + ); + }); t.test('should skip coming soon block', t => { t.plan(1); const firstChallenge = {