@@ -3,37 +3,32 @@ import { server } from "../../vitest.setup.js";
33import fetchJsonLd from "./fetchJsonLd.js" ;
44import { assert , expect , test } from "vitest" ;
55
6+ const httpResponse = {
7+ "@context" : "http://json-ld.org/contexts/person.jsonld" ,
8+ "@id" : "http://dbpedia.org/resource/John_Lennon" ,
9+ name : "John Lennon" ,
10+ born : "1940年10月09日" ,
11+ spouse : "http://dbpedia.org/resource/Cynthia_Lennon" ,
12+ } ;
13+ 614test ( "fetch a JSON-LD document" , async ( ) => {
715 server . use (
816 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- ) ,
17+ HttpResponse . json ( httpResponse , {
18+ headers : { "Content-Type" : "application/ld+json" } ,
19+ status : 200 ,
20+ statusText : "OK" ,
21+ } ) ,
2322 ) ,
2423 ) ;
25- try {
26- const data = await fetchJsonLd ( "http://localhost/foo.jsonld" ) ;
27- expect ( data . response . ok ) . toBe ( true ) ;
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" ,
32- ) ;
33- expect ( data . body [ "name" ] ) . toBe ( "John Lennon" ) ;
34- } catch {
35- assert . fail ( "Should not have thrown an error" ) ;
36- }
24+ 25+ const data = await fetchJsonLd ( "http://localhost/foo.jsonld" ) ;
26+ expect ( data . response . ok ) . toBe ( true ) ;
27+ 28+ assert ( "body" in data , "Response should have a body property" ) ;
29+ assert ( data . body !== null , "Body should not be null" ) ;
30+ assert ( "name" in data . body , "Body should have a name property" ) ;
31+ expect ( data . body [ "name" ] ) . toBe ( "John Lennon" ) ;
3732} ) ;
3833
3934test ( "fetch a non JSON-LD document" , async ( ) => {
@@ -47,80 +42,54 @@ test("fetch a non JSON-LD document", async () => {
4742 } ) ,
4843 ) ;
4944
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- }
45+ const promise = fetchJsonLd ( "http://localhost/foo.jsonld" ) ;
46+ 47+ await expect ( promise ) . rejects . toHaveProperty ( "response.ok" , true ) ;
48+ await expect ( promise ) . rejects . not . toHaveProperty ( "body" ) ;
5849} ) ;
5950
60- test ( "fetch an error with Content-Type application/ld+json" , ( ) => {
51+ test ( "fetch an error with Content-Type application/ld+json" , async ( ) => {
6152 server . use (
6253 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- ) ;
54+ return HttpResponse . json ( httpResponse , {
55+ status : 500 ,
56+ statusText : "Internal Server Error" ,
57+ headers : { "Content-Type" : "application/ld+json" } ,
58+ } ) ;
7759 } ) ,
7860 ) ;
7961
80- return fetchJsonLd ( "http://localhost/foo.jsonld" ) . catch (
81- ( { response } : { response : Response } ) => {
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- } ) ;
91- } ,
62+ const rejectedResponse = await fetchJsonLd (
63+ "http://localhost/foo.jsonld" ,
64+ ) . catch ( ( error ) => error as { response : Response } ) ;
65+ 66+ await expect ( rejectedResponse ) . toHaveProperty ( "response.ok" , false ) ;
67+ await expect ( rejectedResponse . response . json ( ) ) . resolves . toHaveProperty (
68+ "born" ,
69+ "1940年10月09日" ,
9270 ) ;
9371} ) ;
9472
9573test ( "fetch an error with Content-Type application/error+json" , async ( ) => {
9674 server . use (
9775 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- ) ;
76+ return HttpResponse . json ( httpResponse , {
77+ status : 400 ,
78+ statusText : "Bad Request" ,
79+ headers : { "Content-Type" : "application/error+json" } ,
80+ } ) ;
11281 } ) ,
11382 ) ;
11483
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- }
84+ const rejectedResponse = await fetchJsonLd (
85+ "http://localhost/foo.jsonld" ,
86+ ) . catch ( ( error ) => error as { response : Response } ) ;
87+ 88+ await expect ( rejectedResponse ) . toHaveProperty ( " response.ok" , false ) ;
89+ await expect ( rejectedResponse . response . json ( ) ) . resolves . toHaveProperty (
90+ "born" ,
91+ "1940-10-09" ,
92+ ) ;
12493} ) ;
12594
12695test ( "fetch an empty document" , async ( ) => {
@@ -133,11 +102,9 @@ test("fetch an empty document", async () => {
133102 } ) ;
134103 } ) ,
135104 ) ;
136- try {
137- const data = await fetchJsonLd ( "http://localhost/foo.jsonld" ) ;
138- expect ( data . response . ok ) . toBe ( true ) ;
139- expect ( data ) . not . toHaveProperty ( "body" ) ;
140- } catch {
141- assert . fail ( "Should not have thrown an error" ) ;
142- }
105+ 106+ const dataPromise = fetchJsonLd ( "http://localhost/foo.jsonld" ) ;
107+ 108+ await expect ( dataPromise ) . resolves . toHaveProperty ( "response.ok" , true ) ;
109+ await expect ( dataPromise ) . resolves . not . toHaveProperty ( "body" ) ;
143110} ) ;
0 commit comments