Last active
April 21, 2021 12:26
-
-
Save ned-alyona/8abeea442bd12bd347b29ec8aeef51d7 to your computer and use it in GitHub Desktop.
Unit and Integration Testing of Redux/React Forms
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
let store, PropertySelfOwnedFormComponentWrapper, PropertySelfOwnedFormComponent, receivedNamesList; | |
beforeEach(() => { | |
store = mockStore(initialState); | |
PropertySelfOwnedFormComponentWrapper = (props) => ( | |
<PropertySelfOwnedForm {...defaultProps} {...props} store={store} /> | |
); | |
PropertySelfOwnedFormComponent = shallow(<PropertySelfOwnedFormComponentWrapper />).dive(); | |
}); |
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
it('PropertySelfOwnedForm renders correctly with PropertyTypeApartment', () => { | |
const props = { | |
propertyType: 10 | |
}, | |
PropertySelfOwnedFormComponent = shallow(<PropertySelfOwnedFormComponentWrapper {...props} />).dive(); | |
const receivedNamesList = PropertySelfOwnedFormComponent.find('form').find('Col').children().map(node => node.props().name); | |
const expectedNamesList = [ | |
'number_of_apartments', | |
'floor_number', | |
'balcony_terrace_place', | |
'apartments_number_of_outdoor_parking_spaces', | |
'apartments_number_of_garages', | |
'apartments_number_of_garages_individual' | |
]; | |
expect(receivedNamesList).toEqual(expect.arrayContaining(expectedNamesList)); | |
}); |
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
it('PropertySelfOwnedForm renders correctly with PropertyTypeHouse', () => { | |
receivedNamesList = PropertySelfOwnedFormComponent.find('form').find('Col').children().map(node => node.props().name); | |
const expectedNamesList = [ | |
'building_volume', | |
'site_area', | |
'building_volume_standard', | |
'number_of_garages_house', | |
'number_of_garages_separate_building', | |
'number_of_parking_spots_covered', | |
'number_of_parking_spots_uncovered' | |
]; | |
expect(receivedNamesList).toEqual(expect.arrayContaining(expectedNamesList)); | |
}); |
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
it('snapshot difference between 2 React forms state', () => { | |
const props = { | |
propertyType: 10 | |
}, | |
PropertySelfOwnedFormComponentApartment = shallow(<PropertySelfOwnedFormComponentWrapper {...props} />).dive(); | |
expect( | |
snapshotDiff( | |
PropertySelfOwnedFormComponent, | |
PropertySelfOwnedFormComponentApartment | |
) | |
).toMatchSnapshot(); | |
}); |
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
const mockStore = configureStore(); |
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
it('create snapshot for PropertySelfOwnedForm with PropertyTypeHouse fields', () => { | |
expect(PropertySelfOwnedFormComponent).toMatchSnapshot(); | |
}); |
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
it('create snapshot for PropertySelfOwnedForm with PropertyTypeApartment fields', () => { | |
const props = { | |
propertyType: 10 | |
}, | |
PropertySelfOwnedFormComponent = shallow(<PropertySelfOwnedFormComponentWrapper {...props} />).dive(); | |
expect(PropertySelfOwnedFormComponent).toMatchSnapshot(); | |
}); |
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
it('PropertySelfOwnedForm renders correctly with PropertyTypeHouse', () => { | |
expect(PropertySelfOwnedFormComponent).toMatchSnapshot(); | |
}); |
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
it('PropertySelfOwnedForm renders correctly with PropertyTypeApartment', () => { | |
const props = { | |
propertyType: 10 | |
}, | |
PropertyTypeApartmentWrapper = mount(<PropertySelfOwnedFormComponentWrapper {...props} />); | |
expect(PropertyTypeApartmentWrapper).toMatchSnapshot(); | |
}); |
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
global.getDjangoParam = () => djangoParamsChoices; | |
let PropertySelfOwnedFormComponent; | |
const history = { | |
push: jest.fn(), | |
location: { | |
pathname: '/en/data-collection/property-valuation/' | |
}, | |
listen: () => {} | |
}, | |
defaultProps = { | |
propertyType: 1, | |
resource: propertyDetailsResource, | |
handleSubmit: (fn) => fn, | |
onSubmit: jest.fn(), | |
onSubmitAndNavigate: jest.fn(), | |
onNavigate: jest.fn() | |
}, | |
store = createStore(combineReducers({ form: formReducer })), | |
Decorated = reduxForm({ | |
form: 'property-details-form' | |
})(PropertySelfOwnedForm), | |
PropertySelfOwnedFormComponentWrapper = (props) => ( | |
<Provider store={store}> | |
<Router history={history}> | |
<Decorated {...defaultProps} {...props} /> | |
</Router> | |
</Provider> | |
); |
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
import { mount } from 'enzyme'; | |
import { createStore, combineReducers } from 'redux'; | |
import { Provider } from 'react-redux'; | |
import { Router } from 'react-router-dom'; | |
import { reduxForm, reducer as formReducer } from 'redux-form'; | |
import propertyDetailsResource from 'store/propertyDetailsResource'; | |
import djangoParamsChoices from 'tests/__mocks__/djangoParamsChoices'; | |
import PropertySelfOwnedForm from '../PropertySelfOwnedForm'; |
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
import { shallow } from 'enzyme'; | |
import djangoParamsChoices from 'tests/__mocks__/djangoParamsChoices'; | |
import PropertySelfOwnedForm from '../PropertySelfOwnedForm'; | |
import configureStore from 'redux-mock-store'; | |
const snapshotDiff = require('snapshot-diff'); |
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
const initialState = {}; |
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
beforeEach(() => { | |
PropertySelfOwnedFormComponent = mount( | |
<PropertySelfOwnedFormComponentWrapper /> | |
); | |
}); |
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
it('render connected form component', () => { | |
expect(PropertySelfOwnedFormComponent.length).toEqual(1); | |
}); |
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
const defaultProps = { | |
propertyType: 1 | |
}; |
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
<PropertySelfOwnedForm | |
propertyType={1} | |
onSubmit={[Function: mockConstructor]} | |
onSubmitAndNavigate={[Function: mockConstructor]} | |
onNavigate={[Function: mockConstructor]} | |
store={{...}} | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment