This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
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 #817 from mturley/settings-tests
Add unit tests for Settings page components, actions and reducers (cherry picked from commit 430933a) https://bugzilla.redhat.com/show_bug.cgi?id=1657285
- Loading branch information
1 parent
8e04c01
commit f3b16cd
Showing
18 changed files
with
669 additions
and
21 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
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
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
36 changes: 36 additions & 0 deletions
36
app/javascript/react/screens/App/Settings/__tests__/Settings.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,36 @@ | ||
import React from 'react'; | ||
import { Spinner } from 'patternfly-react'; | ||
import { shallow } from 'enzyme'; | ||
import { Settings } from '../Settings'; | ||
import { servers, settings } from '../settings.fixures'; | ||
import { getFormValuesFromApiSettings } from '../helpers'; | ||
|
||
describe('Settings component', () => { | ||
const defaultFormValues = getFormValuesFromApiSettings(settings); | ||
const getBaseProps = () => ({ | ||
fetchServersAction: jest.fn(), | ||
fetchSettingsAction: jest.fn(), | ||
patchSettingsAction: jest.fn(), | ||
savedSettings: defaultFormValues, | ||
settingsForm: { values: defaultFormValues }, | ||
servers: servers.resources | ||
}); | ||
|
||
it('renders the settings page', () => { | ||
const component = shallow(<Settings {...getBaseProps()} />); | ||
expect(component).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders the settings page with Applying spinner when saving', () => { | ||
const component = shallow(<Settings {...getBaseProps()} isSavingSettings />); | ||
const applying = component.find(Spinner).findWhere(child => child.text() === ' Applying...'); | ||
expect(applying).toHaveLength(1); | ||
}); | ||
|
||
it('properly calls patchSettingsAction on apply click', () => { | ||
const patchSettingsAction = jest.fn(); | ||
const component = shallow(<Settings {...getBaseProps()} patchSettingsAction={patchSettingsAction} />); | ||
component.find('Button').simulate('click'); | ||
expect(patchSettingsAction).toBeCalledWith(servers.resources, defaultFormValues); | ||
}); | ||
}); |
103 changes: 103 additions & 0 deletions
103
app/javascript/react/screens/App/Settings/__tests__/SettingsActions.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,103 @@ | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import promiseMiddleware from 'redux-promise-middleware'; | ||
import * as actions from '../SettingsActions'; | ||
import { | ||
fetchServersData, | ||
servers, | ||
settingsFormValues, | ||
fetchSettingsData, | ||
patchSettingsData | ||
} from '../settings.fixures'; | ||
import { initialState } from '../SettingsReducer'; | ||
import { mockRequest, mockReset } from '../../../../../common/mockRequests'; | ||
|
||
const middlewares = [thunk, promiseMiddleware()]; | ||
const mockStore = configureMockStore(middlewares); | ||
const store = mockStore(initialState); | ||
|
||
afterEach(() => { | ||
store.clearActions(); | ||
mockReset(); | ||
}); | ||
|
||
describe('settings actions', () => { | ||
it('should fetch servers and return PENDING and FULFILLED action', () => { | ||
const { method, fetchServersUrl, response } = fetchServersData; | ||
mockRequest({ | ||
method, | ||
url: fetchServersUrl, | ||
status: 200, | ||
response | ||
}); | ||
return store.dispatch(actions.fetchServersAction(fetchServersUrl)).then(() => { | ||
expect(store.getActions()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should fetch servers and return PENDING and REJECTED action', () => { | ||
const { method, fetchServersUrl } = fetchServersData; | ||
mockRequest({ | ||
method, | ||
url: fetchServersUrl, | ||
status: 500 | ||
}); | ||
return store.dispatch(actions.fetchServersAction(fetchServersUrl)).catch(() => { | ||
expect(store.getActions()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should fetch settings and return PENDING and FULFILLED action', () => { | ||
const { method, fetchSettingsUrl, response } = fetchSettingsData; | ||
mockRequest({ | ||
method, | ||
url: fetchSettingsUrl, | ||
status: 200, | ||
response | ||
}); | ||
return store.dispatch(actions.fetchSettingsAction(fetchSettingsUrl)).then(() => { | ||
expect(store.getActions()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should fetch settings and return PENDING and REJECTED action', () => { | ||
const { method, fetchSettingsUrl } = fetchSettingsData; | ||
mockRequest({ | ||
method, | ||
url: fetchSettingsUrl, | ||
status: 500 | ||
}); | ||
return store.dispatch(actions.fetchSettingsAction(fetchSettingsUrl)).catch(() => { | ||
expect(store.getActions()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should patch settings and return PENDING and FULFILLED actions', () => { | ||
const { method, response } = patchSettingsData; | ||
servers.resources.forEach(server => { | ||
mockRequest({ | ||
method, | ||
url: `${server.href}/settings`, | ||
status: 200, | ||
response | ||
}); | ||
}); | ||
return store.dispatch(actions.patchSettingsAction(servers.resources, settingsFormValues)).then(() => { | ||
expect(store.getActions()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should patch settings and return PENDING and REJECTED action', () => { | ||
const { method } = patchSettingsData; | ||
servers.resources.forEach(server => { | ||
mockRequest({ | ||
method, | ||
url: `${server.href}/settings`, | ||
status: 500 | ||
}); | ||
}); | ||
return store.dispatch(actions.patchSettingsAction(servers.resources, settingsFormValues)).catch(() => { | ||
expect(store.getActions()).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
104 changes: 104 additions & 0 deletions
104
app/javascript/react/screens/App/Settings/__tests__/SettingsReducer.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,104 @@ | ||
import settingsReducer, { initialState } from '../SettingsReducer'; | ||
import { V2V_FETCH_SERVERS, V2V_FETCH_SETTINGS, V2V_PATCH_SETTINGS } from '../SettingsConstants'; | ||
import { servers, settings } from '../settings.fixures'; | ||
|
||
it('sets default state', () => { | ||
const action = { type: '@@INIT' }; | ||
const state = settingsReducer(undefined, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
describe('fetching servers', () => { | ||
it('is pending', () => { | ||
const action = { | ||
type: `${V2V_FETCH_SERVERS}_PENDING` | ||
}; | ||
const prevState = initialState.set('fetchingServersRejected', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
it('is rejected', () => { | ||
const action = { | ||
type: `${V2V_FETCH_SERVERS}_REJECTED`, | ||
payload: 'error' | ||
}; | ||
const prevState = initialState.set('isFetchingServers', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
it('is successful', () => { | ||
const action = { | ||
type: `${V2V_FETCH_SERVERS}_FULFILLED`, | ||
payload: { data: servers } | ||
}; | ||
const prevState = initialState.set('fetchingServersRejected', true).set('isFetchingServers', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state.fetchingServersRejected).toBe(false); | ||
expect(state.isFetchingServers).toBe(false); | ||
expect(state.servers).toHaveLength(2); | ||
}); | ||
}); | ||
|
||
describe('fetching settings', () => { | ||
it('is pending', () => { | ||
const action = { | ||
type: `${V2V_FETCH_SETTINGS}_PENDING` | ||
}; | ||
const prevState = initialState.set('fetchingSettingsRejected', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
it('is rejected', () => { | ||
const action = { | ||
type: `${V2V_FETCH_SETTINGS}_REJECTED`, | ||
payload: 'error' | ||
}; | ||
const prevState = initialState.set('isFetchingSettings', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
it('is successful', () => { | ||
const action = { | ||
type: `${V2V_FETCH_SETTINGS}_FULFILLED`, | ||
payload: { data: settings } | ||
}; | ||
const prevState = initialState.set('fetchingSettingsRejected', true).set('isFetchingSettings', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('saving settings', () => { | ||
it('is pending', () => { | ||
const action = { | ||
type: `${V2V_PATCH_SETTINGS}_PENDING` | ||
}; | ||
const prevState = initialState.set('savingSettingsRejected', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
it('is rejected', () => { | ||
const action = { | ||
type: `${V2V_PATCH_SETTINGS}_REJECTED`, | ||
payload: 'error' | ||
}; | ||
const prevState = initialState.set('isSavingSettings', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
it('is successful', () => { | ||
const action = { | ||
type: `${V2V_PATCH_SETTINGS}_FULFILLED`, | ||
payload: settings | ||
}; | ||
const prevState = initialState.set('savingSettingsRejected', true).set('isSavingSettings', true); | ||
const state = settingsReducer(prevState, action); | ||
expect(state).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.