Skip to content

Commit

Permalink
refactor(vuex): change default-state object to a factory function (#183)
Browse files Browse the repository at this point in the history
closes #182
  • Loading branch information
devCrossNet authored and Johannes Werner committed Jul 25, 2018
1 parent da9422f commit 66d31fb
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 61 deletions.
2 changes: 1 addition & 1 deletion docs/guide/clean-up.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { CounterDefaultState, ICounterState } from './counter/state';
...

counter: {
...CounterDefaultState,
...CounterDefaultState(),
},
```

Expand Down
4 changes: 4 additions & 0 deletions docs/guide/styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ $font-family-headings: 'Oxygen', Helvetica, sans-serif;
$font-family: 'Lato', Helvetica, sans-serif;
$space-unit: 0.8rem;

/* Animations */
$fade-animation-transition: opacity $transition-duration * 4 ease-in-out;

/* Grid */
$screen-phone: 320px;
$screen-phone-max: $screen-phone - 1;
Expand All @@ -90,6 +93,7 @@ $screen-tablet-portrait-gutter: $space-unit * 2;
$screen-tablet-landscape-gutter: $space-unit * 2;
$screen-small-desktop-gutter: $space-unit * 4;
$screen-large-desktop-gutter: $space-unit * 4;

```

All the components are dependents of these variables, so everything will look different if you change these values.
Expand Down
5 changes: 0 additions & 5 deletions src/app/app/actions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ActionContext, Commit, Dispatch } from 'vuex';
import { IAppState } from './state';
import { AppActions } from './actions';
import { AppGetters } from './getters';

describe('AppActions', () => {
let testContext: ActionContext<IAppState, IAppState>;
Expand All @@ -10,10 +9,6 @@ describe('AppActions', () => {
testContext = {
dispatch: jest.fn() as Dispatch,
commit: jest.fn() as Commit,
state: {} as IAppState,
getters: {
...AppGetters,
},
} as ActionContext<IAppState, IAppState>;
});

Expand Down
13 changes: 9 additions & 4 deletions src/app/app/getters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { AppGetters } from './getters';
import { AppDefaultState } from './state';
import { AppGetters } from './getters';
import { AppDefaultState, IAppState } from './state';

describe('AppGetters', () => {
let testState: IAppState;

beforeEach(() => {
testState = AppDefaultState();
});

test('it should get the locale', () => {
expect(AppGetters.getLocale(AppDefaultState)).toBe(null);
expect(AppGetters.getLocale(testState)).toBe(null);
});

test('it should get the cookie consent version', () => {
expect(AppGetters.cookieConsentVersion(AppDefaultState)).toBe('');
expect(AppGetters.cookieConsentVersion(testState)).toBe('');
});

});
2 changes: 1 addition & 1 deletion src/app/app/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const AppModule: Module<IAppState, IAppState> = {
...AppGetters,
},
state: {
...AppDefaultState,
...AppDefaultState(),
},
mutations: {
...AppMutations,
Expand Down
3 changes: 1 addition & 2 deletions src/app/app/mutations.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { AppMutations } from './mutations';
import { AppDefaultState, IAppState } from './state';
import { cloneDeep } from 'lodash';

describe('AppMutations', () => {
let testState: IAppState;

beforeEach(() => {
testState = cloneDeep(AppDefaultState);
testState = AppDefaultState();
});

test('it should change the locale', () => {
Expand Down
14 changes: 8 additions & 6 deletions src/app/app/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export interface IAppState {
cookieConsentVersion: string;
}

export const AppDefaultState: IAppState = {
locale: null,
config: null,
defaultMessages: {},
redirectTo: null,
cookieConsentVersion: '',
export const AppDefaultState = (): IAppState => {
return {
locale: null,
config: null,
defaultMessages: {},
redirectTo: null,
cookieConsentVersion: '',
};
};
12 changes: 4 additions & 8 deletions src/app/counter/actions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import MockAdapter from 'axios-mock-adapter';
import { HttpService } from '../shared/services/HttpService';
import { ActionContext, Commit, Dispatch } from 'vuex';
import { CounterActions } from './actions';
import MockAdapter from 'axios-mock-adapter';
import { CounterDefaultState, ICounterState } from './state';
import { CounterGetters } from './getters';
import { CounterActions } from './actions';
import { HttpService } from '../shared/services/HttpService';

describe('CounterActions', () => {
let testContext: ActionContext<ICounterState, ICounterState>;
Expand All @@ -13,10 +12,7 @@ describe('CounterActions', () => {
testContext = {
dispatch: jest.fn() as Dispatch,
commit: jest.fn() as Commit,
state: CounterDefaultState,
getters: {
...CounterGetters,
},
state: CounterDefaultState(),
} as ActionContext<ICounterState, ICounterState>;

mockAxios = new MockAdapter(HttpService);
Expand Down
15 changes: 10 additions & 5 deletions src/app/counter/getters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { CounterGetters } from './getters';
import { CounterDefaultState } from './state';
import { CounterGetters } from './getters';
import { CounterDefaultState, ICounterState } from './state';

describe('CounterGetters', () => {
let testState: ICounterState;

beforeEach(() => {
testState = CounterDefaultState();
});

test('it should get the count', () => {
expect(CounterGetters.count(CounterDefaultState)).toBe(0);
expect(CounterGetters.count(testState)).toBe(0);
});

test('it should get increment pending', () => {
expect(CounterGetters.incrementPending(CounterDefaultState)).toBe(false);
expect(CounterGetters.incrementPending(testState)).toBe(false);
});

test('it should get decrement pending', () => {
expect(CounterGetters.decrementPending(CounterDefaultState)).toBe(false);
expect(CounterGetters.decrementPending(testState)).toBe(false);
});

});
2 changes: 1 addition & 1 deletion src/app/counter/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const CounterModule: Module<ICounterState, ICounterState> = {
...CounterGetters,
},
state: {
...CounterDefaultState,
...CounterDefaultState(),
},
mutations: {
...CounterMutations,
Expand Down
3 changes: 1 addition & 2 deletions src/app/counter/mutations.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { CounterMutations } from './mutations';
import { CounterDefaultState, ICounterState } from './state';
import { cloneDeep } from 'lodash';

describe('CounterMutations', () => {
let testState: ICounterState;

beforeEach(() => {
testState = cloneDeep(CounterDefaultState);
testState = CounterDefaultState();
});

test('it should set the count', () => {
Expand Down
10 changes: 6 additions & 4 deletions src/app/counter/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export interface ICounterState {
count: number;
}

export const CounterDefaultState: ICounterState = {
incrementPending: false,
decrementPending: false,
count: 0,
export const CounterDefaultState = (): ICounterState => {
return {
incrementPending: false,
decrementPending: false,
count: 0,
};
};
1 change: 0 additions & 1 deletion src/app/shared/styles/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ $badge-warn-color: $button-warn-color;
$badge-warn-bg: $button-warn-bg;

/* Carousel */

$carousel-min-height: 500px;
$carousel-copyright-color: $text-color;
$carousel-copyright-bottom: $space-unit * 2;
Expand Down
4 changes: 2 additions & 2 deletions src/app/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export interface IState {

export const DefaultState: IState = {
app: {
...AppDefaultState,
...AppDefaultState(),
},
counter: {
...CounterDefaultState,
...CounterDefaultState(),
},
};
2 changes: 1 addition & 1 deletion tools/generators/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const addModuleToState = (pathToAppActions: string, moduleName: string):
file = insertAt(
file,
findAstNodes(sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true).pop().end + 1,
`\n ${lowerFirst(moduleName)}: {\n ...${upperFirst(moduleName)}DefaultState,\n },`,
`\n ${lowerFirst(moduleName)}: {\n ...${upperFirst(moduleName)}DefaultState(),\n },`,
);

const interfaces: ts.Node[] = findAstNodes(sourceFile, ts.SyntaxKind.InterfaceDeclaration, true);
Expand Down
8 changes: 6 additions & 2 deletions tools/generators/connected/connected.spec.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe('{{ properCase componentName }}.vue', () => {
{{ camelCase moduleName }}: {
namespaced: true,
getters: {
getCount: () => 0,
count: () => 0,
incrementPending: () => false,
decrementPending: () => false,
},
actions: {
increment: jest.fn(),
Expand Down Expand Up @@ -43,7 +45,9 @@ describe('{{ properCase componentName }}.vue', () => {
{{ camelCase moduleName }}: {
namespaced: true,
getters: {
getCount: () => 0,
count: () => 0,
incrementPending: () => false,
decrementPending: () => false,
},
actions,
},
Expand Down
6 changes: 1 addition & 5 deletions tools/generators/module/actions.spec.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HttpService } from '../shared/services/HttpServi
import { ActionContext, Commit, Dispatch } from 'vuex';
import { {{ properCase moduleName }}Actions } from './actions';
import { {{ properCase moduleName }}DefaultState, I{{ properCase moduleName }}State } from './state';
import { {{ properCase moduleName }}Getters } from './getters';

describe('{{ properCase moduleName }}Actions', () => {
let testContext: ActionContext<I{{ properCase moduleName }}State, I{{ properCase moduleName }}State>;
Expand All @@ -13,10 +12,7 @@ describe('{{ properCase moduleName }}Actions', () => {
testContext = {
dispatch: jest.fn() as Dispatch,
commit: jest.fn() as Commit,
state: {{ properCase moduleName }}DefaultState,
getters: {
...{{ properCase moduleName }}Getters,
},
state: {{ properCase moduleName }}DefaultState(),
} as ActionContext<I{{ properCase moduleName }}State, I{{ properCase moduleName }}State>;

mockAxios = new MockAdapter(HttpService);
Expand Down
13 changes: 9 additions & 4 deletions tools/generators/module/getters.spec.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { {{ properCase moduleName }}Getters } from './getters';
import { {{ properCase moduleName }}DefaultState } from './state';
import { {{ properCase moduleName }}DefaultState, I{{ properCase moduleName }}State } from './state';

describe('{{ properCase moduleName }}Getters', () => {
let testState: I{{ properCase moduleName }}State;

beforeEach(() => {
testState = {{ properCase moduleName }}DefaultState();
});

test('it should get the count', () => {
expect({{ properCase moduleName }}Getters.count({{ properCase moduleName }}DefaultState)).toBe(0);
expect({{ properCase moduleName }}Getters.count(testState)).toBe(0);
});

test('it should get increment pending', () => {
expect({{ properCase moduleName }}Getters.incrementPending({{ properCase moduleName }}DefaultState)).toBe(false);
expect({{ properCase moduleName }}Getters.incrementPending(testState)).toBe(false);
});

test('it should get decrement pending', () => {
expect({{ properCase moduleName }}Getters.decrementPending({{ properCase moduleName }}DefaultState)).toBe(false);
expect({{ properCase moduleName }}Getters.decrementPending(testState)).toBe(false);
});

});
2 changes: 1 addition & 1 deletion tools/generators/module/module.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const {{ properCase moduleName }}Module: Module<I{{ properCase moduleName
...{{ properCase moduleName }}Getters,
},
state: {
...{{ properCase moduleName }}DefaultState,
...{{ properCase moduleName }}DefaultState(),
},
mutations: {
...{{ properCase moduleName }}Mutations,
Expand Down
3 changes: 1 addition & 2 deletions tools/generators/module/mutations.spec.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { {{ properCase moduleName }}Mutations } from './mutations';
import { {{ properCase moduleName }}DefaultState, I{{ properCase moduleName }}State } from './state';
import { cloneDeep } from 'lodash';

describe('{{ properCase moduleName }}Mutations', () => {
let testState: I{{ properCase moduleName }}State;

beforeEach(() => {
testState = cloneDeep({{ properCase moduleName }}DefaultState);
testState = {{ properCase moduleName }}DefaultState();
});

test('it should set the count', () => {
Expand Down
10 changes: 6 additions & 4 deletions tools/generators/module/state.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export interface I{{ properCase moduleName }}State {
count: number;
}

export const {{ properCase moduleName }}DefaultState: I{{ properCase moduleName }}State = {
incrementPending: false,
decrementPending: false,
count: 0,
export const {{ properCase moduleName }}DefaultState = (): I{{ properCase moduleName }}State => {
return {
incrementPending: false,
decrementPending: false,
count: 0,
};
};

0 comments on commit 66d31fb

Please sign in to comment.