| 1 |
import { Server, Model, ActiveModelSerializer, RestSerializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("Integration | Server Config", () => { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
environment: "development", |
| 9 |
models: { |
| 10 |
contact: Model, |
| 11 |
post: Model, |
| 12 |
}, |
| 13 |
serializers: { |
| 14 |
contact: ActiveModelSerializer, |
| 15 |
}, |
| 16 |
}); |
| 17 |
server.timing = 0; |
| 18 |
server.logging = false; |
| 19 |
}); |
| 20 |
|
| 21 |
afterEach(function () { |
| 22 |
server.shutdown(); |
| 23 |
}); |
| 24 |
|
| 25 |
test("namespace can be configured", async () => { |
| 26 |
expect.assertions(1); |
| 27 |
|
| 28 |
let contacts = [ |
| 29 |
{ id: "1", name: "Link" }, |
| 30 |
{ id: "2", name: "Zelda" }, |
| 31 |
]; |
| 32 |
server.db.loadData({ |
| 33 |
contacts, |
| 34 |
}); |
| 35 |
server.namespace = "api"; |
| 36 |
server.get("/contacts"); |
| 37 |
|
| 38 |
let res = await fetch("/api/contacts"); |
| 39 |
let data = await res.json(); |
| 40 |
expect(data).toEqual({ contacts }); |
| 41 |
}); |
| 42 |
|
| 43 |
test("urlPrefix can be configured", async () => { |
| 44 |
expect.assertions(1); |
| 45 |
|
| 46 |
let contacts = [ |
| 47 |
{ id: "1", name: "Link" }, |
| 48 |
{ id: "2", name: "Zelda" }, |
| 49 |
]; |
| 50 |
server.db.loadData({ |
| 51 |
contacts, |
| 52 |
}); |
| 53 |
server.urlPrefix = "http://localhost:3000"; |
| 54 |
server.get("/contacts"); |
| 55 |
|
| 56 |
let res = await fetch("http://localhost:3000/contacts"); |
| 57 |
let data = await res.json(); |
| 58 |
|
| 59 |
expect(data).toEqual({ contacts }); |
| 60 |
}); |
| 61 |
|
| 62 |
test("urlPrefix and namespace can be configured simultaneously", async () => { |
| 63 |
expect.assertions(1); |
| 64 |
|
| 65 |
let contacts = [ |
| 66 |
{ id: "1", name: "Link" }, |
| 67 |
{ id: "2", name: "Zelda" }, |
| 68 |
]; |
| 69 |
server.db.loadData({ |
| 70 |
contacts, |
| 71 |
}); |
| 72 |
server.urlPrefix = "http://localhost:3000"; |
| 73 |
server.namespace = "api"; |
| 74 |
server.get("/contacts"); |
| 75 |
|
| 76 |
let res = await fetch("http://localhost:3000/api/contacts"); |
| 77 |
let data = await res.json(); |
| 78 |
|
| 79 |
expect(data).toEqual({ contacts }); |
| 80 |
}); |
| 81 |
|
| 82 |
test("fully qualified domain names can be used in configuration", async () => { |
| 83 |
expect.assertions(1); |
| 84 |
|
| 85 |
let contacts = [ |
| 86 |
{ id: "1", name: "Link" }, |
| 87 |
{ id: "2", name: "Zelda" }, |
| 88 |
]; |
| 89 |
server.db.loadData({ |
| 90 |
contacts, |
| 91 |
}); |
| 92 |
server.get("http://example.org/api/contacts"); |
| 93 |
|
| 94 |
let res = await fetch("http://example.org/api/contacts"); |
| 95 |
let data = await res.json(); |
| 96 |
|
| 97 |
expect(data).toEqual({ contacts }); |
| 98 |
}); |
| 99 |
|
| 100 |
test("urlPrefix/namespace are ignored when fully qualified domain names are used in configuration", async () => { |
| 101 |
expect.assertions(1); |
| 102 |
|
| 103 |
let contacts = [ |
| 104 |
{ id: "1", name: "Link" }, |
| 105 |
{ id: "2", name: "Zelda" }, |
| 106 |
]; |
| 107 |
server.db.loadData({ |
| 108 |
contacts, |
| 109 |
}); |
| 110 |
server.urlPrefix = "https://example.net"; |
| 111 |
server.get("http://example.org/api/contacts"); |
| 112 |
|
| 113 |
let res = await fetch("http://example.org/api/contacts"); |
| 114 |
let data = await res.json(); |
| 115 |
|
| 116 |
expect(data).toEqual({ contacts }); |
| 117 |
}); |
| 118 |
|
| 119 |
test("blank urlPrefix and namespace ends up as /", async () => { |
| 120 |
expect.assertions(1); |
| 121 |
|
| 122 |
let contacts = [ |
| 123 |
{ id: "1", name: "Link" }, |
| 124 |
{ id: "2", name: "Zelda" }, |
| 125 |
]; |
| 126 |
server.db.loadData({ |
| 127 |
contacts, |
| 128 |
}); |
| 129 |
server.namespace = ""; |
| 130 |
server.urlPrefix = ""; |
| 131 |
server.get("contacts"); |
| 132 |
|
| 133 |
let res = await fetch("/contacts"); |
| 134 |
let data = await res.json(); |
| 135 |
|
| 136 |
expect(data).toEqual({ contacts }); |
| 137 |
}); |
| 138 |
|
| 139 |
test("namespace with no slash gets one", async () => { |
| 140 |
expect.assertions(1); |
| 141 |
|
| 142 |
let contacts = [ |
| 143 |
{ id: "1", name: "Link" }, |
| 144 |
{ id: "2", name: "Zelda" }, |
| 145 |
]; |
| 146 |
server.db.loadData({ |
| 147 |
contacts, |
| 148 |
}); |
| 149 |
server.namespace = "api"; |
| 150 |
server.get("contacts"); |
| 151 |
|
| 152 |
let res = await fetch("/api/contacts"); |
| 153 |
let data = await res.json(); |
| 154 |
|
| 155 |
expect(data).toEqual({ contacts }); |
| 156 |
}); |
| 157 |
|
| 158 |
test("urlPrefix with no slash gets one", async () => { |
| 159 |
expect.assertions(1); |
| 160 |
|
| 161 |
let contacts = [ |
| 162 |
{ id: "1", name: "Link" }, |
| 163 |
{ id: "2", name: "Zelda" }, |
| 164 |
]; |
| 165 |
server.db.loadData({ |
| 166 |
contacts, |
| 167 |
}); |
| 168 |
server.urlPrefix = "pre"; |
| 169 |
server.get("contacts"); |
| 170 |
|
| 171 |
let res = await fetch("/pre/contacts"); |
| 172 |
let data = await res.json(); |
| 173 |
|
| 174 |
expect(data).toEqual({ contacts }); |
| 175 |
}); |
| 176 |
|
| 177 |
test("namespace of / works", async () => { |
| 178 |
expect.assertions(1); |
| 179 |
|
| 180 |
let contacts = [ |
| 181 |
{ id: "1", name: "Link" }, |
| 182 |
{ id: "2", name: "Zelda" }, |
| 183 |
]; |
| 184 |
server.db.loadData({ |
| 185 |
contacts, |
| 186 |
}); |
| 187 |
server.namespace = "/"; |
| 188 |
server.get("contacts"); |
| 189 |
|
| 190 |
let res = await fetch("/contacts"); |
| 191 |
let data = await res.json(); |
| 192 |
|
| 193 |
expect(data).toEqual({ contacts }); |
| 194 |
}); |
| 195 |
|
| 196 |
test("redefining options using the config method works", async () => { |
| 197 |
expect.assertions(5); |
| 198 |
|
| 199 |
let contacts = [ |
| 200 |
{ id: "1", name: "Link" }, |
| 201 |
{ id: "2", name: "Zelda" }, |
| 202 |
]; |
| 203 |
server.config({ |
| 204 |
namespace: "api", |
| 205 |
urlPrefix: "http://localhost:3000", |
| 206 |
timing: 1000, |
| 207 |
serializers: { |
| 208 |
post: RestSerializer, |
| 209 |
}, |
| 210 |
}); |
| 211 |
server.db.loadData({ |
| 212 |
contacts, |
| 213 |
}); |
| 214 |
server.get("contacts"); |
| 215 |
|
| 216 |
expect(server.timing).toEqual(1000); |
| 217 |
|
| 218 |
let res = await fetch("http://localhost:3000/api/contacts"); |
| 219 |
let data = await res.json(); |
| 220 |
expect(data).toEqual({ contacts }); |
| 221 |
|
| 222 |
let serializerMap = server.serializerOrRegistry._serializerMap; |
| 223 |
|
| 224 |
expect(Object.keys(serializerMap)).toHaveLength(2); |
| 225 |
expect(serializerMap.contact).toEqual(ActiveModelSerializer); |
| 226 |
expect(serializerMap.post).toEqual(RestSerializer); |
| 227 |
}); |
| 228 |
|
| 229 |
test("changing the environment of the server throws an error", () => { |
| 230 |
expect(() => { |
| 231 |
server.config({ |
| 232 |
environment: "test", |
| 233 |
}); |
| 234 |
}).toThrow(); |
| 235 |
}); |
| 236 |
|
| 237 |
test("changing the trackRequests configuration of the server throws an error", () => { |
| 238 |
expect(() => { |
| 239 |
server.config({ |
| 240 |
trackRequests: true, |
| 241 |
}); |
| 242 |
}).toThrow(); |
| 243 |
}); |
| 244 |
}); |