1
1
import { readFileSync } from 'fs' ;
2
-
3
- const { version : packageVersion , repository } = JSON . parse ( readFileSync ( 'package.json' ) ) ;
2
+ import { vi , beforeEach , describe , expect , it , afterEach } from 'vitest' ;
3
+ import type { MockResponseInitFunction } from 'vitest-fetch-mock' ;
4
+ import createFetchMock from 'vitest-fetch-mock' ;
4
5
5
6
import { createClient } from './Client' ;
6
7
import { Method } from './http' ;
8
+ import type { CoverageEntry } from './types' ;
9
+
10
+ const { version : packageVersion , repository } = JSON . parse ( readFileSync ( 'package.json' ) . toString ( ) ) ;
11
+
12
+ const fetch = createFetchMock ( vi ) ;
7
13
8
14
const BASE_URL = 'https://api.prezly.com' ;
9
15
const DEFAULT_USER_AGENT = `prezly-javascript-sdk/${ packageVersion } (+${ repository . url } )` ;
@@ -18,12 +24,15 @@ const DEFAULT_REQUEST_PROPS = {
18
24
} ,
19
25
} ;
20
26
21
- function successJsonResponse ( body , status = 200 ) {
22
- return new Response ( JSON . stringify ( body ) , {
23
- status,
24
- statusText : 'OK' ,
25
- headers : {
26
- 'Content-Type' : 'application/json' ,
27
+ function successJsonResponse ( body : any , status = 200 ) : MockResponseInitFunction {
28
+ return ( ) => ( {
29
+ body : JSON . stringify ( body ) ,
30
+ init : {
31
+ status,
32
+ statusText : 'OK' ,
33
+ headers : {
34
+ 'Content-Type' : 'application/json' ,
35
+ } ,
27
36
} ,
28
37
} ) ;
29
38
}
@@ -34,23 +43,30 @@ describe('Client', () => {
34
43
35
44
beforeEach ( ( ) => {
36
45
fetch . resetMocks ( ) ;
46
+ fetch . enableMocks ( ) ;
47
+ } ) ;
48
+
49
+ afterEach ( ( ) => {
50
+ fetch . disableMocks ( ) ;
37
51
} ) ;
38
52
39
53
describe ( 'Coverage' , ( ) => {
40
- const getListPayload = ( coverage ) => ( {
41
- sort : '-published_at' ,
42
- pagination : {
43
- offset : 0 ,
44
- limit : 20 ,
45
- matched_records_number : coverage . length ,
46
- total_records_number : coverage . length ,
47
- } ,
48
- coverage,
49
- } ) ;
54
+ function getListPayload ( coverage : Partial < CoverageEntry > [ ] ) {
55
+ return {
56
+ sort : '-published_at' ,
57
+ pagination : {
58
+ offset : 0 ,
59
+ limit : 20 ,
60
+ matched_records_number : coverage . length ,
61
+ total_records_number : coverage . length ,
62
+ } ,
63
+ coverage,
64
+ } ;
65
+ }
50
66
51
67
it ( 'should call the list endpoint' , async ( ) => {
52
68
const expectedPayload = getListPayload ( [ ] ) ;
53
- fetch . mockResolvedValueOnce ( successJsonResponse ( expectedPayload ) ) ;
69
+ fetch . mockResponseOnce ( successJsonResponse ( expectedPayload ) ) ;
54
70
55
71
const prezlySdk = createClient ( {
56
72
accessToken : ACCESS_TOKEN ,
@@ -66,7 +82,7 @@ describe('Client', () => {
66
82
67
83
it ( 'should call the GET :id endpoint' , async ( ) => {
68
84
const coverage = { id : 123 } ;
69
- fetch . mockResolvedValueOnce ( successJsonResponse ( { coverage } ) ) ;
85
+ fetch . mockResponseOnce ( successJsonResponse ( { coverage } ) ) ;
70
86
71
87
const prezlySdk = createClient ( {
72
88
accessToken : ACCESS_TOKEN ,
@@ -82,9 +98,10 @@ describe('Client', () => {
82
98
83
99
it ( 'should allow filtering by externalReferenceId' , async ( ) => {
84
100
const externalReferenceId = 'external-ref-id' ;
101
+
85
102
const coverage = { id : 123 } ;
86
103
const expectedPayload = getListPayload ( [ coverage ] ) ;
87
- fetch . mockResolvedValueOnce ( successJsonResponse ( expectedPayload ) ) ;
104
+ fetch . mockResponseOnce ( successJsonResponse ( expectedPayload ) ) ;
88
105
89
106
const prezlySdk = createClient ( {
90
107
accessToken : ACCESS_TOKEN ,
@@ -101,7 +118,7 @@ describe('Client', () => {
101
118
102
119
it ( 'should return null from externalReferenceId no results found' , async ( ) => {
103
120
const expectedPayload = getListPayload ( [ ] ) ;
104
- fetch . mockResolvedValueOnce ( successJsonResponse ( expectedPayload ) ) ;
121
+ fetch . mockResponseOnce ( successJsonResponse ( expectedPayload ) ) ;
105
122
106
123
const prezlySdk = createClient ( {
107
124
accessToken : ACCESS_TOKEN ,
@@ -115,7 +132,7 @@ describe('Client', () => {
115
132
const coverage = {
116
133
url : 'https://example.com' ,
117
134
} ;
118
- fetch . mockResolvedValueOnce ( successJsonResponse ( { coverage } ) ) ;
135
+ fetch . mockResponseOnce ( successJsonResponse ( { coverage } ) ) ;
119
136
120
137
const prezlySdk = createClient ( {
121
138
accessToken : ACCESS_TOKEN ,
@@ -135,7 +152,7 @@ describe('Client', () => {
135
152
const coverage = {
136
153
url : 'https://prezly.com' ,
137
154
} ;
138
- fetch . mockResolvedValueOnce ( successJsonResponse ( { coverage } ) ) ;
155
+ fetch . mockResponseOnce ( successJsonResponse ( { coverage } ) ) ;
139
156
140
157
const prezlySdk = createClient ( {
141
158
accessToken : ACCESS_TOKEN ,
@@ -152,7 +169,7 @@ describe('Client', () => {
152
169
153
170
it ( 'should call delete endpoint' , async ( ) => {
154
171
const id = 123 ;
155
- fetch . mockResolvedValueOnce ( successJsonResponse ( undefined , 204 ) ) ;
172
+ fetch . mockResponseOnce ( successJsonResponse ( undefined , 204 ) ) ;
156
173
157
174
const prezlySdk = createClient ( {
158
175
accessToken : ACCESS_TOKEN ,
@@ -169,7 +186,7 @@ describe('Client', () => {
169
186
170
187
describe ( 'custom options' , ( ) => {
171
188
it ( 'should allow overriding the url' , async ( ) => {
172
- fetch . mockResolvedValueOnce ( successJsonResponse ( { } ) ) ;
189
+ fetch . mockResponseOnce ( successJsonResponse ( { } ) ) ;
173
190
174
191
// Intentionally using trailing `/` to test url sanitizing.
175
192
const baseUrl = 'https://api.prezly.test/' ;
@@ -186,7 +203,7 @@ describe('Client', () => {
186
203
} ) ;
187
204
188
205
it ( 'should allow overriding the headers' , async ( ) => {
189
- fetch . mockResolvedValueOnce ( successJsonResponse ( { } ) ) ;
206
+ fetch . mockResponseOnce ( successJsonResponse ( { } ) ) ;
190
207
191
208
const customHeaders = {
192
209
'User-Agent' : 'Test' ,
0 commit comments