File size: 3,327 Bytes
bd98362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
describe('update', function () {
  it('should update', function () {
    var Test = this

    setTimeout(function () {
      Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(Test.p1))
    }, 30)

    return Test.adapter.update(Test.Post, 1, { author: 'John', age: 30 })
      .then(function (data) {
        Test.assert.equal(1, Test.requests.length)
        Test.assert.equal(Test.requests[0].url, 'api/posts/1')
        Test.assert.equal(Test.requests[0].method, 'PUT')
        Test.assert.equal(Test.requests[0].requestBody, JSON.stringify({ author: 'John', age: 30 }))
        Test.assert.deepEqual(data, Test.p1, 'post 5 should have been updated')

        setTimeout(function () {
          Test.requests[1].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(Test.p1))
        }, 30)

        return Test.adapter.update(Test.Post, 1, { author: 'John', age: 30 }, { basePath: 'api2' })
      })
      .then(function (data) {
        Test.assert.equal(2, Test.requests.length)
        Test.assert.equal(Test.requests[1].url, 'api2/posts/1')
        Test.assert.equal(Test.requests[1].method, 'PUT')
        Test.assert.equal(Test.requests[1].requestBody, JSON.stringify({ author: 'John', age: 30 }))
        Test.assert.deepEqual(data, Test.p1, 'post 5 should have been updated')
      })
  })
  it('should send nested relations', function () {
    var Test = this
    var JSData = Test.JSData
    var store = new JSData.Container()
    store.registerAdapter('http', Test.adapter, { default: true })
    store.defineMapper('user', {
      relations: {
        hasMany: {
          post: {
            localField: 'posts',
            foreignKey: 'userId'
          },
          address: {
            localField: 'addresses',
            foreignKey: 'userId'
          }
        }
      }
    })
    store.defineMapper('post', {
      relations: {
        belongsTo: {
          user: {
            localField: 'user',
            foreignKey: 'userId'
          }
        }
      }
    })
    store.defineMapper('address', {
      relations: {
        belongsTo: {
          user: {
            localField: 'user',
            foreignKey: 'userId'
          }
        }
      }
    })

    setTimeout(function () {
      Test.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({
        id: 1,
        posts: [
          {
            id: 2,
            userId: 1
          }
        ]
      }))
    }, 30)

    return store.update('user', 1, {
      id: 1,
      posts: [
        {
          id: 2,
          userId: 1
        }
      ],
      addresses: [
        {
          id: 3,
          userId: 1
        }
      ]
    }, { with: ['posts'] })
      .then(function (data) {
        Test.assert.equal(1, Test.requests.length)
        Test.assert.equal(Test.requests[0].url, 'user/1')
        Test.assert.equal(Test.requests[0].method, 'PUT')
        Test.assert.equal(Test.requests[0].requestBody, JSON.stringify({
          id: 1,
          posts: [
            {
              id: 2,
              userId: 1
            }
          ]
        }))
        Test.assert.deepEqual(data, {
          id: 1,
          posts: [
            {
              id: 2,
              userId: 1
            }
          ]
        })
      })
  })
})