Last active
May 7, 2017 01:08
-
-
Save alexbrillant/228e1926504413cfbb8261e995267d12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import update from 'immutability-helper'; | |
import {cloneDeep} from 'lodash'; | |
const selectedChoice = {isChosen: true}; | |
const choices = [selectedChoice, selectedChoice, selectedChoice]; | |
const options = {choices: choices}; | |
const selectedOptions = [options, options]; | |
const OPTION_INDEX = 0; | |
const CHOICE_INDEX = 1; | |
function unselectChoiceSpreadOperator(optionIndex, choiceIndex) { | |
return selectedOptions.map((option, currentOptionIndex) => { | |
if (currentOptionIndex === optionIndex) { | |
return { | |
...option, | |
choices: option.choices.map((choice, currentChoiceIndex) => { | |
if (currentChoiceIndex === choiceIndex ) { | |
return { | |
...choice, | |
isChosen: false | |
} | |
} | |
return choice; | |
}) | |
} | |
} | |
return option; | |
}); | |
} | |
function unselectChoiceLoadHashDeepClone(optionIndex, choiceIndex) { | |
let newSelectedOptions = cloneDeep(selectedOptions); | |
newSelectedOptions[optionIndex].choices[choiceIndex].isChosen = false; | |
return newSelectedOptions; | |
} | |
function unselectChoiceImmutablityHelper(optionIndex, choiceIndex) { | |
return update(selectedOptions, { | |
[optionIndex]: {choices: {[choiceIndex]: {isChosen: {$set: false}}}} | |
}); | |
} | |
test('Given option 0 And choice 1 When unselecting with es6 spread operator and map Then should unselect option 0 choice 1', () => { | |
const newSelectedOptions = unselectChoiceSpreadOperator(OPTION_INDEX, CHOICE_INDEX); | |
expectOptionZeroChoice1ToNotBeChosen(newSelectedOptions); | |
}); | |
test('Given option 0 And choice 1 When unselecting with deep clone Then should unselect option 0 choice 1', () => { | |
const newSelectedOptions = unselectChoiceLoadHashDeepClone(OPTION_INDEX, CHOICE_INDEX); | |
expectOptionZeroChoice1ToNotBeChosen(newSelectedOptions); | |
}); | |
test('Given option 0 And choice 1 When unselecting with immutability-helper Then should unselect option 0 choice 1', () => { | |
const newSelectedOptions = unselectChoiceImmutablityHelper(OPTION_INDEX, CHOICE_INDEX); | |
expectOptionZeroChoice1ToNotBeChosen(newSelectedOptions); | |
}); | |
function expectOptionZeroChoice1ToNotBeChosen(newSelectedOptions) { | |
expect(newSelectedOptions[OPTION_INDEX].choices[CHOICE_INDEX].isChosen).toBe(false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment