Skip to content

Commit

Permalink
Merge pull request #361 from SWE574-Fall2023-Group1/360-unit-test-for…
Browse files Browse the repository at this point in the history
…-story-details

Add unit test for story details #360
  • Loading branch information
aykutkantas authored Dec 28, 2023
2 parents b3d88ae + e3a00f7 commit c43acc7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions backend/frontend/src/pages/story/StoryDetails.test.js
Original file line number Diff line number Diff line change
@@ -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(<StoryDetails currentTheme='default' />);
expect(axios.get).toHaveBeenCalled();
});

it('handles API error gracefully', async () => {
axios.get.mockRejectedValue(new Error('API Error'));
render(<StoryDetails currentTheme='default' />);
});
});

0 comments on commit c43acc7

Please # to comment.