Comparing version 0.2.12 to 0.2.13
{ | ||
"plugins": ["mocha"], | ||
"parserOptions": { | ||
@@ -11,3 +12,4 @@ "ecmaVersion": 7,//2016 | ||
"node": true, | ||
"es6": true | ||
"es6": true, | ||
"mocha": true | ||
}, | ||
@@ -14,0 +16,0 @@ "rules": { |
@@ -45,3 +45,3 @@ class Masker { | ||
const settled = {}; | ||
if (!destination[prop] && !source[prop]) { | ||
if (destination[prop] === undefined && source[prop] === undefined) { | ||
return settled; | ||
@@ -48,0 +48,0 @@ } |
{ | ||
"name": "carnaval", | ||
"version": "0.2.12", | ||
"version": "0.2.13", | ||
"repository": { | ||
@@ -9,3 +9,3 @@ "type": "git", | ||
"scripts": { | ||
"test": "ava --verbose", | ||
"test": "mocha --recursive test/", | ||
"lint": "eslint lib test" | ||
@@ -19,5 +19,6 @@ }, | ||
"ajv": "7.0.x", | ||
"ava": "2.4.x", | ||
"bluebird": "3.7.x", | ||
"eslint": "7.11.x" | ||
"chai": "4.3.x", | ||
"eslint": "7.11.x", | ||
"mocha": "10.3.x" | ||
}, | ||
@@ -24,0 +25,0 @@ "ava": { |
@@ -1,2 +0,2 @@ | ||
const test = require('ava'); | ||
const {expect} = require('chai'); | ||
@@ -13,25 +13,27 @@ const Domain = require('../lib/Domain'); | ||
test('domain creation', t => { | ||
const name = 'Shoes'; | ||
const thing = new Thing({name}); | ||
describe("domain", () => { | ||
it('domain creation', () => { | ||
const name = 'Shoes'; | ||
const thing = new Thing({name}); | ||
t.is(thing.name, name); | ||
}); | ||
expect(thing.name).to.equal(name); | ||
}); | ||
test('domain update', t => { | ||
const name = 'Shirt'; | ||
const thing = new Thing({name: 'Shoes'}); | ||
it('domain update', () => { | ||
const name = 'Shirt'; | ||
const thing = new Thing({name: 'Shoes'}); | ||
thing.name = name; | ||
thing.name = name; | ||
t.is(thing.name, name); | ||
}); | ||
expect(thing.name).to.equal(name); | ||
}); | ||
test('domain assign', t => { | ||
const name = 'Shirt'; | ||
const thing = new Thing({name: 'Shoes'}); | ||
it('domain assign', () => { | ||
const name = 'Shirt'; | ||
const thing = new Thing({name: 'Shoes'}); | ||
thing.assign({name: name}); | ||
thing.assign({name: name}); | ||
t.is(thing.name, name); | ||
expect(thing.name).to.equal(name); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
const test = require('ava'); | ||
const {expect} = require('chai'); | ||
@@ -21,364 +21,366 @@ const validate = require('./validator-ajv'); | ||
test('validate', t => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
describe("validator-ajv", () => { | ||
it('validate', () => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.then(thing => { | ||
t.is(thing.name, json.name); | ||
return mapping.decode(json) | ||
.then(thing => { | ||
expect(thing.name).to.equal(json.name); | ||
}); | ||
}); | ||
}); | ||
test('validate as promise', t => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => Promise.resolve(object).then(object => validate(object))); | ||
it('validate as promise', () => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => Promise.resolve(object).then(object => validate(object))); | ||
return mapping.decode(json) | ||
.then(thing => { | ||
t.is(thing.name, json.name); | ||
return mapping.decode(json) | ||
.then(thing => { | ||
expect(thing.name).to.equal(json.name); | ||
}); | ||
}); | ||
}); | ||
test('validate required error', t => { | ||
const json = {}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
it('validate required error', () => { | ||
const json = {}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'should have required property \'name\''); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('should have required property \'name\''); | ||
}); | ||
}); | ||
}); | ||
test('validate typed error', t => { | ||
const json = {name: 12}; | ||
const mapping = Mapping.map(Thing).normalize(false).afterDecode(object => validate(object)); | ||
it('validate typed error', () => { | ||
const json = {name: 12}; | ||
const mapping = Mapping.map(Thing).normalize(false).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'name should be string'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('name should be string'); | ||
}); | ||
}); | ||
}); | ||
test('validate no typed error with normalize', t => { | ||
const json = {name: 12}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
it('validate no typed error with normalize', () => { | ||
const json = {name: 12}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
return mapping.decode(json).then(thing => { | ||
t.is(thing.name, String(json.name)); | ||
return mapping.decode(json).then(thing => { | ||
expect(thing.name).to.equal(String(json.name)); | ||
}); | ||
}); | ||
}); | ||
class Box extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
class Box extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
size: {required: true}, | ||
thing: { | ||
name: {required: true} | ||
} | ||
}; | ||
} | ||
} | ||
get rules() { | ||
return { | ||
size: {required: true}, | ||
thing: { | ||
name: {required: true} | ||
} | ||
}; | ||
} | ||
} | ||
test('validate deep error', t => { | ||
const json = {size: 'Medium', thing: {}}; | ||
const mapping = Mapping.map(Box).afterDecode(object => validate(object)); | ||
it('validate deep error', () => { | ||
const json = {size: 'Medium', thing: {}}; | ||
const mapping = Mapping.map(Box).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'thing should have required property \'name\''); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('thing should have required property \'name\''); | ||
}); | ||
}); | ||
}); | ||
class EmptyBox extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
thing: { | ||
name: { | ||
value: {enum: ['valued']} | ||
class EmptyBox extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
thing: { | ||
name: { | ||
value: {enum: ['valued']} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
} | ||
test('validate deep (two levels) optionnal', t => { | ||
const json = {}; | ||
const mapping = Mapping.map(EmptyBox).afterDecode(object => validate(object)); | ||
it('validate deep (two levels) optionnal', () => { | ||
const json = {}; | ||
const mapping = Mapping.map(EmptyBox).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.then(emptyBox => { | ||
t.true(emptyBox instanceof EmptyBox); | ||
t.is(emptyBox.thing, undefined); | ||
return mapping.decode(json) | ||
.then(emptyBox => { | ||
expect(emptyBox instanceof EmptyBox).to.equal(true); | ||
expect(emptyBox.thing).to.equal(undefined); | ||
}); | ||
}); | ||
}); | ||
class NotEmptyBox extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
thing: { | ||
name: { | ||
value: {required: true, enum: ['valued']} | ||
class NotEmptyBox extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
thing: { | ||
name: { | ||
value: {required: true, enum: ['valued']} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
} | ||
test('validate deep (two levels) error', t => { | ||
const json = {}; | ||
const mapping = Mapping.map(NotEmptyBox).afterDecode(object => validate(object)); | ||
it('validate deep (two levels) error', () => { | ||
const json = {}; | ||
const mapping = Mapping.map(NotEmptyBox).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'should have required property \'thing\''); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('should have required property \'thing\''); | ||
}); | ||
}); | ||
}); | ||
class EmptyOrNotBox extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
thing: { | ||
required: false, | ||
name: { | ||
value: {required: true, enum: ['valued']} | ||
class EmptyOrNotBox extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
thing: { | ||
required: false, | ||
name: { | ||
value: {required: true, enum: ['valued']} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
} | ||
test('validate deep (two levels) empty', t => { | ||
const json = {}; | ||
const mapping = Mapping.map(EmptyOrNotBox).afterDecode(object => validate(object)); | ||
it('validate deep (two levels) empty', () => { | ||
const json = {}; | ||
const mapping = Mapping.map(EmptyOrNotBox).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.then(emptyBox => { | ||
t.true(emptyBox instanceof EmptyOrNotBox); | ||
t.is(emptyBox.thing, undefined); | ||
return mapping.decode(json) | ||
.then(emptyBox => { | ||
expect(emptyBox instanceof EmptyOrNotBox).to.equal(true); | ||
expect(emptyBox.thing).to.equal(undefined); | ||
}); | ||
}); | ||
}); | ||
test('validate deep (two levels) empty but not', t => { | ||
const json = {thing: {}}; | ||
const mapping = Mapping.map(EmptyOrNotBox).afterDecode(object => validate(object)); | ||
it('validate deep (two levels) empty but not', () => { | ||
const json = {thing: {}}; | ||
const mapping = Mapping.map(EmptyOrNotBox).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'thing should have required property \'name\''); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('thing should have required property \'name\''); | ||
}); | ||
}); | ||
}); | ||
class Gift extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
names: [String] | ||
}; | ||
class Gift extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
names: [String] | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
names: {maxItems: 2, enum: ['Shoes', 'Shirt', '12']} | ||
}; | ||
} | ||
} | ||
get rules() { | ||
return { | ||
names: {maxItems: 2, enum: ['Shoes', 'Shirt', '12']} | ||
}; | ||
} | ||
} | ||
test('validate array', t => { | ||
const json = {size: 'Medium', names: ['Shoes', 'Shirt']}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
it('validate array', () => { | ||
const json = {size: 'Medium', names: ['Shoes', 'Shirt']}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
return mapping.decode(json).then(gift => { | ||
t.true(gift instanceof Gift); | ||
t.is(gift.size, json.size); | ||
t.true(gift.names instanceof Array); | ||
t.is(gift.names[0].constructor, String); | ||
t.is(gift.names[1].constructor, String); | ||
t.is(gift.names[0], json.names[0]); | ||
t.is(gift.names[1], json.names[1]); | ||
return mapping.decode(json).then(gift => { | ||
expect(gift instanceof Gift).to.equal(true); | ||
expect(gift.size).to.equal(json.size); | ||
expect(gift.names instanceof Array).to.equal(true); | ||
expect(gift.names[0].constructor).to.equal(String); | ||
expect(gift.names[1].constructor).to.equal(String); | ||
expect(gift.names[0]).to.equal(json.names[0]); | ||
expect(gift.names[1]).to.equal(json.names[1]); | ||
}); | ||
}); | ||
}); | ||
test('validate array typed error', t => { | ||
const json = {size: 'Medium', names: ['Shoes', 12]}; | ||
const mapping = Mapping.map(Gift).normalize(false).afterDecode(object => validate(object)); | ||
it('validate array typed error', () => { | ||
const json = {size: 'Medium', names: ['Shoes', 12]}; | ||
const mapping = Mapping.map(Gift).normalize(false).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'names/1 should be string'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('names/1 should be string'); | ||
}); | ||
}); | ||
}); | ||
test('validate array no typed error with normalize', t => { | ||
const json = {size: 'Medium', names: ['Shoes', 12]}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
it('validate array no typed error with normalize', () => { | ||
const json = {size: 'Medium', names: ['Shoes', 12]}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
return mapping.decode(json).then(gift => { | ||
t.true(gift instanceof Gift); | ||
t.is(gift.size, json.size); | ||
t.true(gift.names instanceof Array); | ||
t.is(gift.names[0].constructor, String); | ||
t.is(gift.names[1].constructor, String); | ||
t.is(gift.names[0], json.names[0]); | ||
t.is(gift.names[1], String(json.names[1])); | ||
return mapping.decode(json).then(gift => { | ||
expect(gift instanceof Gift).to.equal(true); | ||
expect(gift.size).to.equal(json.size); | ||
expect(gift.names instanceof Array).to.equal(true); | ||
expect(gift.names[0].constructor).to.equal(String); | ||
expect(gift.names[1].constructor).to.equal(String); | ||
expect(gift.names[0]).to.equal(json.names[0]); | ||
expect(gift.names[1]).to.equal(String(json.names[1])); | ||
}); | ||
}); | ||
}); | ||
test('validate array condition error', t => { | ||
const json = {size: 'Medium', names: ['Shoes', 'Shirt', 'Pants']}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
it('validate array condition error', () => { | ||
const json = {size: 'Medium', names: ['Shoes', 'Shirt', 'Pants']}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'names should NOT have more than 2 items'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('names should NOT have more than 2 items'); | ||
}); | ||
}); | ||
}); | ||
test('validate array content error', t => { | ||
const json = {size: 'Medium', names: ['Shoe']}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
it('validate array content error', () => { | ||
const json = {size: 'Medium', names: ['Shoe']}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'names/0 should be equal to one of the allowed values'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('names/0 should be equal to one of the allowed values'); | ||
}); | ||
}); | ||
}); | ||
class Bookcase extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
things: [Thing] | ||
}; | ||
class Bookcase extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
things: [Thing] | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
things: { | ||
name: {required: true} | ||
} | ||
}; | ||
} | ||
} | ||
get rules() { | ||
return { | ||
things: { | ||
name: {required: true} | ||
} | ||
}; | ||
} | ||
} | ||
test('validate array deep class error', t => { | ||
const json = {size: 'Medium', things: [{name: 'Shoes'}, {name: 12}]}; | ||
const mapping = Mapping.map(Bookcase).normalize(false).afterDecode(object => validate(object)); | ||
it('validate array deep class error', () => { | ||
const json = {size: 'Medium', things: [{name: 'Shoes'}, {name: 12}]}; | ||
const mapping = Mapping.map(Bookcase).normalize(false).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'things/1/name should be string'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('things/1/name should be string'); | ||
}); | ||
}); | ||
}); | ||
test('validate array no deep class error with normalize', t => { | ||
const json = {size: 'Medium', things: [{name: 'Shoes'}, {name: 12}]}; | ||
const mapping = Mapping.map(Bookcase).afterDecode(object => validate(object)); | ||
it('validate array no deep class error with normalize', () => { | ||
const json = {size: 'Medium', things: [{name: 'Shoes'}, {name: 12}]}; | ||
const mapping = Mapping.map(Bookcase).afterDecode(object => validate(object)); | ||
return mapping.decode(json).then(bookcase => { | ||
t.true(bookcase instanceof Bookcase); | ||
t.is(bookcase.size, json.size); | ||
t.true(bookcase.things instanceof Array); | ||
t.is(bookcase.things[0].constructor, Thing); | ||
t.is(bookcase.things[1].constructor, Thing); | ||
t.is(bookcase.things[0].name, json.things[0].name); | ||
t.is(bookcase.things[1].name, String(json.things[1].name)); | ||
return mapping.decode(json).then(bookcase => { | ||
expect(bookcase instanceof Bookcase).to.equal(true); | ||
expect(bookcase.size).to.equal(json.size); | ||
expect(bookcase.things instanceof Array).to.equal(true); | ||
expect(bookcase.things[0].constructor).to.equal(Thing); | ||
expect(bookcase.things[1].constructor).to.equal(Thing); | ||
expect(bookcase.things[0].name).to.equal(json.things[0].name); | ||
expect(bookcase.things[1].name).to.equal(String(json.things[1].name)); | ||
}); | ||
}); | ||
}); | ||
class UnreferencedBoxes extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [{ | ||
name: String | ||
}] | ||
}; | ||
class UnreferencedBoxes extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [{ | ||
name: String | ||
}] | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
things: { | ||
name: {required: true} | ||
} | ||
}; | ||
} | ||
} | ||
get rules() { | ||
return { | ||
things: { | ||
name: {required: true} | ||
} | ||
}; | ||
} | ||
} | ||
test('validate untyped deep error', t => { | ||
const json = {size: 40, things: [{name: 'Shoes'}, {}]}; | ||
const mapping = Mapping.map(UnreferencedBoxes).afterDecode(object => validate(object)); | ||
it('validate untyped deep error', () => { | ||
const json = {size: 40, things: [{name: 'Shoes'}, {}]}; | ||
const mapping = Mapping.map(UnreferencedBoxes).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'things/1 should have required property \'name\''); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('things/1 should have required property \'name\''); | ||
}); | ||
}); | ||
}); | ||
class UnknownBoxes extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [Object] | ||
}; | ||
class UnknownBoxes extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [Object] | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
things: {minItems: 1} | ||
}; | ||
} | ||
} | ||
get rules() { | ||
return { | ||
things: {minItems: 1} | ||
}; | ||
} | ||
} | ||
test('validate free deep error', t => { | ||
const json = {size: 40}; | ||
const mapping = Mapping.map(UnknownBoxes).afterDecode(object => validate(object)); | ||
it('validate free deep error', () => { | ||
const json = {size: 40}; | ||
const mapping = Mapping.map(UnknownBoxes).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'things should NOT have fewer than 1 items'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('things should NOT have fewer than 1 items'); | ||
}); | ||
}); | ||
}); | ||
class UnknownAlternativeBoxes extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [Object] | ||
}; | ||
class UnknownAlternativeBoxes extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [Object] | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
things: [{minItems: 1}] | ||
}; | ||
} | ||
} | ||
get rules() { | ||
return { | ||
things: [{minItems: 1}] | ||
}; | ||
} | ||
} | ||
test('validate free deep error (alternative)', t => { | ||
const json = {size: 40}; | ||
const mapping = Mapping.map(UnknownAlternativeBoxes).afterDecode(object => validate(object)); | ||
it('validate free deep error (alternative)', () => { | ||
const json = {size: 40}; | ||
const mapping = Mapping.map(UnknownAlternativeBoxes).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'things should NOT have fewer than 1 items'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('things should NOT have fewer than 1 items'); | ||
}); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
const test = require('ava'); | ||
const {expect} = require('chai'); | ||
@@ -35,104 +35,102 @@ const validate = require('./validator'); | ||
test('decode', t => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing); | ||
describe("validator", () => { | ||
it('decode', () => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing); | ||
return mapping.decode(json).then(thing => { | ||
t.true(thing instanceof Thing); | ||
t.is(thing.name, json.name); | ||
return mapping.decode(json).then(thing => { | ||
expect(thing instanceof Thing).to.equal(true); | ||
expect(thing.name).to.equal(json.name); | ||
}); | ||
}); | ||
}); | ||
test('freeze', t => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => Object.freeze(object)); | ||
it('freeze', () => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => Object.freeze(object)); | ||
return mapping.decode(json).then(thing => { | ||
const error = t.throws(() => { | ||
return mapping.decode(json).then(thing => { | ||
thing.name = 'Dress'; | ||
expect(thing.name).to.equal('Shoes'); | ||
}); | ||
t.is(error.message, 'Cannot assign to read only property \'name\' of object \'#<Thing>\''); | ||
}); | ||
}); | ||
test('validate', t => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
it('validate', () => { | ||
const json = {name: 'Shoes'}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
return mapping.decode(json).then(thing => { | ||
t.is(thing.name, json.name); | ||
return mapping.decode(json).then(thing => { | ||
expect(thing.name).to.equal(json.name); | ||
}); | ||
}); | ||
}); | ||
test('validate required error', t => { | ||
const json = {name: undefined}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
it('validate required error', () => { | ||
const json = {name: undefined}; | ||
const mapping = Mapping.map(Thing).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'name is required'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('name is required'); | ||
}); | ||
}); | ||
}); | ||
class Box extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
class Box extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
thing: Thing | ||
}; | ||
} | ||
get rules() { | ||
return { | ||
size: {required: true}, | ||
thing: { | ||
name: {required: true} | ||
} | ||
}; | ||
} | ||
} | ||
get rules() { | ||
return { | ||
size: {required: true}, | ||
thing: { | ||
name: {required: true} | ||
} | ||
}; | ||
} | ||
} | ||
test('freeze deep', t => { | ||
const json = {size: 'Medium', thing: {name: 'Shoes'}}; | ||
const mapping = Mapping.map(Box).afterDecode(object => deepFreeze(object)); | ||
it('freeze deep', () => { | ||
const json = {size: 'Medium', thing: {name: 'Shoes'}}; | ||
const mapping = Mapping.map(Box).afterDecode(object => deepFreeze(object)); | ||
return mapping.decode(json).then(box => { | ||
const error = t.throws(() => { | ||
return mapping.decode(json).then(box => { | ||
box.thing.name = 'Dress'; | ||
expect(box.thing.name).to.equal('Shoes'); | ||
}); | ||
t.is(error.message, 'Cannot assign to read only property \'name\' of object \'#<Thing>\''); | ||
}); | ||
}); | ||
test('validate deep error', t => { | ||
const json = {size: 'Medium', thing: null}; | ||
const mapping = Mapping.map(Box).afterDecode(object => validate(object)); | ||
it('validate deep error', () => { | ||
const json = {size: 'Medium', thing: null}; | ||
const mapping = Mapping.map(Box).afterDecode(object => validate(object)); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
t.is(error.message, 'thing.name is required'); | ||
return mapping.decode(json) | ||
.catch(error => { | ||
expect(error.message).to.equal('thing.name is required'); | ||
}); | ||
}); | ||
}); | ||
class Gift extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
names: [String] | ||
}; | ||
class Gift extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
names: [String] | ||
}; | ||
} | ||
} | ||
} | ||
test('validate array', t => { | ||
const json = {size: 'Medium', names: ['Shoes', 'Shirt']}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
it('validate array', () => { | ||
const json = {size: 'Medium', names: ['Shoes', 'Shirt']}; | ||
const mapping = Mapping.map(Gift).afterDecode(object => validate(object)); | ||
return mapping.decode(json).then(gift => { | ||
t.true(gift instanceof Gift); | ||
t.is(gift.size, json.size); | ||
t.true(gift.names instanceof Array); | ||
t.is(gift.names[0].constructor, String); | ||
t.is(gift.names[1].constructor, String); | ||
t.is(gift.names[0], json.names[0]); | ||
t.is(gift.names[1], json.names[1]); | ||
return mapping.decode(json).then(gift => { | ||
expect(gift instanceof Gift).to.equal(true); | ||
expect(gift.size).to.equal(json.size); | ||
expect(gift.names instanceof Array).to.equal(true); | ||
expect(gift.names[0].constructor).to.equal(String); | ||
expect(gift.names[1].constructor).to.equal(String); | ||
expect(gift.names[0]).to.equal(json.names[0]); | ||
expect(gift.names[1]).to.equal(json.names[1]); | ||
}); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
const test = require('ava'); | ||
const {expect} = require('chai'); | ||
@@ -14,1009 +14,1011 @@ const Mapping = require('../lib/Mapping'); | ||
test('decode through mapping', t => { | ||
const mapping = Mapping.map(Thing); | ||
const json = {name: 'Shoes'}; | ||
describe("Mapping", () => { | ||
it('decode through mapping', () => { | ||
const mapping = Mapping.map(Thing); | ||
const json = {name: 'Shoes'}; | ||
return mapping.decode(json).then(thing => { | ||
t.true(thing instanceof Thing); | ||
t.is(thing.name, json.name); | ||
return mapping.decode(json).then(thing => { | ||
expect(thing instanceof Thing).to.equal(true); | ||
expect(thing.name).to.equal(json.name); | ||
}); | ||
}); | ||
}); | ||
test('encode through mapping', t => { | ||
const mapping = Mapping.map(Thing); | ||
const thing = new Thing({name: 'Shoes'}); | ||
it('encode through mapping', () => { | ||
const mapping = Mapping.map(Thing); | ||
const thing = new Thing({name: 'Shoes'}); | ||
return mapping.encode(thing).then(json => { | ||
t.is(json.name, thing.name); | ||
return mapping.encode(thing).then(json => { | ||
expect(json.name).to.equal(thing.name); | ||
}); | ||
}); | ||
}); | ||
test('decode list through mapping', t => { | ||
const mapping = Mapping.map(Thing); | ||
const jsons = [{name: 'Shoes'}, {name: 'Shirt'}]; | ||
it('decode list through mapping', () => { | ||
const mapping = Mapping.map(Thing); | ||
const jsons = [{name: 'Shoes'}, {name: 'Shirt'}]; | ||
return mapping.decode(jsons).then(things => { | ||
t.true(things instanceof Array); | ||
t.true(things[0] instanceof Thing); | ||
t.true(things[1] instanceof Thing); | ||
t.is(things[0].name, jsons[0].name); | ||
t.is(things[1].name, jsons[1].name); | ||
return mapping.decode(jsons).then(things => { | ||
expect(things instanceof Array).to.equal(true); | ||
expect(things[0] instanceof Thing).to.equal(true); | ||
expect(things[1] instanceof Thing).to.equal(true); | ||
expect(things[0].name).to.equal(jsons[0].name); | ||
expect(things[1].name).to.equal(jsons[1].name); | ||
}); | ||
}); | ||
}); | ||
test('encode list through mapping', t => { | ||
const mapping = Mapping.map(Thing); | ||
const things = [new Thing({name: 'Shoes'}), new Thing({name: 'Shirt'})]; | ||
it('encode list through mapping', () => { | ||
const mapping = Mapping.map(Thing); | ||
const things = [new Thing({name: 'Shoes'}), new Thing({name: 'Shirt'})]; | ||
return mapping.encode(things).then(jsons => { | ||
t.true(jsons instanceof Array); | ||
t.is(jsons[0].name, things[0].name); | ||
t.is(jsons[1].name, things[1].name); | ||
return mapping.encode(things).then(jsons => { | ||
expect(jsons instanceof Array).to.equal(true); | ||
expect(jsons[0].name).to.equal(things[0].name); | ||
expect(jsons[1].name).to.equal(things[1].name); | ||
}); | ||
}); | ||
}); | ||
class Box extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
thing: Thing | ||
}; | ||
class Box extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
thing: Thing | ||
}; | ||
} | ||
} | ||
} | ||
test('decode class tree through mapping', t => { | ||
const mapping = Mapping.map(Box); | ||
const json = {size: 40, thing: {name: 'Shoes'}}; | ||
it('decode class tree through mapping', () => { | ||
const mapping = Mapping.map(Box); | ||
const json = {size: 40, thing: {name: 'Shoes'}}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof Box); | ||
t.is(box.size, json.size); | ||
t.true(box.thing instanceof Thing); | ||
t.is(box.thing.name, json.thing.name); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof Box).to.equal(true); | ||
expect(box.size).to.equal(json.size); | ||
expect(box.thing instanceof Thing).to.equal(true); | ||
expect(box.thing.name).to.equal(json.thing.name); | ||
}); | ||
}); | ||
}); | ||
test('encode class tree through mapping', t => { | ||
const mapping = Mapping.map(Box); | ||
const box = new Box({size: 40, thing: new Thing({name: 'Shoes'})}); | ||
it('encode class tree through mapping', () => { | ||
const mapping = Mapping.map(Box); | ||
const box = new Box({size: 40, thing: new Thing({name: 'Shoes'})}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.size, box.size); | ||
t.is(json.thing.name, box.thing.name); | ||
return mapping.encode(box).then(json => { | ||
expect(json.size).to.equal(box.size); | ||
expect(json.thing.name).to.equal(box.thing.name); | ||
}); | ||
}); | ||
}); | ||
class UnreferencedBox extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
thing: { | ||
name: String, | ||
details: { | ||
more: Boolean | ||
class UnreferencedBox extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
thing: { | ||
name: String, | ||
details: { | ||
more: Boolean | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
} | ||
test('decode deeply through mapping', t => { | ||
const mapping = Mapping.map(UnreferencedBox); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: true}}}; | ||
it('decode deeply through mapping', () => { | ||
const mapping = Mapping.map(UnreferencedBox); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: true}}}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof UnreferencedBox); | ||
t.is(box.size, json.size); | ||
t.true(box.thing instanceof Object); | ||
t.is(box.thing.name, json.thing.name); | ||
t.is(box.thing.details.more, json.thing.details.more); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof UnreferencedBox).to.equal(true); | ||
expect(box.size).to.equal(json.size); | ||
expect(box.thing instanceof Object).to.equal(true); | ||
expect(box.thing.name).to.equal(json.thing.name); | ||
expect(box.thing.details.more).to.equal(json.thing.details.more); | ||
}); | ||
}); | ||
}); | ||
test('encode deeply through mapping', t => { | ||
const mapping = Mapping.map(UnreferencedBox); | ||
const box = new UnreferencedBox({size: 40, thing: {name: 'Shoes', details: {more: false}}}); | ||
it('encode deeply through mapping', () => { | ||
const mapping = Mapping.map(UnreferencedBox); | ||
const box = new UnreferencedBox({size: 40, thing: {name: 'Shoes', details: {more: false}}}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.size, box.size); | ||
t.is(json.thing.name, box.thing.name); | ||
t.is(json.thing.details.more, box.thing.details.more); | ||
return mapping.encode(box).then(json => { | ||
expect(json.size).to.equal(box.size); | ||
expect(json.thing.name).to.equal(box.thing.name); | ||
expect(json.thing.details.more).to.equal(box.thing.details.more); | ||
}); | ||
}); | ||
}); | ||
class UnreferencedBoxes extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [{ | ||
name: String | ||
}] | ||
}; | ||
class UnreferencedBoxes extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [{ | ||
name: String | ||
}] | ||
}; | ||
} | ||
} | ||
} | ||
test('decode deeply through untyped mapping', t => { | ||
const mapping = Mapping.map(UnreferencedBoxes); | ||
const json = {size: 40, things: [{name: 'Shoes'}, {name: 'Shirt'}]}; | ||
it('decode deeply through untyped mapping', () => { | ||
const mapping = Mapping.map(UnreferencedBoxes); | ||
const json = {size: 40, things: [{name: 'Shoes'}, {name: 'Shirt'}]}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof UnreferencedBoxes); | ||
t.is(box.size, json.size); | ||
t.true(box.things instanceof Array); | ||
t.is(box.things[0].name, json.things[0].name); | ||
t.is(box.things[1].name, json.things[1].name); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof UnreferencedBoxes).to.equal(true); | ||
expect(box.size).to.equal(json.size); | ||
expect(box.things instanceof Array).to.equal(true); | ||
expect(box.things[0].name).to.equal(json.things[0].name); | ||
expect(box.things[1].name).to.equal(json.things[1].name); | ||
}); | ||
}); | ||
}); | ||
test('encode deeply through untyped mapping', t => { | ||
const mapping = Mapping.map(UnreferencedBoxes); | ||
const box = new UnreferencedBoxes({size: 40, things: [{name: 'Shoes'}, {name: 'Shirt'}]}); | ||
it('encode deeply through untyped mapping', () => { | ||
const mapping = Mapping.map(UnreferencedBoxes); | ||
const box = new UnreferencedBoxes({size: 40, things: [{name: 'Shoes'}, {name: 'Shirt'}]}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.size, box.size); | ||
t.true(json.things instanceof Array); | ||
t.is(json.things[0].name, box.things[0].name); | ||
t.is(json.things[1].name, box.things[1].name); | ||
return mapping.encode(box).then(json => { | ||
expect(json.size).to.equal(box.size); | ||
expect(json.things instanceof Array).to.equal(true); | ||
expect(json.things[0].name).to.equal(box.things[0].name); | ||
expect(json.things[1].name).to.equal(box.things[1].name); | ||
}); | ||
}); | ||
}); | ||
class UnknownBox extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
thing: Object | ||
}; | ||
class UnknownBox extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
thing: Object | ||
}; | ||
} | ||
} | ||
} | ||
test('decode deeply through free mapping', t => { | ||
const mapping = Mapping.map(UnknownBox); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: true}}}; | ||
it('decode deeply through free mapping', () => { | ||
const mapping = Mapping.map(UnknownBox); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: true}}}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof UnknownBox); | ||
t.is(box.size, json.size); | ||
t.true(box.thing instanceof Object); | ||
t.is(box.thing.name, json.thing.name); | ||
t.is(box.thing.details.more, json.thing.details.more); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof UnknownBox).to.equal(true); | ||
expect(box.size).to.equal(json.size); | ||
expect(box.thing instanceof Object).to.equal(true); | ||
expect(box.thing.name).to.equal(json.thing.name); | ||
expect(box.thing.details.more).to.equal(json.thing.details.more); | ||
}); | ||
}); | ||
}); | ||
test('encode deeply through free mapping', t => { | ||
const mapping = Mapping.map(UnknownBox); | ||
const box = new UnknownBox({size: 40, thing: {name: 'Shoes', details: {more: false}}}); | ||
it('encode deeply through free mapping', () => { | ||
const mapping = Mapping.map(UnknownBox); | ||
const box = new UnknownBox({size: 40, thing: {name: 'Shoes', details: {more: false}}}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.size, box.size); | ||
t.is(json.thing.name, box.thing.name); | ||
t.is(json.thing.details.more, box.thing.details.more); | ||
return mapping.encode(box).then(json => { | ||
expect(json.size).to.equal(box.size); | ||
expect(json.thing.name).to.equal(box.thing.name); | ||
expect(json.thing.details.more).to.equal(box.thing.details.more); | ||
}); | ||
}); | ||
}); | ||
class Shipping extends Domain { | ||
get props() { | ||
return { | ||
box: Box | ||
}; | ||
class Shipping extends Domain { | ||
get props() { | ||
return { | ||
box: Box | ||
}; | ||
} | ||
} | ||
} | ||
test('decode deep class tree through mapping', t => { | ||
const mapping = Mapping.map(Shipping); | ||
const json = {box: {size: 42, thing: {name: 'Shoes'}}}; | ||
it('decode deep class tree through mapping', () => { | ||
const mapping = Mapping.map(Shipping); | ||
const json = {box: {size: 42, thing: {name: 'Shoes'}}}; | ||
return mapping.decode(json).then(shipping => { | ||
t.true(shipping instanceof Shipping); | ||
t.true(shipping.box instanceof Box); | ||
t.is(shipping.box.size, json.box.size); | ||
t.true(shipping.box.thing instanceof Thing); | ||
t.is(shipping.box.thing.name, json.box.thing.name); | ||
return mapping.decode(json).then(shipping => { | ||
expect(shipping instanceof Shipping).to.equal(true); | ||
expect(shipping.box instanceof Box).to.equal(true); | ||
expect(shipping.box.size).to.equal(json.box.size); | ||
expect(shipping.box.thing instanceof Thing).to.equal(true); | ||
expect(shipping.box.thing.name).to.equal(json.box.thing.name); | ||
}); | ||
}); | ||
}); | ||
test('encode deep class through mapping', t => { | ||
const mapping = Mapping.map(Shipping); | ||
const thing = new Shipping({box: new Box({size: 42, thing: new Thing({name: 'Shoes'})})}); | ||
it('encode deep class through mapping', () => { | ||
const mapping = Mapping.map(Shipping); | ||
const thing = new Shipping({box: new Box({size: 42, thing: new Thing({name: 'Shoes'})})}); | ||
return mapping.encode(thing).then(json => { | ||
t.true(json.box instanceof Object); | ||
t.is(json.box.size, thing.box.size); | ||
t.true(json.box.thing instanceof Object); | ||
t.is(json.box.thing.name, thing.box.thing.name); | ||
return mapping.encode(thing).then(json => { | ||
expect(json.box instanceof Object).to.equal(true); | ||
expect(json.box.size).to.equal(thing.box.size); | ||
expect(json.box.thing instanceof Object).to.equal(true); | ||
expect(json.box.thing.name).to.equal(thing.box.thing.name); | ||
}); | ||
}); | ||
}); | ||
class Gift extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
names: [String] | ||
}; | ||
class Gift extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
names: [String] | ||
}; | ||
} | ||
} | ||
} | ||
test('decode array through mapping', t => { | ||
const mapping = Mapping.map(Gift); | ||
const json = {size: 'Medium', names: ['Shoes', 'Shirt']}; | ||
it('decode array through mapping', () => { | ||
const mapping = Mapping.map(Gift); | ||
const json = {size: 'Medium', names: ['Shoes', 'Shirt']}; | ||
return mapping.decode(json).then(gift => { | ||
t.true(gift instanceof Gift); | ||
t.is(gift.size, json.size); | ||
t.true(gift.names instanceof Array); | ||
t.is(gift.names[0].constructor, String); | ||
t.is(gift.names[1].constructor, String); | ||
t.is(gift.names[0], json.names[0]); | ||
t.is(gift.names[1], json.names[1]); | ||
return mapping.decode(json).then(gift => { | ||
expect(gift instanceof Gift).to.equal(true); | ||
expect(gift.size).to.equal(json.size); | ||
expect(gift.names instanceof Array).to.equal(true); | ||
expect(gift.names[0].constructor).to.equal(String); | ||
expect(gift.names[1].constructor).to.equal(String); | ||
expect(gift.names[0]).to.equal(json.names[0]); | ||
expect(gift.names[1]).to.equal(json.names[1]); | ||
}); | ||
}); | ||
}); | ||
test('encode array through mapping', t => { | ||
const mapping = Mapping.map(Gift); | ||
const gift = new Gift({size: 'Medium', names: ['Shoes', 'Shirt']}); | ||
it('encode array through mapping', () => { | ||
const mapping = Mapping.map(Gift); | ||
const gift = new Gift({size: 'Medium', names: ['Shoes', 'Shirt']}); | ||
return mapping.encode(gift).then(json => { | ||
t.is(json.size, gift.size); | ||
t.true(json.names instanceof Array); | ||
t.is(json.names[0], gift.names[0]); | ||
t.is(json.names[1], gift.names[1]); | ||
return mapping.encode(gift).then(json => { | ||
expect(json.size).to.equal(gift.size); | ||
expect(json.names instanceof Array).to.equal(true); | ||
expect(json.names[0]).to.equal(gift.names[0]); | ||
expect(json.names[1]).to.equal(gift.names[1]); | ||
}); | ||
}); | ||
}); | ||
test('decode empty array through mapping', t => { | ||
const mapping = Mapping.map(Gift); | ||
const json = {size: 'Medium'}; | ||
it('decode empty array through mapping', () => { | ||
const mapping = Mapping.map(Gift); | ||
const json = {size: 'Medium'}; | ||
return mapping.decode(json).then(gift => { | ||
t.true(gift instanceof Gift); | ||
t.is(gift.size, json.size); | ||
t.true(gift.names instanceof Array); | ||
t.is(gift.names.length, 0); | ||
return mapping.decode(json).then(gift => { | ||
expect(gift instanceof Gift).to.equal(true); | ||
expect(gift.size).to.equal(json.size); | ||
expect(gift.names instanceof Array).to.equal(true); | ||
expect(gift.names.length).to.equal(0); | ||
}); | ||
}); | ||
}); | ||
test('encode empty array through mapping', t => { | ||
const mapping = Mapping.map(Gift); | ||
const gift = new Gift({size: 'Medium'}); | ||
it('encode empty array through mapping', () => { | ||
const mapping = Mapping.map(Gift); | ||
const gift = new Gift({size: 'Medium'}); | ||
return mapping.encode(gift).then(json => { | ||
t.is(json.size, gift.size); | ||
t.true(json.names instanceof Array); | ||
t.is(json.names.length, 0); | ||
return mapping.encode(gift).then(json => { | ||
expect(json.size).to.equal(gift.size); | ||
expect(json.names instanceof Array).to.equal(true); | ||
expect(json.names.length).to.equal(0); | ||
}); | ||
}); | ||
}); | ||
test('decode literal to array through mapping', t => { | ||
const mapping = Mapping.map(Gift).with({ | ||
names: { | ||
set: value => Object.keys(value).reduce((memo, item) => { | ||
memo.push(item); | ||
return memo; | ||
}, []) | ||
} | ||
}); | ||
const json = {names: {'Shoes': true, 'Shirt': true}}; | ||
it('decode literal to array through mapping', () => { | ||
const mapping = Mapping.map(Gift).with({ | ||
names: { | ||
set: value => Object.keys(value).reduce((memo, item) => { | ||
memo.push(item); | ||
return memo; | ||
}, []) | ||
} | ||
}); | ||
const json = {names: {'Shoes': true, 'Shirt': true}}; | ||
return mapping.decode(json).then(gift => { | ||
t.true(gift instanceof Gift); | ||
t.true(gift.names instanceof Array); | ||
t.is(gift.names[0], 'Shoes'); | ||
t.is(gift.names[1], 'Shirt'); | ||
return mapping.decode(json).then(gift => { | ||
expect(gift instanceof Gift).to.equal(true); | ||
expect(gift.names instanceof Array).to.equal(true); | ||
expect(gift.names[0]).to.equal('Shoes'); | ||
expect(gift.names[1]).to.equal('Shirt'); | ||
}); | ||
}); | ||
}); | ||
test('encode array to literal through mapping', t => { | ||
const mapping = Mapping.map(Gift).with({ | ||
names: { | ||
get: value => value.reduce((memo, item) => { | ||
memo[item] = true; | ||
return memo; | ||
}, {}) | ||
} | ||
}); | ||
const gift = new Gift({names: ['Shoes', 'Shirt']}); | ||
it('encode array to literal through mapping', () => { | ||
const mapping = Mapping.map(Gift).with({ | ||
names: { | ||
get: value => value.reduce((memo, item) => { | ||
memo[item] = true; | ||
return memo; | ||
}, {}) | ||
} | ||
}); | ||
const gift = new Gift({names: ['Shoes', 'Shirt']}); | ||
return mapping.encode(gift).then(json => { | ||
t.true(json.names instanceof Object); | ||
t.true(json.names['Shoes']); | ||
t.true(json.names['Shirt']); | ||
return mapping.encode(gift).then(json => { | ||
expect(json.names instanceof Object).to.equal(true); | ||
expect(json.names['Shoes']).to.equal(true); | ||
expect(json.names['Shirt']).to.equal(true); | ||
}); | ||
}); | ||
}); | ||
class Bookcase extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
things: [Thing] | ||
}; | ||
class Bookcase extends Domain { | ||
get props() { | ||
return { | ||
size: String, | ||
things: [Thing] | ||
}; | ||
} | ||
} | ||
} | ||
test('decode class array through mapping', t => { | ||
const mapping = Mapping.map(Bookcase); | ||
const json = {size: 'Medium', things: [{name: 'Shoes'}, {name: 'Shirt'}]}; | ||
it('decode class array through mapping', () => { | ||
const mapping = Mapping.map(Bookcase); | ||
const json = {size: 'Medium', things: [{name: 'Shoes'}, {name: 'Shirt'}]}; | ||
return mapping.decode(json).then(bookcase => { | ||
t.true(bookcase instanceof Bookcase); | ||
t.is(bookcase.size, json.size); | ||
t.true(bookcase.things instanceof Array); | ||
t.true(bookcase.things[0] instanceof Thing); | ||
t.true(bookcase.things[1] instanceof Thing); | ||
t.is(bookcase.things.name, json.things.name); | ||
t.is(bookcase.things.name, json.things.name); | ||
return mapping.decode(json).then(bookcase => { | ||
expect(bookcase instanceof Bookcase).to.equal(true); | ||
expect(bookcase.size).to.equal(json.size); | ||
expect(bookcase.things instanceof Array).to.equal(true); | ||
expect(bookcase.things[0] instanceof Thing).to.equal(true); | ||
expect(bookcase.things[1] instanceof Thing).to.equal(true); | ||
expect(bookcase.things.name).to.equal(json.things.name); | ||
expect(bookcase.things.name).to.equal(json.things.name); | ||
}); | ||
}); | ||
}); | ||
test('encode class array through mapping', t => { | ||
const mapping = Mapping.map(Bookcase); | ||
const bookcase = new Bookcase({size: 'Medium', things: [new Thing({name: 'Shoes'}), new Thing({name: 'Shirt'})]}); | ||
it('encode class array through mapping', () => { | ||
const mapping = Mapping.map(Bookcase); | ||
const bookcase = new Bookcase({size: 'Medium', things: [new Thing({name: 'Shoes'}), new Thing({name: 'Shirt'})]}); | ||
return mapping.encode(bookcase).then(json => { | ||
t.is(json.size, bookcase.size); | ||
t.true(bookcase.things instanceof Array); | ||
t.is(json.things[0].constructor, Object); | ||
t.is(json.things[1].constructor, Object); | ||
t.is(json.things.name, bookcase.things.name); | ||
t.is(json.things.name, bookcase.things.name); | ||
return mapping.encode(bookcase).then(json => { | ||
expect(json.size).to.equal(bookcase.size); | ||
expect(bookcase.things instanceof Array).to.equal(true); | ||
expect(json.things[0].constructor).to.equal(Object); | ||
expect(json.things[1].constructor).to.equal(Object); | ||
expect(json.things.name).to.equal(bookcase.things.name); | ||
expect(json.things.name).to.equal(bookcase.things.name); | ||
}); | ||
}); | ||
}); | ||
class Event extends Domain { | ||
get props() { | ||
return { | ||
date: Date | ||
}; | ||
class Event extends Domain { | ||
get props() { | ||
return { | ||
date: Date | ||
}; | ||
} | ||
} | ||
} | ||
test('decode date through mapping', t => { | ||
const mapping = Mapping.map(Event); | ||
const json = {date: new Date('01/09/2017')}; | ||
it('decode date through mapping', () => { | ||
const mapping = Mapping.map(Event); | ||
const json = {date: new Date('01/09/2017')}; | ||
return mapping.decode(json).then(event => { | ||
t.is(event.date.getTime(), new Date('01/09/2017').getTime()); | ||
return mapping.decode(json).then(event => { | ||
expect(event.date.getTime()).to.equal(new Date('01/09/2017').getTime()); | ||
}); | ||
}); | ||
}); | ||
test('encode date through mapping', t => { | ||
const mapping = Mapping.map(Event); | ||
const event = new Event({date: new Date('01/09/2017')}); | ||
it('encode date through mapping', () => { | ||
const mapping = Mapping.map(Event); | ||
const event = new Event({date: new Date('01/09/2017')}); | ||
return mapping.encode(event).then(json => { | ||
t.is(json.date.getTime(), new Date('01/09/2017').getTime()); | ||
return mapping.encode(event).then(json => { | ||
expect(json.date.getTime()).to.equal(new Date('01/09/2017').getTime()); | ||
}); | ||
}); | ||
}); | ||
test('decode date through mapping & transform', t => { | ||
const mapping = Mapping.map(Event).with({ | ||
date: { | ||
set: value => new Date(value) | ||
} | ||
}); | ||
const json = {date: 1483916400000}; | ||
it('decode date through mapping & transform', () => { | ||
const mapping = Mapping.map(Event).with({ | ||
date: { | ||
set: value => new Date(value) | ||
} | ||
}); | ||
const json = {date: 1483916400000}; | ||
return mapping.decode(json).then(event => { | ||
t.is(event.date.getTime(), new Date('01/09/2017').getTime()); | ||
return mapping.decode(json).then(event => { | ||
expect(event.date.getTime()).to.equal(new Date('01/09/2017').getTime()); | ||
}); | ||
}); | ||
}); | ||
test('encode date through mapping & transform', t => { | ||
const mapping = Mapping.map(Event).with({ | ||
date: { | ||
get: value => value.getTime() | ||
} | ||
}); | ||
const event = new Event({date: new Date('01/09/2017')}); | ||
it('encode date through mapping & transform', () => { | ||
const mapping = Mapping.map(Event).with({ | ||
date: { | ||
get: value => value.getTime() | ||
} | ||
}); | ||
const event = new Event({date: new Date('01/09/2017')}); | ||
return mapping.encode(event).then(json => { | ||
t.is(json.date, 1483916400000); | ||
return mapping.encode(event).then(json => { | ||
expect(json.date).to.equal(1483916400000); | ||
}); | ||
}); | ||
}); | ||
class Period extends Domain { | ||
get props() { | ||
return { | ||
dates: [Date] | ||
}; | ||
class Period extends Domain { | ||
get props() { | ||
return { | ||
dates: [Date] | ||
}; | ||
} | ||
} | ||
} | ||
test('decode date array through mapping & transform', t => { | ||
const mapping = Mapping.map(Period).with({ | ||
dates: [{ | ||
set: value => new Date(value) | ||
}] | ||
}); | ||
const json = {dates: [1483916400000, 1484002800000]}; | ||
it('decode date array through mapping & transform', () => { | ||
const mapping = Mapping.map(Period).with({ | ||
dates: [{ | ||
set: value => new Date(value) | ||
}] | ||
}); | ||
const json = {dates: [1483916400000, 1484002800000]}; | ||
return mapping.decode(json).then(period => { | ||
t.true(period instanceof Period); | ||
t.true(period.dates instanceof Array); | ||
t.is(period.dates[0].constructor, Date); | ||
t.is(period.dates[1].constructor, Date); | ||
t.is(period.dates[0].getTime(), new Date(json.dates[0]).getTime()); | ||
t.is(period.dates[1].getTime(), new Date(json.dates[1]).getTime()); | ||
return mapping.decode(json).then(period => { | ||
expect(period instanceof Period).to.equal(true); | ||
expect(period.dates instanceof Array).to.equal(true); | ||
expect(period.dates[0].constructor).to.equal(Date); | ||
expect(period.dates[1].constructor).to.equal(Date); | ||
expect(period.dates[0].getTime()).to.equal(new Date(json.dates[0]).getTime()); | ||
expect(period.dates[1].getTime()).to.equal(new Date(json.dates[1]).getTime()); | ||
}); | ||
}); | ||
}); | ||
test('encode date array through mapping & transform', t => { | ||
const mapping = Mapping.map(Period).with({ | ||
dates: [{ | ||
get: value => value.getTime() | ||
}] | ||
}); | ||
const period = new Period({dates: [new Date('01/09/2017'), new Date('10/09/2017')]}); | ||
it('encode date array through mapping & transform', () => { | ||
const mapping = Mapping.map(Period).with({ | ||
dates: [{ | ||
get: value => value.getTime() | ||
}] | ||
}); | ||
const period = new Period({dates: [new Date('01/09/2017'), new Date('10/09/2017')]}); | ||
return mapping.encode(period).then(json => { | ||
t.true(period.dates instanceof Array); | ||
t.is(json.dates[0], period.dates[0].getTime()); | ||
t.is(json.dates[1], period.dates[1].getTime()); | ||
return mapping.encode(period).then(json => { | ||
expect(period.dates instanceof Array).to.equal(true); | ||
expect(json.dates[0]).to.equal(period.dates[0].getTime()); | ||
expect(json.dates[1]).to.equal(period.dates[1].getTime()); | ||
}); | ||
}); | ||
}); | ||
class Travel extends Domain { | ||
get props() { | ||
return { | ||
from: Event, | ||
to: Event | ||
}; | ||
class Travel extends Domain { | ||
get props() { | ||
return { | ||
from: Event, | ||
to: Event | ||
}; | ||
} | ||
} | ||
} | ||
test('decode date deeply though mapping & transform', t => { | ||
const mapping = Mapping.map(Travel).with({ | ||
from: Mapping.map(Event).with({ | ||
date: { | ||
set: value => new Date(value) | ||
} | ||
}) | ||
}); | ||
const json = {from: {date: 1483916400000}, to: {date: new Date('10/09/2017')}}; | ||
it('decode date deeply though mapping & transform', () => { | ||
const mapping = Mapping.map(Travel).with({ | ||
from: Mapping.map(Event).with({ | ||
date: { | ||
set: value => new Date(value) | ||
} | ||
}) | ||
}); | ||
const json = {from: {date: 1483916400000}, to: {date: new Date('10/09/2017')}}; | ||
return mapping.decode(json).then(travel => { | ||
t.true(travel.from instanceof Event); | ||
t.true(travel.to instanceof Event); | ||
t.is(travel.from.date.getTime(), new Date(json.from.date).getTime()); | ||
t.is(travel.to.date.getTime(), json.to.date.getTime()); | ||
return mapping.decode(json).then(travel => { | ||
expect(travel.from instanceof Event).to.equal(true); | ||
expect(travel.to instanceof Event).to.equal(true); | ||
expect(travel.from.date.getTime()).to.equal(new Date(json.from.date).getTime()); | ||
expect(travel.to.date.getTime()).to.equal(json.to.date.getTime()); | ||
}); | ||
}); | ||
}); | ||
test('encode date deeply though mapping & transform', t => { | ||
const mapping = Mapping.map(Travel).with({ | ||
from: Mapping.map(Event).with({ | ||
date: { | ||
get: value => value.getTime() | ||
} | ||
}) | ||
}); | ||
const travel = new Travel({from: new Event({date: new Date('01/09/2017')}), to: new Event({date: new Date('10/09/2017')})}); | ||
it('encode date deeply though mapping & transform', () => { | ||
const mapping = Mapping.map(Travel).with({ | ||
from: Mapping.map(Event).with({ | ||
date: { | ||
get: value => value.getTime() | ||
} | ||
}) | ||
}); | ||
const travel = new Travel({from: new Event({date: new Date('01/09/2017')}), to: new Event({date: new Date('10/09/2017')})}); | ||
return mapping.encode(travel).then(json => { | ||
t.is(json.from.date, 1483916400000); | ||
t.is(json.to.date.getTime(), travel.to.date.getTime()); | ||
return mapping.encode(travel).then(json => { | ||
expect(json.from.date).to.equal(1483916400000); | ||
expect(json.to.date.getTime()).to.equal(travel.to.date.getTime()); | ||
}); | ||
}); | ||
}); | ||
test('decode deeply through mapping & transform', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
set: value => value === 'visible' | ||
it('decode deeply through mapping & transform', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
set: value => value === 'visible' | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: 'visible'}}}; | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: 'visible'}}}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof UnreferencedBox); | ||
t.is(box.size, json.size); | ||
t.true(box.thing instanceof Object); | ||
t.is(box.thing.name, json.thing.name); | ||
t.is(box.thing.details.more, true); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof UnreferencedBox).to.equal(true); | ||
expect(box.size).to.equal(json.size); | ||
expect(box.thing instanceof Object).to.equal(true); | ||
expect(box.thing.name).to.equal(json.thing.name); | ||
expect(box.thing.details.more).to.equal(true); | ||
}); | ||
}); | ||
}); | ||
test('decode deeply through mapping & transform, two conditions failed', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
set: (value, json) => value && json.size > 50 | ||
it('decode deeply through mapping & transform, two conditions failed', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
set: (value, json) => value && json.size > 50 | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: 'invisible'}}}; | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: 'invisible'}}}; | ||
return mapping.decode(json).then(box => { | ||
t.is(box.thing.details.more, false); | ||
return mapping.decode(json).then(box => { | ||
expect(box.thing.details.more).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
test('decode deeply through mapping & transform, one condition failed', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
set: (value, json) => value && json.size > 50 | ||
it('decode deeply through mapping & transform, one condition failed', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
set: (value, json) => value && json.size > 50 | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: 'visible'}}}; | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes', details: {more: 'visible'}}}; | ||
return mapping.decode(json).then(box => { | ||
t.is(box.thing.details.more, false); | ||
return mapping.decode(json).then(box => { | ||
expect(box.thing.details.more).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
test('decode deeply through mapping & transform, two conditions passed', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
set: (value, json) => value && json.size > 50 | ||
it('decode deeply through mapping & transform, two conditions passed', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
set: (value, json) => value && json.size > 50 | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const json = {size: 60, thing: {name: 'Shoes', details: {more: 'visible'}}}; | ||
}); | ||
const json = {size: 60, thing: {name: 'Shoes', details: {more: 'visible'}}}; | ||
return mapping.decode(json).then(box => { | ||
t.is(box.thing.details.more, true); | ||
return mapping.decode(json).then(box => { | ||
expect(box.thing.details.more).to.equal(true); | ||
}); | ||
}); | ||
}); | ||
test('encode deeply through mapping & transform', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
get: value => value ? 'visible' : 'invisible' | ||
it('encode deeply through mapping & transform', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
get: value => value ? 'visible' : 'invisible' | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const box = new UnreferencedBox({size: 40, thing: {name: 'Shoes', details: {more: false}}}); | ||
}); | ||
const box = new UnreferencedBox({size: 40, thing: {name: 'Shoes', details: {more: false}}}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.size, box.size); | ||
t.is(json.thing.name, box.thing.name); | ||
t.is(json.thing.details.more, 'invisible'); | ||
return mapping.encode(box).then(json => { | ||
expect(json.size).to.equal(box.size); | ||
expect(json.thing.name).to.equal(box.thing.name); | ||
expect(json.thing.details.more).to.equal('invisible'); | ||
}); | ||
}); | ||
}); | ||
test('encode deeply through mapping & dependency transform, two conditions failed', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
get: (value, object) => value && object.size > 50 ? 'visible' : 'invisible' | ||
it('encode deeply through mapping & dependency transform, two conditions failed', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
get: (value, object) => value && object.size > 50 ? 'visible' : 'invisible' | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const box = new UnreferencedBox({size: 40, thing: {name: 'Shoes', details: {more: false}}}); | ||
}); | ||
const box = new UnreferencedBox({size: 40, thing: {name: 'Shoes', details: {more: false}}}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.thing.details.more, 'invisible'); | ||
return mapping.encode(box).then(json => { | ||
expect(json.thing.details.more).to.equal('invisible'); | ||
}); | ||
}); | ||
}); | ||
test('encode deeply through mapping & dependency transform, one condition failed', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
get: (value, object) => value && object.size > 50 ? 'visible' : 'invisible' | ||
it('encode deeply through mapping & dependency transform, one condition failed', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
get: (value, object) => value && object.size > 50 ? 'visible' : 'invisible' | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const box = new UnreferencedBox({size: 40, thing: {name: 'Shoes', details: {more: 'visible'}}}); | ||
}); | ||
const box = new UnreferencedBox({size: 40, thing: {name: 'Shoes', details: {more: 'visible'}}}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.thing.details.more, 'invisible'); | ||
return mapping.encode(box).then(json => { | ||
expect(json.thing.details.more).to.equal('invisible'); | ||
}); | ||
}); | ||
}); | ||
test('encode deeply through mapping & dependency transform, two conditions passed', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
get: (value, object) => value && object.size > 50 ? 'visible' : 'invisible' | ||
it('encode deeply through mapping & dependency transform, two conditions passed', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
thing: { | ||
details: { | ||
more: { | ||
get: (value, object) => value && object.size > 50 ? 'visible' : 'invisible' | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const box = new UnreferencedBox({size: 60, thing: {name: 'Shoes', details: {more: 'visible'}}}); | ||
}); | ||
const box = new UnreferencedBox({size: 60, thing: {name: 'Shoes', details: {more: 'visible'}}}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.thing.details.more, 'visible'); | ||
return mapping.encode(box).then(json => { | ||
expect(json.thing.details.more).to.equal('visible'); | ||
}); | ||
}); | ||
}); | ||
test('decode class tree through mapping & transform', t => { | ||
const mapping = Mapping.map(Box).with({ | ||
thing: Mapping.map(Thing).with({ | ||
name: { | ||
set: value => value.toUpperCase() | ||
} | ||
}) | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes'}}; | ||
it('decode class tree through mapping & transform', () => { | ||
const mapping = Mapping.map(Box).with({ | ||
thing: Mapping.map(Thing).with({ | ||
name: { | ||
set: value => value.toUpperCase() | ||
} | ||
}) | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes'}}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof Box); | ||
t.is(box.size, json.size); | ||
t.true(box.thing instanceof Thing); | ||
t.is(box.thing.name, json.thing.name.toUpperCase()); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof Box).to.equal(true); | ||
expect(box.size).to.equal(json.size); | ||
expect(box.thing instanceof Thing).to.equal(true); | ||
expect(box.thing.name).to.equal(json.thing.name.toUpperCase()); | ||
}); | ||
}); | ||
}); | ||
test('encode class tree through mapping & transform', t => { | ||
const mapping = Mapping.map(Box).with({ | ||
thing: Mapping.map(Thing).with({ | ||
name: { | ||
get: value => value.toLowerCase() | ||
} | ||
}) | ||
}); | ||
const box = new Box({size: 40, thing: new Thing({name: 'Shoes'})}); | ||
it('encode class tree through mapping & transform', () => { | ||
const mapping = Mapping.map(Box).with({ | ||
thing: Mapping.map(Thing).with({ | ||
name: { | ||
get: value => value.toLowerCase() | ||
} | ||
}) | ||
}); | ||
const box = new Box({size: 40, thing: new Thing({name: 'Shoes'})}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.size, 40); | ||
t.is(json.thing.name, box.thing.name.toLowerCase()); | ||
return mapping.encode(box).then(json => { | ||
expect(json.size).to.equal(40); | ||
expect(json.thing.name).to.equal(box.thing.name.toLowerCase()); | ||
}); | ||
}); | ||
}); | ||
test('decode array class tree through mapping & transform', t => { | ||
const mapping = Mapping.map(UnreferencedBoxes).with({ | ||
things: [Mapping.map(Thing).with({ | ||
name: { | ||
set: value => value.toUpperCase() | ||
} | ||
})] | ||
}); | ||
const json = {size: 40, things: [{name: 'Shoes'}]}; | ||
it('decode array class tree through mapping & transform', () => { | ||
const mapping = Mapping.map(UnreferencedBoxes).with({ | ||
things: [Mapping.map(Thing).with({ | ||
name: { | ||
set: value => value.toUpperCase() | ||
} | ||
})] | ||
}); | ||
const json = {size: 40, things: [{name: 'Shoes'}]}; | ||
return mapping.decode(json).then(boxes => { | ||
t.true(boxes instanceof UnreferencedBoxes); | ||
t.is(boxes.size, json.size); | ||
t.true(boxes.things instanceof Array); | ||
t.is(boxes.things[0].name, json.things[0].name.toUpperCase()); | ||
return mapping.decode(json).then(boxes => { | ||
expect(boxes instanceof UnreferencedBoxes).to.equal(true); | ||
expect(boxes.size).to.equal(json.size); | ||
expect(boxes.things instanceof Array).to.equal(true); | ||
expect(boxes.things[0].name).to.equal(json.things[0].name.toUpperCase()); | ||
}); | ||
}); | ||
}); | ||
test('encode array class tree through mapping & transform', t => { | ||
const mapping = Mapping.map(UnreferencedBoxes).with({ | ||
things: [Mapping.map(Thing).with({ | ||
name: { | ||
get: value => value.toLowerCase() | ||
} | ||
})] | ||
}); | ||
const boxes = new UnreferencedBoxes({size: 40, things: [new Thing({name: 'Shoes'})]}); | ||
it('encode array class tree through mapping & transform', () => { | ||
const mapping = Mapping.map(UnreferencedBoxes).with({ | ||
things: [Mapping.map(Thing).with({ | ||
name: { | ||
get: value => value.toLowerCase() | ||
} | ||
})] | ||
}); | ||
const boxes = new UnreferencedBoxes({size: 40, things: [new Thing({name: 'Shoes'})]}); | ||
return mapping.encode(boxes).then(json => { | ||
t.is(json.size, boxes.size); | ||
t.true(json.things instanceof Array); | ||
t.is(json.size, 40); | ||
t.is(json.things[0].name, boxes.things[0].name.toLowerCase()); | ||
return mapping.encode(boxes).then(json => { | ||
expect(json.size).to.equal(boxes.size); | ||
expect(json.things instanceof Array).to.equal(true); | ||
expect(json.size).to.equal(40); | ||
expect(json.things[0].name).to.equal(boxes.things[0].name.toLowerCase()); | ||
}); | ||
}); | ||
}); | ||
test('decode class tree through mapping & visibility transform', t => { | ||
const mapping = Mapping.map(Box).with({ | ||
size: { | ||
set: false | ||
} | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes'}}; | ||
it('decode class tree through mapping & visibility transform', () => { | ||
const mapping = Mapping.map(Box).with({ | ||
size: { | ||
set: false | ||
} | ||
}); | ||
const json = {size: 40, thing: {name: 'Shoes'}}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof Box); | ||
t.is(box.size, undefined); | ||
t.true(box.thing instanceof Thing); | ||
t.is(box.thing.name, json.thing.name); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof Box).to.equal(true); | ||
expect(box.size).to.equal(undefined); | ||
expect(box.thing instanceof Thing).to.equal(true); | ||
expect(box.thing.name).to.equal(json.thing.name); | ||
}); | ||
}); | ||
}); | ||
test('encode class tree through mapping & visibility transform', t => { | ||
const mapping = Mapping.map(Box).with({ | ||
size: { | ||
get: false | ||
} | ||
}); | ||
const box = new Box({size: 40, thing: new Thing({name: 'Shoes'})}); | ||
it('encode class tree through mapping & visibility transform', () => { | ||
const mapping = Mapping.map(Box).with({ | ||
size: { | ||
get: false | ||
} | ||
}); | ||
const box = new Box({size: 40, thing: new Thing({name: 'Shoes'})}); | ||
return mapping.encode(box).then(json => { | ||
t.false(json.hasOwnProperty('size')); | ||
t.is(json.thing.name, box.thing.name); | ||
return mapping.encode(box).then(json => { | ||
expect(json.hasOwnProperty('size')).to.equal(false); | ||
expect(json.thing.name).to.equal(box.thing.name); | ||
}); | ||
}); | ||
}); | ||
test('decode through mapping & alias', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
size: {alias: 'fullsize'}, | ||
thing: {name: {alias: 'fullname'}} | ||
}); | ||
const json = {fullsize: 40, thing: {fullname: 'Shoes'}}; | ||
it('decode through mapping & alias', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
size: {alias: 'fullsize'}, | ||
thing: {name: {alias: 'fullname'}} | ||
}); | ||
const json = {fullsize: 40, thing: {fullname: 'Shoes'}}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof UnreferencedBox); | ||
t.is(box.size, json.fullsize); | ||
t.is(box.thing.name, json.thing.fullname); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof UnreferencedBox).to.equal(true); | ||
expect(box.size).to.equal(json.fullsize); | ||
expect(box.thing.name).to.equal(json.thing.fullname); | ||
}); | ||
}); | ||
}); | ||
test('encode through mapping & alias', t => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
size: {alias: 'fullsize'}, | ||
thing: {name: {alias: 'fullname'}} | ||
}); | ||
const box = new UnreferencedBox({size: 40, thing: new Thing({name: 'Shoes'})}); | ||
it('encode through mapping & alias', () => { | ||
const mapping = Mapping.map(UnreferencedBox).with({ | ||
size: {alias: 'fullsize'}, | ||
thing: {name: {alias: 'fullname'}} | ||
}); | ||
const box = new UnreferencedBox({size: 40, thing: new Thing({name: 'Shoes'})}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.fullsize, box.size); | ||
t.is(json.thing.fullname, box.thing.name); | ||
return mapping.encode(box).then(json => { | ||
expect(json.fullsize).to.equal(box.size); | ||
expect(json.thing.fullname).to.equal(box.thing.name); | ||
}); | ||
}); | ||
}); | ||
test('decode array through mapping & alias', t => { | ||
const mapping = Mapping.map(UnreferencedBoxes).with({ | ||
size: {alias: 'fullsize'}, | ||
things: [{name: {alias: 'fullname'}}] | ||
}); | ||
const json = {fullsize: 40, things: [{fullname: 'Shoes'}]}; | ||
it('decode array through mapping & alias', () => { | ||
const mapping = Mapping.map(UnreferencedBoxes).with({ | ||
size: {alias: 'fullsize'}, | ||
things: [{name: {alias: 'fullname'}}] | ||
}); | ||
const json = {fullsize: 40, things: [{fullname: 'Shoes'}]}; | ||
return mapping.decode(json).then(boxes => { | ||
t.true(boxes instanceof UnreferencedBoxes); | ||
t.is(boxes.size, json.fullsize); | ||
t.true(boxes.things instanceof Array); | ||
t.is(boxes.things[0].name, json.things[0].fullname); | ||
return mapping.decode(json).then(boxes => { | ||
expect(boxes instanceof UnreferencedBoxes).to.equal(true); | ||
expect(boxes.size).to.equal(json.fullsize); | ||
expect(boxes.things instanceof Array).to.equal(true); | ||
expect(boxes.things[0].name).to.equal(json.things[0].fullname); | ||
}); | ||
}); | ||
}); | ||
test('encode array through mapping & alias', t => { | ||
const mapping = Mapping.map(UnreferencedBoxes).with({ | ||
size: {alias: 'fullsize'}, | ||
things: [{name: {alias: 'fullname'}}] | ||
}); | ||
const boxes = new UnreferencedBoxes({size: 40, things: [new Thing({name: 'Shoes'})]}); | ||
it('encode array through mapping & alias', () => { | ||
const mapping = Mapping.map(UnreferencedBoxes).with({ | ||
size: {alias: 'fullsize'}, | ||
things: [{name: {alias: 'fullname'}}] | ||
}); | ||
const boxes = new UnreferencedBoxes({size: 40, things: [new Thing({name: 'Shoes'})]}); | ||
return mapping.encode(boxes).then(json => { | ||
t.is(json.fullsize, boxes.size); | ||
t.true(json.things instanceof Array); | ||
t.is(json.things[0].fullname, boxes.things[0].name); | ||
return mapping.encode(boxes).then(json => { | ||
expect(json.fullsize).to.equal(boxes.size); | ||
expect(json.things instanceof Array).to.equal(true); | ||
expect(json.things[0].fullname).to.equal(boxes.things[0].name); | ||
}); | ||
}); | ||
}); | ||
class UnreferencedBoxesWithin extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [{ | ||
details: [{ | ||
name: String | ||
class UnreferencedBoxesWithin extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [{ | ||
details: [{ | ||
name: String | ||
}] | ||
}] | ||
}] | ||
}; | ||
}; | ||
} | ||
} | ||
} | ||
test('decode deeply and deeply through untyped mapping', t => { | ||
const mapping = Mapping.map(UnreferencedBoxesWithin).with({ | ||
things: [{details: [{name: {alias: 'fullname'}}]}] | ||
}); | ||
const json = {size: 40, things: [{details: [{fullname: 'Shoes'}, {fullname: 'Small'}]}, {details: [{fullname: 'Shirt'}, {fullname: 'Large'}]}]}; | ||
it('decode deeply and deeply through untyped mapping', () => { | ||
const mapping = Mapping.map(UnreferencedBoxesWithin).with({ | ||
things: [{details: [{name: {alias: 'fullname'}}]}] | ||
}); | ||
const json = {size: 40, things: [{details: [{fullname: 'Shoes'}, {fullname: 'Small'}]}, {details: [{fullname: 'Shirt'}, {fullname: 'Large'}]}]}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof UnreferencedBoxesWithin); | ||
t.is(box.size, json.size); | ||
t.true(box.things instanceof Array); | ||
t.is(box.things[0].details[0].name, json.things[0].details[0].fullname); | ||
t.is(box.things[1].details[1].name, json.things[1].details[1].fullname); | ||
return mapping.decode(json).then(box => { | ||
expect(box instanceof UnreferencedBoxesWithin).to.equal(true); | ||
expect(box.size).to.equal(json.size); | ||
expect(box.things instanceof Array).to.equal(true); | ||
expect(box.things[0].details[0].name).to.equal(json.things[0].details[0].fullname); | ||
expect(box.things[1].details[1].name).to.equal(json.things[1].details[1].fullname); | ||
}); | ||
}); | ||
}); | ||
test('encode deeply and deeply through untyped mapping', t => { | ||
const mapping = Mapping.map(UnreferencedBoxesWithin).with({ | ||
things: [{details: [{name: {alias: 'fullname'}}]}] | ||
}); | ||
const box = new UnreferencedBoxesWithin({size: 40, things: [{details: [{name: 'Shoes'}, {name: 'Small'}]}, {details: [{name: 'Shirt'}, {name: 'Large'}]}]}); | ||
it('encode deeply and deeply through untyped mapping', () => { | ||
const mapping = Mapping.map(UnreferencedBoxesWithin).with({ | ||
things: [{details: [{name: {alias: 'fullname'}}]}] | ||
}); | ||
const box = new UnreferencedBoxesWithin({size: 40, things: [{details: [{name: 'Shoes'}, {name: 'Small'}]}, {details: [{name: 'Shirt'}, {name: 'Large'}]}]}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.size, box.size); | ||
t.true(json.things instanceof Array); | ||
t.is(json.things[0].details[0].fullname, box.things[0].details[0].name); | ||
t.is(json.things[1].details[1].fullname, box.things[1].details[1].name); | ||
return mapping.encode(box).then(json => { | ||
expect(json.size).to.equal(box.size); | ||
expect(json.things instanceof Array).to.equal(true); | ||
expect(json.things[0].details[0].fullname).to.equal(box.things[0].details[0].name); | ||
expect(json.things[1].details[1].fullname).to.equal(box.things[1].details[1].name); | ||
}); | ||
}); | ||
}); | ||
test('decode through mapping & hook', t => { | ||
const mapping = Mapping.map(Thing) | ||
.beforeDecode(json => { | ||
const clone = Object.assign({}, json); | ||
clone.name = `2x ${clone.name}`; | ||
return clone; | ||
}) | ||
.afterDecode(object => { | ||
object.formattedName = object.name.toLowerCase(); | ||
return object; | ||
}); | ||
const json = {name: 'Shoes'}; | ||
it('decode through mapping & hook', () => { | ||
const mapping = Mapping.map(Thing) | ||
.beforeDecode(json => { | ||
const clone = Object.assign({}, json); | ||
clone.name = `2x ${clone.name}`; | ||
return clone; | ||
}) | ||
.afterDecode(object => { | ||
object.formattedName = object.name.toLowerCase(); | ||
return object; | ||
}); | ||
const json = {name: 'Shoes'}; | ||
return mapping.decode(json).then(thing => { | ||
t.true(thing instanceof Thing); | ||
t.is(thing.name, `2x ${json.name}`); | ||
t.is(thing.formattedName, `2x ${json.name}`.toLowerCase()); | ||
return mapping.decode(json).then(thing => { | ||
expect(thing instanceof Thing).to.equal(true); | ||
expect(thing.name).to.equal(`2x ${json.name}`); | ||
expect(thing.formattedName).to.equal(`2x ${json.name}`.toLowerCase()); | ||
}); | ||
}); | ||
}); | ||
test('encode through mapping & hook', t => { | ||
const mapping = Mapping.map(Thing) | ||
.beforeEncode(object => { | ||
const clone = new Thing(object); | ||
clone.name = `2x ${clone.name}`; | ||
return clone; | ||
}) | ||
.afterEncode(json => { | ||
json.formattedName = json.name.toLowerCase(); | ||
return json; | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
it('encode through mapping & hook', () => { | ||
const mapping = Mapping.map(Thing) | ||
.beforeEncode(object => { | ||
const clone = new Thing(object); | ||
clone.name = `2x ${clone.name}`; | ||
return clone; | ||
}) | ||
.afterEncode(json => { | ||
json.formattedName = json.name.toLowerCase(); | ||
return json; | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
return mapping.encode(thing).then(json => { | ||
t.is(json.name, `2x ${thing.name}`); | ||
t.is(json.formattedName, `2x ${thing.name}`.toLowerCase()); | ||
return mapping.encode(thing).then(json => { | ||
expect(json.name).to.equal(`2x ${thing.name}`); | ||
expect(json.formattedName).to.equal(`2x ${thing.name}`.toLowerCase()); | ||
}); | ||
}); | ||
}); | ||
test('decode array through mapping & hook', t => { | ||
const mapping = Mapping.map(Thing) | ||
.beforeDecode(json => { | ||
const clone = Object.assign({}, json); | ||
clone.name = `2x ${clone.name}`; | ||
return clone; | ||
}) | ||
.afterDecode(object => { | ||
object.formattedName = object.name.toLowerCase(); | ||
return object; | ||
}); | ||
const jsons = [{name: 'Shoes'}]; | ||
it('decode array through mapping & hook', () => { | ||
const mapping = Mapping.map(Thing) | ||
.beforeDecode(json => { | ||
const clone = Object.assign({}, json); | ||
clone.name = `2x ${clone.name}`; | ||
return clone; | ||
}) | ||
.afterDecode(object => { | ||
object.formattedName = object.name.toLowerCase(); | ||
return object; | ||
}); | ||
const jsons = [{name: 'Shoes'}]; | ||
return mapping.decode(jsons).then(things => { | ||
t.true(things instanceof Array); | ||
t.true(things[0] instanceof Thing); | ||
t.is(things[0].name, `2x ${jsons[0].name}`); | ||
t.is(things[0].formattedName, `2x ${jsons[0].name}`.toLowerCase()); | ||
return mapping.decode(jsons).then(things => { | ||
expect(things instanceof Array).to.equal(true); | ||
expect(things[0] instanceof Thing).to.equal(true); | ||
expect(things[0].name).to.equal(`2x ${jsons[0].name}`); | ||
expect(things[0].formattedName).to.equal(`2x ${jsons[0].name}`.toLowerCase()); | ||
}); | ||
}); | ||
}); | ||
test('encode array through mapping & hook', t => { | ||
const mapping = Mapping.map(Thing) | ||
.beforeEncode(object => { | ||
const clone = new Thing(object); | ||
clone.name = `2x ${clone.name}`; | ||
return clone; | ||
}) | ||
.afterEncode(json => { | ||
json.formattedName = json.name.toLowerCase(); | ||
return json; | ||
}); | ||
const things = [new Thing({name: 'Shoes'})]; | ||
it('encode array through mapping & hook', () => { | ||
const mapping = Mapping.map(Thing) | ||
.beforeEncode(object => { | ||
const clone = new Thing(object); | ||
clone.name = `2x ${clone.name}`; | ||
return clone; | ||
}) | ||
.afterEncode(json => { | ||
json.formattedName = json.name.toLowerCase(); | ||
return json; | ||
}); | ||
const things = [new Thing({name: 'Shoes'})]; | ||
return mapping.encode(things).then(jsons => { | ||
t.true(jsons instanceof Array); | ||
t.is(jsons[0].name, `2x ${things[0].name}`); | ||
t.is(jsons[0].formattedName, `2x ${things[0].name}`.toLowerCase()); | ||
return mapping.encode(things).then(jsons => { | ||
expect(jsons instanceof Array).to.equal(true); | ||
expect(jsons[0].name).to.equal(`2x ${things[0].name}`); | ||
expect(jsons[0].formattedName).to.equal(`2x ${things[0].name}`.toLowerCase()); | ||
}); | ||
}); | ||
}); | ||
test('decode through mapping & defaults', t => { | ||
const mapping = Mapping.map(Thing) | ||
.defaults({ | ||
permissions: 'r-' | ||
}); | ||
const json = {name: 'Shoes'}; | ||
it('decode through mapping & defaults', () => { | ||
const mapping = Mapping.map(Thing) | ||
.defaults({ | ||
permissions: 'r-' | ||
}); | ||
const json = {name: 'Shoes'}; | ||
return mapping.decode(json).then(thing => { | ||
t.true(thing instanceof Thing); | ||
t.is(thing.name, undefined); | ||
return mapping.decode(json).then(thing => { | ||
expect(thing instanceof Thing).to.equal(true); | ||
expect(thing.name).to.equal(undefined); | ||
}); | ||
}); | ||
}); | ||
test('decode through mapping & defaults overriden', t => { | ||
const mapping = Mapping.map(Thing) | ||
.with({ | ||
name: {} | ||
}) | ||
.defaults({ | ||
permissions: 'r-' | ||
}); | ||
const json = {name: 'Shoes'}; | ||
it('decode through mapping & defaults overriden', () => { | ||
const mapping = Mapping.map(Thing) | ||
.with({ | ||
name: {} | ||
}) | ||
.defaults({ | ||
permissions: 'r-' | ||
}); | ||
const json = {name: 'Shoes'}; | ||
return mapping.decode(json).then(thing => { | ||
t.true(thing instanceof Thing); | ||
t.is(thing.name, json.name); | ||
return mapping.decode(json).then(thing => { | ||
expect(thing instanceof Thing).to.equal(true); | ||
expect(thing.name).to.equal(json.name); | ||
}); | ||
}); | ||
}); | ||
test('decode through mapping & defaults overriden x2', t => { | ||
const mapping = Mapping.map(Thing) | ||
.with({ | ||
name: {set: false} | ||
}) | ||
.defaults({ | ||
permissions: 'r-' | ||
}); | ||
const json = {name: 'Shoes'}; | ||
it('decode through mapping & defaults overriden x2', () => { | ||
const mapping = Mapping.map(Thing) | ||
.with({ | ||
name: {set: false} | ||
}) | ||
.defaults({ | ||
permissions: 'r-' | ||
}); | ||
const json = {name: 'Shoes'}; | ||
return mapping.decode(json).then(thing => { | ||
t.true(thing instanceof Thing); | ||
t.is(thing.name, undefined); | ||
return mapping.decode(json).then(thing => { | ||
expect(thing instanceof Thing).to.equal(true); | ||
expect(thing.name).to.equal(undefined); | ||
}); | ||
}); | ||
}); | ||
test('encode through mapping & defaults', t => { | ||
const mapping = Mapping.map(Thing) | ||
.defaults({ | ||
permissions: '-w' | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
it('encode through mapping & defaults', () => { | ||
const mapping = Mapping.map(Thing) | ||
.defaults({ | ||
permissions: '-w' | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
return mapping.encode(thing).then(json => { | ||
t.false(json.hasOwnProperty('name')); | ||
return mapping.encode(thing).then(json => { | ||
expect(json.hasOwnProperty('name')).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
test('encode through mapping & defaults overriden', t => { | ||
const mapping = Mapping.map(Thing) | ||
.with({ | ||
name: {} | ||
}) | ||
.defaults({ | ||
permissions: '-w' | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
it('encode through mapping & defaults overriden', () => { | ||
const mapping = Mapping.map(Thing) | ||
.with({ | ||
name: {} | ||
}) | ||
.defaults({ | ||
permissions: '-w' | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
return mapping.encode(thing).then(json => { | ||
t.is(json.name, thing.name); | ||
return mapping.encode(thing).then(json => { | ||
expect(json.name).to.equal(thing.name); | ||
}); | ||
}); | ||
}); | ||
test('encode through mapping & defaults overriden x2', t => { | ||
const mapping = Mapping.map(Thing) | ||
.with({ | ||
name: {get: false} | ||
}) | ||
.defaults({ | ||
permissions: '-w' | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
it('encode through mapping & defaults overriden x2', () => { | ||
const mapping = Mapping.map(Thing) | ||
.with({ | ||
name: {get: false} | ||
}) | ||
.defaults({ | ||
permissions: '-w' | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
return mapping.encode(thing).then(json => { | ||
t.false(json.hasOwnProperty('name')); | ||
return mapping.encode(thing).then(json => { | ||
expect(json.hasOwnProperty('name')).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
test('encode through mapping & providers', t => { | ||
const mapping = Mapping.map(Thing) | ||
.providers({ | ||
toUpperCase: value => { | ||
return value.toUpperCase(); | ||
} | ||
}) | ||
.afterEncode((json, providers) => { | ||
json.formattedName = providers.toUpperCase(json.name); | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
it('encode through mapping & providers', () => { | ||
const mapping = Mapping.map(Thing) | ||
.providers({ | ||
toUpperCase: value => { | ||
return value.toUpperCase(); | ||
} | ||
}) | ||
.afterEncode((json, providers) => { | ||
json.formattedName = providers.toUpperCase(json.name); | ||
}); | ||
const thing = new Thing({name: 'Shoes'}); | ||
return mapping.encode(thing).then(json => { | ||
t.is(json.name, thing.name); | ||
t.is(json.formattedName, thing.name.toUpperCase()); | ||
return mapping.encode(thing).then(json => { | ||
expect(json.name).to.equal(thing.name); | ||
expect(json.formattedName).to.equal(thing.name.toUpperCase()); | ||
}); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
const test = require('ava'); | ||
const {expect} = require('chai'); | ||
@@ -17,814 +17,840 @@ const Mask = require('../lib/Mask'); | ||
test('assign, touched & schema', t => { | ||
const mask = Mask.cover(Thing).with({ | ||
size: true, | ||
physical: true | ||
}); | ||
describe("Mark", () => { | ||
it('assign, touched & schema', () => { | ||
const mask = Mask.cover(Thing).with({ | ||
size: true, | ||
physical: true | ||
}); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const thing = new Thing({name, size: 'ignored', physical: 'ignored'}); | ||
const touched = mask.settle( | ||
thing, | ||
new Thing({name: 'overriden', description, physical}) | ||
); | ||
const thing = new Thing({name, size: 'ignored', physical: 'ignored'}); | ||
const touched = mask.settle( | ||
thing, | ||
new Thing({name: 'overriden', description, physical}) | ||
); | ||
t.is(thing.name, name); | ||
t.is(thing.description, undefined); | ||
t.is(thing.size, undefined); | ||
t.is(thing.physical, physical); | ||
expect(thing.name).to.equal(name); | ||
expect(thing.description).to.equal(undefined); | ||
expect(thing.size).to.equal(undefined); | ||
expect(thing.physical).to.equal(physical); | ||
t.true(touched.name); | ||
t.true(touched.description); | ||
t.is(touched.size, undefined); | ||
t.is(touched.physical, undefined); | ||
}); | ||
test('assign missing, touched & schema', t => { | ||
const mask = Mask.cover(Thing).with({ | ||
size: true, | ||
physical: true | ||
expect(touched.name).to.equal(true); | ||
expect(touched.description).to.equal(true); | ||
expect(touched.size).to.equal(undefined); | ||
expect(touched.physical).to.equal(undefined); | ||
}); | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
it('assign missing, touched & schema', () => { | ||
const mask = Mask.cover(Thing).with({ | ||
size: true, | ||
physical: true | ||
}); | ||
const thing = new Thing({}); | ||
const touched = mask.settle( | ||
thing, | ||
new Thing({name: 'overriden', description, physical}) | ||
); | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
t.is(thing.name, undefined); | ||
t.is(thing.description, undefined); | ||
t.is(thing.size, undefined); | ||
t.is(thing.physical, physical); | ||
const thing = new Thing({}); | ||
const touched = mask.settle( | ||
thing, | ||
new Thing({name: 'overriden', description, physical}) | ||
); | ||
t.true(touched.name); | ||
t.true(touched.description); | ||
t.is(touched.size, undefined); | ||
t.is(touched.physical, undefined); | ||
}); | ||
expect(thing.name).to.equal(undefined); | ||
expect(thing.description).to.equal(undefined); | ||
expect(thing.size).to.equal(undefined); | ||
expect(thing.physical).to.equal(physical); | ||
class Gift extends Domain { | ||
get props() { | ||
return { | ||
names: [String] | ||
}; | ||
expect(touched.name).to.equal(true); | ||
expect(touched.description).to.equal(true); | ||
expect(touched.size).to.equal(undefined); | ||
expect(touched.physical).to.equal(undefined); | ||
}); | ||
class Gift extends Domain { | ||
get props() { | ||
return { | ||
names: [String] | ||
}; | ||
} | ||
} | ||
} | ||
test('assign array, touched (less)', t => { | ||
const mask = Mask.cover(Gift); | ||
it('assign array, touched (less)', () => { | ||
const mask = Mask.cover(Gift); | ||
const names = ['Shoes', 'Shirt', 'Jeans']; | ||
const names = ['Shoes', 'Shirt', 'Jeans']; | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt']}) | ||
); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt']}) | ||
); | ||
t.deepEqual(gift.names, ['Shoes', 'Shirt', 'Jeans']); | ||
t.deepEqual(touched.names, [true, false, true]); | ||
}); | ||
expect(gift.names).to.deep.equal(['Shoes', 'Shirt', 'Jeans']); | ||
expect(touched.names).to.deep.equal([true, false, true]); | ||
}); | ||
test('assign array, touched (more)', t => { | ||
const mask = Mask.cover(Gift); | ||
it('assign array, touched (more)', () => { | ||
const mask = Mask.cover(Gift); | ||
const names = ['Shoes', 'Shirt']; | ||
const names = ['Shoes', 'Shirt']; | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt', 'Jeans']}) | ||
); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt', 'Jeans']}) | ||
); | ||
t.deepEqual(gift.names, ['Shoes', 'Shirt']); | ||
t.deepEqual(touched.names, [true, false]); | ||
}); | ||
expect(gift.names).to.deep.equal(['Shoes', 'Shirt']); | ||
expect(touched.names).to.deep.equal([true, false]); | ||
}); | ||
test('assign empty array, touched', t => { | ||
const mask = Mask.cover(Gift); | ||
it('assign empty array, touched', () => { | ||
const mask = Mask.cover(Gift); | ||
const names = []; | ||
const names = []; | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt', 'Jeans']}) | ||
); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt', 'Jeans']}) | ||
); | ||
t.deepEqual(gift.names, []); | ||
t.deepEqual(touched.names, []); | ||
}); | ||
test('assign array, untouched', t => { | ||
const mask = Mask.cover(Gift).with({ | ||
names: true | ||
expect(gift.names).to.deep.equal([]); | ||
expect(touched.names).to.deep.equal([]); | ||
}); | ||
const names = []; | ||
it('assign array, untouched', () => { | ||
const mask = Mask.cover(Gift).with({ | ||
names: true | ||
}); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt', 'Jeans']}) | ||
); | ||
const names = []; | ||
t.deepEqual(gift.names, ['Jeans', 'Shirt', 'Jeans']); | ||
t.deepEqual(touched.names, undefined); | ||
}); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt', 'Jeans']}) | ||
); | ||
test('assign array, touched & schema', t => { | ||
const mask = Mask.cover(Gift).with({ | ||
names: true | ||
expect(gift.names).to.deep.equal(['Jeans', 'Shirt', 'Jeans']); | ||
expect(touched.names).to.deep.equal(undefined); | ||
}); | ||
const names = ['Shoes', 'Shirt', 'Jeans']; | ||
it('assign array, touched & schema', () => { | ||
const mask = Mask.cover(Gift).with({ | ||
names: true | ||
}); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt']}) | ||
); | ||
const names = ['Shoes', 'Shirt', 'Jeans']; | ||
t.deepEqual(gift.names, ['Jeans', 'Shirt']); | ||
t.is(touched.names, undefined); | ||
}); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: ['Jeans', 'Shirt']}) | ||
); | ||
test('assign empty array, touched & schema', t => { | ||
const mask = Mask.cover(Gift).with({ | ||
names: true | ||
expect(gift.names).to.deep.equal(['Jeans', 'Shirt']); | ||
expect(touched.names).to.equal(undefined); | ||
}); | ||
const names = ['Shoes', 'Shirt', 'Jeans']; | ||
it('assign empty array, touched & schema', () => { | ||
const mask = Mask.cover(Gift).with({ | ||
names: true | ||
}); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: []}) | ||
); | ||
const names = ['Shoes', 'Shirt', 'Jeans']; | ||
t.deepEqual(gift.names, []); | ||
t.is(touched.names, undefined); | ||
}); | ||
const gift = new Gift({names}); | ||
const touched = mask.settle( | ||
gift, | ||
new Gift({names: []}) | ||
); | ||
class Box extends Domain { | ||
get props() { | ||
return { | ||
thing: Thing | ||
}; | ||
expect(gift.names).to.deep.equal([]); | ||
expect(touched.names).to.equal(undefined); | ||
}); | ||
class Box extends Domain { | ||
get props() { | ||
return { | ||
thing: Thing | ||
}; | ||
} | ||
} | ||
} | ||
test('assign deep, touched & schema', t => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: { | ||
physical: true | ||
} | ||
it('assign deep, touched & schema', () => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: { | ||
physical: true | ||
} | ||
}); | ||
const name = 'Shoes'; | ||
const size = 40; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const box = new Box({thing: {name, description, size, physical: 'ignored'}}); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: {name: 'overriden', description, physical}}) | ||
); | ||
expect(box.thing.name).to.equal(name); | ||
expect(box.thing.description).to.equal(description); | ||
expect(box.thing.size).to.equal(size); | ||
expect(box.thing.physical).to.equal(physical); | ||
expect(touched.thing.name).to.equal(true); | ||
expect(touched.thing.description).to.equal(undefined); | ||
expect(touched.thing.size).to.equal(true); | ||
expect(touched.thing.physical).to.equal(undefined); | ||
}); | ||
const name = 'Shoes'; | ||
const size = 40; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
it('assign deep with zero, touched & schema', () => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: true | ||
}); | ||
const box = new Box({thing: {name, description, size, physical: 'ignored'}}); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: {name: 'overriden', description, physical}}) | ||
); | ||
const name = 'Shoes'; | ||
const size = 0; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
t.is(box.thing.name, name); | ||
t.is(box.thing.description, description); | ||
t.is(box.thing.size, size); | ||
t.is(box.thing.physical, physical); | ||
const box = new Box({thing: undefined}); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: {name, description, size, physical}}) | ||
); | ||
t.true(touched.thing.name); | ||
t.is(touched.thing.description, undefined); | ||
t.true(touched.thing.size); | ||
t.is(touched.thing.physical, undefined); | ||
}); | ||
expect(box.thing.name).to.equal(name); | ||
expect(box.thing.description).to.equal(description); | ||
expect(box.thing.size).to.equal(size); | ||
expect(box.thing.physical).to.equal(physical); | ||
test('assign empty deep, touched & schema', t => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: { | ||
physical: true | ||
} | ||
expect(touched.thing).to.equal(undefined); | ||
}); | ||
const physical = true; | ||
it('assign empty deep, touched & schema', () => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: { | ||
physical: true | ||
} | ||
}); | ||
const box = new Box(); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: new Thing({physical})}) | ||
); | ||
const physical = true; | ||
t.true(box.thing instanceof Thing); | ||
t.is(box.thing.name, undefined); | ||
t.is(box.thing.description, undefined); | ||
t.is(box.thing.size, undefined); | ||
t.is(box.thing.physical, physical); | ||
const box = new Box(); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: new Thing({physical})}) | ||
); | ||
t.is(touched.thing, undefined); | ||
}); | ||
expect(box.thing instanceof Thing).to.equal(true); | ||
expect(box.thing.name).to.equal(undefined); | ||
expect(box.thing.description).to.equal(undefined); | ||
expect(box.thing.size).to.equal(undefined); | ||
expect(box.thing.physical).to.equal(physical); | ||
test('assign empty root deep, touched & schema', t => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: true | ||
expect(touched.thing).to.equal(undefined); | ||
}); | ||
const physical = true; | ||
it('assign empty root deep, touched & schema', () => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: true | ||
}); | ||
const box = new Box(); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: {physical}}) | ||
); | ||
const physical = true; | ||
t.is(box.thing.name, undefined); | ||
t.is(box.thing.description, undefined); | ||
t.is(box.thing.size, undefined); | ||
t.is(box.thing.physical, physical); | ||
t.true(box.thing instanceof Thing); | ||
const box = new Box(); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: {physical}}) | ||
); | ||
t.is(touched.thing, undefined); | ||
}); | ||
expect(box.thing.name).to.equal(undefined); | ||
expect(box.thing.description).to.equal(undefined); | ||
expect(box.thing.size).to.equal(undefined); | ||
expect(box.thing.physical).to.equal(physical); | ||
expect(box.thing instanceof Thing).to.equal(true); | ||
test('assign empty deeply, touched & schema', t => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: { | ||
physical: true | ||
} | ||
expect(touched.thing).to.equal(undefined); | ||
}); | ||
const physical = true; | ||
it('assign empty deeply, touched & schema', () => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: { | ||
physical: true | ||
} | ||
}); | ||
const box = new Box(); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: new Thing({physical})}) | ||
); | ||
const physical = true; | ||
t.true(box.thing instanceof Thing); | ||
t.is(box.thing.name, undefined); | ||
t.is(box.thing.description, undefined); | ||
t.is(box.thing.size, undefined); | ||
t.is(box.thing.physical, physical); | ||
const box = new Box(); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: new Thing({physical})}) | ||
); | ||
t.is(touched.thing, undefined); | ||
}); | ||
expect(box.thing instanceof Thing).to.equal(true); | ||
expect(box.thing.name).to.equal(undefined); | ||
expect(box.thing.description).to.equal(undefined); | ||
expect(box.thing.size).to.equal(undefined); | ||
expect(box.thing.physical).to.equal(physical); | ||
test('assign empty tuned deeply, touched & schema', t => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: Mask.cover(Thing).with({ | ||
physical: true | ||
}) | ||
expect(touched.thing).to.equal(undefined); | ||
}); | ||
const physical = true; | ||
it('assign empty tuned deeply, touched & schema', () => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: Mask.cover(Thing).with({ | ||
physical: true | ||
}) | ||
}); | ||
const box = new Box(); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: new Thing({physical})}) | ||
); | ||
const physical = true; | ||
t.true(box.thing instanceof Thing); | ||
t.is(box.thing.name, undefined); | ||
t.is(box.thing.description, undefined); | ||
t.is(box.thing.size, undefined); | ||
t.is(box.thing.physical, physical); | ||
const box = new Box(); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: new Thing({physical})}) | ||
); | ||
t.is(touched.thing, undefined); | ||
}); | ||
expect(box.thing instanceof Thing).to.equal(true); | ||
expect(box.thing.name).to.equal(undefined); | ||
expect(box.thing.description).to.equal(undefined); | ||
expect(box.thing.size).to.equal(undefined); | ||
expect(box.thing.physical).to.equal(physical); | ||
class Garage extends Domain { | ||
get props() { | ||
return { | ||
boxes: [{ | ||
thing: Thing | ||
}] | ||
}; | ||
expect(touched.thing).to.equal(undefined); | ||
}); | ||
class Garage extends Domain { | ||
get props() { | ||
return { | ||
boxes: [{ | ||
thing: Thing | ||
}] | ||
}; | ||
} | ||
} | ||
} | ||
test('assign empty array deeply, touched & schema', t => { | ||
const mask = Mask.cover(Garage).except({ | ||
boxes: { | ||
thing: { | ||
name: false, | ||
description: false, | ||
size: false | ||
it('assign empty array deeply, touched & schema', () => { | ||
const mask = Mask.cover(Garage).except({ | ||
boxes: { | ||
thing: { | ||
name: false, | ||
description: false, | ||
size: false | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
const physical = true; | ||
const physical = true; | ||
const garage = new Garage({boxes: []}); | ||
const touched = mask.settle( | ||
garage, | ||
new Garage({boxes: [{thing: new Thing({physical})}]}) | ||
); | ||
const garage = new Garage({boxes: []}); | ||
const touched = mask.settle( | ||
garage, | ||
new Garage({boxes: [{thing: new Thing({physical})}]}) | ||
); | ||
t.is(garage.boxes.length, 1); | ||
t.true(garage.boxes[0].thing instanceof Thing); | ||
t.is(garage.boxes[0].thing.name, undefined); | ||
t.is(garage.boxes[0].thing.description, undefined); | ||
t.is(garage.boxes[0].thing.size, undefined); | ||
t.is(garage.boxes[0].thing.physical, physical); | ||
expect(garage.boxes.length).to.equal(1); | ||
expect(garage.boxes[0].thing instanceof Thing).to.equal(true); | ||
expect(garage.boxes[0].thing.name).to.equal(undefined); | ||
expect(garage.boxes[0].thing.description).to.equal(undefined); | ||
expect(garage.boxes[0].thing.size).to.equal(undefined); | ||
expect(garage.boxes[0].thing.physical).to.equal(physical); | ||
t.is(touched.boxes, undefined); | ||
}); | ||
expect(touched.boxes).to.equal(undefined); | ||
}); | ||
class Shipping extends Domain { | ||
get props() { | ||
return { | ||
box: Box | ||
}; | ||
class Shipping extends Domain { | ||
get props() { | ||
return { | ||
box: Box | ||
}; | ||
} | ||
} | ||
} | ||
test('assign double empty deep, touched & schema', t => { | ||
const mask = Mask.cover(Shipping).with({ | ||
box: { | ||
thing: { | ||
size: true | ||
it('assign double empty deep, touched & schema', () => { | ||
const mask = Mask.cover(Shipping).with({ | ||
box: { | ||
thing: { | ||
size: true | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
const shipping = new Shipping(); | ||
const touched = mask.settle( | ||
shipping, | ||
new Shipping() | ||
); | ||
const shipping = new Shipping(); | ||
const touched = mask.settle( | ||
shipping, | ||
new Shipping() | ||
); | ||
t.is(shipping.box, undefined); | ||
t.is(touched.box, undefined); | ||
}); | ||
expect(shipping.box).to.equal(undefined); | ||
expect(touched.box).to.equal(undefined); | ||
}); | ||
class UnknownBox extends Domain { | ||
get props() { | ||
return { | ||
thing: Object | ||
}; | ||
class UnknownBox extends Domain { | ||
get props() { | ||
return { | ||
thing: Object | ||
}; | ||
} | ||
} | ||
} | ||
test('assign empty class tree, touched & schema', t => { | ||
const mask = Mask.cover(UnknownBox).with({ | ||
thing: { | ||
name: true | ||
} | ||
}); | ||
it('assign empty class tree, touched & schema', () => { | ||
const mask = Mask.cover(UnknownBox).with({ | ||
thing: { | ||
name: true | ||
} | ||
}); | ||
const name = 'Shoes'; | ||
const name = 'Shoes'; | ||
const box = new UnknownBox(); | ||
const touched = mask.settle( | ||
box, | ||
new UnknownBox({thing: {name}}) | ||
); | ||
const box = new UnknownBox(); | ||
const touched = mask.settle( | ||
box, | ||
new UnknownBox({thing: {name}}) | ||
); | ||
t.is(box.thing.name, name); | ||
t.is(box.thing.size, undefined); | ||
expect(box.thing.name).to.equal(name); | ||
expect(box.thing.size).to.equal(undefined); | ||
t.is(touched.thing, undefined); | ||
}); | ||
expect(touched.thing).to.equal(undefined); | ||
}); | ||
class Boxes extends Domain { | ||
get props() { | ||
return { | ||
things: [Thing] | ||
}; | ||
class Boxes extends Domain { | ||
get props() { | ||
return { | ||
things: [Thing] | ||
}; | ||
} | ||
} | ||
} | ||
test('assign deep array, touched', t => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: [{ | ||
physical: true | ||
}] | ||
}); | ||
it('assign deep array, touched', () => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: [{ | ||
physical: true | ||
}] | ||
}); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const boxes = new Boxes({things: [{name, description, physical: 'ignored'}, {name, description, physical: 'ignored'}]}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name: 'overriden', description, physical}), new Thing({name: 'overriden', description, physical})]})) | ||
); | ||
const boxes = new Boxes({things: [{name, description, physical: 'ignored'}, {name, description, physical: 'ignored'}]}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name: 'overriden', description, physical}), new Thing({name: 'overriden', description, physical})]})) | ||
); | ||
for (let i = 0; i < 2; i++) { | ||
t.is(boxes.things[i].name, name); | ||
t.is(boxes.things[i].description, description); | ||
t.is(boxes.things[i].size, undefined); | ||
t.is(boxes.things[i].physical, physical); | ||
} | ||
for (let i = 0; i < 2; i++) { | ||
expect(boxes.things[i].name).to.equal(name); | ||
expect(boxes.things[i].description).to.equal(description); | ||
expect(boxes.things[i].size).to.equal(undefined); | ||
expect(boxes.things[i].physical).to.equal(physical); | ||
} | ||
t.is(touched.things.length, 2); | ||
expect(touched.things.length).to.equal(2); | ||
for (let i = 0; i < 2; i++) { | ||
t.true(touched.things[i].name); | ||
t.is(touched.things[i].description, undefined); | ||
t.is(touched.things[i].size, undefined); | ||
t.is(touched.things[i].physical, undefined); | ||
} | ||
}); | ||
test('assign deep array, touched (less)', t => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
for (let i = 0; i < 2; i++) { | ||
expect(touched.things[i].name).to.equal(true); | ||
expect(touched.things[i].description).to.equal(undefined); | ||
expect(touched.things[i].size).to.equal(undefined); | ||
expect(touched.things[i].physical).to.equal(undefined); | ||
} | ||
}); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
it('assign deep array, touched (less)', () => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
}); | ||
const boxes = new Boxes({things: [{name: 'ignored', description, size: 40, physical: 'ignored'}]}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name, description, physical}), new Thing({name, description, physical})]})) | ||
); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
for (let i = 0; i < 2; i++) { | ||
t.is(boxes.things[i].name, name); | ||
t.is(boxes.things[i].description, description); | ||
t.is(boxes.things[i].size, undefined); | ||
t.is(boxes.things[i].physical, physical); | ||
} | ||
const boxes = new Boxes({things: [{name: 'ignored', description, size: 40, physical: 'ignored'}]}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name, description, physical}), new Thing({name, description, physical})]})) | ||
); | ||
t.is(touched.things, undefined); | ||
}); | ||
for (let i = 0; i < 2; i++) { | ||
expect(boxes.things[i].name).to.equal(name); | ||
expect(boxes.things[i].description).to.equal(description); | ||
expect(boxes.things[i].size).to.equal(undefined); | ||
expect(boxes.things[i].physical).to.equal(physical); | ||
} | ||
test('assign deep array, touched (more)', t => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
expect(touched.things).to.equal(undefined); | ||
}); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
it('assign deep array, touched (more)', () => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
}); | ||
const boxes = new Boxes({things: [{name: 'ignored', description, physical: 'ignored'}, {name: 'ignored', description, physical: 'ignored'}]}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name, description, physical})]})) | ||
); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
t.is(boxes.things.length, 1); | ||
const boxes = new Boxes({things: [{name: 'ignored', description, physical: 'ignored'}, {name: 'ignored', description, physical: 'ignored'}]}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name, description, physical})]})) | ||
); | ||
for (let i = 0; i < 1; i++) { | ||
t.is(boxes.things[i].name, name); | ||
t.is(boxes.things[i].description, description); | ||
t.is(boxes.things[i].size, undefined); | ||
t.is(boxes.things[i].physical, physical); | ||
} | ||
expect(boxes.things.length).to.equal(1); | ||
t.is(touched.things, undefined); | ||
}); | ||
for (let i = 0; i < 1; i++) { | ||
expect(boxes.things[i].name).to.equal(name); | ||
expect(boxes.things[i].description).to.equal(description); | ||
expect(boxes.things[i].size).to.equal(undefined); | ||
expect(boxes.things[i].physical).to.equal(physical); | ||
} | ||
test('assign deep array, typed', t => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: [{ | ||
physical: true | ||
}] | ||
expect(touched.things).to.equal(undefined); | ||
}); | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
it('assign deep array, typed', () => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: [{ | ||
physical: true | ||
}] | ||
}); | ||
const boxes = new Boxes({things: []}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name: 'overriden', description, physical})]})) | ||
); | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
t.true(boxes.things[0] instanceof Thing); | ||
t.is(boxes.things[0].name, undefined); | ||
t.is(boxes.things[0].description, undefined); | ||
t.is(boxes.things[0].size, undefined); | ||
t.is(boxes.things[0].physical, physical); | ||
const boxes = new Boxes({things: []}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name: 'overriden', description, physical})]})) | ||
); | ||
t.true(touched.things[0].name); | ||
t.true(touched.things[0].description); | ||
}); | ||
expect(boxes.things[0] instanceof Thing).to.equal(true); | ||
expect(boxes.things[0].name).to.equal(undefined); | ||
expect(boxes.things[0].description).to.equal(undefined); | ||
expect(boxes.things[0].size).to.equal(undefined); | ||
expect(boxes.things[0].physical).to.equal(physical); | ||
test('assign deep array, untouched', t => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
expect(touched.things[0].name).to.equal(true); | ||
expect(touched.things[0].description).to.equal(true); | ||
}); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
it('assign deep array, untouched', () => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
}); | ||
const boxes = new Boxes({things: []}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes({things: [new Thing({name, description, physical}), new Thing({name, description, physical})]}) | ||
); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
for (let i = 0; i < 2; i++) { | ||
t.is(boxes.things[i].name, name); | ||
t.is(boxes.things[i].description, description); | ||
t.is(boxes.things[i].size, undefined); | ||
t.is(boxes.things[i].physical, physical); | ||
} | ||
const boxes = new Boxes({things: []}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes({things: [new Thing({name, description, physical}), new Thing({name, description, physical})]}) | ||
); | ||
t.is(touched.thing, undefined); | ||
}); | ||
for (let i = 0; i < 2; i++) { | ||
expect(boxes.things[i].name).to.equal(name); | ||
expect(boxes.things[i].description).to.equal(description); | ||
expect(boxes.things[i].size).to.equal(undefined); | ||
expect(boxes.things[i].physical).to.equal(physical); | ||
} | ||
test('assign missing deep array, untouched', t => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
expect(touched.thing).to.equal(undefined); | ||
}); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
it('assign missing deep array, untouched', () => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
}); | ||
const boxes = new Boxes({}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes({things: [new Thing({name, description, physical}), new Thing({name, description, physical})]}) | ||
); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
for (let i = 0; i < 2; i++) { | ||
t.is(boxes.things[i].name, name); | ||
t.is(boxes.things[i].description, description); | ||
t.is(boxes.things[i].size, undefined); | ||
t.is(boxes.things[i].physical, physical); | ||
} | ||
const boxes = new Boxes({}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes({things: [new Thing({name, description, physical}), new Thing({name, description, physical})]}) | ||
); | ||
t.is(touched.thing, undefined); | ||
}); | ||
for (let i = 0; i < 2; i++) { | ||
expect(boxes.things[i].name).to.equal(name); | ||
expect(boxes.things[i].description).to.equal(description); | ||
expect(boxes.things[i].size).to.equal(undefined); | ||
expect(boxes.things[i].physical).to.equal(physical); | ||
} | ||
class Cart extends Domain { | ||
get props() { | ||
return { | ||
gifts: [Gift] | ||
}; | ||
expect(touched.thing).to.equal(undefined); | ||
}); | ||
class Cart extends Domain { | ||
get props() { | ||
return { | ||
gifts: [Gift] | ||
}; | ||
} | ||
} | ||
} | ||
test('assign deep array within array, touched (less)', t => { | ||
const mask = Mask.cover(Cart); | ||
it('assign deep array within array, touched (less)', () => { | ||
const mask = Mask.cover(Cart); | ||
const names = ['Shoes', 'Shirt', 'Jeans']; | ||
const names = ['Shoes', 'Shirt', 'Jeans']; | ||
const cart = new Cart({gifts: [new Gift({names}), new Gift({names})]}); | ||
const touched = mask.settle( | ||
cart, | ||
new Cart({gifts: [new Gift({names: ['Jeans', 'Shirt']})]}) | ||
); | ||
const cart = new Cart({gifts: [new Gift({names}), new Gift({names})]}); | ||
const touched = mask.settle( | ||
cart, | ||
new Cart({gifts: [new Gift({names: ['Jeans', 'Shirt']})]}) | ||
); | ||
t.deepEqual(cart.gifts[0].names, ['Shoes', 'Shirt', 'Jeans']); | ||
t.deepEqual(cart.gifts[1].names, ['Shoes', 'Shirt', 'Jeans']); | ||
t.deepEqual(touched.gifts[0].names, [true, false, true]); | ||
t.deepEqual(touched.gifts[1].names, [true, true, true]); | ||
}); | ||
expect(cart.gifts[0].names).to.deep.equal(['Shoes', 'Shirt', 'Jeans']); | ||
expect(cart.gifts[1].names).to.deep.equal(['Shoes', 'Shirt', 'Jeans']); | ||
expect(touched.gifts[0].names).to.deep.equal([true, false, true]); | ||
expect(touched.gifts[1].names).to.deep.equal([true, true, true]); | ||
}); | ||
class UnreferencedBoxes extends Domain { | ||
get props() { | ||
return { | ||
class UnreferencedBoxes extends Domain { | ||
get props() { | ||
return { | ||
things: [{ | ||
name: String, | ||
size: Boolean, | ||
details: { | ||
more: Boolean, | ||
less: Boolean | ||
} | ||
}] | ||
}; | ||
} | ||
} | ||
it('assign class tree array, touched & schema', () => { | ||
const mask = Mask.cover(UnreferencedBoxes).with({ | ||
things: [{ | ||
name: String, | ||
size: Boolean, | ||
size: true, | ||
details: { | ||
more: Boolean, | ||
less: Boolean | ||
less: true | ||
} | ||
}] | ||
}; | ||
} | ||
} | ||
}); | ||
test('assign class tree array, touched & schema', t => { | ||
const mask = Mask.cover(UnreferencedBoxes).with({ | ||
things: [{ | ||
size: true, | ||
details: { | ||
less: true | ||
} | ||
}] | ||
}); | ||
const name = 'Shoes'; | ||
const name = 'Shoes'; | ||
const boxes = new UnreferencedBoxes({things: [{name, size: 'ignored', details: {more: true, less: true}}, {name, size: 'ignored', details: {more: true, less: true}}]}); | ||
const touched = mask.settle( | ||
boxes, | ||
new UnreferencedBoxes({things: [{name: 'overriden', details: {more: false, less: false}}, {name: 'overriden', details: {more: false, less: false}}]}) | ||
); | ||
const boxes = new UnreferencedBoxes({things: [{name, size: 'ignored', details: {more: true, less: true}}, {name, size: 'ignored', details: {more: true, less: true}}]}); | ||
const touched = mask.settle( | ||
boxes, | ||
new UnreferencedBoxes({things: [{name: 'overriden', details: {more: false, less: false}}, {name: 'overriden', details: {more: false, less: false}}]}) | ||
); | ||
for (let i = 0; i < 2; i++) { | ||
expect(boxes.things[i].name).to.equal(name); | ||
expect(boxes.things[i].size).to.equal(undefined); | ||
expect(boxes.things[i].details.more).to.equal(true); | ||
expect(boxes.things[i].details.less).to.equal(false); | ||
} | ||
for (let i = 0; i < 2; i++) { | ||
t.is(boxes.things[i].name, name); | ||
t.is(boxes.things[i].size, undefined); | ||
t.true(boxes.things[i].details.more); | ||
t.false(boxes.things[i].details.less); | ||
} | ||
expect(touched.things.length).to.equal(2); | ||
t.is(touched.things.length, 2); | ||
for (let i = 0; i < 2; i++) { | ||
expect(touched.things[i].name).to.equal(true); | ||
expect(touched.things[i].size).to.equal(undefined); | ||
expect(touched.things[i].details.more).to.equal(true); | ||
expect(touched.things[i].details.less).to.equal(undefined); | ||
} | ||
}); | ||
for (let i = 0; i < 2; i++) { | ||
t.true(touched.things[i].name); | ||
t.is(touched.things[i].size, undefined); | ||
t.true(touched.things[i].details.more); | ||
t.is(touched.things[i].details.less, undefined); | ||
} | ||
}); | ||
it('assign deep mask, touched & schema', () => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: Mask.cover(Thing).with({ | ||
physical: true | ||
}) | ||
}); | ||
test('assign deep mask, touched & schema', t => { | ||
const mask = Mask.cover(Box).with({ | ||
thing: Mask.cover(Thing).with({ | ||
physical: true | ||
}) | ||
}); | ||
const name = 'Shoes'; | ||
const size = 40; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const name = 'Shoes'; | ||
const size = 40; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const box = new Box({thing: {name, description, size, physical: 'ignored'}}); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: {name: 'overriden', description, physical}}) | ||
); | ||
const box = new Box({thing: {name, description, size, physical: 'ignored'}}); | ||
const touched = mask.settle( | ||
box, | ||
new Box({thing: {name: 'overriden', description, physical}}) | ||
); | ||
expect(box.thing.name).to.equal(name); | ||
expect(box.thing.description).to.equal(description); | ||
expect(box.thing.size).to.equal(size); | ||
expect(box.thing.physical).to.equal(physical); | ||
t.is(box.thing.name, name); | ||
t.is(box.thing.description, description); | ||
t.is(box.thing.size, size); | ||
t.is(box.thing.physical, physical); | ||
expect(touched.thing.name).to.equal(true); | ||
expect(touched.thing.description).to.equal(undefined); | ||
expect(touched.thing.size).to.equal(true); | ||
expect(touched.thing.physical).to.equal(undefined); | ||
}); | ||
t.true(touched.thing.name); | ||
t.is(touched.thing.description, undefined); | ||
t.true(touched.thing.size); | ||
t.is(touched.thing.physical, undefined); | ||
}); | ||
it('domain cover props', () => { | ||
const mask = Mask.cover(Thing).except(); | ||
const name = 'Shoes'; | ||
test('domain cover props', t => { | ||
const mask = Mask.cover(Thing).except(); | ||
const name = 'Shoes'; | ||
const thing = new Thing({name: 'ignored'}); | ||
mask.settle(thing, new Thing({name: name})); | ||
expect(thing.name).to.equal(name); | ||
}); | ||
const thing = new Thing({name: 'ignored'}); | ||
mask.settle(thing, new Thing({name: name})); | ||
t.is(thing.name, name); | ||
}); | ||
it('domain cover & omit props', () => { | ||
const mask = Mask.cover(Thing).except({name: false}); | ||
const name = 'Shoes'; | ||
test('domain cover & omit props', t => { | ||
const mask = Mask.cover(Thing).except({name: false}); | ||
const name = 'Shoes'; | ||
const thing = new Thing({name: name}); | ||
mask.settle(thing, new Thing({name: 'overriden'})); | ||
expect(thing.name).to.equal(name); | ||
}); | ||
const thing = new Thing({name: name}); | ||
mask.settle(thing, new Thing({name: 'overriden'})); | ||
t.is(thing.name, name); | ||
}); | ||
it('domain cover deep props', () => { | ||
const mask = Mask.cover(Box).except(); | ||
const name = 'Shoes'; | ||
test('domain cover deep props', t => { | ||
const mask = Mask.cover(Box).except(); | ||
const name = 'Shoes'; | ||
const box = new Box({thing: {name: 'ignored'}}); | ||
mask.settle(box, new Box({thing: {name: name}})); | ||
expect(box.thing.name).to.equal(name); | ||
}); | ||
const box = new Box({thing: {name: 'ignored'}}); | ||
mask.settle(box, new Box({thing: {name: name}})); | ||
t.is(box.thing.name, name); | ||
}); | ||
it('domain cover & omit deep props', () => { | ||
const mask = Mask.cover(Box).except({thing: {name: false}}); | ||
const name = 'Shoes'; | ||
test('domain cover & omit deep props', t => { | ||
const mask = Mask.cover(Box).except({thing: {name: false}}); | ||
const name = 'Shoes'; | ||
const box = new Box({thing: {name: name}}); | ||
mask.settle(box, new Box({thing: {name: 'overriden'}})); | ||
expect(box.thing.name).to.equal(name); | ||
}); | ||
const box = new Box({thing: {name: name}}); | ||
mask.settle(box, new Box({thing: {name: 'overriden'}})); | ||
t.is(box.thing.name, name); | ||
}); | ||
it('domain cover deep free props', () => { | ||
const mask = Mask.cover(UnknownBox).except(); | ||
const name = 'Shoes'; | ||
test('domain cover deep free props', t => { | ||
const mask = Mask.cover(UnknownBox).except(); | ||
const name = 'Shoes'; | ||
const box = new UnknownBox({thing: {name: 'ignored'}}); | ||
mask.settle(box, new UnknownBox({thing: {name: name}})); | ||
expect(box.thing.name).to.equal(name); | ||
}); | ||
const box = new UnknownBox({thing: {name: 'ignored'}}); | ||
mask.settle(box, new UnknownBox({thing: {name: name}})); | ||
t.is(box.thing.name, name); | ||
}); | ||
it('domain cover & omit deep free props', () => { | ||
const mask = Mask.cover(UnknownBox).except({thing: false}); | ||
const name = 'Shoes'; | ||
test('domain cover & omit deep free props', t => { | ||
const mask = Mask.cover(UnknownBox).except({thing: false}); | ||
const name = 'Shoes'; | ||
const box = new UnknownBox({thing: {name: name}}); | ||
mask.settle(box, new UnknownBox({thing: {name: 'overriden'}})); | ||
expect(box.thing.name).to.equal(name); | ||
}); | ||
const box = new UnknownBox({thing: {name: name}}); | ||
mask.settle(box, new UnknownBox({thing: {name: 'overriden'}})); | ||
t.is(box.thing.name, name); | ||
}); | ||
class UnreferencedBox extends Domain { | ||
get props() { | ||
return { | ||
thing: { | ||
name: String | ||
} | ||
}; | ||
class UnreferencedBox extends Domain { | ||
get props() { | ||
return { | ||
thing: { | ||
name: String | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
test('domain cover deep untyped props', t => { | ||
const mask = Mask.cover(UnreferencedBox).except(); | ||
const name = 'Shoes'; | ||
it('domain cover deep untyped props', () => { | ||
const mask = Mask.cover(UnreferencedBox).except(); | ||
const name = 'Shoes'; | ||
const box = new UnreferencedBox({thing: {name: 'ignored'}}); | ||
mask.settle(box, new UnreferencedBox({thing: {name: name}})); | ||
t.is(box.thing.name, name); | ||
}); | ||
const box = new UnreferencedBox({thing: {name: 'ignored'}}); | ||
mask.settle(box, new UnreferencedBox({thing: {name: name}})); | ||
expect(box.thing.name).to.equal(name); | ||
}); | ||
test('domain cover & omit deep untyped props', t => { | ||
const mask = Mask.cover(UnreferencedBox).except({thing: {name: false}}); | ||
const name = 'Shoes'; | ||
it('domain cover & omit deep untyped props', () => { | ||
const mask = Mask.cover(UnreferencedBox).except({thing: {name: false}}); | ||
const name = 'Shoes'; | ||
const box = new UnreferencedBox({thing: {name: name}}); | ||
mask.settle(box, new UnreferencedBox({thing: {name: 'overriden'}})); | ||
t.is(box.thing.name, name); | ||
}); | ||
const box = new UnreferencedBox({thing: {name: name}}); | ||
mask.settle(box, new UnreferencedBox({thing: {name: 'overriden'}})); | ||
expect(box.thing.name).to.equal(name); | ||
}); | ||
class DatedBox extends Domain { | ||
get props() { | ||
return { | ||
date: Date | ||
}; | ||
class DatedBox extends Domain { | ||
get props() { | ||
return { | ||
date: Date | ||
}; | ||
} | ||
} | ||
} | ||
test('assign, date untouched', t => { | ||
const mask = Mask.cover(DatedBox); | ||
it('assign, date untouched', () => { | ||
const mask = Mask.cover(DatedBox); | ||
const date = new Date(); | ||
const date = new Date(); | ||
const box = new DatedBox({date}); | ||
const touched = mask.settle( | ||
box, | ||
new DatedBox({date}) | ||
); | ||
const box = new DatedBox({date}); | ||
const touched = mask.settle( | ||
box, | ||
new DatedBox({date}) | ||
); | ||
t.is(box.date, date); | ||
expect(box.date).to.equal(date); | ||
t.is(touched.date, undefined); | ||
}); | ||
expect(touched.date).to.equal(undefined); | ||
}); | ||
test('assign, date touched', t => { | ||
const mask = Mask.cover(DatedBox); | ||
it('assign, date touched', () => { | ||
const mask = Mask.cover(DatedBox); | ||
const date = new Date(); | ||
const dateClone = new Date(date.getTime()); | ||
const date = new Date(); | ||
const dateClone = new Date(date.getTime()); | ||
const box = new DatedBox({date}); | ||
const touched = mask.settle( | ||
box, | ||
new DatedBox({date: dateClone}) | ||
); | ||
const box = new DatedBox({date}); | ||
const touched = mask.settle( | ||
box, | ||
new DatedBox({date: dateClone}) | ||
); | ||
t.is(box.date, date); | ||
expect(box.date).to.equal(date); | ||
t.is(touched.date, undefined); | ||
}); | ||
expect(touched.date).to.equal(undefined); | ||
}); | ||
test('assign, date touched & updated', t => { | ||
const mask = Mask.cover(DatedBox); | ||
it('assign, date touched & updated', () => { | ||
const mask = Mask.cover(DatedBox); | ||
const date = new Date(); | ||
const epoch = new Date(); | ||
epoch.setTime(0); // 1970-01-01 | ||
const date = new Date(); | ||
const epoch = new Date(); | ||
epoch.setTime(0); // 1970-01-01 | ||
const box = new DatedBox({date}); | ||
const touched = mask.settle( | ||
box, | ||
new DatedBox({date: epoch}) | ||
); | ||
const box = new DatedBox({date}); | ||
const touched = mask.settle( | ||
box, | ||
new DatedBox({date: epoch}) | ||
); | ||
t.is(box.date, date); | ||
expect(box.date).to.equal(date); | ||
t.true(touched.date); | ||
expect(touched.date).to.equal(true); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
const test = require('ava'); | ||
const {expect} = require('chai'); | ||
const Promise = require('bluebird'); | ||
@@ -92,120 +92,122 @@ | ||
test('get /route', t => { | ||
const user = {company: 'Green'}; | ||
describe("router", () => { | ||
it('get /route', () => { | ||
const user = {company: 'Green'}; | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}); | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}); | ||
return repository.shield(user).find() | ||
.then(datas => repositoryMapping.decode(datas)) | ||
.then(things => viewMapping.encode(things)) | ||
.then(jsons => { | ||
t.is(jsons.length, 6); | ||
for (let i = 0; i < 6; i++) { | ||
t.is(jsons[i].id, i + 1); | ||
} | ||
return repository.shield(user).find() | ||
.then(datas => repositoryMapping.decode(datas)) | ||
.then(things => viewMapping.encode(things)) | ||
.then(jsons => { | ||
expect(jsons.length, 6); | ||
for (let i = 0; i < 6; i++) { | ||
expect(jsons[i].id, i + 1); | ||
} | ||
}); | ||
}); | ||
}); | ||
test('get /route/:id', t => { | ||
const user = {company: 'Green'}; | ||
it('get /route/:id', () => { | ||
const user = {company: 'Green'}; | ||
const id = 2; | ||
const formattedPrice = json => { | ||
json.formattedPrice = ((json.price || 0) / 100).toFixed(2) + '€'; | ||
}; | ||
const id = 2; | ||
const formattedPrice = json => { | ||
json.formattedPrice = ((json.price || 0) / 100).toFixed(2) + '€'; | ||
}; | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}) | ||
.afterEncode(json => formattedPrice(json)); | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}) | ||
.afterEncode(json => formattedPrice(json)); | ||
return repository.shield(user).findById(id) | ||
.then(data => repositoryMapping.decode(data)) | ||
.then(thing => viewMapping.encode(thing)) | ||
.then(json => { | ||
t.is(json.id, id); | ||
t.is(json.name, 'Kidstown'); | ||
t.is(json.price, 490); | ||
t.is(json.formattedPrice, '4.90€'); | ||
t.is(json.creation.getTime(), new Date('2017-10-25').getTime()); | ||
t.false(json.hasOwnProperty('company')); | ||
return repository.shield(user).findById(id) | ||
.then(data => repositoryMapping.decode(data)) | ||
.then(thing => viewMapping.encode(thing)) | ||
.then(json => { | ||
expect(json.id).to.equal(id); | ||
expect(json.name).to.equal('Kidstown'); | ||
expect(json.price).to.equal(490); | ||
expect(json.formattedPrice).to.equal('4.90€'); | ||
expect(json.creation.getTime()).to.equal(new Date('2017-10-25').getTime()); | ||
expect(json.hasOwnProperty('company')).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
test('post /route', t => { | ||
const user = {company: 'Green'}; | ||
it('post /route', () => { | ||
const user = {company: 'Green'}; | ||
const body = {name: '319 Men', price: 490}; | ||
const body = {name: '319 Men', price: 490}; | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}) | ||
.afterDecode(object => object.setCompany(user.company)); | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}) | ||
.afterDecode(object => object.setCompany(user.company)); | ||
return viewMapping.decode(body) | ||
.then(thing => repositoryMapping.encode(thing)) | ||
.then(data => repository.shield(user).insert(data)) | ||
.then(data => repositoryMapping.decode(data)) | ||
.then(thing => viewMapping.encode(thing)) | ||
.then(json => { | ||
t.is(json.id, 10); | ||
t.is(json.name, '319 Men'); | ||
t.is(json.price, 490); | ||
t.is(json.creation.getTime(), new Date('2017-10-25').getTime()); | ||
t.false(json.hasOwnProperty('company')); | ||
return viewMapping.decode(body) | ||
.then(thing => repositoryMapping.encode(thing)) | ||
.then(data => repository.shield(user).insert(data)) | ||
.then(data => repositoryMapping.decode(data)) | ||
.then(thing => viewMapping.encode(thing)) | ||
.then(json => { | ||
expect(json.id).to.equal(10); | ||
expect(json.name).to.equal('319 Men'); | ||
expect(json.price).to.equal(490); | ||
expect(json.creation.getTime()).to.equal(new Date('2017-10-25').getTime()); | ||
expect(json.hasOwnProperty('company')).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
test('put /route/:id', t => { | ||
const user = {company: 'Green'}; | ||
it('put /route/:id', () => { | ||
const user = {company: 'Green'}; | ||
const body = {id: 3, name: 'Alamo Military Collectibles', price: 490}; | ||
const body = {id: 3, name: 'Alamo Military Collectibles', price: 490}; | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}) | ||
.afterDecode(object => object.setCompany(user.company)); | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}) | ||
.afterDecode(object => object.setCompany(user.company)); | ||
const repositoryShield = repository.shield(user); | ||
const repositoryShield = repository.shield(user); | ||
return viewMapping.decode(body) | ||
.then(thing => repositoryMapping.encode(thing)) | ||
.then(data => repositoryShield.update(data)) | ||
.then(() => repositoryShield.findById(body.id)) | ||
.then(data => repositoryMapping.decode(data)) | ||
.then(thing => viewMapping.encode(thing)) | ||
.then(json => { | ||
t.is(json.id, 3); | ||
t.is(json.name, 'Alamo Military Collectibles'); | ||
t.is(json.price, 490); | ||
t.is(json.creation.getTime(), new Date('2017-10-25').getTime()); | ||
t.false(json.hasOwnProperty('company')); | ||
return viewMapping.decode(body) | ||
.then(thing => repositoryMapping.encode(thing)) | ||
.then(data => repositoryShield.update(data)) | ||
.then(() => repositoryShield.findById(body.id)) | ||
.then(data => repositoryMapping.decode(data)) | ||
.then(thing => viewMapping.encode(thing)) | ||
.then(json => { | ||
expect(json.id).to.equal(3); | ||
expect(json.name).to.equal('Alamo Military Collectibles'); | ||
expect(json.price).to.equal(490); | ||
expect(json.creation.getTime()).to.equal(new Date('2017-10-25').getTime()); | ||
expect(json.hasOwnProperty('company')).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
test('delete /route/:id', t => { | ||
const user = {company: 'Green'}; | ||
it('delete /route/:id', () => { | ||
const user = {company: 'Green'}; | ||
const id = 4; | ||
const id = 4; | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}); | ||
const viewMapping = Mapping.map(Thing).with({ | ||
company: {get: false} | ||
}); | ||
const repositoryShield = repository.shield(user); | ||
const repositoryShield = repository.shield(user); | ||
return repositoryShield.delete(id) | ||
.then(() => repositoryShield.find()) | ||
.then(datas => repositoryMapping.decode(datas)) | ||
.then(things => viewMapping.encode(things)) | ||
.then(jsons => { | ||
t.is(jsons.length, 5); | ||
for (let i = 0; i < 5; i++) { | ||
t.not(jsons[i].id, 4); | ||
} | ||
return repositoryShield.delete(id) | ||
.then(() => repositoryShield.find()) | ||
.then(datas => repositoryMapping.decode(datas)) | ||
.then(things => viewMapping.encode(things)) | ||
.then(jsons => { | ||
expect(jsons.length, 5); | ||
for (let i = 0; i < 5; i++) { | ||
expect(jsons[i].id).to.not.equal(4); | ||
} | ||
}); | ||
}); | ||
// TODO price 10% discount done via frozen object | ||
}); | ||
// TODO price 10% discount done via frozen object |
138241
3121
5