From 8fccae7c3b349d119665018f089579772226b577 Mon Sep 17 00:00:00 2001 From: abayram <38274063+abdullahbayram@users.noreply.github.com> Date: Wed, 25 Dec 2024 01:25:48 +0100 Subject: [PATCH] test: add unit test for CheckoutList organism --- .../CheckoutList/CheckoutList.test.js | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/components/organisms/CheckoutList/CheckoutList.test.js diff --git a/src/components/organisms/CheckoutList/CheckoutList.test.js b/src/components/organisms/CheckoutList/CheckoutList.test.js new file mode 100644 index 0000000..5006bc9 --- /dev/null +++ b/src/components/organisms/CheckoutList/CheckoutList.test.js @@ -0,0 +1,77 @@ +import React from 'react'; +import { fireEvent, screen } from '@testing-library/react-native'; +import strings from '@constants/strings'; +import { renderInThemeProvider } from '../../../../__tests__/utils/renderInThemeProvider'; +import CheckoutList from '.'; + +describe('', () => { + const mockOnRemoveItem = jest.fn(); + const mockOnQuantityChange = jest.fn(); + + const mockBasketItems = [ + { + id: 1, + title: 'Test Product 1', + description: 'A description for product 1', + price: 10.0, + quantity: 2, + image: 'https://example.com/image1.jpg', + rating: { rate: 4.5, count: 20 }, + }, + { + id: 2, + title: 'Test Product 2', + description: 'A description for product 2', + price: 20.0, + quantity: 1, + image: 'https://example.com/image2.jpg', + rating: { rate: 3.5, count: 10 }, + }, + ]; + + const renderCheckoutList = (props) => { + return renderInThemeProvider( + , + ); + }; + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('renders correctly with basket items', () => { + renderCheckoutList(); + + expect(screen.getByText('Test Product 1')).toBeTruthy(); + expect(screen.getByText('Test Product 2')).toBeTruthy(); + }); + + it('renders empty message when basket is empty', () => { + renderCheckoutList({ basketItems: [] }); + + expect(screen.getByText(strings.checkout.emptyBasket)).toBeTruthy(); + }); + + it('calls onRemoveItem when remove button is pressed', () => { + renderCheckoutList(); + + const removeButton = screen.getAllByText('Remove Item')[0]; + fireEvent.press(removeButton); + + expect(mockOnRemoveItem).toHaveBeenCalledWith(mockBasketItems[0]); + }); + + it('calls onQuantityChange when quantity is updated', () => { + renderCheckoutList(); + + const increaseButton = screen.getAllByTestId('increase-button')[0]; + fireEvent.press(increaseButton); + + expect(mockOnQuantityChange).toHaveBeenCalledWith(mockBasketItems[0], mockBasketItems[0].quantity + 1); + }); +});