@@ -8,14 +8,15 @@ const baseProductPriceData = {
88 id : "price_123" ,
99 amountType : "FIXED" as const ,
1010 priceAmount : null ,
11+ currency : "USD" ,
1112} ;
1213
1314const baseProductData = {
1415 id : "product_123" ,
1516 name : "Test Product" ,
1617 description : null ,
1718 recurringInterval : null ,
18- prices : [ baseProductPriceData ] ,
19+ price : baseProductPriceData ,
1920} ;
2021
2122describe ( "Product Schemas" , ( ) => {
@@ -144,24 +145,18 @@ describe("Product Schemas", () => {
144145 } ) ;
145146 } ) ;
146147
147- test ( "should validate product with multiple prices " , ( ) => {
148- const productWithMultiplePrices = {
148+ test ( "should validate product with a custom price " , ( ) => {
149+ const productWithCustomPrice = {
149150 ...baseProductData ,
150- prices : [
151- {
152- id : "price_1" ,
153- amountType : "FIXED" as const ,
154- priceAmount : 999 ,
155- } ,
156- {
157- id : "price_2" ,
158- amountType : "CUSTOM" as const ,
159- priceAmount : null ,
160- } ,
161- ] ,
151+ price : {
152+ ...baseProductPriceData ,
153+ id : "price_2" ,
154+ amountType : "CUSTOM" as const ,
155+ priceAmount : null ,
156+ } ,
162157 } ;
163158
164- const result = CheckoutProductSchema . safeParse ( productWithMultiplePrices ) ;
159+ const result = CheckoutProductSchema . safeParse ( productWithCustomPrice ) ;
165160 expect ( result . success ) . toBe ( true ) ;
166161 } ) ;
167162
@@ -185,23 +180,23 @@ describe("Product Schemas", () => {
185180 expect ( result . success ) . toBe ( false ) ;
186181 } ) ;
187182
188- test ( "should reject product without prices array " , ( ) => {
189- const productWithoutPrices = {
183+ test ( "should reject product without price field " , ( ) => {
184+ const productWithoutPrice = {
190185 ...baseProductData ,
191- prices : undefined ,
186+ price : undefined ,
192187 } ;
193188
194- const result = CheckoutProductSchema . safeParse ( productWithoutPrices ) ;
189+ const result = CheckoutProductSchema . safeParse ( productWithoutPrice ) ;
195190 expect ( result . success ) . toBe ( false ) ;
196191 } ) ;
197192
198- test ( "should allow product with empty prices array " , ( ) => {
199- const productWithEmptyPrices = {
193+ test ( "should allow product with null price " , ( ) => {
194+ const productWithNullPrice = {
200195 ...baseProductData ,
201- prices : [ ] ,
196+ price : null ,
202197 } ;
203198
204- const result = CheckoutProductSchema . safeParse ( productWithEmptyPrices ) ;
199+ const result = CheckoutProductSchema . safeParse ( productWithNullPrice ) ;
205200 expect ( result . success ) . toBe ( true ) ;
206201 } ) ;
207202
@@ -215,15 +210,13 @@ describe("Product Schemas", () => {
215210 expect ( result . success ) . toBe ( false ) ;
216211 } ) ;
217212
218- test ( "should reject product with invalid price in prices array " , ( ) => {
213+ test ( "should reject product with invalid price object " , ( ) => {
219214 const productWithInvalidPrice = {
220215 ...baseProductData ,
221- prices : [
222- {
223- ...baseProductPriceData ,
224- amountType : "INVALID_TYPE" as any ,
225- } ,
226- ] ,
216+ price : {
217+ ...baseProductPriceData ,
218+ amountType : "INVALID_TYPE" as any ,
219+ } ,
227220 } ;
228221
229222 const result = CheckoutProductSchema . safeParse ( productWithInvalidPrice ) ;
@@ -252,33 +245,44 @@ describe("Product Schemas", () => {
252245 } ) ;
253246
254247 describe ( "Integration scenarios" , ( ) => {
255- test ( "should validate complete product with all supported price types" , ( ) => {
256- const completeProduct = {
257- id : "product_complete" ,
258- name : "Complete Product" ,
259- description : "A product with all types of prices" ,
260- recurringInterval : "MONTH" as const ,
261- prices : [
262- {
248+ test ( "should validate products with all supported price types" , ( ) => {
249+ const products = [
250+ {
251+ ...baseProductData ,
252+ id : "product_fixed" ,
253+ price : {
254+ ...baseProductPriceData ,
263255 id : "price_fixed" ,
264256 amountType : "FIXED" as const ,
265257 priceAmount : 2999 ,
266258 } ,
267- {
259+ } ,
260+ {
261+ ...baseProductData ,
262+ id : "product_custom" ,
263+ price : {
264+ ...baseProductPriceData ,
268265 id : "price_custom" ,
269266 amountType : "CUSTOM" as const ,
270267 priceAmount : null ,
271268 } ,
272- {
269+ } ,
270+ {
271+ ...baseProductData ,
272+ id : "product_free" ,
273+ price : {
274+ ...baseProductPriceData ,
273275 id : "price_free" ,
274276 amountType : "FREE" as const ,
275277 priceAmount : 0 ,
276278 } ,
277- ] ,
278- } ;
279+ } ,
280+ ] ;
279281
280- const result = CheckoutProductSchema . safeParse ( completeProduct ) ;
281- expect ( result . success ) . toBe ( true ) ;
282+ products . forEach ( ( product ) => {
283+ const result = CheckoutProductSchema . safeParse ( product ) ;
284+ expect ( result . success ) . toBe ( true ) ;
285+ } ) ;
282286 } ) ;
283287 } ) ;
284288} ) ;
0 commit comments