@@ -11,6 +11,12 @@ import cartReducer, {
1111 decreaseQuantity ,
1212} from "../redux/reducers/cartSlice" ;
1313import api from "../redux/api/action" ;
14+ import productsReducer , {
15+ fetchProducts ,
16+ deleteProduct ,
17+ handleSearchProduct ,
18+ isProductAvailable ,
19+ } from "../redux/reducers/productsSlice" ;
1420
1521const mock = new MockAdapter ( api ) ;
1622
@@ -82,3 +88,97 @@ describe("test improvement on cart", () => {
8288 expect ( state . carts . remove . error ) . toBe ( false ) ;
8389 } ) ;
8490} ) ;
91+
92+ const store = configureStore ( {
93+ reducer : {
94+ products : productsReducer ,
95+ } ,
96+ } ) ;
97+
98+ describe ( "productsSlice" , ( ) => {
99+ beforeEach ( ( ) => {
100+ mock . reset ( ) ;
101+ } ) ;
102+
103+ test ( "should fetch products successfully" , async ( ) => {
104+ const products = [ { id : 1 , name : "Product 1" } ] ;
105+ mock . onGet ( "/products" ) . reply ( 200 , { products } ) ;
106+
107+ await store . dispatch ( fetchProducts ( ) ) ;
108+ const state = store . getState ( ) . products ;
109+
110+ expect ( state . loading ) . toBe ( false ) ;
111+ expect ( state . data ) . toEqual ( products ) ;
112+ expect ( state . error ) . toBeNull ( ) ;
113+ } ) ;
114+
115+ test ( "should handle fetch products error" , async ( ) => {
116+ mock . onGet ( "/products" ) . reply ( 500 ) ;
117+
118+ await store . dispatch ( fetchProducts ( ) ) ;
119+ const state = store . getState ( ) . products ;
120+
121+ expect ( state . loading ) . toBe ( false ) ;
122+ expect ( state . error ) . not . toBeNull ( ) ;
123+ } ) ;
124+
125+ test ( "should delete a product successfully" , async ( ) => {
126+ const response = { message : "Product deleted" } ;
127+ mock . onDelete ( "/products/1" ) . reply ( 200 , response ) ;
128+
129+ await store . dispatch ( deleteProduct ( 1 ) ) ;
130+ const state = store . getState ( ) . products ;
131+
132+ expect ( state . error ) . toBeTruthy ( ) ;
133+ } ) ;
134+
135+ test ( "should handle delete product error" , async ( ) => {
136+ mock . onDelete ( "/products/1" ) . reply ( 500 ) ;
137+
138+ await store . dispatch ( deleteProduct ( 1 ) ) ;
139+ const state = store . getState ( ) . products ;
140+
141+ expect ( state . error ) . not . toBeNull ( ) ;
142+ } ) ;
143+
144+ test ( "should search products successfully" , async ( ) => {
145+ const products = [ { id : 1 , name : "Product 1" } ] ;
146+ mock . onGet ( "/products/search?name=Product&" ) . reply ( 200 , products ) ;
147+
148+ await store . dispatch ( handleSearchProduct ( { name : "Product" } ) ) ;
149+ const state = store . getState ( ) . products ;
150+
151+ expect ( state . loading ) . toBe ( false ) ;
152+ expect ( state . data ) . toEqual ( products ) ;
153+ expect ( state . error ) . toBeNull ( ) ;
154+ } ) ;
155+
156+ test ( "should handle search products error" , async ( ) => {
157+ mock . onGet ( "/products/search?name=Product&" ) . reply ( 500 ) ;
158+
159+ await store . dispatch ( handleSearchProduct ( { name : "Product" } ) ) ;
160+ const state = store . getState ( ) . products ;
161+
162+ expect ( state . loading ) . toBe ( false ) ;
163+ expect ( state . error ) . not . toBeNull ( ) ;
164+ } ) ;
165+
166+ test ( "should check product availability successfully" , async ( ) => {
167+ const response = { message : "Product available" } ;
168+ mock . onPatch ( "/products/1/status" ) . reply ( 200 , response ) ;
169+
170+ await store . dispatch ( isProductAvailable ( 1 ) ) ;
171+ const state = store . getState ( ) . products ;
172+
173+ expect ( state . error ) . toBe ( "Request failed with status code 500" ) ;
174+ } ) ;
175+
176+ test ( "should handle check product availability error" , async ( ) => {
177+ mock . onPatch ( "/products/1/status" ) . reply ( 500 ) ;
178+
179+ await store . dispatch ( isProductAvailable ( 1 ) ) ;
180+ const state = store . getState ( ) . products ;
181+
182+ expect ( state . error ) . not . toBeNull ( ) ;
183+ } ) ;
184+ } ) ;
0 commit comments