Commit 7b5f3136 authored by Nathan Friend's avatar Nathan Friend

Pass initial state to store creation function

This commit updates the Release Edit and Show pages to pass their
initial Vuex store state as a parameter to the store creation function
rather than setting this state data using a `setInitialState` action.
parent 3c4caf0b
import Vue from 'vue'; import Vue from 'vue';
import ReleaseEditApp from './components/app_edit.vue'; import ReleaseEditApp from './components/app_edit.vue';
import createStore from './stores'; import createStore from './stores';
import detailModule from './stores/modules/detail'; import createDetailModule from './stores/modules/detail';
export default () => { export default () => {
const el = document.getElementById('js-edit-release-page'); const el = document.getElementById('js-edit-release-page');
const store = createStore({ const store = createStore({
modules: { modules: {
detail: detailModule, detail: createDetailModule(el.dataset),
}, },
featureFlags: { featureFlags: {
releaseShowPage: Boolean(gon.features?.releaseShowPage), releaseShowPage: Boolean(gon.features?.releaseShowPage),
}, },
}); });
store.dispatch('detail/setInitialState', el.dataset);
return new Vue({ return new Vue({
el, el,
store, store,
......
import Vue from 'vue'; import Vue from 'vue';
import ReleaseShowApp from './components/app_show.vue'; import ReleaseShowApp from './components/app_show.vue';
import createStore from './stores'; import createStore from './stores';
import detailModule from './stores/modules/detail'; import createDetailModule from './stores/modules/detail';
export default () => { export default () => {
const el = document.getElementById('js-show-release-page'); const el = document.getElementById('js-show-release-page');
const store = createStore({ const store = createStore({
modules: { modules: {
detail: detailModule, detail: createDetailModule(el.dataset),
}, },
}); });
store.dispatch('detail/setInitialState', el.dataset);
return new Vue({ return new Vue({
el, el,
......
...@@ -5,9 +5,6 @@ import { s__ } from '~/locale'; ...@@ -5,9 +5,6 @@ import { s__ } from '~/locale';
import { redirectTo } from '~/lib/utils/url_utility'; import { redirectTo } from '~/lib/utils/url_utility';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils'; import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
export const setInitialState = ({ commit }, initialState) =>
commit(types.SET_INITIAL_STATE, initialState);
export const requestRelease = ({ commit }) => commit(types.REQUEST_RELEASE); export const requestRelease = ({ commit }) => commit(types.REQUEST_RELEASE);
export const receiveReleaseSuccess = ({ commit }, data) => export const receiveReleaseSuccess = ({ commit }, data) =>
commit(types.RECEIVE_RELEASE_SUCCESS, data); commit(types.RECEIVE_RELEASE_SUCCESS, data);
......
import * as actions from './actions'; import * as actions from './actions';
import mutations from './mutations'; import mutations from './mutations';
import state from './state'; import createState from './state';
export default { export default initialState => ({
namespaced: true, namespaced: true,
actions, actions,
mutations, mutations,
state, state: createState(initialState),
}; });
export const SET_INITIAL_STATE = 'SET_INITIAL_STATE';
export const REQUEST_RELEASE = 'REQUEST_RELEASE'; export const REQUEST_RELEASE = 'REQUEST_RELEASE';
export const RECEIVE_RELEASE_SUCCESS = 'RECEIVE_RELEASE_SUCCESS'; export const RECEIVE_RELEASE_SUCCESS = 'RECEIVE_RELEASE_SUCCESS';
export const RECEIVE_RELEASE_ERROR = 'RECEIVE_RELEASE_ERROR'; export const RECEIVE_RELEASE_ERROR = 'RECEIVE_RELEASE_ERROR';
......
import * as types from './mutation_types'; import * as types from './mutation_types';
export default { export default {
[types.SET_INITIAL_STATE](state, initialState) {
Object.keys(state).forEach(key => {
state[key] = initialState[key];
});
},
[types.REQUEST_RELEASE](state) { [types.REQUEST_RELEASE](state) {
state.isFetchingRelease = true; state.isFetchingRelease = true;
}, },
......
export default () => ({ export default ({
projectId: null, projectId,
tagName: null, tagName,
releasesPagePath: null, releasesPagePath,
markdownDocsPath: null, markdownDocsPath,
markdownPreviewPath: null, markdownPreviewPath,
updateReleaseApiDocsPath: null, updateReleaseApiDocsPath,
}) => ({
projectId,
tagName,
releasesPagePath,
markdownDocsPath,
markdownPreviewPath,
updateReleaseApiDocsPath,
release: null, release: null,
......
...@@ -24,7 +24,14 @@ describe('Release detail actions', () => { ...@@ -24,7 +24,14 @@ describe('Release detail actions', () => {
let error; let error;
beforeEach(() => { beforeEach(() => {
state = createState(); state = createState({
projectId: '18',
tagName: 'v1.3',
releasesPagePath: 'path/to/releases/page',
markdownDocsPath: 'path/to/markdown/docs',
markdownPreviewPath: 'path/to/markdown/preview',
updateReleaseApiDocsPath: 'path/to/api/docs',
});
release = cloneDeep(originalRelease); release = cloneDeep(originalRelease);
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
gon.api_version = 'v4'; gon.api_version = 'v4';
...@@ -36,16 +43,6 @@ describe('Release detail actions', () => { ...@@ -36,16 +43,6 @@ describe('Release detail actions', () => {
mock.restore(); mock.restore();
}); });
describe('setInitialState', () => {
it(`commits ${types.SET_INITIAL_STATE} with the provided object`, () => {
const initialState = {};
return testAction(actions.setInitialState, initialState, state, [
{ type: types.SET_INITIAL_STATE, payload: initialState },
]);
});
});
describe('requestRelease', () => { describe('requestRelease', () => {
it(`commits ${types.REQUEST_RELEASE}`, () => it(`commits ${types.REQUEST_RELEASE}`, () =>
testAction(actions.requestRelease, undefined, state, [{ type: types.REQUEST_RELEASE }])); testAction(actions.requestRelease, undefined, state, [{ type: types.REQUEST_RELEASE }]));
......
...@@ -5,115 +5,106 @@ ...@@ -5,115 +5,106 @@
* is resolved * is resolved
*/ */
import state from '~/releases/stores/modules/detail/state'; import createState from '~/releases/stores/modules/detail/state';
import mutations from '~/releases/stores/modules/detail/mutations'; import mutations from '~/releases/stores/modules/detail/mutations';
import * as types from '~/releases/stores/modules/detail/mutation_types'; import * as types from '~/releases/stores/modules/detail/mutation_types';
import { release } from '../../../mock_data'; import { release } from '../../../mock_data';
describe('Release detail mutations', () => { describe('Release detail mutations', () => {
let stateClone; let state;
let releaseClone; let releaseClone;
beforeEach(() => { beforeEach(() => {
stateClone = state(); state = createState({
releaseClone = JSON.parse(JSON.stringify(release));
});
describe(types.SET_INITIAL_STATE, () => {
it('populates the state with initial values', () => {
const initialState = {
projectId: '18', projectId: '18',
tagName: 'v1.3', tagName: 'v1.3',
releasesPagePath: 'path/to/releases/page', releasesPagePath: 'path/to/releases/page',
markdownDocsPath: 'path/to/markdown/docs', markdownDocsPath: 'path/to/markdown/docs',
markdownPreviewPath: 'path/to/markdown/preview', markdownPreviewPath: 'path/to/markdown/preview',
}; updateReleaseApiDocsPath: 'path/to/api/docs',
mutations[types.SET_INITIAL_STATE](stateClone, initialState);
expect(stateClone).toEqual(expect.objectContaining(initialState));
}); });
releaseClone = JSON.parse(JSON.stringify(release));
}); });
describe(types.REQUEST_RELEASE, () => { describe(types.REQUEST_RELEASE, () => {
it('set state.isFetchingRelease to true', () => { it('set state.isFetchingRelease to true', () => {
mutations[types.REQUEST_RELEASE](stateClone); mutations[types.REQUEST_RELEASE](state);
expect(stateClone.isFetchingRelease).toEqual(true); expect(state.isFetchingRelease).toEqual(true);
}); });
}); });
describe(types.RECEIVE_RELEASE_SUCCESS, () => { describe(types.RECEIVE_RELEASE_SUCCESS, () => {
it('handles a successful response from the server', () => { it('handles a successful response from the server', () => {
mutations[types.RECEIVE_RELEASE_SUCCESS](stateClone, releaseClone); mutations[types.RECEIVE_RELEASE_SUCCESS](state, releaseClone);
expect(stateClone.fetchError).toEqual(undefined); expect(state.fetchError).toEqual(undefined);
expect(stateClone.isFetchingRelease).toEqual(false); expect(state.isFetchingRelease).toEqual(false);
expect(stateClone.release).toEqual(releaseClone); expect(state.release).toEqual(releaseClone);
}); });
}); });
describe(types.RECEIVE_RELEASE_ERROR, () => { describe(types.RECEIVE_RELEASE_ERROR, () => {
it('handles an unsuccessful response from the server', () => { it('handles an unsuccessful response from the server', () => {
const error = { message: 'An error occurred!' }; const error = { message: 'An error occurred!' };
mutations[types.RECEIVE_RELEASE_ERROR](stateClone, error); mutations[types.RECEIVE_RELEASE_ERROR](state, error);
expect(stateClone.isFetchingRelease).toEqual(false); expect(state.isFetchingRelease).toEqual(false);
expect(stateClone.release).toBeUndefined(); expect(state.release).toBeUndefined();
expect(stateClone.fetchError).toEqual(error); expect(state.fetchError).toEqual(error);
}); });
}); });
describe(types.UPDATE_RELEASE_TITLE, () => { describe(types.UPDATE_RELEASE_TITLE, () => {
it("updates the release's title", () => { it("updates the release's title", () => {
stateClone.release = releaseClone; state.release = releaseClone;
const newTitle = 'The new release title'; const newTitle = 'The new release title';
mutations[types.UPDATE_RELEASE_TITLE](stateClone, newTitle); mutations[types.UPDATE_RELEASE_TITLE](state, newTitle);
expect(stateClone.release.name).toEqual(newTitle); expect(state.release.name).toEqual(newTitle);
}); });
}); });
describe(types.UPDATE_RELEASE_NOTES, () => { describe(types.UPDATE_RELEASE_NOTES, () => {
it("updates the release's notes", () => { it("updates the release's notes", () => {
stateClone.release = releaseClone; state.release = releaseClone;
const newNotes = 'The new release notes'; const newNotes = 'The new release notes';
mutations[types.UPDATE_RELEASE_NOTES](stateClone, newNotes); mutations[types.UPDATE_RELEASE_NOTES](state, newNotes);
expect(stateClone.release.description).toEqual(newNotes); expect(state.release.description).toEqual(newNotes);
}); });
}); });
describe(types.REQUEST_UPDATE_RELEASE, () => { describe(types.REQUEST_UPDATE_RELEASE, () => {
it('set state.isUpdatingRelease to true', () => { it('set state.isUpdatingRelease to true', () => {
mutations[types.REQUEST_UPDATE_RELEASE](stateClone); mutations[types.REQUEST_UPDATE_RELEASE](state);
expect(stateClone.isUpdatingRelease).toEqual(true); expect(state.isUpdatingRelease).toEqual(true);
}); });
}); });
describe(types.RECEIVE_UPDATE_RELEASE_SUCCESS, () => { describe(types.RECEIVE_UPDATE_RELEASE_SUCCESS, () => {
it('handles a successful response from the server', () => { it('handles a successful response from the server', () => {
mutations[types.RECEIVE_UPDATE_RELEASE_SUCCESS](stateClone, releaseClone); mutations[types.RECEIVE_UPDATE_RELEASE_SUCCESS](state, releaseClone);
expect(stateClone.updateError).toEqual(undefined); expect(state.updateError).toEqual(undefined);
expect(stateClone.isUpdatingRelease).toEqual(false); expect(state.isUpdatingRelease).toEqual(false);
}); });
}); });
describe(types.RECEIVE_UPDATE_RELEASE_ERROR, () => { describe(types.RECEIVE_UPDATE_RELEASE_ERROR, () => {
it('handles an unsuccessful response from the server', () => { it('handles an unsuccessful response from the server', () => {
const error = { message: 'An error occurred!' }; const error = { message: 'An error occurred!' };
mutations[types.RECEIVE_UPDATE_RELEASE_ERROR](stateClone, error); mutations[types.RECEIVE_UPDATE_RELEASE_ERROR](state, error);
expect(stateClone.isUpdatingRelease).toEqual(false); expect(state.isUpdatingRelease).toEqual(false);
expect(stateClone.updateError).toEqual(error); expect(state.updateError).toEqual(error);
}); });
}); });
}); });
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