-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor RenderingStrategy and add test
- Loading branch information
1 parent
e744748
commit 25d9700
Showing
2 changed files
with
169 additions
and
117 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
88 changes: 88 additions & 0 deletions
88
src/components/periods/__tests__/RenderingStrategy.spec.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,88 @@ | ||
import { mount } from 'enzyme' | ||
import React from 'react' | ||
import { Provider } from 'react-redux' | ||
import configureMockStore from 'redux-mock-store' | ||
import RenderingStrategy from '../RenderingStrategy' | ||
import { | ||
RENDERING_STRATEGY_SINGLE, | ||
RENDERING_STRATEGY_TIMELINE, | ||
RENDERING_STRATEGY_SPLIT_BY_PERIOD, | ||
} from '../../../constants/layers' | ||
import { countPeriods } from '../../../util/periods' | ||
|
||
const mockStore = configureMockStore() | ||
|
||
const store = mockStore({ | ||
map: { | ||
mapViews: [{ id: 'layer1', renderingStrategy: 'SINGLE' }], | ||
}, | ||
layerEdit: { | ||
filters: [], | ||
}, | ||
}) | ||
|
||
jest.mock('../../../util/periods', () => ({ | ||
countPeriods: jest.fn(), | ||
})) | ||
|
||
describe('RenderingStrategy', () => { | ||
const renderWithProps = (props) => | ||
mount( | ||
<Provider store={store}> | ||
<RenderingStrategy {...props} /> | ||
</Provider> | ||
) | ||
|
||
const layerId = 'layer1' | ||
const value = RENDERING_STRATEGY_SPLIT_BY_PERIOD | ||
const periods = [] | ||
const mockOnChange = jest.fn() | ||
let props | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
props = { | ||
layerId, | ||
value, | ||
periods, | ||
onChange: mockOnChange, | ||
} | ||
}) | ||
|
||
it('renders all radio buttons with correct labels', () => { | ||
countPeriods.mockReturnValue(5) | ||
const wrapper = renderWithProps(props) | ||
expect(wrapper.find('input').length).toBe(3) // Three radio buttons | ||
expect(wrapper.find('label').at(0).text()).toBe('Period display mode') | ||
expect(wrapper.find('label').at(1).text()).toBe( | ||
'Single (combine periods)' | ||
) | ||
expect(wrapper.find('label').at(2).text()).toBe('Timeline') | ||
expect(wrapper.find('label').at(3).text()).toBe('Split map views') | ||
}) | ||
|
||
it('disables timeline and split map views when total periods are below the minimum', () => { | ||
countPeriods.mockReturnValue(1) | ||
const wrapper = renderWithProps(props) | ||
expect(wrapper.find('input').at(1).prop('disabled')).toBeDefined() // Timeline should be disabled | ||
expect(wrapper.find('input').at(2).prop('disabled')).toBeDefined() | ||
}) | ||
|
||
it('calls onChange with correct value when a radio button is clicked', () => { | ||
countPeriods.mockReturnValue(5) | ||
const wrapper = renderWithProps(props) | ||
wrapper | ||
.find('input') | ||
.at(1) | ||
.simulate('change', { | ||
target: { value: RENDERING_STRATEGY_TIMELINE }, | ||
}) | ||
expect(mockOnChange).toHaveBeenCalledWith(RENDERING_STRATEGY_TIMELINE) | ||
}) | ||
|
||
it('automatically switches to SINGLE when conditions are not met', () => { | ||
countPeriods.mockReturnValue(1) | ||
const wrapper = renderWithProps(props) | ||
expect(mockOnChange).toHaveBeenCalledWith(RENDERING_STRATEGY_SINGLE) | ||
}) | ||
}) |