Commit 78530032 authored by mfluharty's avatar mfluharty

Rename mutations to match conventions

Make actions call mutations with corresponding names
Adjust specs to test modified actions and mutations
parent 72772c5d
...@@ -120,11 +120,11 @@ export const requestProjects = ({ commit }) => { ...@@ -120,11 +120,11 @@ export const requestProjects = ({ commit }) => {
}; };
export const receiveProjectsSuccess = ({ commit }, data) => { export const receiveProjectsSuccess = ({ commit }, data) => {
commit(types.SET_PROJECTS, data.projects); commit(types.RECEIVE_PROJECTS_SUCCESS, data.projects);
}; };
export const receiveProjectsError = ({ commit }) => { export const receiveProjectsError = ({ commit }) => {
commit(types.SET_PROJECTS, null); commit(types.RECEIVE_PROJECTS_ERROR);
createFlash(__('Something went wrong, unable to get operations projects')); createFlash(__('Something went wrong, unable to get operations projects'));
}; };
...@@ -135,53 +135,36 @@ export const removeProject = ({ dispatch }, removePath) => { ...@@ -135,53 +135,36 @@ export const removeProject = ({ dispatch }, removePath) => {
.catch(() => dispatch('receiveRemoveProjectError')); .catch(() => dispatch('receiveRemoveProjectError'));
}; };
export const receiveRemoveProjectSuccess = ({ dispatch }) => { export const receiveRemoveProjectSuccess = ({ dispatch }) => dispatch('fetchProjects');
dispatch('fetchProjects');
};
export const receiveRemoveProjectError = () => { export const receiveRemoveProjectError = () => {
createFlash(__('Something went wrong, unable to remove project')); createFlash(__('Something went wrong, unable to remove project'));
}; };
export const setSearchQuery = ({ commit }, query) => { export const setSearchQuery = ({ commit }, query) => commit(types.SET_SEARCH_QUERY, query);
commit(types.SET_SEARCH_QUERY, query);
};
export const fetchSearchResults = ({ commit, state, dispatch }) => {
if (!state.searchQuery) {
commit(types.SEARCHED_WITH_NO_QUERY);
} else if (state.searchQuery.length < API_MINIMUM_QUERY_LENGTH) {
commit(types.SEARCHED_WITH_TOO_SHORT_QUERY);
} else {
commit(types.INCREMENT_PROJECT_SEARCH_COUNT, 1);
export const fetchSearchResults = ({ state, dispatch }) => {
dispatch('requestSearchResults'); dispatch('requestSearchResults');
if (!state.searchQuery) {
dispatch('receiveSearchResultsError');
} else if (state.searchQuery.lengh < API_MINIMUM_QUERY_LENGTH) {
dispatch('receiveSearchResultsError', 'minimumQuery');
} else {
Api.projects(state.searchQuery, {}) Api.projects(state.searchQuery, {})
.then(results => dispatch('receiveSearchResultsSuccess', results)) .then(results => dispatch('receiveSearchResultsSuccess', results))
.catch(() => dispatch('receiveSearchResultsError')); .catch(() => dispatch('receiveSearchResultsError'));
} }
}; };
export const requestSearchResults = ({ commit }) => { export const requestSearchResults = ({ commit }) => commit(types.REQUEST_SEARCH_RESULTS);
// Flipping this property separately to allows the UI
// to hide the "minimum query" message
// before the seach results arrive from the API
commit(types.SET_MESSAGE_MINIMUM_QUERY, false);
};
export const receiveSearchResultsSuccess = ({ commit }, results) => { export const receiveSearchResultsSuccess = ({ commit }, results) => {
if (results.length === 0) { commit(types.RECEIVE_SEARCH_RESULTS_SUCCESS, results);
commit(types.SEARCHED_SUCCESSFULLY_NO_RESULTS);
} else {
commit(types.SEARCHED_SUCCESSFULLY_WITH_RESULTS, results);
}
return commit(types.DECREMENT_PROJECT_SEARCH_COUNT, 1);
}; };
export const receiveSearchResultsError = ({ commit }) => { export const receiveSearchResultsError = ({ commit }) => {
commit(types.SEARCHED_WITH_API_ERROR); commit(types.RECEIVE_SEARCH_RESULTS_ERROR);
commit(types.DECREMENT_PROJECT_SEARCH_COUNT, 1);
}; };
export const setProjectEndpoints = ({ commit }, endpoints) => { export const setProjectEndpoints = ({ commit }, endpoints) => {
......
export const INCREMENT_PROJECT_SEARCH_COUNT = 'INCREMENT_PROJECT_SEARCH_COUNT';
export const DECREMENT_PROJECT_SEARCH_COUNT = 'DECREMENT_PROJECT_SEARCH_COUNT';
export const SET_PROJECT_ENDPOINT_LIST = 'SET_PROJECT_ENDPOINT_LIST'; export const SET_PROJECT_ENDPOINT_LIST = 'SET_PROJECT_ENDPOINT_LIST';
export const SET_PROJECT_ENDPOINT_ADD = 'SET_PROJECT_ENDPOINT_ADD'; export const SET_PROJECT_ENDPOINT_ADD = 'SET_PROJECT_ENDPOINT_ADD';
export const SET_PROJECTS = 'SET_PROJECTS'; export const SET_PROJECTS = 'SET_PROJECTS';
export const SET_SEARCH_QUERY = 'SET_SEARCH_QUERY'; export const SET_SEARCH_QUERY = 'SET_SEARCH_QUERY';
export const REMOVE_PROJECT_TOKEN_AT = 'REMOVE_PROJECT_TOKEN_AT'; export const REMOVE_PROJECT_TOKEN_AT = 'REMOVE_PROJECT_TOKEN_AT';
export const REQUEST_PROJECTS = 'REQUEST_PROJECTS';
export const SET_MESSAGE_MINIMUM_QUERY = 'SET_MESSAGE_MINIMUM_QUERY'; export const SET_MESSAGE_MINIMUM_QUERY = 'SET_MESSAGE_MINIMUM_QUERY';
export const ADD_SELECTED_PROJECT = 'ADD_SELECTED_PROJECT'; export const ADD_SELECTED_PROJECT = 'ADD_SELECTED_PROJECT';
export const REMOVE_SELECTED_PROJECT = 'REMOVE_SELECTED_PROJECT'; export const REMOVE_SELECTED_PROJECT = 'REMOVE_SELECTED_PROJECT';
export const TOGGLE_IS_LOADING_PROJECTS = 'TOGGLE_IS_LOADING_PROJECTS'; export const REQUEST_PROJECTS = 'REQUEST_PROJECTS';
export const RECEIVE_PROJECTS_SUCCESS = 'RECEIVE_PROJECTS_SUCCESS';
export const RECEIVE_PROJECTS_ERROR = 'RECEIVE_PROJECTS_ERROR';
export const CLEAR_SEARCH_RESULTS = 'CLEAR_SEARCH_RESULTS'; export const CLEAR_SEARCH_RESULTS = 'CLEAR_SEARCH_RESULTS';
export const SEARCHED_WITH_NO_QUERY = 'SEARCHED_WITH_NO_QUERY'; export const REQUEST_SEARCH_RESULTS = 'REQUEST_SEARCH_RESULTS';
export const SEARCHED_WITH_TOO_SHORT_QUERY = 'SEARCHED_WITH_TOO_SHORT_QUERY'; export const RECEIVE_SEARCH_RESULTS_SUCCESS = 'RECEIVE_SEARCH_RESULTS_SUCCESS';
export const SEARCHED_WITH_API_ERROR = 'SEARCHED_WITH_API_ERROR'; export const RECEIVE_SEARCH_RESULTS_ERROR = 'RECEIVE_SEARCH_RESULTS_ERROR';
export const SEARCHED_SUCCESSFULLY_WITH_RESULTS = 'SEARCHED_SUCCESSFULLY_WITH_RESULTS';
export const SEARCHED_SUCCESSFULLY_NO_RESULTS = 'SEARCHED_SUCCESSFULLY_NO_RESULTS';
...@@ -2,13 +2,6 @@ import _ from 'underscore'; ...@@ -2,13 +2,6 @@ import _ from 'underscore';
import * as types from './mutation_types'; import * as types from './mutation_types';
export default { export default {
[types.DECREMENT_PROJECT_SEARCH_COUNT](state, value) {
state.searchCount -= value;
},
[types.INCREMENT_PROJECT_SEARCH_COUNT](state, value) {
state.searchCount += value;
},
[types.SET_PROJECT_ENDPOINT_LIST](state, url) { [types.SET_PROJECT_ENDPOINT_LIST](state, url) {
state.projectEndpoints.list = url; state.projectEndpoints.list = url;
}, },
...@@ -26,9 +19,6 @@ export default { ...@@ -26,9 +19,6 @@ export default {
[types.SET_MESSAGE_MINIMUM_QUERY](state, bool) { [types.SET_MESSAGE_MINIMUM_QUERY](state, bool) {
state.messages.minimumQuery = bool; state.messages.minimumQuery = bool;
}, },
[types.REQUEST_PROJECTS](state) {
state.isLoadingProjects = true;
},
[types.ADD_SELECTED_PROJECT](state, project) { [types.ADD_SELECTED_PROJECT](state, project) {
if (!state.selectedProjects.some(p => p.id === project.id)) { if (!state.selectedProjects.some(p => p.id === project.id)) {
...@@ -42,8 +32,16 @@ export default { ...@@ -42,8 +32,16 @@ export default {
); );
}, },
[types.TOGGLE_IS_LOADING_PROJECTS](state) { [types.REQUEST_PROJECTS](state) {
state.isLoadingProjects = !state.isLoadingProjects; state.isLoadingProjects = true;
},
[types.RECEIVE_PROJECTS_SUCCESS](state, projects) {
state.projects = projects;
state.isLoadingProjects = false;
},
[types.RECEIVE_PROJECTS_ERROR](state) {
state.projects = null;
state.isLoadingProjects = false;
}, },
[types.CLEAR_SEARCH_RESULTS](state) { [types.CLEAR_SEARCH_RESULTS](state) {
...@@ -51,38 +49,26 @@ export default { ...@@ -51,38 +49,26 @@ export default {
state.selectedProjects = []; state.selectedProjects = [];
}, },
[types.SEARCHED_WITH_NO_QUERY](state) { [types.REQUEST_SEARCH_RESULTS](state) {
state.projectSearchResults = []; // Flipping this property separately to allows the UI
state.messages.noResults = false; // to hide the "minimum query" message
state.messages.searchError = false; // before the seach results arrive from the API
state.messages.minimumQuery = false; state.messages.minimumQuery = false;
},
[types.SEARCHED_WITH_TOO_SHORT_QUERY](state) { state.searchCount += 1;
state.projectSearchResults = [];
state.messages.noResults = false;
state.messages.searchError = false;
state.messages.minimumQuery = true;
},
[types.SEARCHED_WITH_API_ERROR](state) {
state.projectSearchResults = [];
state.messages.noResults = false;
state.messages.searchError = true;
state.messages.minimumQuery = false;
}, },
[types.RECEIVE_SEARCH_RESULTS_SUCCESS](state, results) {
[types.SEARCHED_SUCCESSFULLY_WITH_RESULTS](state, results) {
state.projectSearchResults = results; state.projectSearchResults = results;
state.messages.noResults = false; state.messages.noResults = state.projectSearchResults.length === 0;
state.messages.searchError = false; state.messages.searchError = false;
state.messages.minimumQuery = false; state.messages.minimumQuery = false;
state.searchCount -= 1;
}, },
[types.RECEIVE_SEARCH_RESULTS_ERROR](state, message) {
[types.SEARCHED_SUCCESSFULLY_NO_RESULTS](state) {
state.projectSearchResults = []; state.projectSearchResults = [];
state.messages.noResults = true; state.messages.noResults = false;
state.messages.searchError = false; state.messages.searchError = true;
state.messages.minimumQuery = false; state.messages.minimumQuery = message === 'minimumQuery';
state.searchCount -= 1;
}, },
}; };
...@@ -243,7 +243,7 @@ describe('actions', () => { ...@@ -243,7 +243,7 @@ describe('actions', () => {
store.state, store.state,
[ [
{ {
type: types.SET_PROJECTS, type: types.RECEIVE_PROJECTS_SUCCESS,
payload: mockProjects, payload: mockProjects,
}, },
], ],
...@@ -264,8 +264,7 @@ describe('actions', () => { ...@@ -264,8 +264,7 @@ describe('actions', () => {
store.state, store.state,
[ [
{ {
type: types.SET_PROJECTS, type: types.RECEIVE_PROJECTS_ERROR,
payload: null,
}, },
], ],
[], [],
...@@ -313,7 +312,7 @@ describe('actions', () => { ...@@ -313,7 +312,7 @@ describe('actions', () => {
null, null,
null, null,
[], [],
[{ type: 'forceProjectsRequest' }], [{ type: 'fetchProjects' }],
done, done,
); );
}); });
...@@ -330,36 +329,42 @@ describe('actions', () => { ...@@ -330,36 +329,42 @@ describe('actions', () => {
}); });
describe('fetchSearchResults', () => { describe('fetchSearchResults', () => {
it('commits the SEARCHED_WITH_NO_QUERY if the search query was empty', done => { it('dispatches an error if the search query was empty', done => {
store.state.searchQuery = ''; store.state.searchQuery = '';
testAction( testAction(
actions.fetchSearchResults, actions.fetchSearchResults,
null, null,
store.state, store.state,
[],
[ [
{ {
type: types.SEARCHED_WITH_NO_QUERY, type: 'requestSearchResults',
},
{
type: 'receiveSearchResultsError',
}, },
], ],
[],
done, done,
); );
}); });
it(`commits the SEARCHED_WITH_TOO_SHORT_QUERY type if the search query wasn't long enough`, done => { it(`dispatches an error if the search query wasn't long enough`, done => {
store.state.searchQuery = 'a'; store.state.searchQuery = 'a';
testAction( testAction(
actions.fetchSearchResults, actions.fetchSearchResults,
null, null,
store.state, store.state,
[],
[ [
{ {
type: types.SEARCHED_WITH_TOO_SHORT_QUERY, type: 'requestSearchResults',
},
{
type: 'receiveSearchResultsError',
}, },
], ],
[],
done, done,
); );
}); });
...@@ -372,12 +377,7 @@ describe('actions', () => { ...@@ -372,12 +377,7 @@ describe('actions', () => {
actions.fetchSearchResults, actions.fetchSearchResults,
null, null,
store.state, store.state,
[ [],
{
type: types.INCREMENT_PROJECT_SEARCH_COUNT,
payload: 1,
},
],
[ [
{ {
type: 'requestSearchResults', type: 'requestSearchResults',
...@@ -393,15 +393,14 @@ describe('actions', () => { ...@@ -393,15 +393,14 @@ describe('actions', () => {
}); });
describe('requestSearchResults', () => { describe('requestSearchResults', () => {
it(`unsets the 'query is too short' warning status`, done => { it(`commits the REQUEST_SEARCH_RESULTS mutation`, done => {
testAction( testAction(
actions.requestSearchResults, actions.requestSearchResults,
null, null,
store.state, store.state,
[ [
{ {
type: types.SET_MESSAGE_MINIMUM_QUERY, type: types.REQUEST_SEARCH_RESULTS,
payload: false,
}, },
], ],
[], [],
...@@ -411,41 +410,16 @@ describe('actions', () => { ...@@ -411,41 +410,16 @@ describe('actions', () => {
}); });
describe('receiveSearchResultsSuccess', () => { describe('receiveSearchResultsSuccess', () => {
it('sets project search results', done => { it('commits the RECEIVE_SEARCH_RESULTS_SUCCESS mutation', done => {
testAction( testAction(
actions.receiveSearchResultsSuccess, actions.receiveSearchResultsSuccess,
mockProjects, mockProjects,
store.state, store.state,
[ [
{ {
type: types.SEARCHED_SUCCESSFULLY_WITH_RESULTS, type: types.RECEIVE_SEARCH_RESULTS_SUCCESS,
payload: mockProjects, payload: mockProjects,
}, },
{
type: types.DECREMENT_PROJECT_SEARCH_COUNT,
payload: 1,
},
],
[],
done,
);
});
it('commits the SEARCHED_SUCCESSFULLY_NO_RESULTS type (among others) if the search returns with no results', done => {
store.state.searchQuery = 'mock-query';
testAction(
actions.receiveSearchResultsSuccess,
[],
store.state,
[
{
type: types.SEARCHED_SUCCESSFULLY_NO_RESULTS,
},
{
type: types.DECREMENT_PROJECT_SEARCH_COUNT,
payload: 1,
},
], ],
[], [],
done, done,
...@@ -454,18 +428,14 @@ describe('actions', () => { ...@@ -454,18 +428,14 @@ describe('actions', () => {
}); });
describe('receiveSearchResultsError', () => { describe('receiveSearchResultsError', () => {
it('commits the SEARCHED_WITH_API_ERROR type (among others) if the search API returns an error code', done => { it('commits the RECEIVE_SEARCH_RESULTS_ERROR mutation', done => {
testAction( testAction(
actions.receiveSearchResultsError, actions.receiveSearchResultsError,
['error'], ['error'],
store.state, store.state,
[ [
{ {
type: types.SEARCHED_WITH_API_ERROR, type: types.RECEIVE_SEARCH_RESULTS_ERROR,
},
{
type: types.DECREMENT_PROJECT_SEARCH_COUNT,
payload: 1,
}, },
], ],
[], [],
......
...@@ -6,34 +6,12 @@ import { mockProjectData } from '../mock_data'; ...@@ -6,34 +6,12 @@ import { mockProjectData } from '../mock_data';
describe('mutations', () => { describe('mutations', () => {
const projects = mockProjectData(3); const projects = mockProjectData(3);
const mockEndpoint = 'https://mock-endpoint'; const mockEndpoint = 'https://mock-endpoint';
const mockSearches = new Array(5).fill(null);
let localState; let localState;
beforeEach(() => { beforeEach(() => {
localState = state(); localState = state();
}); });
describe('DECREMENT_PROJECT_SEARCH_COUNT', () => {
it('removes search from searchCount', () => {
localState.searchCount = mockSearches.length + 2;
mockSearches.forEach(() => {
mutations[types.DECREMENT_PROJECT_SEARCH_COUNT](localState, 1);
});
expect(localState.searchCount).toBe(2);
});
});
describe('INCREMENT_PROJECT_SEARCH_COUNT', () => {
it('adds search to searchCount', () => {
mockSearches.forEach(() => {
mutations[types.INCREMENT_PROJECT_SEARCH_COUNT](localState, 1);
});
expect(localState.searchCount).toBe(mockSearches.length);
});
});
describe('SET_PROJECT_ENDPOINT_LIST', () => { describe('SET_PROJECT_ENDPOINT_LIST', () => {
it('sets project list endpoint', () => { it('sets project list endpoint', () => {
mutations[types.SET_PROJECT_ENDPOINT_LIST](localState, mockEndpoint); mutations[types.SET_PROJECT_ENDPOINT_LIST](localState, mockEndpoint);
...@@ -59,18 +37,6 @@ describe('mutations', () => { ...@@ -59,18 +37,6 @@ describe('mutations', () => {
}); });
}); });
describe('TOGGLE_IS_LOADING_PROJECTS', () => {
it('toggles the isLoadingProjects boolean', () => {
mutations[types.TOGGLE_IS_LOADING_PROJECTS](localState);
expect(localState.isLoadingProjects).toEqual(true);
mutations[types.TOGGLE_IS_LOADING_PROJECTS](localState);
expect(localState.isLoadingProjects).toEqual(false);
});
});
describe('SET_MESSAGE_MINIMUM_QUERY', () => { describe('SET_MESSAGE_MINIMUM_QUERY', () => {
it('sets the messages.minimumQuery boolean', () => { it('sets the messages.minimumQuery boolean', () => {
mutations[types.SET_MESSAGE_MINIMUM_QUERY](localState, true); mutations[types.SET_MESSAGE_MINIMUM_QUERY](localState, true);
...@@ -78,8 +44,15 @@ describe('mutations', () => { ...@@ -78,8 +44,15 @@ describe('mutations', () => {
expect(localState.messages.minimumQuery).toEqual(true); expect(localState.messages.minimumQuery).toEqual(true);
mutations[types.SET_MESSAGE_MINIMUM_QUERY](localState, false); mutations[types.SET_MESSAGE_MINIMUM_QUERY](localState, false);
});
});
describe('SET_SEARCH_QUERY', () => {
it('sets the search query', () => {
const mockQuery = 'mock-query';
mutations[types.SET_SEARCH_QUERY](localState, mockQuery);
expect(localState.messages.minimumQuery).toEqual(false); expect(localState.searchQuery).toBe(mockQuery);
}); });
}); });
...@@ -110,98 +83,86 @@ describe('mutations', () => { ...@@ -110,98 +83,86 @@ describe('mutations', () => {
}); });
}); });
describe('CLEAR_SEARCH_RESULTS', () => { describe('RECEIVE_PROJECTS_SUCCESS', () => {
it('empties both the search results and the list of selected projects', () => { it('sets the project list and clears the loading status', () => {
localState.selectedProjects = [{ id: 1 }]; mutations[types.RECEIVE_PROJECTS_SUCCESS](localState, projects);
localState.projectSearchResults = [{ id: 1 }];
mutations[types.CLEAR_SEARCH_RESULTS](localState);
expect(localState.projectSearchResults).toEqual([]); expect(localState.projects).toEqual(projects);
expect(localState.selectedProjects).toEqual([]); expect(localState.isLoadingProjects).toBe(false);
}); });
}); });
describe('SEARCHED_WITH_NO_QUERY', () => { describe('RECEIVE_PROJECTS_ERROR', () => {
it(`resets all messages and sets state.projectSearchResults to an empty array`, () => { it('clears project list and the loading status', () => {
localState.projectSearchResults = [{ id: 1 }]; mutations[types.RECEIVE_PROJECTS_ERROR](localState);
localState.messages = {
noResults: true,
searchError: true,
minimumQuery: true,
};
mutations[types.SEARCHED_WITH_NO_QUERY](localState);
expect(localState.projectSearchResults).toEqual([]); expect(localState.projects).toEqual(null);
expect(localState.messages.noResults).toBe(false);
expect(localState.messages.searchError).toBe(false);
expect(localState.messages.minimumQuery).toBe(false); expect(localState.isLoadingProjects).toBe(false);
}); });
}); });
describe('SEARCHED_WITH_TOO_SHORT_QUERY', () => { describe('CLEAR_SEARCH_RESULTS', () => {
it(`sets the appropriate messages and sets state.projectSearchResults to an empty array`, () => { it('empties both the search results and the list of selected projects', () => {
localState.selectedProjects = [{ id: 1 }];
localState.projectSearchResults = [{ id: 1 }]; localState.projectSearchResults = [{ id: 1 }];
localState.messages = {
noResults: true,
searchError: true,
minimumQuery: false,
};
mutations[types.SEARCHED_WITH_TOO_SHORT_QUERY](localState); mutations[types.CLEAR_SEARCH_RESULTS](localState);
expect(localState.projectSearchResults).toEqual([]); expect(localState.projectSearchResults).toEqual([]);
expect(localState.messages.noResults).toBe(false); expect(localState.selectedProjects).toEqual([]);
});
});
expect(localState.messages.searchError).toBe(false); describe('REQUEST_SEARCH_RESULTS', () => {
it('turns off the minimum length warning and increments the search count', () => {
mutations[types.REQUEST_SEARCH_RESULTS](localState);
expect(localState.messages.minimumQuery).toBe(false);
expect(localState.messages.minimumQuery).toBe(true); expect(localState.searchCount).toEqual(1);
}); });
}); });
describe('SEARCHED_WITH_API_ERROR', () => { describe('RECEIVE_SEARCH_RESULTS_SUCCESS', () => {
it(`sets the appropriate messages and sets state.projectSearchResults to an empty array`, () => { it('resets all messages and sets state.projectSearchResults to the results from the API', () => {
localState.projectSearchResults = [{ id: 1 }]; localState.projectSearchResults = [];
localState.messages = { localState.messages = {
noResults: true, noResults: true,
searchError: false, searchError: true,
minimumQuery: true, minimumQuery: true,
}; };
mutations[types.SEARCHED_WITH_API_ERROR](localState); const searchResults = [{ id: 1 }];
expect(localState.projectSearchResults).toEqual([]); mutations[types.RECEIVE_SEARCH_RESULTS_SUCCESS](localState, searchResults);
expect(localState.projectSearchResults).toEqual(searchResults);
expect(localState.messages.noResults).toBe(false); expect(localState.messages.noResults).toBe(false);
expect(localState.messages.searchError).toBe(true); expect(localState.messages.searchError).toBe(false);
expect(localState.messages.minimumQuery).toBe(false); expect(localState.messages.minimumQuery).toBe(false);
}); });
});
describe('SEARCHED_SUCCESSFULLY_WITH_RESULTS', () => { it('resets all messages and sets state.projectSearchResults to an empty array if no results', () => {
it(`resets all messages and sets state.projectSearchResults to the results from the API`, () => {
localState.projectSearchResults = []; localState.projectSearchResults = [];
localState.messages = { localState.messages = {
noResults: true, noResults: false,
searchError: true, searchError: true,
minimumQuery: true, minimumQuery: true,
}; };
const searchResults = [{ id: 1 }]; const searchResults = [];
mutations[types.SEARCHED_SUCCESSFULLY_WITH_RESULTS](localState, searchResults); mutations[types.RECEIVE_SEARCH_RESULTS_SUCCESS](localState, searchResults);
expect(localState.projectSearchResults).toEqual(searchResults); expect(localState.projectSearchResults).toEqual(searchResults);
expect(localState.messages.noResults).toBe(false); expect(localState.messages.noResults).toBe(true);
expect(localState.messages.searchError).toBe(false); expect(localState.messages.searchError).toBe(false);
...@@ -209,22 +170,15 @@ describe('mutations', () => { ...@@ -209,22 +170,15 @@ describe('mutations', () => {
}); });
}); });
describe('SEARCHED_SUCCESSFULLY_NO_RESULTS', () => { describe('RECEIVE_SEARCH_RESULTS_ERROR', () => {
it(`sets the appropriate messages and sets state.projectSearchResults to an empty array`, () => { it('clears the search results', () => {
localState.projectSearchResults = [{ id: 1 }]; mutations[types.RECEIVE_SEARCH_RESULTS_ERROR](localState);
localState.messages = {
noResults: false,
searchError: true,
minimumQuery: true,
};
mutations[types.SEARCHED_SUCCESSFULLY_NO_RESULTS](localState);
expect(localState.projectSearchResults).toEqual([]); expect(localState.projectSearchResults).toEqual([]);
expect(localState.messages.noResults).toBe(true); expect(localState.messages.noResults).toBe(false);
expect(localState.messages.searchError).toBe(false); expect(localState.messages.searchError).toBe(true);
expect(localState.messages.minimumQuery).toBe(false); expect(localState.messages.minimumQuery).toBe(false);
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment