diff --git a/frontend/src/__test__/components/OpeningMetricsTab.test.tsx b/frontend/src/__test__/components/OpeningMetricsTab.test.tsx index c7085a75..75a27d69 100644 --- a/frontend/src/__test__/components/OpeningMetricsTab.test.tsx +++ b/frontend/src/__test__/components/OpeningMetricsTab.test.tsx @@ -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()); expect(screen.getByText('Dashboard')).toBeInTheDocument(); @@ -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(); @@ -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; @@ -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; diff --git a/frontend/src/__test__/screens/Opening.test.tsx b/frontend/src/__test__/screens/Opening.test.tsx index 137b583b..5340b7c2 100644 --- a/frontend/src/__test__/screens/Opening.test.tsx +++ b/frontend/src/__test__/screens/Opening.test.tsx @@ -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", @@ -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, @@ -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( @@ -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( + + + + + + )); + }); + await act(async () => getByText('Dashboard').click()); expect(container.querySelector('div.tab-openings')?.childNodes).toHaveLength(2);