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 }) => {
};
export const receiveProjectsSuccess = ({ commit }, data) => {
commit(types.SET_PROJECTS, data.projects);
commit(types.RECEIVE_PROJECTS_SUCCESS, data.projects);
};
export const receiveProjectsError = ({ commit }) => {
commit(types.SET_PROJECTS, null);
commit(types.RECEIVE_PROJECTS_ERROR);
createFlash(__('Something went wrong, unable to get operations projects'));
};
......@@ -135,53 +135,36 @@ export const removeProject = ({ dispatch }, removePath) => {
.catch(() => dispatch('receiveRemoveProjectError'));
};
export const receiveRemoveProjectSuccess = ({ dispatch }) => {
dispatch('fetchProjects');
};
export const receiveRemoveProjectSuccess = ({ dispatch }) => dispatch('fetchProjects');
export const receiveRemoveProjectError = () => {
createFlash(__('Something went wrong, unable to remove project'));
};
export const setSearchQuery = ({ commit }, query) => {
commit(types.SET_SEARCH_QUERY, query);
};
export const setSearchQuery = ({ commit }, query) => commit(types.SET_SEARCH_QUERY, query);
export const fetchSearchResults = ({ state, dispatch }) => {
dispatch('requestSearchResults');
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);
dispatch('receiveSearchResultsError');
} else if (state.searchQuery.lengh < API_MINIMUM_QUERY_LENGTH) {
dispatch('receiveSearchResultsError', 'minimumQuery');
} else {
commit(types.INCREMENT_PROJECT_SEARCH_COUNT, 1);
dispatch('requestSearchResults');
Api.projects(state.searchQuery, {})
.then(results => dispatch('receiveSearchResultsSuccess', results))
.catch(() => dispatch('receiveSearchResultsError'));
}
};
export const requestSearchResults = ({ commit }) => {
// 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 requestSearchResults = ({ commit }) => commit(types.REQUEST_SEARCH_RESULTS);
export const receiveSearchResultsSuccess = ({ commit }, results) => {
if (results.length === 0) {
commit(types.SEARCHED_SUCCESSFULLY_NO_RESULTS);
} else {
commit(types.SEARCHED_SUCCESSFULLY_WITH_RESULTS, results);
}
return commit(types.DECREMENT_PROJECT_SEARCH_COUNT, 1);
commit(types.RECEIVE_SEARCH_RESULTS_SUCCESS, results);
};
export const receiveSearchResultsError = ({ commit }) => {
commit(types.SEARCHED_WITH_API_ERROR);
commit(types.DECREMENT_PROJECT_SEARCH_COUNT, 1);
commit(types.RECEIVE_SEARCH_RESULTS_ERROR);
};
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_ADD = 'SET_PROJECT_ENDPOINT_ADD';
export const SET_PROJECTS = 'SET_PROJECTS';
export const SET_SEARCH_QUERY = 'SET_SEARCH_QUERY';
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 ADD_SELECTED_PROJECT = 'ADD_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 SEARCHED_WITH_NO_QUERY = 'SEARCHED_WITH_NO_QUERY';
export const SEARCHED_WITH_TOO_SHORT_QUERY = 'SEARCHED_WITH_TOO_SHORT_QUERY';
export const SEARCHED_WITH_API_ERROR = 'SEARCHED_WITH_API_ERROR';
export const SEARCHED_SUCCESSFULLY_WITH_RESULTS = 'SEARCHED_SUCCESSFULLY_WITH_RESULTS';
export const SEARCHED_SUCCESSFULLY_NO_RESULTS = 'SEARCHED_SUCCESSFULLY_NO_RESULTS';
export const REQUEST_SEARCH_RESULTS = 'REQUEST_SEARCH_RESULTS';
export const RECEIVE_SEARCH_RESULTS_SUCCESS = 'RECEIVE_SEARCH_RESULTS_SUCCESS';
export const RECEIVE_SEARCH_RESULTS_ERROR = 'RECEIVE_SEARCH_RESULTS_ERROR';
......@@ -2,13 +2,6 @@ import _ from 'underscore';
import * as types from './mutation_types';
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) {
state.projectEndpoints.list = url;
},
......@@ -26,9 +19,6 @@ export default {
[types.SET_MESSAGE_MINIMUM_QUERY](state, bool) {
state.messages.minimumQuery = bool;
},
[types.REQUEST_PROJECTS](state) {
state.isLoadingProjects = true;
},
[types.ADD_SELECTED_PROJECT](state, project) {
if (!state.selectedProjects.some(p => p.id === project.id)) {
......@@ -42,8 +32,16 @@ export default {
);
},
[types.TOGGLE_IS_LOADING_PROJECTS](state) {
state.isLoadingProjects = !state.isLoadingProjects;
[types.REQUEST_PROJECTS](state) {
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) {
......@@ -51,38 +49,26 @@ export default {
state.selectedProjects = [];
},
[types.SEARCHED_WITH_NO_QUERY](state) {
state.projectSearchResults = [];
state.messages.noResults = false;
state.messages.searchError = false;
[types.REQUEST_SEARCH_RESULTS](state) {
// Flipping this property separately to allows the UI
// to hide the "minimum query" message
// before the seach results arrive from the API
state.messages.minimumQuery = false;
},
[types.SEARCHED_WITH_TOO_SHORT_QUERY](state) {
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;
state.searchCount += 1;
},
[types.SEARCHED_SUCCESSFULLY_WITH_RESULTS](state, results) {
[types.RECEIVE_SEARCH_RESULTS_SUCCESS](state, results) {
state.projectSearchResults = results;
state.messages.noResults = false;
state.messages.noResults = state.projectSearchResults.length === 0;
state.messages.searchError = false;
state.messages.minimumQuery = false;
state.searchCount -= 1;
},
[types.SEARCHED_SUCCESSFULLY_NO_RESULTS](state) {
[types.RECEIVE_SEARCH_RESULTS_ERROR](state, message) {
state.projectSearchResults = [];
state.messages.noResults = true;
state.messages.searchError = false;
state.messages.minimumQuery = false;
state.messages.noResults = false;
state.messages.searchError = true;
state.messages.minimumQuery = message === 'minimumQuery';
state.searchCount -= 1;
},
};
......@@ -243,7 +243,7 @@ describe('actions', () => {
store.state,
[
{
type: types.SET_PROJECTS,
type: types.RECEIVE_PROJECTS_SUCCESS,
payload: mockProjects,
},
],
......@@ -264,8 +264,7 @@ describe('actions', () => {
store.state,
[
{
type: types.SET_PROJECTS,
payload: null,
type: types.RECEIVE_PROJECTS_ERROR,
},
],
[],
......@@ -313,7 +312,7 @@ describe('actions', () => {
null,
null,
[],
[{ type: 'forceProjectsRequest' }],
[{ type: 'fetchProjects' }],
done,
);
});
......@@ -330,36 +329,42 @@ describe('actions', () => {
});
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 = '';
testAction(
actions.fetchSearchResults,
null,
store.state,
[],
[
{
type: types.SEARCHED_WITH_NO_QUERY,
type: 'requestSearchResults',
},
{
type: 'receiveSearchResultsError',
},
],
[],
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';
testAction(
actions.fetchSearchResults,
null,
store.state,
[],
[
{
type: types.SEARCHED_WITH_TOO_SHORT_QUERY,
type: 'requestSearchResults',
},
{
type: 'receiveSearchResultsError',
},
],
[],
done,
);
});
......@@ -372,12 +377,7 @@ describe('actions', () => {
actions.fetchSearchResults,
null,
store.state,
[
{
type: types.INCREMENT_PROJECT_SEARCH_COUNT,
payload: 1,
},
],
[],
[
{
type: 'requestSearchResults',
......@@ -393,15 +393,14 @@ describe('actions', () => {
});
describe('requestSearchResults', () => {
it(`unsets the 'query is too short' warning status`, done => {
it(`commits the REQUEST_SEARCH_RESULTS mutation`, done => {
testAction(
actions.requestSearchResults,
null,
store.state,
[
{
type: types.SET_MESSAGE_MINIMUM_QUERY,
payload: false,
type: types.REQUEST_SEARCH_RESULTS,
},
],
[],
......@@ -411,41 +410,16 @@ describe('actions', () => {
});
describe('receiveSearchResultsSuccess', () => {
it('sets project search results', done => {
it('commits the RECEIVE_SEARCH_RESULTS_SUCCESS mutation', done => {
testAction(
actions.receiveSearchResultsSuccess,
mockProjects,
store.state,
[
{
type: types.SEARCHED_SUCCESSFULLY_WITH_RESULTS,
type: types.RECEIVE_SEARCH_RESULTS_SUCCESS,
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,
......@@ -454,18 +428,14 @@ describe('actions', () => {
});
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(
actions.receiveSearchResultsError,
['error'],
store.state,
[
{
type: types.SEARCHED_WITH_API_ERROR,
},
{
type: types.DECREMENT_PROJECT_SEARCH_COUNT,
payload: 1,
type: types.RECEIVE_SEARCH_RESULTS_ERROR,
},
],
[],
......
......@@ -6,34 +6,12 @@ import { mockProjectData } from '../mock_data';
describe('mutations', () => {
const projects = mockProjectData(3);
const mockEndpoint = 'https://mock-endpoint';
const mockSearches = new Array(5).fill(null);
let localState;
beforeEach(() => {
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', () => {
it('sets project list endpoint', () => {
mutations[types.SET_PROJECT_ENDPOINT_LIST](localState, mockEndpoint);
......@@ -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', () => {
it('sets the messages.minimumQuery boolean', () => {
mutations[types.SET_MESSAGE_MINIMUM_QUERY](localState, true);
......@@ -78,8 +44,15 @@ describe('mutations', () => {
expect(localState.messages.minimumQuery).toEqual(true);
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', () => {
});
});
describe('CLEAR_SEARCH_RESULTS', () => {
it('empties both the search results and the list of selected projects', () => {
localState.selectedProjects = [{ id: 1 }];
localState.projectSearchResults = [{ id: 1 }];
describe('RECEIVE_PROJECTS_SUCCESS', () => {
it('sets the project list and clears the loading status', () => {
mutations[types.RECEIVE_PROJECTS_SUCCESS](localState, projects);
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', () => {
it(`resets all messages and sets state.projectSearchResults to an empty array`, () => {
localState.projectSearchResults = [{ id: 1 }];
localState.messages = {
noResults: true,
searchError: true,
minimumQuery: true,
};
mutations[types.SEARCHED_WITH_NO_QUERY](localState);
expect(localState.projectSearchResults).toEqual([]);
describe('RECEIVE_PROJECTS_ERROR', () => {
it('clears project list and the loading status', () => {
mutations[types.RECEIVE_PROJECTS_ERROR](localState);
expect(localState.messages.noResults).toBe(false);
expect(localState.projects).toEqual(null);
expect(localState.messages.searchError).toBe(false);
expect(localState.messages.minimumQuery).toBe(false);
expect(localState.isLoadingProjects).toBe(false);
});
});
describe('SEARCHED_WITH_TOO_SHORT_QUERY', () => {
it(`sets the appropriate messages and sets state.projectSearchResults to an empty array`, () => {
describe('CLEAR_SEARCH_RESULTS', () => {
it('empties both the search results and the list of selected projects', () => {
localState.selectedProjects = [{ 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.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(true);
expect(localState.messages.minimumQuery).toBe(false);
expect(localState.searchCount).toEqual(1);
});
});
describe('SEARCHED_WITH_API_ERROR', () => {
it(`sets the appropriate messages and sets state.projectSearchResults to an empty array`, () => {
localState.projectSearchResults = [{ id: 1 }];
describe('RECEIVE_SEARCH_RESULTS_SUCCESS', () => {
it('resets all messages and sets state.projectSearchResults to the results from the API', () => {
localState.projectSearchResults = [];
localState.messages = {
noResults: true,
searchError: false,
searchError: 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.searchError).toBe(true);
expect(localState.messages.searchError).toBe(false);
expect(localState.messages.minimumQuery).toBe(false);
});
});
describe('SEARCHED_SUCCESSFULLY_WITH_RESULTS', () => {
it(`resets all messages and sets state.projectSearchResults to the results from the API`, () => {
it('resets all messages and sets state.projectSearchResults to an empty array if no results', () => {
localState.projectSearchResults = [];
localState.messages = {
noResults: true,
noResults: false,
searchError: 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.messages.noResults).toBe(false);
expect(localState.messages.noResults).toBe(true);
expect(localState.messages.searchError).toBe(false);
......@@ -209,22 +170,15 @@ describe('mutations', () => {
});
});
describe('SEARCHED_SUCCESSFULLY_NO_RESULTS', () => {
it(`sets the appropriate messages and sets state.projectSearchResults to an empty array`, () => {
localState.projectSearchResults = [{ id: 1 }];
localState.messages = {
noResults: false,
searchError: true,
minimumQuery: true,
};
mutations[types.SEARCHED_SUCCESSFULLY_NO_RESULTS](localState);
describe('RECEIVE_SEARCH_RESULTS_ERROR', () => {
it('clears the search results', () => {
mutations[types.RECEIVE_SEARCH_RESULTS_ERROR](localState);
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);
});
......
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