Skip to content

Commit

Permalink
chore: upgrade UI and jest snapshots (#2096)
Browse files Browse the repository at this point in the history
Changes to jest snapshots to reduce the amount of changes to snapshots caused by ui upgrades.
  • Loading branch information
jenniferarnesen authored Aug 3, 2022
1 parent 6742e70 commit 5051030
Show file tree
Hide file tree
Showing 36 changed files with 1,574 additions and 3,370 deletions.
1 change: 1 addition & 0 deletions .d2styleignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/src/locales/*
/cypress/assets
/cypress/fixtures
cypress.env.json
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"license": "BSD-3-Clause",
"dependencies": {
"@dhis2/analytics": "^20.4.10",
"@dhis2/analytics": "^23.10.3",
"@dhis2/app-runtime": "^2.12.3",
"@dhis2/app-runtime-adapter-d2": "^1.1.0",
"@dhis2/d2-i18n": "^1.1.0",
Expand All @@ -15,7 +15,7 @@
"@dhis2/d2-ui-rich-text": "^7.3.3",
"@dhis2/d2-ui-translation-dialog": "^7.3.3",
"@dhis2/data-visualizer-plugin": "^38.0.16",
"@dhis2/ui": "^6.25.0",
"@dhis2/ui": "^8.4.0",
"classnames": "^2.3.1",
"d2": "^31.10.0",
"d2-utilizr": "^0.2.16",
Expand All @@ -31,7 +31,7 @@
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0",
"styled-jsx": "3.3.2"
"styled-jsx": "^4.0.1"
},
"scripts": {
"start": "d2-app-scripts start",
Expand Down
28 changes: 22 additions & 6 deletions src/__tests__/AppWrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@ import React from 'react'
import ReactDOM from 'react-dom'
import AppWrapper from '../AppWrapper.js'

jest.mock('@dhis2/app-runtime-adapter-d2', () => {
return {
D2Shim: ({ children }) => children({ d2: {} }),
}
})

jest.mock(
'../components/App',
() =>
function MockApp() {
return <div />
}
)
jest.mock('@dhis2/app-runtime-adapter-d2', () => {
return {
D2Shim: ({ children }) => children({ d2: {} }),
}
})

it('renders without crashing', () => {
jest.mock(
'../components/SystemSettingsProvider.js',
() =>
function Mock() {
return <div className="SystemSettingsProvider" />
}
)
jest.mock(
'../components/UserSettingsProvider.js',
() =>
function Mock() {
return <div className="UserSettingsProvider" />
}
)

test('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<AppWrapper />, div)
ReactDOM.unmountComponentAtNode(div)
Expand Down
4 changes: 3 additions & 1 deletion src/components/DashboardsBar/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const Chip = ({ starred, selected, label, dashboardId, onClick }) => {
className={classes.link}
to={`/${dashboardId}`}
onClick={handleClick}
data-test="dashboard-chip"
data-testid={`dashboard-chip${selected ? '-selected' : ''}${
starred ? '-starred' : ''
}`}
>
<UiChip {...chipProps}>
<span
Expand Down
22 changes: 20 additions & 2 deletions src/components/DashboardsBar/__tests__/Chip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ import React from 'react'
import { Router } from 'react-router-dom'
import Chip from '../Chip.js'

/* eslint-disable react/prop-types */
jest.mock('@dhis2/ui', () => {
const originalModule = jest.requireActual('@dhis2/ui')

return {
__esModule: true,
...originalModule,
Chip: function Mock({ children, ...props }) {
return (
<div className="ui-Chip" {...props}>
{children}
</div>
)
},
}
})
/* eslint-enable react/prop-types */

jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: () => ({ online: true }),
useCacheableSection: jest.fn(),
Expand Down Expand Up @@ -42,7 +60,7 @@ const defaultProps = {
},
}

test('renders an unstarred chip for a non-offline dashboard', () => {
test('renders an unstarred chip for a non-cached dashboard', () => {
useCacheableSection.mockImplementation(() => mockNonOfflineDashboard)
const { container } = render(
<Router history={createMemoryHistory()}>
Expand All @@ -53,7 +71,7 @@ test('renders an unstarred chip for a non-offline dashboard', () => {
expect(container).toMatchSnapshot()
})

test('renders an unstarred chip for an offline dashboard', () => {
test('renders an unstarred chip for cached dashboard', () => {
useCacheableSection.mockImplementation(() => mockOfflineDashboard)
const { container } = render(
<Router history={createMemoryHistory()}>
Expand Down
116 changes: 22 additions & 94 deletions src/components/DashboardsBar/__tests__/DashboardsBar.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fireEvent } from '@testing-library/dom'
import { within } from '@testing-library/dom'
import { render } from '@testing-library/react'
import { createMemoryHistory } from 'history'
import React from 'react'
Expand All @@ -11,9 +11,6 @@ import DashboardsBar, {
MAX_ROW_COUNT,
} from '../DashboardsBar.js'

// TODO this spy is an implementation detail
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cb())

jest.mock('@dhis2/app-runtime-adapter-d2', () => ({
useD2: () => ({
d2: {
Expand Down Expand Up @@ -47,14 +44,14 @@ jest.mock('@dhis2/app-runtime', () => ({
})),
}))

test('renders a DashboardsBar with minimum height', () => {
test('minimized DashboardsBar has Show more/less button', () => {
const store = {
dashboards,
dashboardsFilter: '',
controlBar: { userRows: parseInt(MIN_ROW_COUNT) },
selected: { id: 'rainbow123' },
}
const { container } = render(
const { queryAllByRole, queryByLabelText } = render(
<Provider store={mockStore(store)}>
<WindowDimensionsProvider>
<Router history={createMemoryHistory()}>
Expand All @@ -63,71 +60,19 @@ test('renders a DashboardsBar with minimum height', () => {
</WindowDimensionsProvider>
</Provider>
)
expect(container).toMatchSnapshot()
})

test('small screen: renders a DashboardsBar with minimum height', () => {
global.innerWidth = 480
global.innerHeight = 400

const store = {
dashboards,
dashboardsFilter: '',
controlBar: { userRows: 3 },
selected: { id: 'rainbow123' },
}
const { container } = render(
<Provider store={mockStore(store)}>
<WindowDimensionsProvider>
<Router history={createMemoryHistory()}>
<DashboardsBar />
</Router>
</WindowDimensionsProvider>
</Provider>
)
expect(container).toMatchSnapshot()
global.innerWidth = 800
global.innerHeight = 600
})

test('small screen: clicking "Show more" maximizes dashboards bar height', () => {
global.innerWidth = 480
global.innerHeight = 400
const store = {
dashboards,
dashboardsFilter: '',
controlBar: { userRows: 3 },
selected: { id: 'fluttershy123' },
}
const mockExpandedChanged = jest.fn()
const { getByLabelText, asFragment } = render(
<Provider store={mockStore(store)}>
<WindowDimensionsProvider>
<Router history={createMemoryHistory()}>
<DashboardsBar
expanded={false}
onExpandedChanged={mockExpandedChanged}
/>
</Router>
</WindowDimensionsProvider>
</Provider>
)

fireEvent.click(getByLabelText('Show more dashboards'))
expect(asFragment()).toMatchSnapshot()
expect(mockExpandedChanged).toBeCalledWith(true)
global.innerWidth = 800
global.innerHeight = 600
const links = queryAllByRole('link')
expect(links.length).toEqual(Object.keys(dashboards).length)
expect(queryByLabelText('Show more dashboards')).toBeTruthy()
})

test('renders a DashboardsBar with maximum height', () => {
test('maximized DashboardsBar does not have a Show more/less button', () => {
const store = {
dashboards,
dashboardsFilter: '',
controlBar: { userRows: parseInt(MAX_ROW_COUNT) },
selected: { id: 'rainbow123' },
}
const { container } = render(
const { queryByTestId } = render(
<Provider store={mockStore(store)}>
<WindowDimensionsProvider>
<Router history={createMemoryHistory()}>
Expand All @@ -139,7 +84,7 @@ test('renders a DashboardsBar with maximum height', () => {
</WindowDimensionsProvider>
</Provider>
)
expect(container).toMatchSnapshot()
expect(queryByTestId('showmore-button')).toBeNull()
})

test('renders a DashboardsBar with selected item', () => {
Expand All @@ -150,7 +95,7 @@ test('renders a DashboardsBar with selected item', () => {
selected: { id: 'fluttershy123' },
}

const { container } = render(
const { queryByTestId } = render(
<WindowDimensionsProvider>
<Router history={createMemoryHistory()}>
<Provider store={mockStore(store)}>
Expand All @@ -162,7 +107,16 @@ test('renders a DashboardsBar with selected item', () => {
</Router>
</WindowDimensionsProvider>
)
expect(container).toMatchSnapshot()
expect(
within(queryByTestId('dashboard-chip-selected-starred')).queryByText(
'Fluttershy'
)
).toBeTruthy()
expect(
within(queryByTestId('dashboard-chip-selected-starred')).queryByText(
'Rainbow Dash'
)
).toBeNull()
})

test('renders a DashboardsBar with no items', () => {
Expand All @@ -173,7 +127,7 @@ test('renders a DashboardsBar with no items', () => {
selected: { id: 'rainbow123' },
}

const { container } = render(
const { queryByRole } = render(
<Provider store={mockStore(store)}>
<WindowDimensionsProvider>
<Router history={createMemoryHistory()}>
Expand All @@ -185,31 +139,5 @@ test('renders a DashboardsBar with no items', () => {
</WindowDimensionsProvider>
</Provider>
)
expect(container).toMatchSnapshot()
})

test('clicking "Show more" maximizes dashboards bar height', () => {
const store = {
dashboards,
dashboardsFilter: '',
controlBar: { userRows: parseInt(MIN_ROW_COUNT) },
selected: { id: 'fluttershy123' },
}
const mockOnExpandedChanged = jest.fn()
const { getByLabelText, asFragment } = render(
<Provider store={mockStore(store)}>
<WindowDimensionsProvider>
<Router history={createMemoryHistory()}>
<DashboardsBar
expanded={false}
onExpandedChanged={mockOnExpandedChanged}
/>
</Router>
</WindowDimensionsProvider>
</Provider>
)

fireEvent.click(getByLabelText('Show more dashboards'))
expect(mockOnExpandedChanged).toBeCalledWith(true)
expect(asFragment()).toMatchSnapshot()
expect(queryByRole('link')).toBeNull()
})
Loading

0 comments on commit 5051030

Please sign in to comment.