Skip to content

Commit

Permalink
test: fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Nov 6, 2024
1 parent b85a826 commit 61afaa3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 51 deletions.
43 changes: 35 additions & 8 deletions frontend/src/__test__/components/OpeningMetricsTab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,49 @@ import React from 'react';
import { render, act, waitFor, fireEvent, screen } from '@testing-library/react';
import OpeningMetricsTab from '../../components/OpeningMetricsTab';
import { fetchOpeningTrends } from '../../services/OpeningFavoriteService';
import { fetchFreeGrowingMilestones, fetchOpeningsPerYear, fetchRecentOpenings } from '../../services/OpeningService';

vi.mock('../../services/OpeningFavoriteService', () => ({
fetchOpeningTrends: vi.fn(),
}));
vi.mock('../../services/OpeningService', async () => {
const actual = await vi.importActual('../../services/OpeningService');
return {
...actual,
fetchRecentOpenings: vi.fn(),
fetchOpeningsPerYear: vi.fn(),
fetchFreeGrowingMilestones: vi.fn(),
};
});

describe('OpeningMetricsTab', () => {
beforeEach(() => {
vi.clearAllMocks();
(fetchRecentOpenings as vi.Mock).mockResolvedValue([{
id: '123',
openingId: '123',
fileId: '1',
cuttingPermit: '1',
timberMark: '1',
cutBlock: '1',
grossAreaHa: 1,
statusDesc: 'Approved',
categoryDesc: 'Another:Another',
disturbanceStart: '1',
entryTimestamp: '1',
updateTimestamp: '1',
}]);
(fetchOpeningsPerYear as vi.Mock).mockResolvedValue([
{ group: '2022', key: 'Openings', value: 10 },
{ group: '2023', key: 'Openings', value: 15 },
]);
(fetchFreeGrowingMilestones as vi.Mock).mockResolvedValue([{ group: '1-5', value: 11 }]);
(fetchOpeningTrends as vi.Mock).mockResolvedValue([1, 2, 3]);

});

it('should render the OpeningMetricsTab component with all sections', async () => {
const mockTrends = [1, 2, 3];
(fetchOpeningTrends as vi.Mock).mockResolvedValue(mockTrends);

await act(async () => render(<OpeningMetricsTab />));

expect(screen.getByText('Dashboard')).toBeInTheDocument();
Expand All @@ -31,8 +61,7 @@ describe('OpeningMetricsTab', () => {
});

it('should call fetchOpeningTrends and set submissionTrends state', async () => {
const mockTrends = [1, 2, 3];
(fetchOpeningTrends as vi.Mock).mockResolvedValue(mockTrends);


await act(async () => {
render(<OpeningMetricsTab />);
Expand All @@ -47,8 +76,7 @@ describe('OpeningMetricsTab', () => {
});

it('should scroll to "Track Openings" section when scrollTo parameter is "trackOpenings"', async () => {
const mockTrends = [1, 2, 3];
(fetchOpeningTrends as vi.Mock).mockResolvedValue(mockTrends);

const mockScrollIntoView = vi.fn();
window.HTMLElement.prototype.scrollIntoView = mockScrollIntoView;

Expand All @@ -64,8 +92,7 @@ describe('OpeningMetricsTab', () => {
});

it('should not scroll to "Track Openings" section when scrollTo parameter is not "trackOpenings"', async () => {
const mockTrends = [1, 2, 3];
(fetchOpeningTrends as vi.Mock).mockResolvedValue(mockTrends);

const mockScrollIntoView = vi.fn();
window.HTMLElement.prototype.scrollIntoView = mockScrollIntoView;

Expand Down
93 changes: 50 additions & 43 deletions frontend/src/__test__/screens/Opening.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import PaginationContext from '../../contexts/PaginationContext';
import { BrowserRouter } from 'react-router-dom';
import * as redux from 'react-redux';
import { RecentOpening } from '../../types/RecentOpening';
import { getWmsLayersWhitelistUsers } from '../../services/SecretsService';
import { fetchFreeGrowingMilestones, fetchOpeningsPerYear, fetchRecentOpenings } from '../../services/OpeningService';
import { fetchOpeningTrends } from '../../services/OpeningFavoriteService';

const data = {
"activityType": "Update",
Expand All @@ -16,53 +19,24 @@ const data = {
"lastUpdated": "2024-05-16T19:59:21.635Z"
};

vi.mock('../../services/SecretsService', () => ({
getWmsLayersWhitelistUsers: vi.fn(() => [
{
userName: 'TEST'
}
])
vi.mock('../../services/OpeningFavoriteService', () => ({
fetchOpeningTrends: vi.fn(),
}));

vi.mock('../../services/OpeningService', () => ({
fetchRecentOpenings: vi.fn(() => [
{
id: '123',
openingId: '111',
fileId: 'FS7',
cuttingPermit: 'SS',
timberMark: '207S',
cutBlock: '111',
grossAreaHa: 265,
statusDesc: 'Approved',
categoryDesc: 'FTML',
disturbanceStart: '2023-01-02',
entryTimestamp: '',
updateTimestamp: ''
}
]),
fetchOpeningsPerYear: vi.fn(() => Promise.resolve([
{ group: '2022', key: 'Openings', value: 10 },
{ group: '2023', key: 'Openings', value: 15 },
])),
fetchFreeGrowingMilestones: vi.fn(() => Promise.resolve([
{
group: '1-5',
value: 11
}
])),
fetchRecentActions: vi.fn(() => [
{
activityType: data.activityType,
openingId: data.openingId.toString(),
statusCode: data.statusCode,
statusDescription: data.statusDescription,
lastUpdated: data.lastUpdated,
lastUpdatedLabel: data.lastUpdatedLabel
}
]),
vi.mock('../../services/SecretsService', () => ({
getWmsLayersWhitelistUsers: vi.fn()
}));

vi.mock('../../services/OpeningService', async () => {
const actual = await vi.importActual('../../services/OpeningService');
return {
...actual,
fetchRecentOpenings: vi.fn(),
fetchOpeningsPerYear: vi.fn(),
fetchFreeGrowingMilestones: vi.fn(),
};
});

const state = {
userDetails: {
id: 1,
Expand Down Expand Up @@ -100,6 +74,24 @@ const paginationValueMock = {
};

describe('Opening screen test cases', () => {

beforeEach(() => {
vi.clearAllMocks();

(getWmsLayersWhitelistUsers as vi.Mock).mockResolvedValue([{userName: 'TEST'}]);
(fetchRecentOpenings as vi.Mock).mockResolvedValue(rows);
(fetchOpeningsPerYear as vi.Mock).mockResolvedValue([
{ group: '2022', key: 'Openings', value: 10 },
{ group: '2023', key: 'Openings', value: 15 },
]);
(fetchFreeGrowingMilestones as vi.Mock).mockResolvedValue([{ group: '1-5', value: 11 }]);
(fetchOpeningTrends as vi.Mock).mockResolvedValue([1,2,3]);




});

it('should renders Opening Page Title component', async () => {
const { getByTestId } = render(
<BrowserRouter>
Expand Down Expand Up @@ -170,6 +162,21 @@ describe('Opening screen test cases', () => {
expect(container.querySelector('div.tab-openings')?.childNodes).toHaveLength(2);
expect(container.querySelector('div.tab-metrics')?.childNodes).toHaveLength(0);

});

it.only('should render tab only when selected', async () => {
let container: HTMLElement = document.createElement('div');
let getByText: any;
await act(async () => {
({ container, getByText } = render(
<BrowserRouter>
<PaginationContext.Provider value={paginationValueMock}>
<Opening />
</PaginationContext.Provider>
</BrowserRouter>
));
});

await act(async () => getByText('Dashboard').click());

expect(container.querySelector('div.tab-openings')?.childNodes).toHaveLength(2);
Expand Down

0 comments on commit 61afaa3

Please sign in to comment.