Skip to content

Commit fb933c1

Browse files
Пинчук Виталий АПинчук Виталий А
Пинчук Виталий А
authored and
Пинчук Виталий А
committed
Migrating to direct-vuex (runtime esm & systemJs is ok), but jest SomeForm actions.spec.ts is failed
1 parent b1297b3 commit fb933c1

File tree

10 files changed

+90
-45
lines changed

10 files changed

+90
-45
lines changed

gulpfile.esm.js

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ task('postdeploy.dev:fixImportsInIndex',
8888
`${absDest}/**/index.js`,
8989
])
9090
// my export with singlequoted paths
91+
// TODO: create common regexp for resolving modules
92+
.pipe(gReplace(/from[\s]?['|"]direct-vuex['|"]/g, `from '${esmImportmapPaths['direct-vuex']}'`))
9193
.pipe(gReplace(/^(?!.*(\.js|=))(.*)(";)/gm, '$2.js$3'))
9294
.pipe(dest(absDest)));
9395

src/VueApp/store/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/no-cycle */
12
export {
23
moduleActionContext,
34
store,

src/VueApp/store/modules.conf.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// modules.conf filename because of gulp task 'postdeploy.dev:fixImportsNotInIndex'
22
// not in index because of replacing assets not in index
33
// TODO: make gulp working with store/index.js (simple vuex-modules)
4-
import { name as storeNameSomeForm, storeConf as storeConfSomeForm } from '@unique/SomeForm/store';
4+
import { name as storeNameSomeForm, modSomeForm } from '@unique/SomeForm/store';
55

66
export const modulesConf = {
7-
[storeNameSomeForm]: storeConfSomeForm,
7+
[storeNameSomeForm]: modSomeForm,
88
};

src/unique.blocks/SomeForm/SomeForm.spec.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { createLocalVue, mount } from '@vue/test-utils';
22
import Vuex from 'vuex';
3+
import { createDirectStore } from 'direct-vuex';
34

45
import { IamSelect } from '@common/IamSelect';
56

6-
import { storeConf as moduleStoreConf, name as moduleStoreName } from './store';
7+
import { modSomeForm, name as moduleStoreName } from './store';
78
import { SomeForm } from './index';
89
// import { resolvedOptions } from '@services/SomeSvc/SomeSvc.spec.case01.ts';
910

@@ -15,11 +16,12 @@ localVue.use(Vuex);
1516

1617
const storeConf = {
1718
modules: {
18-
[moduleStoreName]: moduleStoreConf,
19+
[moduleStoreName]: modSomeForm,
1920
},
2021
};
2122

22-
const store = new Vuex.Store(storeConf);
23+
// const store = new Vuex.Store(storeConf);
24+
const { store } = createDirectStore(storeConf);
2325

2426
describe('@Component SomeForm', () => {
2527
it('all mock data are rendered', () => {

src/unique.blocks/SomeForm/store/actions.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as actions from './actions';
1+
import { actions } from './actions';
22
import * as confMutations from './mutations.conf';
33

44
jest.mock('@services/SomeSvc/SomeSvc.ts');
55

6-
// @common/IamSelect >>>
6+
// #region @common/IamSelect
77
// Пишем простейшие тесты, чтобы при рефакторинге actions, видеть, что не отвалились
88
describe('vuex SomeForm IamSelect actions', () => {
99
it('getPoints after getting data raise appropriate action', async () => {
@@ -37,4 +37,4 @@ describe('vuex SomeForm IamSelect actions', () => {
3737
expect(commit).toHaveBeenCalledWith(confMutations.SOME_VALUE_SELECT, someValue);
3838
});
3939
});
40-
// <<< @common/IamSelect
40+
// #endregion
+44-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
1+
import { ActionContext } from 'vuex';
2+
import DirectVuex from 'direct-vuex';
3+
14
import { IOption as ISomeValue } from '@common/IamSelect/IamSelect.option.i';
25
import { SomeSvc } from '@services/SomeSvc';
3-
// import { moduleActionContext } from '~/VueApp/store';
6+
import { moduleActionContext } from '~/VueApp/store';
7+
8+
/* eslint-disable import/no-cycle */
9+
import { modSomeForm } from '.';
410

511
import * as mutationTypes from './mutations.conf';
12+
import { IState } from './state.i';
13+
14+
// Not naming imported cause of SystemJs import only default
15+
// https://github.com/paleo/direct-vuex/issues/14#issuecomment-568543969
16+
const { createActions } = DirectVuex;
17+
18+
export const modSomeFormActionContext = (ctx: any) => moduleActionContext(ctx, modSomeForm);
19+
20+
export const actions = createActions({
21+
getSomeValues(
22+
// commit полученных данных выполняется другим (синхронным) action, потому dispatch
23+
// { dispatch }: { dispatch: Function },
24+
context: ActionContext<IState, IState>,
25+
{ label }: ISomeValue,
26+
): Promise <Array<ISomeValue> | void> {
27+
return SomeSvc
28+
.fetchData({ label }) // Все опции запроса в одном параметре-объекте (помимо label)
29+
.then((resp) => {
30+
// const { dispatch } = context;
31+
const { dispatch } = modSomeFormActionContext(context);
32+
dispatch.setSomeValues(resp);
33+
});
34+
},
35+
36+
setSomeValues(
37+
{ commit }: { commit: Function },
38+
values: Array<ISomeValue>,
39+
): void {
40+
commit(mutationTypes.SOME_VALUES_SET, values);
41+
},
642

7-
export const getSomeValues = (
8-
// commit полученных данных выполняется другим (синхронным) action, потому dispatch
9-
{ dispatch }: { dispatch: Function },
10-
{ label }: ISomeValue,
11-
): Promise <Array<ISomeValue> | void> => SomeSvc
12-
.fetchData({ label }) // Все опции запроса в одном параметре-объекте (помимо label)
13-
.then((resp) => dispatch('setSomeValues', resp));
14-
15-
export const setSomeValues = (
16-
{ commit }: { commit: Function },
17-
values: Array<ISomeValue>,
18-
): void => commit(mutationTypes.SOME_VALUES_SET, values);
19-
20-
export const selectSomeValue = (
21-
{ commit }: { commit: Function },
22-
value: ISomeValue | null,
23-
): void => commit(mutationTypes.SOME_VALUE_SELECT, value);
43+
selectSomeValue(
44+
{ commit }: { commit: Function },
45+
value: ISomeValue | null,
46+
): void {
47+
commit(mutationTypes.SOME_VALUE_SELECT, value);
48+
},
49+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { IOption as ISomeValue } from '@common/IamSelect/IamSelect.option.i';
2+
3+
import { IState } from './state.i';
4+
5+
export const someValue = (state: IState): ISomeValue | null => state.iamSelect.value;
6+
export const someValues = (state: IState): Array<ISomeValue> => state.iamSelect.data.options;
+12-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { IOption as ISomeValue } from '@common/IamSelect/IamSelect.option.i';
1+
import DirectVuex from 'direct-vuex';
22

3-
import * as actions from './actions';
4-
import * as mutationTypes from './mutations.conf';
3+
/* eslint-disable import/no-cycle */
4+
import { actions } from './actions';
5+
6+
import * as getters from './getters';
7+
import { mutations } from './mutations';
58
import { IState } from './state.i';
69

710
const defaultState: IState = {
@@ -13,20 +16,6 @@ const defaultState: IState = {
1316
},
1417
};
1518

16-
const getters = {
17-
someValue: (state: IState): ISomeValue | null => state.iamSelect.value,
18-
someValues: (state: IState): Array<ISomeValue> => state.iamSelect.data.options,
19-
};
20-
21-
const mutations = {
22-
[mutationTypes.SOME_VALUE_SELECT](state: IState, value: ISomeValue): void {
23-
state.iamSelect.value = value;
24-
},
25-
[mutationTypes.SOME_VALUES_SET](state: IState, values: Array<ISomeValue>): void {
26-
state.iamSelect.data.options = values;
27-
},
28-
};
29-
3019
export const name = 'someForm';
3120

3221
export const storeConf = {
@@ -39,3 +28,9 @@ export const storeConf = {
3928
// but we declare type with const above
4029
// state: defaultState as IState,
4130
} as const;
31+
32+
// Not naming imported cause of SystemJs import only default
33+
// https://github.com/paleo/direct-vuex/issues/14#issuecomment-568543969
34+
const { createModule } = DirectVuex;
35+
36+
export const modSomeForm = createModule(storeConf);
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// @common/IamSelect >>>
1+
// #region @common/IamSelect
22
// select, чтобы не путать с set, поскольку значение не абы какое,
33
// а соответствует одному из имеющихся в store
44
export const SOME_VALUE_SELECT = 'SOME_VALUE_SELECT';
55
// set подразумевает замещение данных
66
export const SOME_VALUES_SET = 'SOME_VALUES_SET';
7-
// <<< @common/IamSelect
7+
// #endregion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { IOption as ISomeValue } from '@common/IamSelect/IamSelect.option.i';
2+
3+
import { IState } from './state.i';
4+
import * as mutationTypes from './mutations.conf';
5+
6+
export const mutations = {
7+
[mutationTypes.SOME_VALUE_SELECT](state: IState, value: ISomeValue): void {
8+
state.iamSelect.value = value;
9+
},
10+
[mutationTypes.SOME_VALUES_SET](state: IState, values: Array<ISomeValue>): void {
11+
state.iamSelect.data.options = values;
12+
},
13+
};

0 commit comments

Comments
 (0)