-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maryia/WEBREL-2578/test: coverage for routes & routes-config in Repor…
…ts (#14973) * test: routes.tsx in reports * test: routes-config * refactor: test for routes-config * fix: remove extra async expect * refactor: packages/reports/src/Constants/__tests__/routes-config.spec.tsx
- Loading branch information
1 parent
53fe484
commit a97e038
Showing
5 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/reports/src/Constants/__tests__/routes-config.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import React from 'react'; | ||
import { RouteComponentProps, Router } from 'react-router'; | ||
import { createMemoryHistory } from 'history'; | ||
import { mockStore } from '@deriv/stores'; | ||
import { routes } from '@deriv/shared'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import BinaryRoutes from '../../Components/Routes'; | ||
import ReportsProviders from '../../reports-providers'; | ||
import getRoutesConfig from '../routes-config'; | ||
|
||
const mockedError404 = 'Error 404'; | ||
const mockedOpenPositions = 'Open positions'; | ||
const mockedStatement = 'Statement'; | ||
const mockedTradeTable = 'Trade table'; | ||
const reportsPageTitle = 'Reports'; | ||
|
||
jest.mock('Modules/Page404', () => ({ | ||
__esModule: true, | ||
default: () => <div>{mockedError404}</div>, | ||
})); | ||
|
||
jest.mock('../../Containers', () => ({ | ||
__esModule: true, | ||
default: { | ||
...jest.requireActual('../../Containers').default, | ||
OpenPositions: () => <div>{mockedOpenPositions}</div>, | ||
ProfitTable: () => <div>{mockedTradeTable}</div>, | ||
Statement: () => <div>{mockedStatement}</div>, | ||
}, | ||
})); | ||
|
||
describe('Routes Config', () => { | ||
// we need to render BinaryRoutes component that uses routes-config to test that lazy-loaded components are loaded | ||
const MockBinaryRoutes = ({ history }: Partial<RouteComponentProps>) => ( | ||
<ReportsProviders store={mockStore({ client: { is_logged_in: true }, ui: { is_reports_visible: true } })}> | ||
<Router history={history}> | ||
<BinaryRoutes is_logged_in /> | ||
</Router> | ||
</ReportsProviders> | ||
); | ||
|
||
it('should return default routes config', () => { | ||
const routesConfig = getRoutesConfig(); | ||
expect(routesConfig).toHaveLength(2); | ||
}); | ||
it('should return routes with Reports / Open positions route', async () => { | ||
const routesConfig = getRoutesConfig(); | ||
expect(routesConfig?.[0]?.path).toBe(routes.reports); | ||
expect(routesConfig?.[0]?.getTitle?.()).toBe(reportsPageTitle); | ||
expect(routesConfig?.[0]?.is_authenticated).toBe(true); | ||
expect(routesConfig?.[0].routes?.[0].path).toBe(routes.positions); | ||
const history = createMemoryHistory(); | ||
history.push(routes.positions); | ||
render(<MockBinaryRoutes history={history} />); | ||
await waitFor(() => { | ||
expect(screen.getByText(reportsPageTitle)).toBeInTheDocument(); | ||
expect(screen.getByText(mockedOpenPositions)).toBeInTheDocument(); | ||
}); | ||
}); | ||
it('should return routes with Reports / Trade table route', async () => { | ||
const routesConfig = getRoutesConfig(); | ||
expect(routesConfig?.[0].routes?.[1].path).toBe(routes.profit); | ||
expect(routesConfig?.[0].routes?.[1].getTitle()).toBe(mockedTradeTable); | ||
const history = createMemoryHistory(); | ||
history.push(routes.profit); | ||
render(<MockBinaryRoutes history={history} />); | ||
await waitFor(() => { | ||
expect(screen.getByText(reportsPageTitle)).toBeInTheDocument(); | ||
expect(screen.getByText(mockedTradeTable)).toBeInTheDocument(); | ||
}); | ||
}); | ||
it('should return routes with Reports / Statement route', async () => { | ||
const routesConfig = getRoutesConfig(); | ||
expect(routesConfig?.[0].routes?.[2].path).toBe(routes.statement); | ||
expect(routesConfig?.[0].routes?.[2].getTitle()).toBe(mockedStatement); | ||
const history = createMemoryHistory(); | ||
history.push(routes.statement); | ||
render(<MockBinaryRoutes history={history} />); | ||
await waitFor(() => { | ||
expect(screen.getByText(reportsPageTitle)).toBeInTheDocument(); | ||
expect(screen.getByText(mockedStatement)).toBeInTheDocument(); | ||
}); | ||
}); | ||
it('should return routes with route for Page 404 which loads when the path does not exist', async () => { | ||
const routesConfig = getRoutesConfig(); | ||
expect(routesConfig?.[1]?.getTitle?.()).toBe(mockedError404); | ||
const history = createMemoryHistory(); | ||
history.push('/non-existent-path'); | ||
render(<MockBinaryRoutes history={history} />); | ||
await waitFor(() => { | ||
expect(screen.getByText(mockedError404)).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { createMemoryHistory } from 'history'; | ||
import { mockStore } from '@deriv/stores'; | ||
import { Router } from 'react-router-dom'; | ||
import { TCoreStores } from '@deriv/stores/types'; | ||
import BinaryRoutes from '../../Components/Routes'; | ||
import Routes from '../routes'; | ||
import ReportsProviders from '../../reports-providers'; | ||
|
||
const mockedBinaryRoutes = 'BinaryRoutes'; | ||
const mockedErrorComponent = 'ErrorComponent'; | ||
|
||
jest.mock('../../../src/Components/Routes', () => jest.fn(() => mockedBinaryRoutes)); | ||
jest.mock('../../../src/Components/Errors', () => jest.fn(() => mockedErrorComponent)); | ||
|
||
describe('Routes', () => { | ||
const history = createMemoryHistory(); | ||
const store = mockStore({}); | ||
|
||
const renderMockedRoutes = ( | ||
mockedStore: TCoreStores = store, | ||
passthrough?: React.ComponentProps<typeof BinaryRoutes>['passthrough'] | ||
) => { | ||
return render( | ||
<ReportsProviders store={mockedStore}> | ||
<Router history={history}> | ||
<Routes passthrough={passthrough} /> | ||
</Router> | ||
</ReportsProviders> | ||
); | ||
}; | ||
|
||
it('should render BinaryRoutes', () => { | ||
renderMockedRoutes(); | ||
expect(screen.getByText(mockedBinaryRoutes)).toBeInTheDocument(); | ||
}); | ||
it('should render ErrorComponent', () => { | ||
renderMockedRoutes(mockStore({ common: { has_error: true } })); | ||
expect(screen.queryByText(mockedBinaryRoutes)).not.toBeInTheDocument(); | ||
expect(screen.getByText(mockedErrorComponent)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters