From e0c59515653f2baabf1855ae5da5f163e74a51de Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Sun, 25 Dec 2016 08:47:37 +0200 Subject: [PATCH] completed tests --- __tests__/data.js | 7 +++++++ __tests__/general.spec.js | 37 ++++++++++++++++++++++++++++++++++++- src/index.js | 10 ++++++---- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/__tests__/data.js b/__tests__/data.js index 5bb3fba..caa5853 100644 --- a/__tests__/data.js +++ b/__tests__/data.js @@ -2,8 +2,15 @@ export default { users: { 1: { name: "User A", + + testArray: ["a", "b"], + testObject: {a: "a", b: "b"}, + dog: 1, friends: [2], + noRoot: 1, + noEntity: 3, + noEntities: [3, 4], animals: [ {schema: "dogs", id: 1}, {schema: "cats", id: 1} diff --git a/__tests__/general.spec.js b/__tests__/general.spec.js index 61e4f7e..a8f5c2e 100644 --- a/__tests__/general.spec.js +++ b/__tests__/general.spec.js @@ -9,8 +9,12 @@ const join = repath({ }, dogs: { - __keys: ["dog"], + __keys: ["dog", "noEntity", "noEntities"], friends: "dogs" + }, + + noRoot: { + __keys: ["noRoot"] } } }) @@ -29,4 +33,35 @@ test('users[1].friends should contain an array of users', () => { test('dogs[1].friends should contain dogs[2] (override)', () => { expect(dogs[1].friends).toHaveLength(1) expect(dogs[1].friends[0]).toMatchObject(_.omit(dogs[2], ["owner"])) +}) + +test('Parsing only users should not add getters to other entities', () => { + const {users, dogs} = join(data, 'users') + expect(Object.getOwnPropertyDescriptor(users[1], "dog").get).toBeTruthy() + expect(dogs[1].owner).toBe(1) +}) + +test('Parsing only users and dogs should not add getters to other entities', () => { + const {users, dogs, cats} = join(data, ['users', 'dogs']) + expect(Object.getOwnPropertyDescriptor(users[1], "dog").get).toBeTruthy() + expect(Object.getOwnPropertyDescriptor(dogs[1], "owner").get).toBeTruthy() + expect(cats[1].owner).toBe(1) +}) + +test('Parsing a reference to a non existing root should do nothing.', () => { + expect(users[1].noRoot).toBe(1) +}) + +test('Parsing a reference to a non existing entity should do nothing.', () => { + expect(users[1].noEntity).toBe(3) + + expect(users[1].noEntities).toHaveLength(2) + expect(users[1].noEntities).toContain(3) + expect(users[1].noEntities).toContain(4) +}) + +test('Properties not defined in schema should remain unchanged', () => { + expect(users[1].name).toBe(data.users[1].name) + expect(users[1].testArray).toBe(data.users[1].testArray) + expect(users[1].testObject).toMatchObject(data.users[1].testObject) }) \ No newline at end of file diff --git a/src/index.js b/src/index.js index 382bed9..6019210 100644 --- a/src/index.js +++ b/src/index.js @@ -94,15 +94,16 @@ export default (config: Config) => (data: Object, limiter: string | Array { const {id, schema} = constructReference(entityId) - const targetValue = data[schema][id] - if (targetValue) return applyGetters(schema)(targetValue) + const targetEntities = data[schema] + if (targetEntities && targetEntities[id]) return applyGetters(schema)(targetEntities[id]) return entityId }) } const {id, schema} = constructReference(value) + const targetEntities = data[schema] - if (data[schema][id]) return applyGetters(schema)(data[schema][id]) + if (targetEntities && targetEntities[id]) return applyGetters(schema)(targetEntities[id]) return value } }) @@ -111,7 +112,8 @@ export default (config: Config) => (data: Object, limiter: string | Array