|
|
@@ -4,7 +4,7 @@ import {projectData} from './project-data.js';
|
|
|
|
|
|
describe('projectData', function () {
|
|
|
it('should require the "options" argument to be an object', function () {
|
|
|
- const throwable = v => () => projectData({}, 10, v);
|
|
|
+ const throwable = v => () => projectData(10, {}, v);
|
|
|
const error = s =>
|
|
|
format('Projection options must be an Object, but %s was given.', s);
|
|
|
expect(throwable('str')).to.throw(error('"str"'));
|
|
|
@@ -20,7 +20,7 @@ describe('projectData', function () {
|
|
|
});
|
|
|
|
|
|
it('should require the "strict" option to be a boolean', function () {
|
|
|
- const throwable = v => () => projectData({}, 10, {strict: v});
|
|
|
+ const throwable = v => () => projectData(10, {}, {strict: v});
|
|
|
const error = s =>
|
|
|
format(
|
|
|
'Projection option "strict" must be a Boolean, but %s was given.',
|
|
|
@@ -39,7 +39,7 @@ describe('projectData', function () {
|
|
|
});
|
|
|
|
|
|
it('should require the "scope" option to be a non-empty string', function () {
|
|
|
- const throwable = v => () => projectData({}, 10, {scope: v});
|
|
|
+ const throwable = v => () => projectData(10, {}, {scope: v});
|
|
|
const error = s =>
|
|
|
format(
|
|
|
'Projection option "scope" must be a non-empty String, ' +
|
|
|
@@ -59,7 +59,7 @@ describe('projectData', function () {
|
|
|
});
|
|
|
|
|
|
it('should require the "nameResolver" option to be a function', function () {
|
|
|
- const throwable = v => () => projectData({}, 10, {nameResolver: v});
|
|
|
+ const throwable = v => () => projectData(10, {}, {nameResolver: v});
|
|
|
const error = s =>
|
|
|
format(
|
|
|
'Projection option "nameResolver" must be ' +
|
|
|
@@ -80,7 +80,7 @@ describe('projectData', function () {
|
|
|
});
|
|
|
|
|
|
it('should require the "factoryArgs" option to be an Array', function () {
|
|
|
- const throwable = v => () => projectData({}, 10, {factoryArgs: v});
|
|
|
+ const throwable = v => () => projectData(10, {}, {factoryArgs: v});
|
|
|
const error = s =>
|
|
|
format(
|
|
|
'Projection option "factoryArgs" must be an Array, but %s was given.',
|
|
|
@@ -104,7 +104,7 @@ describe('projectData', function () {
|
|
|
invoked++;
|
|
|
return {foo: true, bar: false};
|
|
|
};
|
|
|
- const res = projectData(factory, {foo: 10, bar: 20});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, factory);
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
expect(invoked).to.be.eq(1);
|
|
|
});
|
|
|
@@ -117,7 +117,7 @@ describe('projectData', function () {
|
|
|
expect(args).to.be.eql(factoryArgs);
|
|
|
return {foo: true, bar: false};
|
|
|
};
|
|
|
- const res = projectData(factory, {foo: 10, bar: 20}, {factoryArgs});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, factory, {factoryArgs});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
expect(invoked).to.be.eq(1);
|
|
|
});
|
|
|
@@ -131,8 +131,8 @@ describe('projectData', function () {
|
|
|
return {bar: true, baz: false};
|
|
|
};
|
|
|
const res = projectData(
|
|
|
- {foo: {schema: factory}},
|
|
|
{foo: {bar: 10, baz: 20}},
|
|
|
+ {foo: {schema: factory}},
|
|
|
{factoryArgs},
|
|
|
);
|
|
|
expect(res).to.be.eql({foo: {bar: 10}});
|
|
|
@@ -140,7 +140,7 @@ describe('projectData', function () {
|
|
|
});
|
|
|
|
|
|
it('should require a factory value to be an object or a non-empty string', function () {
|
|
|
- const throwable = v => () => projectData(() => v, {});
|
|
|
+ const throwable = v => () => projectData({}, () => v);
|
|
|
const error = s =>
|
|
|
format(
|
|
|
'Schema factory must return an Object ' +
|
|
|
@@ -156,8 +156,8 @@ describe('projectData', function () {
|
|
|
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
|
expect(throwable(null)).to.throw(error('null'));
|
|
|
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
|
- projectData(() => ({}), {});
|
|
|
- projectData(() => 'str', {}, {nameResolver: () => ({})});
|
|
|
+ projectData({}, () => ({}));
|
|
|
+ projectData({}, () => 'str', {nameResolver: () => ({})});
|
|
|
});
|
|
|
|
|
|
it('should resolve the schema name by the name resolver', function () {
|
|
|
@@ -167,13 +167,13 @@ describe('projectData', function () {
|
|
|
expect(name).to.be.eql('mySchema');
|
|
|
return {foo: true, bar: false};
|
|
|
};
|
|
|
- const res = projectData('mySchema', {foo: 10, bar: 20}, {nameResolver});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, 'mySchema', {nameResolver});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
expect(invoked).to.be.eq(1);
|
|
|
});
|
|
|
|
|
|
it('should require the "nameResolver" option when the schema name is provided', function () {
|
|
|
- const throwable = () => projectData('mySchema', {});
|
|
|
+ const throwable = () => projectData({}, 'mySchema');
|
|
|
expect(throwable).to.throw(
|
|
|
'Projection option "nameResolver" is required ' +
|
|
|
'to resolve "mySchema" name.',
|
|
|
@@ -182,7 +182,7 @@ describe('projectData', function () {
|
|
|
|
|
|
it('should require the name resolver to return an object', function () {
|
|
|
const throwable = v => () =>
|
|
|
- projectData('mySchema', {}, {nameResolver: () => v});
|
|
|
+ projectData({}, 'mySchema', {nameResolver: () => v});
|
|
|
const error = s =>
|
|
|
format('Name resolver must return an Object, but %s was given.', s);
|
|
|
expect(throwable('str')).to.throw(error('"str"'));
|
|
|
@@ -205,11 +205,9 @@ describe('projectData', function () {
|
|
|
expect(name).to.be.eql('mySchema');
|
|
|
return {foo: true, bar: false};
|
|
|
};
|
|
|
- const res = projectData(
|
|
|
- () => 'mySchema',
|
|
|
- {foo: 10, bar: 20},
|
|
|
- {nameResolver},
|
|
|
- );
|
|
|
+ const res = projectData({foo: 10, bar: 20}, () => 'mySchema', {
|
|
|
+ nameResolver,
|
|
|
+ });
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
expect(invoked).to.be.eq(1);
|
|
|
});
|
|
|
@@ -225,7 +223,7 @@ describe('projectData', function () {
|
|
|
}
|
|
|
};
|
|
|
const data = {foo: 10, bar: {baz: 20, qux: 30}};
|
|
|
- const res = projectData('schema1', data, {nameResolver});
|
|
|
+ const res = projectData(data, 'schema1', {nameResolver});
|
|
|
expect(res).to.be.eql({foo: 10, bar: {baz: 20}});
|
|
|
expect(invoked).to.be.eq(2);
|
|
|
});
|
|
|
@@ -233,45 +231,45 @@ describe('projectData', function () {
|
|
|
it('should validate the given schema in the shallow mode', function () {
|
|
|
const schema1 = {foo: '?'};
|
|
|
const schema2 = {foo: true, bar: {schema: {baz: '?'}}};
|
|
|
- expect(() => projectData(schema1, {foo: 10})).to.throw(
|
|
|
+ expect(() => projectData({foo: 10}, schema1)).to.throw(
|
|
|
'Property options must be an Object or a Boolean, but "?" was given.',
|
|
|
);
|
|
|
- const res = projectData(schema2, {foo: 10});
|
|
|
+ const res = projectData({foo: 10}, schema2);
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
- expect(() => projectData(schema2, {bar: {baz: 20}})).to.throw(
|
|
|
+ expect(() => projectData({bar: {baz: 20}}, schema2)).to.throw(
|
|
|
'Property options must be an Object or a Boolean, but "?" was given.',
|
|
|
);
|
|
|
});
|
|
|
|
|
|
it('should return primitive value as is', function () {
|
|
|
- expect(projectData({}, 'str')).to.be.eq('str');
|
|
|
- expect(projectData({}, '')).to.be.eq('');
|
|
|
- expect(projectData({}, 10)).to.be.eq(10);
|
|
|
- expect(projectData({}, 0)).to.be.eq(0);
|
|
|
- expect(projectData({}, true)).to.be.eq(true);
|
|
|
- expect(projectData({}, false)).to.be.eq(false);
|
|
|
- expect(projectData({}, undefined)).to.be.eq(undefined);
|
|
|
- expect(projectData({}, null)).to.be.eq(null);
|
|
|
+ expect(projectData('str', {})).to.be.eq('str');
|
|
|
+ expect(projectData('', {})).to.be.eq('');
|
|
|
+ expect(projectData(10, {})).to.be.eq(10);
|
|
|
+ expect(projectData(0, {})).to.be.eq(0);
|
|
|
+ expect(projectData(true, {})).to.be.eq(true);
|
|
|
+ expect(projectData(false, {})).to.be.eq(false);
|
|
|
+ expect(projectData(undefined, {})).to.be.eq(undefined);
|
|
|
+ expect(projectData(null, {})).to.be.eq(null);
|
|
|
});
|
|
|
|
|
|
it('should project an array items', function () {
|
|
|
const list = [{foo: 10, bar: 20, baz: 30}, {qux: 30}];
|
|
|
const expectedList = [{foo: 10, baz: 30}, {qux: 30}];
|
|
|
- const res = projectData({foo: true, bar: false}, list);
|
|
|
+ const res = projectData(list, {foo: true, bar: false});
|
|
|
expect(res).to.be.eql(expectedList);
|
|
|
});
|
|
|
|
|
|
it('should project an array items in the strict mode', function () {
|
|
|
const list = [{foo: 10, bar: 20, baz: 30}, {qux: 30}];
|
|
|
const expectedList = [{foo: 10}, {}];
|
|
|
- const res = projectData({foo: true, bar: false}, list, {strict: true});
|
|
|
+ const res = projectData(list, {foo: true, bar: false}, {strict: true});
|
|
|
expect(res).to.be.eql(expectedList);
|
|
|
});
|
|
|
|
|
|
it('should exclude properties without rules when the strict mode is enabled', function () {
|
|
|
const res = projectData(
|
|
|
- {foo: true, bar: false},
|
|
|
{foo: 10, bar: 20, baz: 30},
|
|
|
+ {foo: true, bar: false},
|
|
|
{strict: true},
|
|
|
);
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
@@ -282,19 +280,19 @@ describe('projectData', function () {
|
|
|
data.foo = 10;
|
|
|
data.bar = 20;
|
|
|
expect(data).to.be.eql({foo: 10, bar: 20, baz: 30});
|
|
|
- const res = projectData({foo: true, bar: false}, data);
|
|
|
+ const res = projectData(data, {foo: true, bar: false});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
it('should project the property by a boolean rule', function () {
|
|
|
- const res = projectData({foo: true, bar: false}, {foo: 10, bar: 20});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, {foo: true, bar: false});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
it('should project the property by the select option', function () {
|
|
|
const res = projectData(
|
|
|
- {foo: {select: true}, bar: {select: false}},
|
|
|
{foo: 10, bar: 20},
|
|
|
+ {foo: {select: true}, bar: {select: false}},
|
|
|
);
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
@@ -304,7 +302,7 @@ describe('projectData', function () {
|
|
|
foo: {select: true, scopes: {input: false}},
|
|
|
bar: {select: false, scopes: {output: true}},
|
|
|
};
|
|
|
- const res = projectData(schema, {foo: 10, bar: 20});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, schema);
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
@@ -313,7 +311,7 @@ describe('projectData', function () {
|
|
|
foo: {scopes: {input: true}},
|
|
|
bar: {scopes: {input: false}},
|
|
|
};
|
|
|
- const res = projectData(schema, {foo: 10, bar: 20}, {scope: 'input'});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, schema, {scope: 'input'});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
@@ -322,7 +320,7 @@ describe('projectData', function () {
|
|
|
foo: {scopes: {input: {select: true}}},
|
|
|
bar: {scopes: {input: {select: false}}},
|
|
|
};
|
|
|
- const res = projectData(schema, {foo: 10, bar: 20}, {scope: 'input'});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, schema, {scope: 'input'});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
@@ -331,7 +329,7 @@ describe('projectData', function () {
|
|
|
foo: {select: false, scopes: {input: true}},
|
|
|
bar: {select: true, scopes: {input: false}},
|
|
|
};
|
|
|
- const res = projectData(schema, {foo: 10, bar: 20}, {scope: 'input'});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, schema, {scope: 'input'});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
@@ -340,7 +338,7 @@ describe('projectData', function () {
|
|
|
foo: {scopes: {input: true, output: false}},
|
|
|
bar: {scopes: {input: false, output: true}},
|
|
|
};
|
|
|
- const res = projectData(schema, {foo: 10, bar: 20}, {scope: 'input'});
|
|
|
+ const res = projectData({foo: 10, bar: 20}, schema, {scope: 'input'});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
@@ -351,7 +349,7 @@ describe('projectData', function () {
|
|
|
baz: {scopes: {output: true}},
|
|
|
};
|
|
|
const data = {foo: 10, bar: 20, baz: 30, qux: 40};
|
|
|
- const res = projectData(schema, data, {strict: true, scope: 'input'});
|
|
|
+ const res = projectData(data, schema, {strict: true, scope: 'input'});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
@@ -361,7 +359,7 @@ describe('projectData', function () {
|
|
|
bar: {select: false, scopes: {input: {select: true}}},
|
|
|
};
|
|
|
const data = {foo: 10, bar: 20, baz: 30};
|
|
|
- const res = projectData(schema, data, {strict: true, scope: 'input'});
|
|
|
+ const res = projectData(data, schema, {strict: true, scope: 'input'});
|
|
|
expect(res).to.be.eql({foo: 10, bar: 20});
|
|
|
});
|
|
|
|
|
|
@@ -371,7 +369,7 @@ describe('projectData', function () {
|
|
|
bar: {schema: {baz: true, qux: false}},
|
|
|
};
|
|
|
const data = {foo: 10, bar: {baz: 20, qux: 30, buz: 40}};
|
|
|
- const res = projectData(schema, data, {scope: 'input'});
|
|
|
+ const res = projectData(data, schema, {scope: 'input'});
|
|
|
expect(res).to.be.eql({foo: 10, bar: {baz: 20, buz: 40}});
|
|
|
});
|
|
|
|
|
|
@@ -381,7 +379,7 @@ describe('projectData', function () {
|
|
|
bar: {select: true, schema: {baz: true, qux: false}},
|
|
|
};
|
|
|
const data = {foo: 10, bar: {baz: 20, qux: 30, buz: 40}};
|
|
|
- const res = projectData(schema, data, {strict: true, scope: 'input'});
|
|
|
+ const res = projectData(data, schema, {strict: true, scope: 'input'});
|
|
|
expect(res).to.be.eql({foo: 10, bar: {baz: 20}});
|
|
|
});
|
|
|
});
|