| 1 |
import { Server, Model, hasMany, belongsTo, JSONAPISerializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("Integration | Server | Regressions | 1613 Two bidirectional many-to-many with same target model update bug", function () { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
models: { |
| 10 |
user: Model.extend({ |
| 11 |
authoredPosts: hasMany("post", { inverse: "author" }), |
| 12 |
editedPosts: hasMany("post", { inverse: "editor" }), |
| 13 |
}), |
| 14 |
post: Model.extend({ |
| 15 |
author: belongsTo("user", { inverse: "authoredPosts" }), |
| 16 |
editor: belongsTo("user", { inverse: "editedPosts" }), |
| 17 |
}), |
| 18 |
}, |
| 19 |
serializers: { |
| 20 |
application: JSONAPISerializer.extend(), |
| 21 |
user: JSONAPISerializer.extend({ |
| 22 |
alwaysIncludeLinkageData: true, |
| 23 |
}), |
| 24 |
}, |
| 25 |
routes() { |
| 26 |
this.resource("posts"); |
| 27 |
this.resource("users"); |
| 28 |
}, |
| 29 |
}); |
| 30 |
}); |
| 31 |
|
| 32 |
afterEach(function () { |
| 33 |
server.shutdown(); |
| 34 |
}); |
| 35 |
|
| 36 |
test("it stores both relationships", async () => { |
| 37 |
let post = server.create("post"); |
| 38 |
let user = server.create("user"); |
| 39 |
|
| 40 |
expect.assertions(1); |
| 41 |
|
| 42 |
await fetch(`/posts/${post.id}`, { |
| 43 |
method: "PATCH", |
| 44 |
headers: { |
| 45 |
"Content-Type": "application/vnd.api+json", |
| 46 |
}, |
| 47 |
body: JSON.stringify({ |
| 48 |
data: { |
| 49 |
id: post.id, |
| 50 |
attributes: {}, |
| 51 |
relationships: { |
| 52 |
author: { |
| 53 |
data: { |
| 54 |
type: "users", |
| 55 |
id: user.id, |
| 56 |
}, |
| 57 |
}, |
| 58 |
editor: { |
| 59 |
data: { |
| 60 |
type: "users", |
| 61 |
id: user.id, |
| 62 |
}, |
| 63 |
}, |
| 64 |
}, |
| 65 |
type: "posts", |
| 66 |
}, |
| 67 |
}), |
| 68 |
}); |
| 69 |
|
| 70 |
let res = await fetch(`/users/${user.id}`); |
| 71 |
let json = await res.json(); |
| 72 |
|
| 73 |
expect(json.data).toEqual({ |
| 74 |
attributes: {}, |
| 75 |
id: "1", |
| 76 |
relationships: { |
| 77 |
"authored-posts": { |
| 78 |
data: [ |
| 79 |
{ |
| 80 |
id: "1", |
| 81 |
type: "posts", |
| 82 |
}, |
| 83 |
], |
| 84 |
}, |
| 85 |
"edited-posts": { |
| 86 |
data: [ |
| 87 |
{ |
| 88 |
id: "1", |
| 89 |
type: "posts", |
| 90 |
}, |
| 91 |
], |
| 92 |
}, |
| 93 |
}, |
| 94 |
type: "users", |
| 95 |
}); |
| 96 |
}); |
| 97 |
}); |