Debian Codegraph @

__tests__/external/browser-only/fetch-sanity-check-test.js

raw
1 import { Server, Model } from "miragejs";
2
3 describe("External |Browser only | Fetch sanity check", () => {
4 let server;
5
6 beforeEach(() => {
7 server = new Server({
8 environment: "test",
9 models: {
10 contact: Model,
11 },
12 });
13 });
14
15 afterEach(() => {
16 server.shutdown();
17 });
18
19 test("mirage responds to get", async () => {
20 expect.assertions(1);
21
22 server.get("/contacts", function () {
23 return { some: "data" };
24 });
25
26 let res = await fetch("/contacts", { method: "GET" });
27 let data = await res.json();
28
29 expect(data).toEqual({ some: "data" });
30 });
31
32 test("mirage responds to post", async () => {
33 expect.assertions(1);
34
35 server.post("/contacts", function () {
36 return { some: "data" };
37 });
38
39 let res = await fetch("/contacts", {
40 method: "POST",
41 });
42 let data = await res.json();
43
44 expect(data).toEqual({ some: "data" });
45 });
46
47 test("mirage responds to put", async () => {
48 expect.assertions(1);
49
50 server.put("/contacts", function () {
51 return { some: "data" };
52 });
53
54 let res = await fetch("/contacts", {
55 method: "PUT",
56 url: "/contacts",
57 });
58 let data = await res.json();
59
60 expect(data).toEqual({ some: "data" });
61 });
62
63 test("mirage responds to delete", async () => {
64 expect.assertions(1);
65
66 server.delete("/contacts", function () {
67 return { some: "data" };
68 });
69
70 let res = await fetch("/contacts", {
71 method: "DELETE",
72 url: "/contacts",
73 });
74 let data = await res.json();
75
76 expect(data).toEqual({ some: "data" });
77 });
78
79 test("mirage responds to patch", async () => {
80 expect.assertions(1);
81
82 server.patch("/contacts", function () {
83 return { some: "data" };
84 });
85
86 let res = await fetch("/contacts", {
87 method: "PATCH",
88 });
89 let data = await res.json();
90
91 expect(data).toEqual({ some: "data" });
92 });
93
94 test("mirage responds to resource", async () => {
95 expect.assertions(1);
96
97 server.resource("contacts");
98
99 let res = await fetch("/contacts", { method: "GET" });
100 let data = await res.json();
101
102 expect(data).toEqual({ contacts: [] });
103 });
104
105 test("response code can be customized", async () => {
106 expect.assertions(1);
107
108 server.get("/contacts", {}, 404);
109
110 let res = await fetch("/contacts", {
111 method: "GET",
112 });
113
114 expect(res.status).toEqual(404);
115 });
116
117 test("mirage responds to options", async () => {
118 expect.assertions(1);
119
120 server.options("/contacts", function () {
121 return { some: "data" };
122 });
123
124 let res = await fetch("/contacts", {
125 method: "OPTIONS",
126 });
127 let data = await res.json();
128
129 expect(data).toEqual({ some: "data" });
130 });
131 });