This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
65 additions
and
145 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,88 @@ | ||
/* global describe, it, expect */ | ||
|
||
const _ = require('../../../lib/utils/lodash-utils'); // our custom function(s) of lodash | ||
const _ = include('lib/utils/lodash-utils'); // our custom function(s) of lodash | ||
const original_lodash = require('lodash'); | ||
|
||
|
||
describe('Unit | Utils | lodash-utils', function () { | ||
|
||
describe('scope', function () { | ||
it('should not affect original version of lodash', function (done) { | ||
it('should not affect original version of lodash', function () { | ||
expect(original_lodash.elementAfter).not.to.exist; | ||
expect(_.elementAfter).to.exist; | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('elementAfter', function () { | ||
it('for a given array and element in array (but not the last one), should return the element after the one provided', function (done) { | ||
it('for a given array and element in array (but not the last one), should return the element after the one provided', function () { | ||
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'a')).to.equal('b'); | ||
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'b')).to.equal('c'); | ||
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'c')).to.equal('d'); | ||
done(); | ||
}); | ||
it('for a given array and the LAST element in array, should return undefined', function (done) { | ||
it('for a given array and the LAST element in array, should return undefined', function () { | ||
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'd')).to.equal(undefined); | ||
done(); | ||
}); | ||
it('for a given array and an element NOT in array, should return undefined', function (done) { | ||
it('for a given array and an element NOT in array, should return undefined', function () { | ||
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'z')).to.equal(undefined); | ||
done(); | ||
}); | ||
it('for an empty array, should return undefined', function (done) { | ||
it('for an empty array, should return undefined', function () { | ||
expect(_.elementAfter([], 'z')).to.equal(undefined); | ||
done(); | ||
}); | ||
it('if first arg is not an array, should return undefined', function (done) { | ||
it('if first arg is not an array, should return undefined', function () { | ||
expect(_.elementAfter(new Date(), 'a')).to.equal(undefined); | ||
done(); | ||
}); | ||
it('if last arg is missing, should return undefined', function (done) { | ||
it('if last arg is missing, should return undefined', function () { | ||
expect(_.elementAfter(['a', 'b', 'c', 'd'])).to.equal(undefined); | ||
done(); | ||
}); | ||
it('if both args are is missing, should return undefined', function (done) { | ||
it('if both args are is missing, should return undefined', function () { | ||
expect(_.elementAfter()).to.equal(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
describe('areCSVequivalent', function () { | ||
it('when no arg are given, should return false', function (done) { | ||
it('when no arg are given, should return false', function () { | ||
expect(_.areCSVequivalent()).to.equal(false); | ||
done(); | ||
}); | ||
it('when two arg are given, but are not string, should return false', function (done) { | ||
it('when two arg are given, but are not string, should return false', function () { | ||
expect(_.areCSVequivalent(['1,2,3'], ['1,2,3'])).to.equal(false); | ||
expect(_.areCSVequivalent(new Date(), new Date())).to.equal(false); | ||
done(); | ||
}); | ||
it('when two string are the same, should return true', function (done) { | ||
it('when two string are the same, should return true', function () { | ||
expect(_.areCSVequivalent('1,2,3', '1,2,3')).to.equal(true); | ||
expect(_.areCSVequivalent('azerty', 'azerty')).to.equal(true); | ||
done(); | ||
}); | ||
it('when element are the same but in different order, should return true', function (done) { | ||
it('when element are the same but in different order, should return true', function () { | ||
expect(_.areCSVequivalent('1,2,3', '3,1,2')).to.equal(true); | ||
done(); | ||
}); | ||
it('when element have space around values, should return true', function (done) { | ||
it('when element have space around values, should return true', function () { | ||
expect(_.areCSVequivalent('2 , blabla, 1', 'blabla ,1,2')).to.equal(true); | ||
done(); | ||
}); | ||
|
||
}); | ||
|
||
|
||
describe('ensureString', function () { | ||
it('when no input, return an empty String', function (done) { | ||
it('when no input, return an empty String', function () { | ||
expect(_.ensureString()).to.equal(''); | ||
done(); | ||
}); | ||
it('when input is explicitly undefined, return an empty String', function (done) { | ||
it('when input is explicitly undefined, return an empty String', function () { | ||
expect(_.ensureString(undefined)).to.equal(''); | ||
done(); | ||
}); | ||
it('when input is explicitly null, return an empty String', function (done) { | ||
it('when input is explicitly null, return an empty String', function () { | ||
expect(_.ensureString(null)).to.equal(''); | ||
done(); | ||
}); | ||
it('when input is a number (typeof meaning), it returns a toString() version of the input', function (done) { | ||
it('when input is a number (typeof meaning), it returns a toString() version of the input', function () { | ||
expect(_.ensureString(42)).to.equal('42'); | ||
done(); | ||
}); | ||
it('when input is a string (typeof meaning), it returns a toString() version of the input', function (done) { | ||
it('when input is a string (typeof meaning), it returns a toString() version of the input', function () { | ||
expect(_.ensureString('42')).to.equal('42'); | ||
done(); | ||
}); | ||
it('when input is an object (typeof meaning), it returns a toString() version of the input', function (done) { | ||
it('when input is an object (typeof meaning), it returns a toString() version of the input', function () { | ||
expect(_.ensureString(/[aeiou]+/g)).to.equal('/[aeiou]+/g'); | ||
done(); | ||
}); | ||
it('when input is an boolean (typeof meaning), it returns a toString() version of the input', function (done) { | ||
it('when input is an boolean (typeof meaning), it returns a toString() version of the input', function () { | ||
expect(_.ensureString(true)).to.equal('true'); | ||
done(); | ||
}); | ||
}); | ||
}); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
import _ from 'pix-live/utils/lodash-custom'; | ||
|
||
// Example with 4 proposals | ||
// Proposals could be ['prop 1','prop 2','prop 3','prop 4'] | ||
// Answers given are only the first(s) : [false, true], the missing values are false | ||
// Output expected : [['prop 1', false], ['prop 2', true], ['prop 3', false], ['prop 4', false]] | ||
|
||
function calculate(proposals, answers) { | ||
// Example | ||
// proposals = ['prop 1','prop 2','prop 3','prop 4'] | ||
// answers = [false, true] | ||
const sizeDifference = proposals.length - answers.length; | ||
|
||
return _.chain(answers) // [false, true] | ||
.concat(_.times(sizeDifference, _.constant(false))) // [false, true, false, false] | ||
.zip(proposals) // [[false, 'prop 1'], [true, 'prop 2'], [false, 'prop 3'], [false, 'prop 4']] | ||
.map(_.reverse) // [['prop 1', false], ['prop 2', true], ['prop 3', false], ['prop 4', false]] | ||
.value(); | ||
} | ||
|
||
export default function labeledCheckboxes (proposals, answers) { | ||
|
||
const DEFAULT_RETURN_VALUE = []; | ||
/* | ||
* Example : | ||
* => Input : | ||
* proposals : ['is sky red ?' , 'is sun red ?' , 'is grass red ?' , 'is cloud red ?'] | ||
* => Input : | ||
* userAnswers : [false, true] | ||
* | ||
* WARNING : only first(s) userAnswers are given, | ||
* all others have implicitly the boolean value "false" | ||
* | ||
* => Output : | ||
* [['is sky red ?', false], | ||
* ['is sun red ?', true], | ||
* ['is grass red ?', false], | ||
* ['are clouds red ?' false]] | ||
*/ | ||
export default function labeledCheckboxes (proposals, userAnswers) { | ||
|
||
// check pre-conditions | ||
if (_(proposals).isEmpty()) return DEFAULT_RETURN_VALUE; | ||
if (_(proposals).isNotArrayOfString()) return DEFAULT_RETURN_VALUE; | ||
if (_(answers).isNotArrayOfBoolean()) return DEFAULT_RETURN_VALUE; | ||
|
||
return calculate(proposals, answers); | ||
|
||
if (_(proposals).isNotArrayOfString()) return []; | ||
if (_(proposals).isEmpty()) return []; | ||
if (_(userAnswers).isNotArrayOfBoolean()) return []; | ||
if (_(userAnswers).size() > _(proposals).size()) return []; | ||
|
||
const sizeDifference = _(proposals).size() - _(userAnswers).size(); // 2 | ||
const arrayOfFalse = _.times(sizeDifference, _.constant(false)); // [false, false] | ||
|
||
return _.chain(userAnswers) // [false, true] | ||
.concat(arrayOfFalse) // [false, true, false, false] | ||
.zip(proposals) // [[false, 'prop 1'], [true, 'prop 2'], [false, 'prop 3'], [false, 'prop 4']] | ||
.map(_.reverse) // [['prop 1', false], ['prop 2', true], ['prop 3', false], ['prop 4', false]] | ||
.value(); | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.