Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow getters to be reconfigured #420

Open
wants to merge 1 commit into
base: vue2-vuex3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,28 @@ import { config } from 'vuex-module-decorators'
// Set rawError to true by default on all @Action decorators
config.rawError = true
```

## Stubbing for tests

Actions and mutations can be stubbed like any other function using - for example - [`sinon`](https://sinonjs.org/).

By default, getters are readonly, and cannot be reconfigured, which prevents stubbing.

To override this behaviour in a test environment, set the `configurableGetters` flag:

```typescript
import { Module, VuexModule, Mutation } from 'vuex-module-decorators'

@Module({
name: 'MyModule',
configurableGetters: true,
})
export default class MyStoreModule extends VuexModule {
public test: string = 'initial'

@Mutation
public setTest(val: string) {
this.test = val
}
}
```
21 changes: 7 additions & 14 deletions dist/cjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cjs/index.js.map

Large diffs are not rendered by default.

21 changes: 7 additions & 14 deletions dist/esm/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esm/index.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions dist/types/moduleoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface StaticModuleOptions {
* Whether to generate a plain state object, or a state factory for the module
*/
stateFactory?: boolean;
/**
* Allow getters to have their property definitions redefined. Useful for stubbing in tests.
*/
configurableGetters?: boolean;
}
export interface DynamicModuleOptions {
/**
Expand All @@ -41,5 +45,9 @@ export interface DynamicModuleOptions {
* Whether to generate a plain state object, or a state factory for the module
*/
stateFactory?: boolean;
/**
* Allow getters to have their property definitions redefined. Useful for stubbing in tests.
*/
configurableGetters?: boolean;
}
export declare type ModuleOptions = StaticModuleOptions | DynamicModuleOptions;
20 changes: 7 additions & 13 deletions src/module/staticGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,13 @@ export function staticGetterGenerator<S>(
statics: any
) {
Object.keys(module.getters as GetterTree<S, any>).forEach((key) => {
if (module.namespaced) {
Object.defineProperty(statics, key, {
get() {
return statics.store.getters[`${modOpt.name}/${key}`]
}
})
} else {
Object.defineProperty(statics, key, {
get() {
return statics.store.getters[key]
}
})
}
const moduleKey = module.namespaced ? `${modOpt.name}/${key}` : key
Object.defineProperty(statics, key, {
configurable: !!modOpt.configurableGetters,
get() {
return statics.store.getters[moduleKey]
}
})
})
}

Expand Down
9 changes: 9 additions & 0 deletions src/moduleoptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface StaticModuleOptions {
* Whether to generate a plain state object, or a state factory for the module
*/
stateFactory?: boolean
/**
* Allow getters to have their property definitions redefined. Useful for stubbing in tests.
*/
configurableGetters?: boolean
}

export interface DynamicModuleOptions {
Expand Down Expand Up @@ -47,6 +51,11 @@ export interface DynamicModuleOptions {
* Whether to generate a plain state object, or a state factory for the module
*/
stateFactory?: boolean

/**
* Allow getters to have their property definitions redefined. Useful for stubbing in tests.
*/
configurableGetters?: boolean
}

export type ModuleOptions = StaticModuleOptions | DynamicModuleOptions
6 changes: 6 additions & 0 deletions test/getmodule/getmodule_getter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ describe('using getters on getModule()', () => {
expect(getModule(MyNamespacedModule).value).to.equal(10)
expect(getModule(MyNamespacedModule).twofold).to.equal(20)
})

it('cannot reconfigure the getters', function () {
expect(() => {
Object.defineProperty(getModule(MyModule), 'twofold', {value: 'foo'})
}).to.throw(TypeError, 'Cannot redefine property')
})
})
49 changes: 49 additions & 0 deletions test/getmodule/getmodule_getter_configurable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
import { getModule, Module, VuexModule } from '../..'
import { expect } from 'chai'

const store = new Vuex.Store({})

@Module({ name: 'mm', store, dynamic: true, configurableGetters: true })
class DynamicModule extends VuexModule {
public value = 10

public get twofold(): number {
return this.value * 2
}
}

@Module({name: 'mm', store, configurableGetters: true})
class StaticModule extends VuexModule {
public value = 10

public get twofold(): number {
return this.value * 2
}
}

describe('using getters on dynamic getModule()', () => {
it('getter should return adjusted value', function() {
expect(getModule(DynamicModule).value).to.equal(10)
expect(getModule(DynamicModule).twofold).to.equal(20)
})

it('can reconfigure the getters', function () {
Object.defineProperty(getModule(DynamicModule), 'twofold', {value: 'foo'})
expect(getModule(DynamicModule).twofold).to.equal('foo');
})
})

describe('using getters on static getModule()', () => {
it('getter should return adjusted value', function () {
expect(getModule(StaticModule).value).to.equal(10)
expect(getModule(StaticModule).twofold).to.equal(20)
})

it('can reconfigure the getters', function () {
Object.defineProperty(getModule(StaticModule), 'twofold', {value: 'foo'})
expect(getModule(StaticModule).twofold).to.equal('foo');
})
})