| 1 |
import { Server, Model, ActiveModelSerializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("Integration | Server | Shorthands | Active Model Serializer Sanity check", function () { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
models: { |
| 10 |
contact: Model, |
| 11 |
}, |
| 12 |
serializers: { |
| 13 |
application: ActiveModelSerializer, |
| 14 |
}, |
| 15 |
}); |
| 16 |
server.timing = 0; |
| 17 |
server.logging = false; |
| 18 |
}); |
| 19 |
|
| 20 |
afterEach(function () { |
| 21 |
server.shutdown(); |
| 22 |
}); |
| 23 |
|
| 24 |
test("a get shorthand works", async () => { |
| 25 |
expect.assertions(2); |
| 26 |
|
| 27 |
server.db.loadData({ |
| 28 |
contacts: [{ id: 1, name: "Link" }], |
| 29 |
}); |
| 30 |
|
| 31 |
server.get("/contacts"); |
| 32 |
|
| 33 |
let res = await fetch("/contacts"); |
| 34 |
let data = await res.json(); |
| 35 |
|
| 36 |
expect(res.status).toEqual(200); |
| 37 |
expect(data).toEqual({ contacts: [{ id: "1", name: "Link" }] }); |
| 38 |
}); |
| 39 |
|
| 40 |
test("a post shorthand works", async () => { |
| 41 |
expect.assertions(2); |
| 42 |
|
| 43 |
server.post("/contacts"); |
| 44 |
|
| 45 |
let res = await fetch("/contacts", { |
| 46 |
method: "POST", |
| 47 |
body: JSON.stringify({ |
| 48 |
contact: { |
| 49 |
name: "Zelda", |
| 50 |
}, |
| 51 |
}), |
| 52 |
}); |
| 53 |
|
| 54 |
expect(res.status).toEqual(201); |
| 55 |
expect(server.db.contacts).toHaveLength(1); |
| 56 |
}); |
| 57 |
|
| 58 |
test("a put shorthand works", async () => { |
| 59 |
expect.assertions(2); |
| 60 |
|
| 61 |
server.db.loadData({ |
| 62 |
contacts: [{ id: 1, name: "Link" }], |
| 63 |
}); |
| 64 |
|
| 65 |
server.put("/contacts/:id"); |
| 66 |
|
| 67 |
let res = await fetch("/contacts/1", { |
| 68 |
method: "PUT", |
| 69 |
body: JSON.stringify({ |
| 70 |
contact: { |
| 71 |
name: "Zelda", |
| 72 |
}, |
| 73 |
}), |
| 74 |
}); |
| 75 |
|
| 76 |
expect(res.status).toEqual(200); |
| 77 |
expect(server.db.contacts[0].name).toEqual("Zelda"); |
| 78 |
}); |
| 79 |
|
| 80 |
test("a patch shorthand works", async () => { |
| 81 |
expect.assertions(2); |
| 82 |
|
| 83 |
server.db.loadData({ |
| 84 |
contacts: [{ id: 1, name: "Link" }], |
| 85 |
}); |
| 86 |
|
| 87 |
server.patch("/contacts/:id"); |
| 88 |
|
| 89 |
let res = await fetch("/contacts/1", { |
| 90 |
method: "PATCH", |
| 91 |
body: JSON.stringify({ |
| 92 |
contact: { |
| 93 |
name: "Zelda", |
| 94 |
}, |
| 95 |
}), |
| 96 |
}); |
| 97 |
|
| 98 |
expect(res.status).toEqual(200); |
| 99 |
expect(server.db.contacts[0].name).toEqual("Zelda"); |
| 100 |
}); |
| 101 |
|
| 102 |
test("a delete shorthand works", async () => { |
| 103 |
expect.assertions(3); |
| 104 |
|
| 105 |
server.db.loadData({ |
| 106 |
contacts: [{ id: 1, name: "Link" }], |
| 107 |
}); |
| 108 |
|
| 109 |
server.del("/contacts/:id"); |
| 110 |
|
| 111 |
let res = await fetch("/contacts/1", { |
| 112 |
method: "DELETE", |
| 113 |
}); |
| 114 |
let text = await res.text(); |
| 115 |
|
| 116 |
expect(text).toEqual(""); |
| 117 |
expect(res.status).toEqual(204); |
| 118 |
expect(server.db.contacts).toHaveLength(0); |
| 119 |
}); |
| 120 |
}); |