From b0bb899adcb5292ece64970360362641ecebb569 Mon Sep 17 00:00:00 2001 From: Prakhar Srivastav Date: Mon, 6 Jul 2015 13:28:44 +0300 Subject: [PATCH] finished initial implementation on getPath. #26 --- tests/data/survey.js | 6 ++++++ tests/testSurvey.js | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/tests/data/survey.js b/tests/data/survey.js index 69b5344..acf5940 100644 --- a/tests/data/survey.js +++ b/tests/data/survey.js @@ -21,6 +21,12 @@ module.exports = [ "id": "b_39562", "questions": [], "subblocks": [], + "randomize": false + }, + { + "id": "b_99223", + "questions": [], + "subblocks": [], "randomize": true } ], diff --git a/tests/testSurvey.js b/tests/testSurvey.js index 6f817f6..9c34b6d 100644 --- a/tests/testSurvey.js +++ b/tests/testSurvey.js @@ -8,22 +8,39 @@ var survey = Immutable.fromJS(data); var _blockMap = Immutable.fromJS({ "b_41444": "b_90914", "b_53209": "b_10101", - "b_39562": "b_53209" + "b_39562": "b_53209", + "b_99223": "b_53209" }); -function getBlockIndex(blockId, parentBlock = survey, path = []) { - var isSubblock = _blockMap.has(blockId); - if (!isSubblock) { - let index = parentBlock.findIndex(b => b.get('id') === blockId); - return path.concat(index); - } +function getPath(blockID) { + var getIDsList = (id, path = []) => { + if (!_blockMap.has(id)) { + return path.concat([id]).reverse() + } + return getIDsList(_blockMap.get(id), path.concat([id])); + }; + + var [rootID, ...restIDs] = getIDsList(blockID); + var path = [survey.findIndex(b => b.get('id') === rootID)]; + + return restIDs.reduce((path, id) => { + let index = survey.getIn(path.concat(['subblocks'])) + .findIndex(b => b.get('id') === id); + return path.concat(['subblocks', index]); + }, path); } -// test cases -describe('Immutable', function() { - describe("get()", function() { - it("should return the block index of top block correctly", function() { - assert.equal(survey.getIn([0, 'id']), "b_10101"); - }); +describe("getPath", function() { + it("should return the index paths correctly", function() { + assert.deepEqual(getPath('b_10101'), [0]); + assert.deepEqual(getPath('b_90914'), [1]); + assert.deepEqual(getPath('b_41444'), [1, 'subblocks', 0]); + assert.deepEqual(getPath('b_53209'), [0, 'subblocks', 0]); + assert.deepEqual(getPath('b_39562'), [0, 'subblocks', 0, 'subblocks', 0]); + assert.deepEqual(getPath('b_99223'), [0, 'subblocks', 0, 'subblocks', 1]); + }); + it("should get the correct block", function() { + var block = survey.getIn(getPath('b_39562')) + assert(!block.get('randomize')); }); });