10
10
* governing permissions and limitations under the License.
11
11
*/
12
12
13
+ // @ts -nocheck
14
+
13
15
import { strict as assert } from 'assert' ;
14
16
import sinon from 'sinon' ;
15
17
import esmock from 'esmock' ;
16
18
17
19
describe ( 'Product Save Tests' , ( ) => {
18
20
let putProduct ;
21
+ /** @type {import('../../src/catalog/update.js').handleProductSaveRequest } */
19
22
let handleProductSaveRequest ;
23
+ /** @type {sinon.SinonStub } */
20
24
let saveProductsStub ;
25
+ /** @type {sinon.SinonStub } */
21
26
let callAdminStub ;
22
- let errorResponseStub ;
23
- let errorWithResponseStub ;
24
27
25
28
beforeEach ( async ( ) => {
26
29
saveProductsStub = sinon . stub ( ) ;
27
30
callAdminStub = sinon . stub ( ) ;
28
- errorResponseStub = sinon . stub ( ) ;
29
- errorWithResponseStub = sinon . stub ( ) ;
30
31
31
32
const mocks = {
32
33
'../../src/utils/r2.js' : { saveProducts : saveProductsStub } ,
33
34
'../../src/utils/admin.js' : { callAdmin : callAdminStub } ,
34
- '../../src/utils/http.js' : { errorResponse : errorResponseStub , errorWithResponse : errorWithResponseStub } ,
35
35
} ;
36
36
37
37
( { putProduct, handleProductSaveRequest } = await esmock ( '../../src/catalog/update.js' , mocks ) ) ;
@@ -43,16 +43,10 @@ describe('Product Save Tests', () => {
43
43
44
44
describe ( 'putProduct' , ( ) => {
45
45
it ( 'should throw error when product SKU is missing' , async ( ) => {
46
- const error = new Error ( 'invalid request body: missing sku' ) ;
47
- errorWithResponseStub . throws ( error ) ;
48
-
49
46
const product = { } ;
50
47
await assert . rejects ( async ( ) => {
51
48
await putProduct ( { } , { } , product ) ;
52
49
} , / i n v a l i d r e q u e s t b o d y : m i s s i n g s k u / ) ;
53
-
54
- assert ( errorWithResponseStub . calledOnce ) ;
55
- assert ( errorWithResponseStub . calledWith ( 400 , 'invalid request body: missing sku' ) ) ;
56
50
} ) ;
57
51
58
52
it ( 'should save the product when SKU is present' , async ( ) => {
@@ -72,13 +66,9 @@ describe('Product Save Tests', () => {
72
66
const ctx = { log : { error : sinon . stub ( ) } } ;
73
67
const request = { json : sinon . stub ( ) . resolves ( { sku : '1234' } ) } ;
74
68
75
- const mockResponse = new Response ( null , { status : 501 , headers : { 'x-error' : 'not implemented' } } ) ;
76
- errorResponseStub . returns ( mockResponse ) ;
77
-
78
69
const response = await handleProductSaveRequest ( ctx , config , request ) ;
79
70
80
71
assert . equal ( response . status , 501 ) ;
81
- assert ( errorResponseStub . calledWith ( 501 , 'not implemented' ) ) ;
82
72
assert . equal ( response . headers . get ( 'x-error' ) , 'not implemented' ) ;
83
73
} ) ;
84
74
@@ -106,7 +96,7 @@ describe('Product Save Tests', () => {
106
96
assert . equal ( callAdminStub . callCount , 4 ) ;
107
97
} ) ;
108
98
109
- it ( 'should skip calling callAdmin when confMap has no matching env ' , async ( ) => {
99
+ it ( 'should return 404 when no matching path patterns found ' , async ( ) => {
110
100
const config = {
111
101
sku : '1234' ,
112
102
confEnvMap : {
@@ -124,10 +114,11 @@ describe('Product Save Tests', () => {
124
114
const response = await handleProductSaveRequest ( ctx , config , request ) ;
125
115
126
116
assert ( callAdminStub . notCalled ) ;
127
- assert . equal ( response . status , 201 ) ;
117
+ assert . equal ( response . status , 404 ) ;
118
+ assert . equal ( response . headers . get ( 'x-error' ) , 'no path patterns found' ) ;
128
119
} ) ;
129
120
130
- it ( 'should return error when callAdmin fails' , async ( ) => {
121
+ it ( 'should return error when purging fails' , async ( ) => {
131
122
const config = {
132
123
sku : '1234' ,
133
124
confEnvMap : {
@@ -137,37 +128,38 @@ describe('Product Save Tests', () => {
137
128
} ,
138
129
env : 'test' ,
139
130
} ;
140
- const ctx = { log : { error : sinon . stub ( ) } } ;
131
+ const ctx = { log : console } ;
141
132
const request = { json : sinon . stub ( ) . resolves ( { sku : '1234' , urlKey : 'product-url-key' } ) } ;
142
133
143
134
saveProductsStub . resolves ( ) ;
144
- callAdminStub . onFirstCall ( ) . resolves ( { ok : false } ) ;
145
-
146
- const mockResponse = new Response ( null , { status : 400 , headers : { 'x-error' : 'failed to preview product' } } ) ;
147
- errorResponseStub . returns ( mockResponse ) ;
135
+ callAdminStub . onFirstCall ( ) . resolves ( new Response ( '' , { status : 500 , headers : { 'x-error' : 'bad thing happen' } } ) ) ;
148
136
149
137
const response = await handleProductSaveRequest ( ctx , config , request ) ;
150
138
151
- assert . equal ( response . status , 400 ) ;
152
- assert ( callAdminStub . calledOnce ) ;
153
- assert ( errorResponseStub . calledWith ( 400 , 'failed to preview product' ) ) ;
154
- assert . equal ( response . headers . get ( 'x-error' ) , 'failed to preview product' ) ;
139
+ assert . equal ( response . status , 500 ) ;
140
+ assert . ok ( callAdminStub . calledOnce ) ;
141
+ assert . equal ( response . headers . get ( 'x-error' ) , 'purge errors' ) ;
142
+ const respBody = await response . json ( ) ;
143
+ assert . deepStrictEqual ( respBody , {
144
+ errors : [
145
+ {
146
+ op : 'preview' ,
147
+ path : '/path/to/1234' ,
148
+ status : 500 ,
149
+ message : 'bad thing happen' ,
150
+ } ,
151
+ ] ,
152
+ } ) ;
155
153
} ) ;
156
154
157
155
it ( 'should return 400 if request.json throws a JSON parsing error' , async ( ) => {
158
156
const config = { sku : '1234' , confEnvMap : { } } ;
159
157
const ctx = { log : { error : sinon . stub ( ) } } ;
160
158
const request = { json : sinon . stub ( ) . rejects ( new Error ( 'Unexpected token < in JSON at position 0' ) ) } ;
161
159
162
- const mockResponse = new Response ( null , { status : 400 , headers : { 'x-error' : 'invalid JSON' } } ) ;
163
- errorResponseStub . returns ( mockResponse ) ;
164
-
165
160
const response = await handleProductSaveRequest ( ctx , config , request ) ;
166
161
167
162
assert . equal ( response . status , 400 ) ;
168
-
169
- assert ( errorResponseStub . calledWith ( 400 , 'invalid JSON' ) ) ;
170
-
171
163
assert . equal ( response . headers . get ( 'x-error' ) , 'invalid JSON' ) ;
172
164
173
165
assert ( ctx . log . error . calledOnce ) ;
0 commit comments