This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.spec.js
67 lines (54 loc) · 2.05 KB
/
general.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import repath from '../src'
import data from './data'
import _ from 'lodash'
const join = repath({
schemas: {
users: {
__keys: ["owner", "friends"]
},
dogs: {
__keys: ["dog", "noEntity", "noEntities"],
friends: "dogs"
},
noRoot: {
__keys: ["noRoot"]
}
}
})
const {users, dogs} = join(data)
test('users[1].dog should match dogs[1]', () => {
expect(users[1].dog).toMatchObject(_.omit(dogs[1], ["owner", "friends"]))
})
test('users[1].friends should contain an array of users', () => {
expect(users[1].friends).toHaveLength(1)
expect(users[1].friends[0]).toMatchObject(_.omit(users[2], ["friends", "dog", "animals"]))
})
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)
})