Skip to content

Commit

Permalink
Merge pull request #2 from xeontem/feature/functor-test
Browse files Browse the repository at this point in the history
lenses tests
  • Loading branch information
xeontem committed Mar 21, 2018
2 parents 2254dae + 44c7cc2 commit 874be1a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const xambda = () => {
path,
condL,
Apply,
filter,
setter,
getter,
setMap,
Expand Down
8 changes: 8 additions & 0 deletions test/functor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@ describe('Functors', function() {
assert.equal(X.fmap(func)(mock)[2], 20);
});
});

describe('filter', function() {
it('must filter the functor with a given predicate', () => {
const predicate = val => val > 10;
const functor = [11, 2, 33, 9];
assert.equal(X.filter(predicate)(functor)[0], 11);
});
});
});

50 changes: 50 additions & 0 deletions test/lenses.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const mockedData = require('./mockedData.js');


describe('Lenses abstraction', () => {

describe('lens', () => {
Expand Down Expand Up @@ -52,5 +55,52 @@ describe('Lenses abstraction', () => {
});
});

describe('view', () => {
it('must return the result of applying lenses to data', () => {
const lensField = fb => prop => X.lens(fieldGetter(fb)(prop))(X.setter(X.I)(fb)(prop));
const fieldGetter = fb => prop =>
X.S(obj => res => X.condL(X.includes('valid_field')(res))
(x => res)(x => fb))
(X.getter(X.I)(fb)(prop));

const parentLense = X.lensProp({})('parent');
const childLense = X.lensProp([])('child');
const child2Lense = X.lensProp({})('child2');
const fieldLense = lensField('')('field');


const temp = X.view(X.B(X.B(X.B(X.B(parentLense)(child2Lense))(childLense))(X.viewMap))(fieldLense))(mockedData);
// const temp = set(B(B(B(B(parentLense)(child2Lense))(childLense))(setMap))(fieldLense))('new value')(response);
// const temp = over(B(B(B(B(parentLense)(child2Lense))(childLense))(setMap))(fieldLense))(addExcl)(response);
// const temp = set(B(parentLense)(childLense))('hahahha')(child);
// const temp = over(B(B(B(parentLense)(childLense))(overMap))(fieldLense))(addExcl)(child);
// const temp = over(path([parentLense, childLense, overMap, fieldLense]))(addExcl)(child);
// const temp = over(B(parentLense)(childLense))(fmap(over(fieldLense)(addExcl)))(child);
assert.equal(temp[0], 'valid_field11111');
assert.equal(temp[1], '');
});
});

describe('set', () => {
it('must set a given value in place given by lenses', () => {
const mock = {parent: {child: [{field:'valid_field'},{field:'valid22_field2'}]}};

const lensField = fb => prop => X.lens(fieldGetter(fb)(prop))(X.setter(X.I)(fb)(prop));
const fieldGetter = fb => prop =>
X.S(obj => res => X.condL(X.includes('valid_field')(res))
(x => res)(x => fb))
(X.getter(X.I)(fb)(prop));

const parentLense = X.lensProp({})('parent');
const childLense = X.lensProp([])('child');
const child2Lense = X.lensProp({})('child2');
const fieldLense = lensField('')('field');

const temp = X.set(X.B(X.B(X.B(parentLense)(childLense))(X.overMap))(fieldLense))('test-data')(mock);
assert.equal(temp.parent.child[0].field, 'test-data');
assert.equal(temp.parent.child[1].field, 'test-data');
});
});

});

30 changes: 30 additions & 0 deletions test/mockedData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
parent: {
child: [
{inner: [{field: 'valid_field1'}, {field: 'bad_field2'}, {field: 'valid_field3'}, false]},
{inner: [{field: 'valid_field4'}, {field: 'valid_field5'}, {field: null}, {}]},
{inner: [{field: 'valid_field7'}, {field: 'valid_field8'}, {field: 'valid_notfield9'}, true]},
{},
undefined,
null,
false
],
child2: {
child: [{field: 'valid_field11111'}, {field: 'valid22_field22222'}]
},
// flights: [],
flights: [
[{flightRPH: '1', selected: true}, {flightRPH: '2', selected: true}, {flightRPH: '3', selected: true}, null],
[{flightRPH: '5', selected: true}, {flightRPH: '6', selected: true}, {flightRPH: '7', selected: true}],
[{flightRPH: '8', selected: false}, {flightRPH: '9', selected: false}, {flightRPH: '10', selected: false}],
[{flightRPH: '11', selected: true}, {flightRPH: '12', selected: null}, {flightRPH: '13', selected: true}],
[{flightRPH: '14', selected: true}, {flightRPH: '15', selected: true}, {flightRPH: '16', selected: true}],
null,
false,
[{flightRPH: '17', selected: false}, {flightRPH: '18', selected: false}, {flightRPH: '19', selected: false}],
[{flightRPH: '20', selected: true}, {flightRPH: '21', selected: true}, {flightRPH: '22', selected: true}],
[{flightRPH: '23', selected: false}, {flightRPH: '24', selected: false}, {flightRPH: '25', selected: false}],
[]
]
}
};

0 comments on commit 874be1a

Please sign in to comment.