Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
completed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Dec 25, 2016
1 parent 36e6f65 commit e0c5951
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
7 changes: 7 additions & 0 deletions __tests__/data.js
Expand Up @@ -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}
Expand Down
37 changes: 36 additions & 1 deletion __tests__/general.spec.js
Expand Up @@ -9,8 +9,12 @@ const join = repath({
},

dogs: {
__keys: ["dog"],
__keys: ["dog", "noEntity", "noEntities"],
friends: "dogs"
},

noRoot: {
__keys: ["noRoot"]
}
}
})
Expand All @@ -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)
})
10 changes: 6 additions & 4 deletions src/index.js
Expand Up @@ -94,15 +94,16 @@ export default (config: Config) => (data: Object, limiter: string | Array<string
return _.map(value, (entityId) => {
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
}
})
Expand All @@ -111,7 +112,8 @@ export default (config: Config) => (data: Object, limiter: string | Array<string
/* If the entity property is an object, continue
* checking nested properties for any relationships.
* */
if (typeof value === 'object') parsedEntity[property] = applyGetters(root)(value)
if (!Array.isArray(value) && typeof value === 'object')
parsedEntity[property] = applyGetters(root)(value)
}
})

Expand Down

0 comments on commit e0c5951

Please sign in to comment.