@@ -3,37 +3,32 @@ import { server } from "../../vitest.setup.js";
3
3
import fetchJsonLd from "./fetchJsonLd.js" ;
4
4
import { assert , expect , test } from "vitest" ;
5
5
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
+
6
14
test ( "fetch a JSON-LD document" , async ( ) => {
7
15
server . use (
8
16
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
+ } ) ,
23
22
) ,
24
23
) ;
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" ) ;
37
32
} ) ;
38
33
39
34
test ( "fetch a non JSON-LD document" , async ( ) => {
@@ -47,80 +42,54 @@ test("fetch a non JSON-LD document", async () => {
47
42
} ) ,
48
43
) ;
49
44
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" ) ;
58
49
} ) ;
59
50
60
- test ( "fetch an error with Content-Type application/ld+json" , ( ) => {
51
+ test ( "fetch an error with Content-Type application/ld+json" , async ( ) => {
61
52
server . use (
62
53
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
+ } ) ;
77
59
} ) ,
78
60
) ;
79
61
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" ,
92
70
) ;
93
71
} ) ;
94
72
95
73
test ( "fetch an error with Content-Type application/error+json" , async ( ) => {
96
74
server . use (
97
75
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
+ } ) ;
112
81
} ) ,
113
82
) ;
114
83
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
+ ) ;
124
93
} ) ;
125
94
126
95
test ( "fetch an empty document" , async ( ) => {
@@ -133,11 +102,9 @@ test("fetch an empty document", async () => {
133
102
} ) ;
134
103
} ) ,
135
104
) ;
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" ) ;
143
110
} ) ;
0 commit comments