|
@@ -7,8 +7,8 @@ describe('projectData', function () {
|
|
|
const throwable = v => () => projectData(v, {});
|
|
const throwable = v => () => projectData(v, {});
|
|
|
const error = s =>
|
|
const error = s =>
|
|
|
format(
|
|
format(
|
|
|
- 'Projection schema must be an Object, a Function, ' +
|
|
|
|
|
- 'a non-empty String or a Symbol, but %s was given.',
|
|
|
|
|
|
|
+ 'Projection schema must be an Object, a Function ' +
|
|
|
|
|
+ 'or a non-empty String, but %s was given.',
|
|
|
s,
|
|
s,
|
|
|
);
|
|
);
|
|
|
expect(throwable('')).to.throw(error('""'));
|
|
expect(throwable('')).to.throw(error('""'));
|
|
@@ -152,8 +152,8 @@ describe('projectData', function () {
|
|
|
const throwable = v => () => projectData(() => v, {});
|
|
const throwable = v => () => projectData(() => v, {});
|
|
|
const error = s =>
|
|
const error = s =>
|
|
|
format(
|
|
format(
|
|
|
- 'Projection schema factory must return an Object, ' +
|
|
|
|
|
- 'a non-empty String or a Symbol, but %s was given.',
|
|
|
|
|
|
|
+ 'Projection schema factory must return an Object ' +
|
|
|
|
|
+ 'or a non-empty String, but %s was given.',
|
|
|
s,
|
|
s,
|
|
|
);
|
|
);
|
|
|
expect(throwable('')).to.throw(error('""'));
|
|
expect(throwable('')).to.throw(error('""'));
|
|
@@ -166,7 +166,6 @@ describe('projectData', function () {
|
|
|
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
|
projectData(() => ({}), {});
|
|
projectData(() => ({}), {});
|
|
|
projectData(() => 'mySchema', {}, {resolver: () => ({})});
|
|
projectData(() => 'mySchema', {}, {resolver: () => ({})});
|
|
|
- projectData(() => Symbol('mySchema'), {}, {resolver: () => ({})});
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('should resolve a schema object from the given factory', function () {
|
|
it('should resolve a schema object from the given factory', function () {
|
|
@@ -195,7 +194,7 @@ describe('projectData', function () {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- describe('string key', function () {
|
|
|
|
|
|
|
+ describe('named schema', function () {
|
|
|
it('should throw an error if the schema resolver returns an invalid value', function () {
|
|
it('should throw an error if the schema resolver returns an invalid value', function () {
|
|
|
const throwable = v => () =>
|
|
const throwable = v => () =>
|
|
|
projectData('mySchema', {}, {resolver: () => v});
|
|
projectData('mySchema', {}, {resolver: () => v});
|
|
@@ -216,7 +215,7 @@ describe('projectData', function () {
|
|
|
throwable({})();
|
|
throwable({})();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('should throw an error if no schema resolver is provided when a string key is given', function () {
|
|
|
|
|
|
|
+ it('should throw an error if no schema resolver is provided when a schema name is given', function () {
|
|
|
const throwable = () => projectData('mySchema', {});
|
|
const throwable = () => projectData('mySchema', {});
|
|
|
expect(throwable).to.throw(
|
|
expect(throwable).to.throw(
|
|
|
'Unable to resolve the projection schema "mySchema" ' +
|
|
'Unable to resolve the projection schema "mySchema" ' +
|
|
@@ -224,7 +223,7 @@ describe('projectData', function () {
|
|
|
);
|
|
);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('should pass the string key to the schema resolver and project the given data', function () {
|
|
|
|
|
|
|
+ it('should pass the schema name to the schema resolver and project the given data', function () {
|
|
|
let invoked = 0;
|
|
let invoked = 0;
|
|
|
const resolver = key => {
|
|
const resolver = key => {
|
|
|
expect(key).to.be.eq('mySchema');
|
|
expect(key).to.be.eq('mySchema');
|
|
@@ -252,7 +251,7 @@ describe('projectData', function () {
|
|
|
expect(invoked).to.be.eq(1);
|
|
expect(invoked).to.be.eq(1);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('should resolve the string key from the schema factory', function () {
|
|
|
|
|
|
|
+ it('should use the schema name from the schema factory', function () {
|
|
|
let invoked = 0;
|
|
let invoked = 0;
|
|
|
const resolver = key => {
|
|
const resolver = key => {
|
|
|
expect(key).to.be.eq('mySchema');
|
|
expect(key).to.be.eq('mySchema');
|
|
@@ -265,79 +264,6 @@ describe('projectData', function () {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- describe('symbol key', function () {
|
|
|
|
|
- it('should throw an error if the schema resolver returns an invalid value', function () {
|
|
|
|
|
- const throwable = v => () =>
|
|
|
|
|
- projectData(Symbol('mySchema'), {}, {resolver: () => v});
|
|
|
|
|
- const error = s =>
|
|
|
|
|
- format(
|
|
|
|
|
- 'Projection schema resolver must return an Object, but %s was given.',
|
|
|
|
|
- s,
|
|
|
|
|
- );
|
|
|
|
|
- expect(throwable('str')).to.throw(error('"str"'));
|
|
|
|
|
- expect(throwable('')).to.throw(error('""'));
|
|
|
|
|
- expect(throwable(10)).to.throw(error('10'));
|
|
|
|
|
- expect(throwable(0)).to.throw(error('0'));
|
|
|
|
|
- expect(throwable(true)).to.throw(error('true'));
|
|
|
|
|
- expect(throwable(false)).to.throw(error('false'));
|
|
|
|
|
- expect(throwable([])).to.throw(error('Array'));
|
|
|
|
|
- expect(throwable(null)).to.throw(error('null'));
|
|
|
|
|
- expect(throwable(undefined)).to.throw(error('undefined'));
|
|
|
|
|
- throwable({})();
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('should throw an error if no schema resolver is provided when a symbol key is given', function () {
|
|
|
|
|
- const throwable = () => projectData(Symbol('mySchema'), {});
|
|
|
|
|
- expect(throwable).to.throw(
|
|
|
|
|
- 'Unable to resolve the projection schema Symbol("mySchema") ' +
|
|
|
|
|
- 'without a provided resolver.',
|
|
|
|
|
- );
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('should pass the symbol key to the schema resolver and project the given data', function () {
|
|
|
|
|
- let invoked = 0;
|
|
|
|
|
- const symbolKey = Symbol('mySchema');
|
|
|
|
|
- const resolver = key => {
|
|
|
|
|
- expect(key).to.be.eq(symbolKey);
|
|
|
|
|
- invoked++;
|
|
|
|
|
- return {foo: true, bar: false};
|
|
|
|
|
- };
|
|
|
|
|
- const res = projectData(symbolKey, {foo: 10, bar: 20}, {resolver});
|
|
|
|
|
- expect(res).to.be.eql({foo: 10});
|
|
|
|
|
- expect(invoked).to.be.eq(1);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('should use the schema resolver in the nested schema', function () {
|
|
|
|
|
- let invoked = 0;
|
|
|
|
|
- const symbolKey = Symbol('mySchema');
|
|
|
|
|
- const resolver = key => {
|
|
|
|
|
- expect(key).to.be.eq(symbolKey);
|
|
|
|
|
- invoked++;
|
|
|
|
|
- return {baz: true, qux: false};
|
|
|
|
|
- };
|
|
|
|
|
- const res = projectData(
|
|
|
|
|
- {foo: true, bar: {schema: symbolKey}},
|
|
|
|
|
- {foo: 10, bar: {baz: 20, qux: 30}},
|
|
|
|
|
- {resolver},
|
|
|
|
|
- );
|
|
|
|
|
- expect(res).to.be.eql({foo: 10, bar: {baz: 20}});
|
|
|
|
|
- expect(invoked).to.be.eq(1);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('should resolve the symbol key from the schema factory', function () {
|
|
|
|
|
- let invoked = 0;
|
|
|
|
|
- const symbolKey = Symbol('mySchema');
|
|
|
|
|
- const resolver = key => {
|
|
|
|
|
- expect(key).to.be.eq(symbolKey);
|
|
|
|
|
- invoked++;
|
|
|
|
|
- return {foo: true, bar: false};
|
|
|
|
|
- };
|
|
|
|
|
- const res = projectData(() => symbolKey, {foo: 10, bar: 20}, {resolver});
|
|
|
|
|
- expect(res).to.be.eql({foo: 10});
|
|
|
|
|
- expect(invoked).to.be.eq(1);
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
describe('strict mode', function () {
|
|
describe('strict mode', function () {
|
|
|
it('should preserve fields not defined in the schema when the strict option is false', function () {
|
|
it('should preserve fields not defined in the schema when the strict option is false', function () {
|
|
|
const res = projectData({}, {foo: 10});
|
|
const res = projectData({}, {foo: 10});
|