-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[To Feature ] DESENG-605 - Adding Unit test #2518
Changes from 12 commits
44e47ad
4a8614f
172d250
d09d1c0
f09f7df
977bd08
9680cf1
4c0d0a0
cb2056b
7b5fa78
309236e
88f9c8a
02c33dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,127 @@ | ||||||
import React, { ReactNode } from 'react'; | ||||||
import { render, waitFor, screen } from '@testing-library/react'; | ||||||
import '@testing-library/jest-dom'; | ||||||
import * as reactRedux from 'react-redux'; | ||||||
import * as reactRouter from 'react-router'; | ||||||
import * as tenantService from 'services/tenantService'; | ||||||
import TenantDetail from '../../../../src/components/tenantManagement/Detail'; | ||||||
import { MemoryRouter, Route, Routes, useParams } from 'react-router-dom'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed this unused import :p
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||||||
import { USER_ROLES } from 'services/userService/constants'; | ||||||
|
||||||
const mockTenant = { | ||||||
id: 1, | ||||||
name: 'Tenant One', | ||||||
title: 'Title One', | ||||||
description: 'Description One', | ||||||
contact_name: 'Contact One', | ||||||
short_name: 'tenantone', | ||||||
contact_email: 'contactone@example.com', | ||||||
logo_url: 'https://example.com/logo.png', | ||||||
logo_credit: 'Photographer One', | ||||||
logo_description: 'Logo Description One', | ||||||
}; | ||||||
|
||||||
jest.mock('axios'); | ||||||
|
||||||
jest.mock('@mui/material', () => ({ | ||||||
...jest.requireActual('@mui/material'), | ||||||
Box: ({ children }: { children: ReactNode }) => <div>{children}</div>, | ||||||
Grid: ({ children }: { children: ReactNode }) => <div>{children}</div>, | ||||||
Skeleton: ({ children }: { children: ReactNode }) => <div>{children}</div>, | ||||||
})); | ||||||
|
||||||
jest.mock('components/common/Typography/', () => ({ | ||||||
Header1: ({ children }: { children: ReactNode }) => <h1>{children}</h1>, | ||||||
Header2: ({ children }: { children: ReactNode }) => <h2>{children}</h2>, | ||||||
BodyText: ({ children }: { children: ReactNode }) => <p>{children}</p>, | ||||||
})); | ||||||
|
||||||
jest.mock('components/common/Layout', () => ({ | ||||||
ResponsiveContainer: ({ children }: { children: ReactNode }) => <div>{children}</div>, | ||||||
DetailsContainer: ({ children }: { children: ReactNode }) => <div>{children}</div>, | ||||||
Detail: ({ children }: { children: ReactNode }) => <div>{children}</div>, | ||||||
})); | ||||||
|
||||||
jest.mock('react-redux', () => ({ | ||||||
...jest.requireActual('react-redux'), | ||||||
useSelector: jest.fn(() => { | ||||||
return { | ||||||
roles: [USER_ROLES.SUPER_ADMIN], | ||||||
}; | ||||||
}), | ||||||
useDispatch: jest.fn(), | ||||||
})); | ||||||
|
||||||
jest.mock('react-router-dom', () => ({ | ||||||
...jest.requireActual('react-router-dom'), | ||||||
useParams: jest.fn(() => { | ||||||
return { tenantId: mockTenant.short_name }; | ||||||
}), | ||||||
useNavigate: jest.fn(), | ||||||
})); | ||||||
|
||||||
jest.mock('services/tenantService', () => ({ | ||||||
getTenant: jest.fn(), | ||||||
deleteTenant: jest.fn(), | ||||||
})); | ||||||
|
||||||
jest.mock('services/notificationService/notificationSlice', () => ({ | ||||||
openNotification: jest.fn(), | ||||||
})); | ||||||
|
||||||
jest.mock('services/notificationModalService/notificationModalSlice', () => ({ | ||||||
openNotificationModal: jest.fn(), | ||||||
})); | ||||||
|
||||||
// Mocking BreadcrumbTrail component | ||||||
jest.mock('components/common/Navigation/Breadcrumb', () => ({ | ||||||
BreadcrumbTrail: ({ children }: { children: ReactNode }) => <div>{children}</div>, | ||||||
})); | ||||||
|
||||||
describe('Tenant Detail Page tests', () => { | ||||||
const dispatch = jest.fn(); | ||||||
const navigate = jest.fn(); | ||||||
|
||||||
beforeEach(() => { | ||||||
jest.clearAllMocks(); | ||||||
jest.spyOn(reactRedux, 'useDispatch').mockReturnValue(dispatch); | ||||||
jest.spyOn(reactRouter, 'useNavigate').mockReturnValue(navigate); | ||||||
jest.spyOn(tenantService, 'getTenant').mockResolvedValue(mockTenant); | ||||||
}); | ||||||
|
||||||
test('Tenant detail is rendered', async () => { | ||||||
render( | ||||||
<MemoryRouter initialEntries={['/tenantadmin/1/detail']}> | ||||||
<Routes> | ||||||
<Route path="/tenantadmin/:tenantId/detail" element={<TenantDetail />} /> | ||||||
</Routes> | ||||||
</MemoryRouter>, | ||||||
); | ||||||
|
||||||
await waitFor(() => { | ||||||
const tenantNames = screen.getAllByText('Tenant One'); | ||||||
expect(tenantNames).toHaveLength(2); | ||||||
expect(screen.getByText('Title One')).toBeVisible(); | ||||||
expect(screen.getByText('Description One')).toBeVisible(); | ||||||
expect(screen.getByText('Contact One')).toBeVisible(); | ||||||
expect(screen.getByText('contactone@example.com')).toBeVisible(); | ||||||
expect(screen.getByText('Photographer One')).toBeVisible(); | ||||||
expect(screen.getByText('Logo Description One')).toBeVisible(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I appreciate how thorough these tests are :) |
||||||
}); | ||||||
}); | ||||||
|
||||||
test('Loading state is rendered initially', () => { | ||||||
jest.spyOn(tenantService, 'getTenant').mockReturnValue(new Promise(() => {})); // Mock unresolved promise | ||||||
|
||||||
render( | ||||||
<MemoryRouter initialEntries={['/tenantadmin/1/detail']}> | ||||||
<Routes> | ||||||
<Route path="/tenantadmin/:tenantId/detail" element={<TenantDetail />} /> | ||||||
</Routes> | ||||||
</MemoryRouter>, | ||||||
); | ||||||
|
||||||
const loadingTexts = screen.getAllByText('Loading...'); | ||||||
expect(loadingTexts.length).toBeGreaterThan(0); // Adjust based on expected occurrences | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this left in intentionally? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||||||
}); | ||||||
}); |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||
import React, { ReactNode } from 'react'; | ||||
import { render, waitFor, screen } from '@testing-library/react'; | ||||
import '@testing-library/jest-dom'; | ||||
import { setupEnv } from '../setEnvVars'; | ||||
import * as reactRedux from 'react-redux'; | ||||
import * as reactRouter from 'react-router'; | ||||
import * as tenantService from 'services/tenantService'; | ||||
import { draftEngagement } from '../factory'; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is unused too 🙃
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||||
import TenantListingPage from '../../../../src/components/tenantManagement/Listing'; | ||||
import { USER_ROLES } from 'services/userService/constants'; | ||||
import { MemoryRouter } from 'react-router-dom'; | ||||
|
||||
const mockTenantOne = { | ||||
id: 1, | ||||
name: 'Tenant One', | ||||
title: 'Title One', | ||||
description: 'Description One', | ||||
contact_name: 'Contact One', | ||||
short_name: 'tenantone', | ||||
}; | ||||
|
||||
const mockTenantTwo = { | ||||
id: 2, | ||||
name: 'Tenant Two', | ||||
title: 'Title Two', | ||||
description: 'Description Two', | ||||
contact_name: 'Contact Two', | ||||
short_name: 'tenanttwo', | ||||
}; | ||||
|
||||
jest.mock('axios'); | ||||
|
||||
jest.mock('@mui/material', () => ({ | ||||
...jest.requireActual('@mui/material'), | ||||
Link: ({ children }: { children: ReactNode }) => { | ||||
return <a>{children}</a>; | ||||
}, | ||||
})); | ||||
|
||||
jest.mock('components/common', () => ({ | ||||
...jest.requireActual('components/common'), | ||||
PrimaryButtonOld: ({ children, onClick }: { children: ReactNode; onClick: () => void }) => { | ||||
return <button onClick={onClick}>{children}</button>; | ||||
}, | ||||
})); | ||||
|
||||
jest.mock('react-redux', () => ({ | ||||
...jest.requireActual('react-redux'), | ||||
useSelector: jest.fn(() => { | ||||
return { | ||||
roles: [USER_ROLES.SUPER_ADMIN], | ||||
}; | ||||
}), | ||||
})); | ||||
|
||||
jest.mock('react-router-dom', () => ({ | ||||
...jest.requireActual('react-router-dom'), | ||||
useNavigate: jest.fn(), | ||||
useLocation: jest.fn(() => ({ | ||||
search: '', | ||||
})), | ||||
})); | ||||
|
||||
describe('Tenant Listing Page tests', () => { | ||||
jest.spyOn(reactRedux, 'useSelector').mockImplementation(() => jest.fn()); | ||||
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn()); | ||||
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn()); | ||||
jest.spyOn(tenantService, 'getAllTenants').mockReturnValue(Promise.resolve([mockTenantOne, mockTenantTwo])); | ||||
|
||||
beforeEach(() => { | ||||
setupEnv(); | ||||
}); | ||||
|
||||
test('Tenant table is rendered', async () => { | ||||
render( | ||||
<MemoryRouter> | ||||
<TenantListingPage /> | ||||
</MemoryRouter>, | ||||
); | ||||
|
||||
await waitFor(() => { | ||||
expect(screen.getByText('Tenant One')).toBeVisible(); | ||||
expect(screen.getByText('Tenant Two')).toBeVisible(); | ||||
expect(screen.getByText('Description One')).toBeVisible(); | ||||
expect(screen.getByText('Description Two')).toBeVisible(); | ||||
|
||||
expect(screen.getByText('Add Instance')).toBeVisible(); | ||||
}); | ||||
}); | ||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were these tests timing out when running locally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sometimes old tests also timing out locally