Skip to content

Commit

Permalink
fix(webiny-entity): EntityCollection must only accept instances of En…
Browse files Browse the repository at this point in the history
…tity class.

close #6, close #18
  • Loading branch information
adrians5j committed Jan 24, 2018
1 parent 94b0d31 commit 31e50ed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class EntitiesAttribute extends Attribute {
* @param value
* @returns {Promise<void>}
*/
setValue(value): Promise<void> {
setValue(value): any {
return new Promise((resolve, reject) => {
this.value.load(() => {
if (!this.canSetValue()) {
Expand Down Expand Up @@ -211,7 +211,7 @@ class EntitiesAttribute extends Attribute {
* It will return only valid IDs, other values will be ignored because they must not enter storage.
* @returns {Promise<*>}
*/
async getStorageValue() {
async getStorageValue(): any {
if (_.isArray(this.value.getCurrent())) {
// Not using getValue method because it would load the entity without need.
const storageValue = [];
Expand Down
19 changes: 11 additions & 8 deletions packages-api/webiny-entity/src/entityCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ class EntityCollection extends Array<Entity> {

constructor(values: Array<Entity> = []) {
super();
if (!values.forEach) {
values = [];
}
values.forEach(v => this.push(v));
this.entityCollection = {
params: null,
meta: null
};
this.entityCollection = { params: null, meta: null };
values.forEach &&
values.forEach(item => {
if (item instanceof Entity) {
this.push(item);
return true;
}
throw Error(
"EntityCollection cannot accept a value that is not an instance of Entity."
);
});
}

setParams(params: Object): this {
Expand Down
41 changes: 41 additions & 0 deletions packages-api/webiny-entity/tests/entityCollection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { assert, expect } from "chai";
import User from "./entities/user";
import { EntityCollection } from "./..";

const getEntities = () => [
new User().populate({ age: 30 }),
new User().populate({ age: 35 }),
new User().populate({ age: 40 })
];

describe("EntityCollection test", function() {
it("must correctly accept an array of entities", async () => {
const entities = getEntities();
const collection = new EntityCollection(entities);
expect(collection).to.have.lengthOf(3);
expect(collection[0].age).to.equal(30);
expect(collection[1].age).to.equal(35);
expect(collection[2].age).to.equal(40);
});

it("must correctly push new entities", async () => {
const entities = getEntities();
const collection = new EntityCollection(entities);

collection.push(new User().populate({ age: 45 }));
expect(collection).to.have.lengthOf(4);
expect(collection[3].age).to.equal(45);

expect(() => collection.push(1)).to.throw(Error);
expect(() => collection.push({ age: 50 })).to.throw(Error);

expect(collection).to.have.lengthOf(4);
expect(collection[3].age).to.equal(45);
});

it("must throw an error on construct, if one of the values is not an instance of Entity", async () => {
expect(() => {
new EntityCollection([new User(), new User(), { id: 123 }]);
}).to.throw(Error);
});
});

0 comments on commit 31e50ed

Please sign in to comment.