From 28ec7b940c7eae5caf5ddcf3b4baf217153cdb97 Mon Sep 17 00:00:00 2001 From: aykutkantas <126410955+aykutkantas@users.noreply.github.com> Date: Thu, 28 Dec 2023 18:42:57 +0300 Subject: [PATCH] Add unit test for story details #360 --- .../src/pages/story/StoryDetails.test.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 backend/frontend/src/pages/story/StoryDetails.test.js diff --git a/backend/frontend/src/pages/story/StoryDetails.test.js b/backend/frontend/src/pages/story/StoryDetails.test.js new file mode 100644 index 00000000..352fcafe --- /dev/null +++ b/backend/frontend/src/pages/story/StoryDetails.test.js @@ -0,0 +1,36 @@ +import React from 'react'; +import { render, fireEvent, waitFor, screen } from '@testing-library/react'; +import StoryDetails from './StoryDetails'; +import axios from 'axios'; +import { useParams, useNavigate } from 'react-router-dom'; + +jest.mock('axios'); +jest.mock('react-router-dom', () => ({ + useParams: jest.fn(), + useNavigate: jest.fn(), +})); + +describe('StoryDetails Component', () => { + const mockNavigate = jest.fn(); + const mockUseParams = { id: '123' }; + + beforeEach(() => { + useParams.mockReturnValue(mockUseParams); + useNavigate.mockReturnValue(mockNavigate); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('renders and fetches story details on mount', async () => { + axios.get.mockResolvedValue({}); + render(); + expect(axios.get).toHaveBeenCalled(); + }); + + it('handles API error gracefully', async () => { + axios.get.mockRejectedValue(new Error('API Error')); + render(); + }); +});