Skip to content

Commit

Permalink
chore: align tests to new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikThePendric committed Dec 4, 2024
1 parent c04146a commit d7fe6da
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,109 @@ import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import { render, screen } from '@testing-library/react'
import React from 'react'
import { Provider } from 'react-redux'
import configureMockStore from 'redux-mock-store'
import { createStore } from 'redux'
import useDimensions from '../../../../modules/useDimensions.js'
import FilterSelector from '../FilterSelector.js'

const mockStore = configureMockStore()

jest.mock('@dhis2/app-runtime', () => ({
useDhis2ConnectionStatus: jest.fn(() => ({ isDisconnected: false })),
}))

/* eslint-disable react/prop-types */
jest.mock(
'../../../../components/DropdownButton/DropdownButton.js',
() =>
function Mock({ children, ...props }) {
return (
<button className="DropdownButton" type="button" {...props}>
{children}
</button>
)
}
)
/* eslint-enable react/prop-types */

jest.mock('../../../../modules/useDimensions', () => jest.fn())
useDimensions.mockImplementation(() => ['Moomin', 'Snorkmaiden'])

const baseState = { activeModalDimension: {}, itemFilters: {} }
const createMockStore = (state) =>
createStore(() => Object.assign({}, baseState, state))

test('is disabled when offline', () => {
useDhis2ConnectionStatus.mockImplementationOnce(
jest.fn(() => ({ isDisconnected: true }))
)

const store = { activeModalDimension: {}, itemFilters: {} }

const props = {
allowedFilters: [],
restrictFilters: false,
}

const { container } = render(
<Provider store={mockStore(store)}>
const { getByTestId } = render(
<Provider store={createMockStore()}>
<FilterSelector {...props} />
</Provider>
)
expect(container).toMatchSnapshot()
expect(getByTestId('dhis2-uicore-button')).toBeDisabled()
})

test('is enabled when online', () => {
// useDhis2ConnectionStatus.mockImplementation(jest.fn(() => ({ isDisconnected: false })))

const store = { activeModalDimension: {}, itemFilters: {} }
useDhis2ConnectionStatus.mockImplementation(
jest.fn(() => ({ isDisconnected: false }))
)

const props = {
allowedFilters: [],
restrictFilters: false,
}

const { container } = render(
<Provider store={mockStore(store)}>
const { getByTestId } = render(
<Provider store={createMockStore()}>
<FilterSelector {...props} />
</Provider>
)
expect(container).toMatchSnapshot()
expect(getByTestId('dhis2-uicore-button')).toBeEnabled()
})

test('is null when no filters are restricted and no filters are allowed', () => {
const store = { activeModalDimension: {}, itemFilters: {} }

const props = {
allowedFilters: [],
restrictFilters: true,
}

const { container } = render(
<Provider store={mockStore(store)}>
<Provider store={createMockStore()}>
<FilterSelector {...props} />
</Provider>
)
expect(container.firstChild).toBeNull()
})

test('is null when no filters are restricted and allowedFilters undefined', () => {
const store = { activeModalDimension: {}, itemFilters: {} }

const props = {
restrictFilters: true,
}

const { container } = render(
<Provider store={mockStore(store)}>
<Provider store={createMockStore()}>
<FilterSelector {...props} />
</Provider>
)

expect(container.firstChild).toBeNull()
})

test('shows button when filters are restricted and at least one filter is allowed', () => {
const store = { activeModalDimension: {}, itemFilters: {} }

const props = {
allowedFilters: ['Moomin'],
restrictFilters: true,
}

render(
<Provider store={mockStore(store)}>
<Provider store={createMockStore()}>
<FilterSelector {...props} />
</Provider>
)
expect(screen.getByRole('button')).toBeTruthy()
expect(screen.getByRole('button')).toBeVisible()
})

test('shows button when filters are not restricted', () => {
const store = { activeModalDimension: {}, itemFilters: {} }

const props = {
allowedFilters: [],
restrictFilters: false,
}

render(
<Provider store={mockStore(store)}>
<Provider store={createMockStore()}>
<FilterSelector {...props} />
</Provider>
)
expect(screen.getByRole('button')).toBeTruthy()
expect(screen.getByRole('button')).toBeVisible()
})

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ test('renders a notification if no dashboards are available', () => {
</Provider>
)

expect(getByText('No dashboards available.')).toBeTruthy()
expect(getByText('Create a new dashboard using the + button.')).toBeTruthy()
expect(getByText('No dashboards available.')).toBeVisible()
expect(
getByText('Create a new dashboard using the + button.')
).toBeVisible()
})

test('renders a placeholder list item if no dashboards meet the filter criteria', () => {
Expand All @@ -81,6 +83,8 @@ test('renders a placeholder list item if no dashboards meet the filter criteria'
</Router>
</Provider>
)
expect(getByPlaceholderText('Search for a dashboard').value).toBe(filterStr)
expect(getByText('No dashboards found')).toBeTruthy()
expect(getByPlaceholderText('Search for a dashboard')).toHaveValue(
filterStr
)
expect(getByText('No dashboards found')).toBeVisible()
})
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test('renders an inactive MenuItem with a star icon, for a starred dashboard', (
)

expect(container.querySelector('.container').childNodes).toHaveLength(2)
expect(getByTestId('starred-dashboard')).toBeTruthy()
expect(getByTestId('starred-dashboard')).toBeVisible()
})

test('renders an inactive MenuItem with an offline icon for a cached dashboard', () => {
Expand All @@ -96,7 +96,7 @@ test('renders an inactive MenuItem with an offline icon for a cached dashboard',
</Provider>
)
expect(container.querySelector('.container').childNodes).toHaveLength(2)
expect(getByTestId('dashboard-saved-offline')).toBeTruthy()
expect(getByTestId('dashboard-saved-offline')).toBeVisible()
})

test('renders an active MenuItem for the currently selected dashboard', () => {
Expand All @@ -111,7 +111,7 @@ test('renders an active MenuItem for the currently selected dashboard', () => {
)

expect(container.querySelector('.container').childNodes).toHaveLength(1)
expect(getByTestId('dhis2-uicore-menuitem')).toBeTruthy()
expect(getByTestId('dhis2-uicore-menuitem')).toBeVisible()
})

test('Navigates to the related menu item when an item is clicked', () => {
Expand Down
145 changes: 0 additions & 145 deletions src/components/DashboardsBar/__tests__/DashboardsBar.spec.js

This file was deleted.

Loading

0 comments on commit d7fe6da

Please sign in to comment.