Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from ooni/rebased_tests
Week 2 tests
- Loading branch information
Showing
5 changed files
with
149 additions
and
6 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
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,74 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import React from 'react' | ||
import { screen, render, cleanup, fireEvent } from '@testing-library/react' | ||
import { theme } from 'ooni-components' | ||
import { ThemeProvider } from 'styled-components' | ||
import { IntlProvider } from 'react-intl' | ||
import English from '../../../lang/en.json' | ||
|
||
import Sidebar from '../Sidebar' | ||
import { version } from '../../../package.json' | ||
|
||
// Mocking useRouter() | ||
const mockRouterPush = jest.fn() | ||
jest.mock('next/router', () => ({ | ||
useRouter() { | ||
return { | ||
route: '/', | ||
pathname: '/dashboard', | ||
push: mockRouterPush, | ||
} | ||
}, | ||
})) | ||
|
||
const renderComponent = (component, locale = 'en', messages = English) => { | ||
return render( | ||
<IntlProvider locale={locale} messages={messages}> | ||
<ThemeProvider theme={theme}>{component}</ThemeProvider> | ||
</IntlProvider> | ||
) | ||
} | ||
|
||
describe('Tests for Sidebar component', () => { | ||
beforeEach(() => { | ||
renderComponent( | ||
<Sidebar> | ||
<></> | ||
</Sidebar> | ||
) | ||
}) | ||
|
||
afterEach(() => { | ||
cleanup() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('Displays the correct version', async () => { | ||
const versionNumber = screen.getByTestId('sidebar-version-number') | ||
expect(versionNumber.innerHTML).toMatch(version) | ||
}) | ||
|
||
test('Clicking on Dashboard NavItem calls pushes router to /dashboard', async () => { | ||
const navItemDashboard = screen.getByTestId('sidebar-item-dashboard') | ||
fireEvent.click(navItemDashboard) | ||
|
||
expect(mockRouterPush).toHaveBeenLastCalledWith('/dashboard') | ||
}) | ||
|
||
test('Clicking on Test Results NavItem calls pushes router to /test-results', async () => { | ||
const navItemTestResults = screen.getByTestId('sidebar-item-test-results') | ||
fireEvent.click(navItemTestResults) | ||
|
||
expect(mockRouterPush).toHaveBeenLastCalledWith('/test-results') | ||
}) | ||
|
||
test('Clicking on Settings NavItem calls pushes router to /settings', async () => { | ||
const navItemSettings = screen.getByTestId('sidebar-item-settings') | ||
fireEvent.click(navItemSettings) | ||
|
||
expect(mockRouterPush).toHaveBeenLastCalledWith('/settings') | ||
}) | ||
}) |
61 changes: 61 additions & 0 deletions
61
renderer/components/dashboard/__tests__/RunTestCard.test.js
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,61 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import React from 'react' | ||
import { screen, render, cleanup, fireEvent } from '@testing-library/react' | ||
import { theme } from 'ooni-components' | ||
import { ThemeProvider } from 'styled-components' | ||
import { IntlProvider } from 'react-intl' | ||
import English from '../../../../lang/en.json' | ||
import { useRouter } from 'next/router' | ||
|
||
import RunTestCard from '../RunTestCard' | ||
import { testList } from '../../nettests' | ||
|
||
// Mocking useRouter() | ||
const mockPush = jest.fn() | ||
jest.mock('next/router', () => ({ | ||
useRouter() { | ||
return { | ||
route: '/', | ||
pathname: '/dashboard', | ||
push: mockPush | ||
} | ||
}, | ||
})) | ||
|
||
const renderComponent = (component, locale = 'en', messages = English) => { | ||
return render( | ||
<IntlProvider locale={locale} messages={messages}> | ||
<ThemeProvider theme={theme}>{component}</ThemeProvider> | ||
</IntlProvider> | ||
) | ||
} | ||
|
||
describe('Tests for RunTestCard component', () => { | ||
const router = useRouter() | ||
|
||
afterEach(() => { | ||
cleanup() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('Individual Test Cards work as expected', async () => { | ||
const websiteDetails = testList[0] | ||
renderComponent( | ||
<RunTestCard | ||
onClick={() => | ||
router.push('/dashboard/[testGroup]', `/dashboard/${websiteDetails.key}`) | ||
} | ||
id={websiteDetails.key} | ||
{...websiteDetails} | ||
/> | ||
) | ||
|
||
const testCard = screen.getByTestId('card') | ||
fireEvent.click(testCard) | ||
|
||
expect(mockPush).toHaveBeenCalledWith('/dashboard/[testGroup]', `/dashboard/${websiteDetails.key}`) | ||
}) | ||
}) |
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