-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathbelongsToPolymorphic.test.js
More file actions
206 lines (193 loc) · 5.53 KB
/
Copy pathbelongsToPolymorphic.test.js
File metadata and controls
206 lines (193 loc) · 5.53 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict'
var request = require('supertest')
var loopback = require('loopback')
var expect = require('chai').expect
var JSONAPIComponent = require('../')
var app
var ds
var Post
var FileModel
describe('loopback json api belongsTo polymorphic relationships', function () {
beforeEach(function () {
app = loopback()
app.set('legacyExplorer', false)
ds = loopback.createDataSource('memory')
Post = ds.createModel('post', {
id: { type: Number, id: true },
title: String,
content: String
})
app.model(Post)
FileModel = ds.createModel('fileModel', {
id: { type: Number, id: true },
fileName: String,
parentId: Number,
parentType: String
})
FileModel.belongsTo('parent', {
polymorphic: {
foreignKey: 'parentId',
discriminator: 'parentType'
}
})
app.model(FileModel)
app.use(loopback.rest())
JSONAPIComponent(app)
})
describe('File belonging to a Post', function () {
beforeEach(function (done) {
Post.create(
{
title: 'Post One',
content: 'Content'
},
function (err, post) {
expect(err).to.equal(null)
FileModel.create(
{
fileName: 'blah.jpg',
parentId: post.id,
parentType: 'post'
},
done
)
}
)
})
it('should have a relationship to Post', function (done) {
request(app).get('/fileModels/1').end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.relationships.parent).to.be.an('object')
done()
})
})
it(
'should return the Post that this file belongs to when included flag is present',
function (done) {
request(app)
.get('/fileModels/1?include=parent')
.end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.included).to.be.an('array')
expect(res.body.included[0].type).to.equal('posts')
expect(res.body.included[0].id).to.equal('1')
done()
})
}
)
it(
'should return the Post that this file belongs to when following the relationship link',
function (done) {
request(app).get('/fileModels/1').end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
request(app)
.get(
res.body.data.relationships.parent.links.related.split('api')[1]
)
.end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.type).to.equal('posts')
expect(res.body.data.id).to.equal('1')
done()
})
})
}
)
})
describe('File with no relationship to a Post', function () {
beforeEach(function (done) {
FileModel.create(
{
fileName: 'blah.jpg'
},
done
)
})
it('should return a null value for relationship data', function (done) {
request(app).get('/fileModels/1').end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.relationships.parent).to.be.an('object')
expect(res.body.data.relationships.parent.data).to.equal(null)
done()
})
})
})
describe('Create relationship via API', function () {
beforeEach(function (done) {
Post.create(
{
title: 'Post One',
content: 'Content'
},
done
)
})
it('should define a relationship to Post when file is created', function (
done
) {
request(app)
.post('/fileModels')
.send({
data: {
type: 'fileModels',
attributes: {
fileName: 'blah.jpg'
},
relationships: {
parent: {
data: {
id: 1,
type: 'posts'
}
}
}
}
})
.set('accept', 'application/vnd.api+json')
.set('content-type', 'application/json')
.expect(201)
.end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.relationships.parent).to.be.an('object')
expect(res.body.data.relationships.parent.data).to.be.an('object')
done()
})
})
it('should ignore relationships with an invalid type', function (done) {
request(app)
.post('/fileModels')
.send({
data: {
type: 'fileModels',
attributes: {
fileName: 'blah.jpg'
},
relationships: {
parent: {
data: {
id: 1,
type: 'invalidType'
}
}
}
}
})
.set('accept', 'application/vnd.api+json')
.set('content-type', 'application/json')
.expect(201)
.end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.relationships.parent).to.be.an('object')
expect(res.body.data.relationships.parent.data).to.equal(null)
done()
})
})
})
})