File tree Expand file tree Collapse file tree 3 files changed +23
-4
lines changed Expand file tree Collapse file tree 3 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -164,6 +164,7 @@ ReactDOM.render(
164
164
| ` onItemAdd ` | _ No_ | Triggered on items added to your cart, unless the item already exists, then ` onItemUpdate ` will be invoked. |
165
165
| ` onItemUpdate ` | _ No_ | Triggered on items updated in your cart, unless you are setting the quantity to ` 0 ` , then ` onItemRemove ` will be invoked. |
166
166
| ` onItemRemove ` | _ No_ | Triggered on items removed from your cart. |
167
+ | ` onEmptyCart ` | _ No_ | Triggered on empty cart. |
167
168
| ` storage ` | _ No_ | Must return ` [getter, setter] ` . |
168
169
| ` metadata ` | _ No_ | Custom global state on the cart. Stored inside of ` metadata ` . |
169
170
## ` useCart `
Original file line number Diff line number Diff line change @@ -174,6 +174,7 @@ export const CartProvider: React.FC<{
174
174
onItemAdd ?: ( payload : Item ) => void ;
175
175
onItemUpdate ?: ( payload : object ) => void ;
176
176
onItemRemove ?: ( id : Item [ "id" ] ) => void ;
177
+ onEmptyCart ?: ( ) => void ;
177
178
storage ?: (
178
179
key : string ,
179
180
initialValue : string
@@ -187,6 +188,7 @@ export const CartProvider: React.FC<{
187
188
onItemAdd,
188
189
onItemUpdate,
189
190
onItemRemove,
191
+ onEmptyCart,
190
192
storage = useLocalStorage ,
191
193
metadata,
192
194
} ) => {
@@ -291,10 +293,11 @@ export const CartProvider: React.FC<{
291
293
onItemRemove && onItemRemove ( id ) ;
292
294
} ;
293
295
294
- const emptyCart = ( ) =>
295
- dispatch ( {
296
- type : "EMPTY_CART" ,
297
- } ) ;
296
+ const emptyCart = ( ) => {
297
+ dispatch ( { type : "EMPTY_CART" } ) ;
298
+
299
+ onEmptyCart && onEmptyCart ( ) ;
300
+ }
298
301
299
302
const getItem = ( id : Item [ "id" ] ) =>
300
303
state . items . find ( ( i : Item ) => i . id === id ) ;
Original file line number Diff line number Diff line change @@ -406,6 +406,21 @@ describe("emptyCart", () => {
406
406
expect ( result . current . totalUniqueItems ) . toBe ( 0 ) ;
407
407
expect ( result . current . isEmpty ) . toBe ( true ) ;
408
408
} ) ;
409
+
410
+ test ( "triggers onEmptyCart when empty cart" , ( ) => {
411
+ let called = false ;
412
+
413
+ const wrapper : FC < Props > = ( { children } ) => (
414
+ < CartProvider onEmptyCart = { ( ) => ( called = true ) } >
415
+ { children }
416
+ </ CartProvider >
417
+ ) ;
418
+
419
+ const { result } = renderHook ( ( ) => useCart ( ) , { wrapper } ) ;
420
+
421
+ act ( ( ) => result . current . emptyCart ( ) ) ;
422
+ expect ( called ) . toBe ( true ) ;
423
+ } ) ;
409
424
} ) ;
410
425
411
426
describe ( "updateCartMetadata" , ( ) => {
You can’t perform that action at this time.
0 commit comments