| 1 |
import { Model, Factory, belongsTo, Server } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Factories | afterCreate", () => { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
models: { |
| 10 |
author: Model, |
| 11 |
post: Model.extend({ |
| 12 |
author: belongsTo(), |
| 13 |
}), |
| 14 |
comment: Model.extend({ |
| 15 |
post: belongsTo(), |
| 16 |
}), |
| 17 |
}, |
| 18 |
factories: { |
| 19 |
author: Factory.extend({ |
| 20 |
afterCreate(author, server) { |
| 21 |
author.update({ name: "Sam" }); |
| 22 |
server.create("post", { author }); |
| 23 |
}, |
| 24 |
}), |
| 25 |
post: Factory.extend({ |
| 26 |
title: "Lorem ipsum", |
| 27 |
afterCreate(post, server) { |
| 28 |
server.create("comment", { post }); |
| 29 |
}, |
| 30 |
}), |
| 31 |
comment: Factory.extend({ |
| 32 |
text: "Yo soy el nino", |
| 33 |
}), |
| 34 |
}, |
| 35 |
}); |
| 36 |
}); |
| 37 |
|
| 38 |
afterEach(() => { |
| 39 |
server.shutdown(); |
| 40 |
}); |
| 41 |
|
| 42 |
test("it works for models", () => { |
| 43 |
let author = server.create("author"); |
| 44 |
|
| 45 |
expect(author.name).toEqual("Sam"); |
| 46 |
expect(server.db.posts).toHaveLength(1); |
| 47 |
expect(server.db.posts[0]).toEqual({ |
| 48 |
id: "1", |
| 49 |
title: "Lorem ipsum", |
| 50 |
authorId: "1", |
| 51 |
}); |
| 52 |
expect(server.db.comments).toHaveLength(1); |
| 53 |
expect(server.db.comments[0]).toEqual({ |
| 54 |
id: "1", |
| 55 |
text: "Yo soy el nino", |
| 56 |
postId: "1", |
| 57 |
}); |
| 58 |
}); |
| 59 |
}); |