Comparing version 0.3.5 to 0.3.6
@@ -12,3 +12,3 @@ import faussaire, {Route, Controller, Response} from '../src/faussaire'; | ||
let userStore = storeFactory.createStore("Users", [ | ||
storeFactory.createStorable({id: 1, username: "Rewieer", password: "azerty_rewieer", photos: storableLinkFactory.toMany(photoStore, [1, 2])}), | ||
storeFactory.createStorable({id: 1, username: "Rewieer", password: "azerty_rewieer", photos: storableLinkFactory.toMany(faussaire.storage, "Photos", [1, 2])}), | ||
storeFactory.createStorable({id: 2, username: "John", password: "azerty_john"}), | ||
@@ -83,3 +83,3 @@ ]); | ||
it('fetch the user at the given URL when in-test added', async() => { | ||
faussaire.storage.getStore("Users").add({id: 3, username: "Doe", password: "azerty_john", photos: storableLinkFactory.toMany(photoStore, [1])}); | ||
faussaire.storage.getStore("Users").add({id: 3, username: "Doe", password: "azerty_john", photos: storableLinkFactory.toMany(faussaire.storage, "Photos", [1])}); | ||
@@ -86,0 +86,0 @@ let response = await faussaire.fetch("http://foo.com/3", "GET"); |
@@ -29,2 +29,67 @@ import storableFactory from '../../src/storage/storable'; | ||
test('create a storable with null values', () => { | ||
const storable = storableFactory.createStorable({ | ||
id: 1, | ||
name: "test", | ||
photo: null, | ||
}); | ||
expect(storable.getData()).toEqual({ | ||
id: 1, | ||
name: "test", | ||
photo: null, | ||
}); | ||
expect(storable.getSchema()).toEqual({ | ||
id: { | ||
name: "id", | ||
type: "number", | ||
}, | ||
name: { | ||
name: "name", | ||
type: "string", | ||
}, | ||
photo: { | ||
name: "photo", | ||
type: "object", | ||
}, | ||
}); | ||
}); | ||
test('create a storable with nested object', () => { | ||
const storable = storableFactory.createStorable({ | ||
id: 1, | ||
name: "test", | ||
progress: { | ||
level: 1, | ||
exp: 200 | ||
}, | ||
}); | ||
expect(storable.getData()).toEqual({ | ||
id: 1, | ||
name: "test", | ||
progress: { | ||
level: 1, | ||
exp: 200 | ||
}, | ||
}); | ||
expect(storable.getSchema()).toEqual({ | ||
id: { | ||
name: "id", | ||
type: "number", | ||
}, | ||
name: { | ||
name: "name", | ||
type: "string", | ||
}, | ||
progress: { | ||
name: "progress", | ||
type: "object", | ||
}, | ||
}); | ||
}); | ||
test('merge without ID', () => { | ||
@@ -67,12 +132,2 @@ const storable = storableFactory.createStorable({ | ||
test('creating a storable with an Object as a value throws', () => { | ||
expect(() => { | ||
storableFactory.createStorable({ | ||
id: 1, | ||
name: "test", | ||
obj: {}, | ||
}); | ||
}).toThrow(); | ||
}); | ||
test('creating a storable with an Array as a value throws', () => { | ||
@@ -79,0 +134,0 @@ expect(() => { |
import storableLinkFactory from '../../src/storage/storableLink'; | ||
import storeContainerFactory from '../../src/storage/storeContainer'; | ||
import storeFactory from '../../src/storage/store'; | ||
@@ -10,7 +11,10 @@ | ||
expect(storableLinkFactory.toOne(store, 1).getStorable().getData()).toEqual({ id: 1, username: "John"}); | ||
const storeContainer = storeContainerFactory.createStoreContainer(); | ||
storeContainer.addStore(store); | ||
expect(storableLinkFactory.toOne(storeContainer, "Users", 1).getStorable().getData()).toEqual({ id: 1, username: "John"}); | ||
// Checking for persistence | ||
store.get(1).merge({ username: "Will"}); | ||
expect(storableLinkFactory.toOne(store, 1).getStorable().getData()).toEqual({ id: 1, username: "Will"}); | ||
expect(storableLinkFactory.toOne(storeContainer, "Users", 1).getStorable().getData()).toEqual({ id: 1, username: "Will"}); | ||
}); | ||
@@ -24,4 +28,7 @@ | ||
let toMany = storableLinkFactory.toMany(store, [1, 2]); | ||
const storeContainer = storeContainerFactory.createStoreContainer(); | ||
storeContainer.addStore(store); | ||
let toMany = storableLinkFactory.toMany(storeContainer, "Users", [1, 2]); | ||
expect(toMany.getStorable()[0].getData()).toEqual({ id: 1, username: "John"}); | ||
@@ -28,0 +35,0 @@ expect(toMany.getStorable()[1].getData()).toEqual({ id: 2, username: "Doe"}); |
@@ -22,9 +22,14 @@ import storeContainerFactory from '../../src/storage/storeContainer'; | ||
const albumStore = storeFactory.createStore("Albums", [ | ||
storeFactory.createStorable({ id: 1, name: "An Album" }), | ||
storeFactory.createStorable({ id: 2, name: "Other album" }), | ||
]); | ||
const photosStore = storeFactory.createStore("Photos", [ | ||
storeFactory.createStorable({ id: 1, path: "/some/picture.jpg" }), | ||
storeFactory.createStorable({ id: 2, path: "/some/other_picture.jpg" }), | ||
storeFactory.createStorable({ id: 1, path: "/some/picture.jpg", album: storableLinkFactory.toOne(storeContainer, "Albums", 1)}), | ||
storeFactory.createStorable({ id: 2, path: "/some/other_picture.jpg", album: storableLinkFactory.toOne(storeContainer, "Albums", 2)}), | ||
]); | ||
let john = storeFactory.createStorable({id: 1, name: "John", profilePicture: storableLinkFactory.toOne(photosStore, 1)}); | ||
let doe = storeFactory.createStorable({id: 2, name: "Doe", profilePicture: storableLinkFactory.toOne(photosStore, 2)}); | ||
let john = storeFactory.createStorable({id: 1, name: "John", profilePicture: storableLinkFactory.toOne(storeContainer, "Photos", 1)}); | ||
let doe = storeFactory.createStorable({id: 2, name: "Doe", profilePicture: storableLinkFactory.toOne(storeContainer, "Photos", 2)}); | ||
@@ -36,2 +41,3 @@ const userStore = storeFactory.createStore("Users", [ | ||
storeContainer.addStore(photosStore); | ||
storeContainer.addStore(albumStore); | ||
storeContainer.addStore(userStore); | ||
@@ -50,4 +56,4 @@ | ||
let john = storeFactory.createStorable({id: 1, name: "John", photos: storableLinkFactory.toMany(photosStore, [1, 2])}); | ||
let doe = storeFactory.createStorable({id: 2, name: "Doe", photos: storableLinkFactory.toMany(photosStore, [1])}); | ||
let john = storeFactory.createStorable({id: 1, name: "John", photos: storableLinkFactory.toMany(storeContainer, "Photos", [1, 2])}); | ||
let doe = storeFactory.createStorable({id: 2, name: "Doe", photos: storableLinkFactory.toMany(storeContainer, "Photos", [1])}); | ||
@@ -121,3 +127,10 @@ const userStore = storeFactory.createStore("Users", [ | ||
name: "John", | ||
profilePicture: { id: 1, path: "/some/picture.jpg" }, | ||
profilePicture: { | ||
id: 1, | ||
path: "/some/picture.jpg", | ||
album: { | ||
id: 1, | ||
name: "An Album" | ||
} | ||
}, | ||
}); | ||
@@ -132,2 +145,25 @@ }); | ||
})).toEqual({ name: "Doe" }); | ||
}); | ||
test('assemble alla', () => { | ||
const storeContainer = createSingleLinkingStoreContainer(); | ||
const doe = storeContainer.getStore("Users").get(1); | ||
expect(storeContainer.assembleAll({ | ||
user: doe, | ||
role: "ADMIN", | ||
})).toEqual({ | ||
user: { | ||
id: 1, | ||
name: "John", | ||
profilePicture: { | ||
id: 1, | ||
path: "/some/picture.jpg", | ||
album: { | ||
id: 1, | ||
name: "An Album" | ||
} | ||
}, | ||
}, | ||
role: "ADMIN", | ||
}); | ||
}); |
@@ -26,3 +26,21 @@ 'use strict'; | ||
}; | ||
var checkObjectValues = function checkObjectValues(object) { | ||
Object.keys(object).forEach(function (key) { | ||
if (!isScalar(object[key]) && !(0, _storableLink.isStorableLink)(object[key]) && object[key] !== null) { | ||
if (_typeof(object[key]) === "object" && !Array.isArray(object[key])) { | ||
checkObjectValues(object[key]); | ||
} else { | ||
throw (0, _storableException2.default)("A storable object must only have scalar values. Check the value for key " + key); | ||
} | ||
} | ||
}); | ||
}; | ||
var createTypeObject = function createTypeObject(key, value) { | ||
return { | ||
name: key, | ||
type: typeof value === 'undefined' ? 'undefined' : _typeof(value) | ||
}; | ||
}; | ||
/** | ||
@@ -42,7 +60,3 @@ * Create a Storable from the given object. | ||
Object.keys(object).forEach(function (key) { | ||
if (!isScalar(object[key]) && !(0, _storableLink.isStorableLink)(object[key])) { | ||
throw (0, _storableException2.default)("A storable object must only have scalar values. Check the value for key " + key); | ||
} | ||
}); | ||
checkObjectValues(object); | ||
@@ -54,6 +68,3 @@ return function (entity) { | ||
Object.keys(entity).forEach(function (key) { | ||
_schema[key] = { | ||
name: key, | ||
type: _typeof(entity[key]) | ||
}; | ||
_schema[key] = createTypeObject(key, entity[key]); | ||
}); | ||
@@ -60,0 +71,0 @@ |
@@ -7,5 +7,7 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
/** | ||
* Representa oneToOne relationship | ||
* @param container | ||
* @param store | ||
@@ -15,6 +17,6 @@ * @param id | ||
*/ | ||
var toOne = exports.toOne = function toOne(store, id) { | ||
var toOne = exports.toOne = function toOne(container, store, id) { | ||
return { | ||
getStorable: function getStorable() { | ||
return store.get(id); | ||
return container.getStore(store).get(id); | ||
} | ||
@@ -26,2 +28,3 @@ }; | ||
* Represents a oneToMany relationship | ||
* @param container | ||
* @param store | ||
@@ -31,8 +34,7 @@ * @param ids | ||
*/ | ||
var toMany = exports.toMany = function toMany(store, ids) { | ||
var toMany = exports.toMany = function toMany(container, store, ids) { | ||
return { | ||
getStorable: function getStorable() { | ||
return ids.map(function (id) { | ||
return store.get(id); | ||
return container.getStore(store).get(id); | ||
}); | ||
@@ -48,3 +50,3 @@ } | ||
var isStorableLink = exports.isStorableLink = function isStorableLink(value) { | ||
return value.getStorable && typeof value.getStorable === "function"; | ||
return value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === "object" && value.getStorable && typeof value.getStorable === "function"; | ||
}; | ||
@@ -51,0 +53,0 @@ exports.default = { |
@@ -11,2 +11,25 @@ 'use strict'; | ||
var storableToScalarObject = function storableToScalarObject(data) { | ||
var nextData = {}; | ||
// We process the object in case of storableLink to manage | ||
Object.keys(data).forEach(function (key) { | ||
if ((0, _storableLink.isStorableLink)(data[key])) { | ||
var storable = data[key].getStorable(); | ||
if (Array.isArray(storable)) { | ||
nextData[key] = storable.map(function (item) { | ||
return storableToScalarObject(item.getData()); | ||
}); | ||
} else { | ||
nextData[key] = storableToScalarObject(storable.getData()); | ||
} | ||
} else { | ||
nextData[key] = data[key]; | ||
} | ||
}); | ||
return nextData; | ||
}; | ||
/** | ||
@@ -73,3 +96,2 @@ * Create a StoreContainer | ||
var data = Object.assign({}, entity.getData()); | ||
var nextData = {}; | ||
@@ -90,14 +112,40 @@ // If there's schema | ||
// We process the object in case of storableLink to manage | ||
return storableToScalarObject(data); | ||
}, | ||
/** | ||
* Assemble all the keys of the object | ||
* This is used when you want to assemble an object composed of multiple StorableType combined with | ||
* scalar values | ||
* | ||
* @param object | ||
* @param config | ||
* @returns {{}} | ||
*/ | ||
assembleAll: function assembleAll(object) { | ||
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
schema: [] | ||
}; | ||
var data = Object.assign({}, object); | ||
// If there's schema | ||
if (config.hasOwnProperty("schema") && Array.isArray(config.schema) && config.schema.length) { | ||
(function () { | ||
// We remove all the keys we don't want | ||
var schema = config.schema; | ||
Object.keys(data).forEach(function (key) { | ||
if (schema.indexOf(key) < 0) { | ||
delete data[key]; | ||
} | ||
}); | ||
})(); | ||
} | ||
var nextData = {}; | ||
Object.keys(data).forEach(function (key) { | ||
if ((0, _storableLink.isStorableLink)(data[key])) { | ||
var storable = data[key].getStorable(); | ||
if (Array.isArray(storable)) { | ||
nextData[key] = storable.map(function (item) { | ||
return item.getData(); | ||
}); | ||
} else { | ||
nextData[key] = storable.getData(); | ||
} | ||
if (data[key].getData) { | ||
nextData[key] = storeContainer.assemble(data[key]); | ||
} else { | ||
@@ -104,0 +152,0 @@ nextData[key] = data[key]; |
{ | ||
"name": "faussaire", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "Lightweight library to mock API for testing purpose", | ||
@@ -5,0 +5,0 @@ "main": "build/faussaire.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54551
1279