Skip to content

Commit

Permalink
Adds unit tests to app reducer (magento#921)
Browse files Browse the repository at this point in the history
* Adds unit tests to app reducer
* Updates tests to be less fragile
  • Loading branch information
supernova-at committed Mar 6, 2019
1 parent 40314b3 commit 8ade70b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
6 changes: 3 additions & 3 deletions packages/venia-concept/src/components/Header/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import Header from './header';
import { toggleSearch } from 'src/actions/app';

const mapStateToProps = ({ app }) => {
const { searchOpen, autocompleteOpen } = app;
const { searchOpen } = app;

return {
searchOpen,
autocompleteOpen
searchOpen
};
};

Expand Down
8 changes: 1 addition & 7 deletions packages/venia-concept/src/components/Header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ class Header extends Component {
}

render() {
const {
autocompleteOpen,
searchOpen,
classes,
toggleSearch
} = this.props;
const { searchOpen, classes, toggleSearch } = this.props;

const rootClass = searchOpen ? classes.open : classes.closed;

Expand Down Expand Up @@ -69,7 +64,6 @@ class Header extends Component {
<Route
render={({ history, location }) => (
<SearchBar
autocompleteOpen={autocompleteOpen}
isOpen={searchOpen}
history={history}
location={location}
Expand Down
82 changes: 82 additions & 0 deletions packages/venia-concept/src/reducers/__tests__/app.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import reducer from '../app';

const state = {
drawer: null,
hasBeenOffline: false,
isOnline: true,
overlay: false,
pending: {},
query: '',
searchOpen: false
};

test('toggleDrawer sets the overlay flag and the drawer to the action payload', () => {
const action = {
payload: 'cart',
type: 'APP/TOGGLE_DRAWER'
};

const result = reducer(state, action);

expect(result).toHaveProperty('drawer', action.payload);
expect(result).toHaveProperty('overlay', true);
});

describe('toggleSearch', () => {
test('toggleSearch flips the searchOpen flag to true', () => {
const action = {
type: 'APP/TOGGLE_SEARCH'
};

const result = reducer(state, action);

expect(result).toHaveProperty('searchOpen', true);
});

test('toggleSearch flips the searchOpen flag to false', () => {
const action = {
type: 'APP/TOGGLE_SEARCH'
};

const testState = {
...state,
searchOpen: true
};

const result = reducer(testState, action);

expect(result).toHaveProperty('searchOpen', false);
});
});

test('executeSearch sets the query to the action payload', () => {
const action = {
payload: 'unit test',
type: 'APP/EXECUTE_SEARCH'
};

const result = reducer(state, action);

expect(result).toHaveProperty('query', 'unit test');
});

test('setOnline sets the isOnline flag to true', () => {
const action = {
type: 'APP/SET_ONLINE'
};

const result = reducer(state, action);

expect(result).toHaveProperty('isOnline', true);
});

test('setOffline sets the isOnline and hasBeenOffline flags appropriately', () => {
const action = {
type: 'APP/SET_OFFLINE'
};

const result = reducer(state, action);

expect(result).toHaveProperty('isOnline', false);
expect(result).toHaveProperty('hasBeenOffline', true);
});
3 changes: 1 addition & 2 deletions packages/venia-concept/src/reducers/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const reducerMap = {
[actions.executeSearch]: (state, { payload }) => {
return {
...state,
query: payload,
autocompleteOpen: false
query: payload
};
},
[actions.setOnline]: state => {
Expand Down

0 comments on commit 8ade70b

Please sign in to comment.