-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP ⚠ configure jest with enzyme #105
Open
rady-ben
wants to merge
18
commits into
unfoldingWord:master
Choose a base branch
from
rady-ben:test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4043947
add new config to jest
rady-ben 3f2a646
configure enzyme and test the render of FileForm
rady-ben b7b0960
remove unused componenent
rady-ben 75f927e
test the render of the element inside FileForm component
rady-ben c05a2ec
add propTypes tests
rady-ben dfb0c78
add default propos
rady-ben 24846a9
add text input behaviour tests
rady-ben 2ffcbde
refractor the Enzyme utils in separate file to not repeat the config …
rady-ben 6390bb4
add some messing propTypes to FileCard
rady-ben 99bf78d
replace the authentication props by is authenticated
rady-ben afbd3d7
add the render tests fot the FileCard
rady-ben b9e0e77
add prop types test for theFileCard
rady-ben 6a0dd50
add unit test for the useState on FileCard
rady-ben 4b3cdd4
use deferents initials states on jest tests
rady-ben fb37448
allow useing the destructuring import of useState without breaking tests
rady-ben 0aadb90
return the tests removed by mistake
rady-ben 1313723
reset useState to its initial value befor run rendering tests
rady-ben fe79d01
test the conditionel rendering of Pageview icon
rady-ben File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React from "react"; | ||
import Enzyme, {shallow} from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import FileForm from '../src/components/file/FileForm'; | ||
import { findByAttribute, checkProps } from './testUtils'; | ||
|
||
Enzyme.configure({ | ||
adapter: new Adapter(), | ||
}); | ||
|
||
const defaultProps = { onSubmit: () => {} } | ||
|
||
const setupWrapper = (props = {}) => { | ||
const setupProps = {...defaultProps, ...props} | ||
return shallow(<FileForm {...setupProps}/>) | ||
} | ||
|
||
const mockSetBranch = jest.fn(); | ||
|
||
jest.mock('react', () => ({ | ||
...jest.requireActual('react'), | ||
useState: (initialState) => [initialState, mockSetBranch] | ||
})) | ||
|
||
// render tests | ||
describe('render fileForm elements',() => { | ||
test('render fileForm', () => { | ||
const wrapper = setupWrapper(); | ||
const fileForm = findByAttribute(wrapper, 'component-fileForm'); | ||
expect(fileForm.length).toBe(1); | ||
}); | ||
|
||
test('render branch-textField', () => { | ||
const wrapper = setupWrapper(); | ||
const branch = findByAttribute(wrapper, 'branch-textField'); | ||
expect(branch.length).toBe(1); | ||
}); | ||
|
||
test('render filepath-textField', () => { | ||
const wrapper = setupWrapper(); | ||
const filepath = findByAttribute(wrapper, 'filepath-textField'); | ||
expect(filepath.length).toBe(1); | ||
}); | ||
|
||
test('render defaultContent-textField', () => { | ||
const wrapper = setupWrapper(); | ||
const defaultContent = findByAttribute(wrapper, 'defaultContent-textField'); | ||
expect(defaultContent.length).toBe(1); | ||
}); | ||
|
||
test('render submit-button', () => { | ||
const wrapper = setupWrapper(); | ||
const submit = findByAttribute(wrapper, 'submit-button'); | ||
expect(submit.length).toBe(1); | ||
}); | ||
}); | ||
|
||
// PropTypes tests | ||
test('FileForm PropTypes', () => { | ||
const conformingProps = { | ||
submitText: "text", | ||
onSubmit: () => {}, | ||
branch: 'branch', | ||
filepath: 'filepath', | ||
defaultContent: 'defaultContent', | ||
} | ||
checkProps(FileForm, conformingProps) | ||
}); | ||
|
||
|
||
// input text | ||
describe('text input change',() => { | ||
test('update the state with the input value of branch', () => { | ||
const wrapper = setupWrapper(); | ||
const textField = findByAttribute(wrapper, 'branch-textField'); | ||
const mockEvent = { target: { value: 'branch' } }; | ||
textField.simulate('change', mockEvent); | ||
expect(mockSetBranch).toBeCalledWith('branch') | ||
}); | ||
|
||
test('update the state with the input value of filepath', () => { | ||
const wrapper = setupWrapper(); | ||
const textField = findByAttribute(wrapper, 'filepath-textField'); | ||
const mockEvent = { target: { value: 'filepath' } }; | ||
textField.simulate('change', mockEvent); | ||
expect(mockSetBranch).toBeCalledWith('filepath') | ||
}); | ||
|
||
test('update the state with the input value of defaultContent', () => { | ||
const wrapper = setupWrapper(); | ||
const textField = findByAttribute(wrapper, 'defaultContent-textField'); | ||
const mockEvent = { target: { value: 'defaultContent' } }; | ||
textField.simulate('change', mockEvent); | ||
expect(mockSetBranch).toBeCalledWith('defaultContent') | ||
}); | ||
}) |
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 @@ | ||
module.exports = 'test-file-stub'; |
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,12 @@ | ||
import checkPropTypes from 'check-prop-types'; | ||
|
||
export const findByAttribute = (wrapper, attribute) => wrapper.find(`[data-test='${attribute}']`) | ||
|
||
export const checkProps = (component, conformingProps) => { | ||
const propError = checkPropTypes( | ||
component.propTypes, | ||
conformingProps, | ||
'prop', | ||
component.name); | ||
expect(propError).toBeUndefined(); | ||
} |
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,9 @@ | ||
module.exports = { | ||
'roots': [ | ||
'<rootDir>/__mocks__', | ||
], | ||
'moduleNameMapper': { | ||
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js", | ||
"\\.(css|less)$": "identity-obj-proxy" | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,28 +17,35 @@ function FileForm({ | |
const disabled = !(filepath); | ||
|
||
return ( | ||
<Paper style={{ marginBottom: '1em', padding: '1.3em' }}> | ||
<Paper | ||
style={{ marginBottom: '1em', padding: '1.3em' }} | ||
data-test="component-fileForm" | ||
> | ||
<form> | ||
<button type="submit" disabled style={{ display: 'none' }} aria-hidden="true"></button> | ||
<TextField | ||
name='branch' label='branch' type='text' autoComplete={null} | ||
variant="outlined" margin="normal" fullWidth defaultValue={branch} | ||
variant="outlined" margin="normal" fullWidth defaultValue={'branch'} | ||
onChange={(e) => setBranch(e.target.value)} | ||
data-test="branch-textField" | ||
/> | ||
<TextField | ||
name='filepath' label='filepath' type='text' autoComplete={null} | ||
variant="outlined" margin="normal" fullWidth defaultValue={filepath} | ||
onChange={(e) => setFilepath(e.target.value)} | ||
data-test="filepath-textField" | ||
/> | ||
<TextField | ||
name='defaultContent' label='defaultContent' type='text' multiline={true} autoComplete={null} | ||
variant="outlined" margin="normal" fullWidth defaultValue={defaultContent} | ||
onChange={(e) => setDefaultContent(e.target.value)} | ||
data-test="defaultContent-textField" | ||
/> | ||
<Button type="button" disabled={disabled} fullWidth variant="contained" color="primary" | ||
onClick={() => onSubmit({ | ||
branch, filepath, defaultContent, | ||
})} | ||
data-test="submit-button" | ||
> | ||
{submitText} | ||
</Button> | ||
|
@@ -52,6 +59,9 @@ FileForm.propTypes = { | |
submitText: PropTypes.string, | ||
/** Function run when submit button is clicked */ | ||
onSubmit: PropTypes.func.isRequired, | ||
branch: PropTypes.string, | ||
filepath: PropTypes.string, | ||
defaultContent: PropTypes.string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOTE: there were a few missing proptypes 👍 |
||
}; | ||
|
||
FileForm.defaultProps = { submitText: 'Submit' }; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: discuss with the team:
Seems like a helpful idea to verify prop-types on PUBLIC libraries.