Skip to content

Commit

Permalink
created test actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lambrugeorge committed Jun 11, 2024
1 parent a1f99c0 commit e66e056
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 90 deletions.
120 changes: 89 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/redux-mock-store": "^1.0.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "^5.0.1",
Expand All @@ -29,6 +30,8 @@
},
"devDependencies": {
"@cfaester/enzyme-adapter-react-18": "^0.8.0",
"jest": "^27.5.1"
"jest": "^27.5.1",
"redux-mock-store": "^1.5.4",
"redux-thunk": "^3.1.0"
}
}
18 changes: 18 additions & 0 deletions src/actions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as actions from './actions';
import { REQUEST_ROBOTS_PENDING } from './constants';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

describe("Fetch robots action PENDING", () => {
it("should create a Pending action on request Robots", () => {
const store = mockStore({});
return store.dispatch(actions.requestRobots())
.then(() => {
const action = store.getActions();
expect(action[0]).toEqual({ type: REQUEST_ROBOTS_PENDING });
});
});
});
4 changes: 2 additions & 2 deletions src/components/ErrorBoundry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react'

class ErrorBoundary extends Component {
class ErrorBoundry extends Component {
constructor (props) {
super(props)
this.state = { hasError: false }
Expand All @@ -18,4 +18,4 @@ class ErrorBoundary extends Component {
}
}

export default ErrorBoundary
export default ErrorBoundry
27 changes: 10 additions & 17 deletions src/components/MainPage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import React, { Component } from 'react';

import React from 'react';
import CardList from './CardList';
import SearchBox from './SearchBox';
import Scroll from './Scroll';
import ErrorBoundry from './ErrorBoundry';
import Header from './Header';

export class MainPage extends Component {
class MainPage extends React.Component {
componentDidMount() {
this.props.onRequestRobots();
}
Expand All @@ -15,26 +11,23 @@ export class MainPage extends Component {
const { robots, searchField } = this.props;
return robots.filter(robot => {
return robot.name.toLowerCase().includes(searchField.toLowerCase());
})
});
}

render() {
const { onSearchChange, isPending } = this.props;

return (
<div className='tc'>
<Header />
<SearchBox searchChange={onSearchChange}/>
<Scroll>
{ isPending ? <h1>Loading</h1> :
<ErrorBoundry>
<CardList robots={this.filterRobots()} />
</ErrorBoundry>
}
</Scroll>
<h1 className='f1'>RoboFriends</h1>
<SearchBox searchChange={onSearchChange} />
{ isPending ?
<h2>Loading...</h2> :
<CardList robots={this.filterRobots()} />
}
</div>
);
}
}

export default MainPage
export default MainPage;
50 changes: 50 additions & 0 deletions src/components/MainPage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import { shallow } from 'enzyme';
import MainPage from './MainPage';

describe('MainPage component', () => {
it('renders MainPage without crashing', () => {
const mockProps = {
onRequestRobots: jest.fn(),
robots: [],
searchField: '',
isPending: false
};
const wrapper = shallow(<MainPage {...mockProps} />);
expect(wrapper).toMatchSnapshot();
});

it('filters robots correctly', () => {
const mockProps = {
onRequestRobots: jest.fn(),
robots: [{
id: 3,
name: 'John',
email: '[email protected]'
}],
searchField: 'j',
isPending: false
};
const wrapper = shallow(<MainPage {...mockProps} />);
expect(wrapper.instance().filterRobots()).toEqual([{
id: 3,
name: 'John',
email: '[email protected]'
}]);
});

it('filters robots correctly with different search term', () => {
const mockProps = {
onRequestRobots: jest.fn(),
robots: [{
id: 3,
name: 'John',
email: '[email protected]'
}],
searchField: 'a',
isPending: false
};
const wrapper = shallow(<MainPage {...mockProps} />);
expect(wrapper.instance().filterRobots()).toEqual([]);
});
});
Loading

0 comments on commit e66e056

Please sign in to comment.