Skip to content

Commit

Permalink
Add react testing library, wrapper for redux store provider, and impl…
Browse files Browse the repository at this point in the history
…ement an input test
  • Loading branch information
ari7946 committed Nov 9, 2021
1 parent 7052584 commit 446bfa0
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 33 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"@fortawesome/fontawesome-svg-core": "^1.2.27",
"@fortawesome/free-solid-svg-icons": "^5.12.1",
"@fortawesome/react-fontawesome": "^0.1.8",
"@testing-library/dom": "^8.11.0",
"@testing-library/jest-dom": "^5.15.0",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^26.0.21",
"@types/node": "^14.14.35",
"@types/react": "^17.0.3",
Expand Down
23 changes: 23 additions & 0 deletions src/features/_global/test-utils.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// https://testing-library.com/docs/react-testing-library/setup/
// https://redux.js.org/usage/writing-tests#components

import React from 'react'
import {render as rtlRender } from '@testing-library/react';
import { Provider } from 'react-redux';
import { store } from './redux/store';

const render = (
ui,
{
preloadedState,
...renderOptions
} = {}
) => {
const Wrapper = ({ children }) => {
return <Provider store={store}>{children}</Provider>
}
return rtlRender(ui, { wrapper: Wrapper, ...renderOptions})
}

export * from '@testing-library/react';
export { render }
13 changes: 13 additions & 0 deletions src/features/book/components/book-form/__test__/book-form.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { render, screen } from '../../../../_global/test-utils'
import BookForm from '../book-form.component'

describe('BookForm', () => {
it('should render BookForm', async () => {
render(
<BookForm />
)
const inputElement = screen.getByPlaceholderText('name');
expect(inputElement).toBeInTheDocument()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { selectStartAddress, selectEndAddress, selectLoading, selectAlertSuccess
} from '../../redux/book.selectors';

import { submitForm } from '../../redux/book.actions';
import { bindActionCreators } from 'redux';

interface ActionCreators {
submitForm: (obj: FormFields) => any,
Expand Down Expand Up @@ -58,6 +59,8 @@ const bookFormReducer = (state: FormFields, action: Action) => {
}
}

type TaxiFormProps = ActionCreators & ReduxProps;

const TaxiForm = ({
// action creators
submitForm,
Expand All @@ -66,7 +69,7 @@ const TaxiForm = ({
alertSuccess,
startAddress,
endAddress,
} : ActionCreators & ReduxProps) : React.ReactElement => {
} : TaxiFormProps) : React.ReactElement => {
const [values, dispatch] = React.useReducer(bookFormReducer, initialFormFields);

const handleChange = (name: string, value: string) => {
Expand Down Expand Up @@ -114,7 +117,7 @@ const TaxiForm = ({
</FormGroup>

{/* EMAIL */}
<FormGroup form>
<FormGroup>
<Label for="exampleEmail">Email: <span className="text-flat-orange small ml-2">required</span></Label>
<Input
type="email"
Expand Down
25 changes: 0 additions & 25 deletions src/features/trips/redux/trips.actions.test.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ReactDOM.render(
</PersistGate>
</Router>
</Provider>
), document.getElementById('root'));
), document.getElementById('root')
);

serviceWorker.register();
16 changes: 11 additions & 5 deletions src/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
// import { configure } from 'enzyme';
// import Adapter from 'enzyme-adapter-react-16';

configure({
adapter: new Adapter()
})
// configure({
// adapter: new Adapter()
// })

// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
Loading

0 comments on commit 446bfa0

Please sign in to comment.