|
| 1 | +import { http, HttpResponse } from "msw"; |
| 2 | +import { server } from "../../vitest.setup.js"; |
1 | 3 | import fetchJsonLd from "./fetchJsonLd.js";
|
2 |
| -import type { FetchMock } from "jest-fetch-mock"; |
3 |
| -import type { ResponseDocument } from "./fetchJsonLd.js"; |
| 4 | +import { assert, expect, test } from "vitest"; |
4 | 5 |
|
5 |
| -const fetchMock = fetch as FetchMock; |
6 |
| - |
7 |
| -test("fetch a JSON-LD document", () => { |
8 |
| - fetchMock.mockResponseOnce( |
9 |
| - `{ |
10 |
| - "@context": "http://json-ld.org/contexts/person.jsonld", |
11 |
| - "@id": "http://dbpedia.org/resource/John_Lennon", |
12 |
| - "name": "John Lennon", |
13 |
| - "born": "1940-10-09", |
14 |
| - "spouse": "http://dbpedia.org/resource/Cynthia_Lennon" |
15 |
| -}`, |
16 |
| - { |
17 |
| - status: 200, |
18 |
| - statusText: "OK", |
19 |
| - headers: { "Content-Type": "application/ld+json" }, |
20 |
| - }, |
| 6 | +test("fetch a JSON-LD document", async () => { |
| 7 | + server.use( |
| 8 | + http.get("http://localhost/foo.jsonld", () => |
| 9 | + HttpResponse.json( |
| 10 | + { |
| 11 | + "@context": "http://json-ld.org/contexts/person.jsonld", |
| 12 | + "@id": "http://dbpedia.org/resource/John_Lennon", |
| 13 | + name: "John Lennon", |
| 14 | + born: "1940-10-09", |
| 15 | + spouse: "http://dbpedia.org/resource/Cynthia_Lennon", |
| 16 | + }, |
| 17 | + { |
| 18 | + headers: { "Content-Type": "application/ld+json" }, |
| 19 | + status: 200, |
| 20 | + statusText: "OK", |
| 21 | + }, |
| 22 | + ), |
| 23 | + ), |
21 | 24 | );
|
22 |
| - |
23 |
| - return fetchJsonLd("/foo.jsonld").then((data) => { |
| 25 | + try { |
| 26 | + const data = await fetchJsonLd("http://localhost/foo.jsonld"); |
24 | 27 | expect(data.response.ok).toBe(true);
|
25 |
| - expect(((data as ResponseDocument).body as { name: string }).name).toBe( |
26 |
| - "John Lennon", |
| 28 | + expect(data).toHaveProperty("body"); |
| 29 | + assert( |
| 30 | + "body" in data && "name" in data.body, |
| 31 | + "Response should contain a body with a name property", |
27 | 32 | );
|
28 |
| - }); |
| 33 | + expect(data.body["name"]).toBe("John Lennon"); |
| 34 | + } catch { |
| 35 | + assert.fail("Should not have thrown an error"); |
| 36 | + } |
29 | 37 | });
|
30 | 38 |
|
31 |
| -test("fetch a non JSON-LD document", () => { |
32 |
| - fetchMock.mockResponseOnce(`<body>Hello</body>`, { |
33 |
| - status: 200, |
34 |
| - statusText: "OK", |
35 |
| - headers: { "Content-Type": "text/html" }, |
36 |
| - }); |
37 |
| - |
38 |
| - return fetchJsonLd("/foo.jsonld").catch( |
39 |
| - (data: { response: Response; body: undefined }) => { |
40 |
| - expect(data.response.ok).toBe(true); |
41 |
| - expect(typeof data.body).toBe("undefined"); |
42 |
| - }, |
| 39 | +test("fetch a non JSON-LD document", async () => { |
| 40 | + server.use( |
| 41 | + http.get("http://localhost/foo.jsonld", () => { |
| 42 | + return new HttpResponse(`<body>Hello</body>`, { |
| 43 | + headers: { "Content-Type": "text/html" }, |
| 44 | + status: 200, |
| 45 | + statusText: "OK", |
| 46 | + }); |
| 47 | + }), |
43 | 48 | );
|
| 49 | + |
| 50 | + try { |
| 51 | + await fetchJsonLd("http://localhost/foo.jsonld"); |
| 52 | + assert.fail("Should have thrown an error"); |
| 53 | + } catch (error) { |
| 54 | + const data = error as unknown as { response: Response; body: undefined }; |
| 55 | + expect(data.response.ok).toBe(true); |
| 56 | + expect(typeof data.body).toBe("undefined"); |
| 57 | + } |
44 | 58 | });
|
45 | 59 |
|
46 | 60 | test("fetch an error with Content-Type application/ld+json", () => {
|
47 |
| - fetchMock.mockResponseOnce( |
48 |
| - `{ |
49 |
| - "@context": "http://json-ld.org/contexts/person.jsonld", |
50 |
| - "@id": "http://dbpedia.org/resource/John_Lennon", |
51 |
| - "name": "John Lennon", |
52 |
| - "born": "1940-10-09", |
53 |
| - "spouse": "http://dbpedia.org/resource/Cynthia_Lennon" |
54 |
| -}`, |
55 |
| - { |
56 |
| - status: 400, |
57 |
| - statusText: "Bad Request", |
58 |
| - headers: { "Content-Type": "application/ld+json" }, |
59 |
| - }, |
| 61 | + server.use( |
| 62 | + http.get("http://localhost/foo.jsonld", () => { |
| 63 | + return HttpResponse.json( |
| 64 | + { |
| 65 | + "@context": "http://json-ld.org/contexts/person.jsonld", |
| 66 | + "@id": "http://dbpedia.org/resource/John_Lennon", |
| 67 | + name: "John Lennon", |
| 68 | + born: "1940-10-09", |
| 69 | + spouse: "http://dbpedia.org/resource/Cynthia_Lennon", |
| 70 | + }, |
| 71 | + { |
| 72 | + status: 400, |
| 73 | + statusText: "Bad Request", |
| 74 | + headers: { "Content-Type": "application/ld+json" }, |
| 75 | + }, |
| 76 | + ); |
| 77 | + }), |
60 | 78 | );
|
61 | 79 |
|
62 |
| - return fetchJsonLd("/foo.jsonld").catch( |
| 80 | + return fetchJsonLd("http://localhost/foo.jsonld").catch( |
63 | 81 | ({ response }: { response: Response }) => {
|
64 |
| - void response.json().then((body: { born: string }) => { |
65 |
| - expect(response.ok).toBe(false); |
66 |
| - expect(body.born).toBe("1940-10-09"); |
67 |
| - }); |
| 82 | + response |
| 83 | + .json() |
| 84 | + .then((body: { born: string }) => { |
| 85 | + expect(response.ok).toBe(false); |
| 86 | + expect(body.born).toBe("1940-10-09"); |
| 87 | + }) |
| 88 | + .catch(() => { |
| 89 | + assert.fail("Response should have been JSON parsable"); |
| 90 | + }); |
68 | 91 | },
|
69 | 92 | );
|
70 | 93 | });
|
71 | 94 |
|
72 |
| -test("fetch an error with Content-Type application/error+json", () => { |
73 |
| - fetchMock.mockResponseOnce( |
74 |
| - `{ |
75 |
| - "@context": "http://json-ld.org/contexts/person.jsonld", |
76 |
| - "@id": "http://dbpedia.org/resource/John_Lennon", |
77 |
| - "name": "John Lennon", |
78 |
| - "born": "1940-10-09", |
79 |
| - "spouse": "http://dbpedia.org/resource/Cynthia_Lennon" |
80 |
| -}`, |
81 |
| - { |
82 |
| - status: 400, |
83 |
| - statusText: "Bad Request", |
84 |
| - headers: { "Content-Type": "application/error+json" }, |
85 |
| - }, |
| 95 | +test("fetch an error with Content-Type application/error+json", async () => { |
| 96 | + server.use( |
| 97 | + http.get("http://localhost/foo.jsonld", () => { |
| 98 | + return HttpResponse.json( |
| 99 | + { |
| 100 | + "@context": "http://json-ld.org/contexts/person.jsonld", |
| 101 | + "@id": "http://dbpedia.org/resource/John_Lennon", |
| 102 | + name: "John Lennon", |
| 103 | + born: "1940-10-09", |
| 104 | + spouse: "http://dbpedia.org/resource/Cynthia_Lennon", |
| 105 | + }, |
| 106 | + { |
| 107 | + status: 400, |
| 108 | + statusText: "Bad Request", |
| 109 | + headers: { "Content-Type": "application/error+json" }, |
| 110 | + }, |
| 111 | + ); |
| 112 | + }), |
86 | 113 | );
|
87 | 114 |
|
88 |
| - return fetchJsonLd("/foo.jsonld").catch( |
89 |
| - ({ response }: { response: Response }) => { |
90 |
| - void response.json().then((body: { born: string }) => { |
91 |
| - expect(response.ok).toBe(false); |
92 |
| - expect(body.born).toBe("1940-10-09"); |
93 |
| - }); |
94 |
| - }, |
95 |
| - ); |
| 115 | + try { |
| 116 | + await fetchJsonLd("http://localhost/foo.jsonld"); |
| 117 | + assert.fail("Should have thrown an error"); |
| 118 | + } catch (error) { |
| 119 | + const data = error as unknown as { response: Response }; |
| 120 | + const body = await data.response.json(); |
| 121 | + expect(data.response.ok).toBe(false); |
| 122 | + expect(body.born).toBe("1940-10-09"); |
| 123 | + } |
96 | 124 | });
|
97 | 125 |
|
98 |
| -test("fetch an empty document", () => { |
99 |
| - fetchMock.mockResponseOnce("", { |
100 |
| - status: 204, |
101 |
| - statusText: "No Content", |
102 |
| - headers: { "Content-Type": "text/html" }, |
103 |
| - }); |
104 |
| - |
105 |
| - return fetchJsonLd("/foo.jsonld").then((data) => { |
| 126 | +test("fetch an empty document", async () => { |
| 127 | + server.use( |
| 128 | + http.get("http://localhost/foo.jsonld", () => { |
| 129 | + return new HttpResponse(``, { |
| 130 | + status: 204, |
| 131 | + statusText: "No Content", |
| 132 | + headers: { "Content-Type": "text/html" }, |
| 133 | + }); |
| 134 | + }), |
| 135 | + ); |
| 136 | + try { |
| 137 | + const data = await fetchJsonLd("http://localhost/foo.jsonld"); |
106 | 138 | expect(data.response.ok).toBe(true);
|
107 |
| - expect((data as ResponseDocument).body).toBe(undefined); |
108 |
| - }); |
| 139 | + expect(data).not.toHaveProperty("body"); |
| 140 | + } catch { |
| 141 | + assert.fail("Should not have thrown an error"); |
| 142 | + } |
109 | 143 | });
|
0 commit comments