Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldziher committed May 12, 2020
1 parent 2f6ac99 commit ec02814
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
12 changes: 6 additions & 6 deletions __test__/index.spec.ts
Expand Up @@ -56,15 +56,15 @@ describe('actionFactory tests', () => {
expect(mockAction).toBeCalled()
})
it('dispatches action with function value', () => {
const value = jest.fn((arr: number[]) => arr.reverse())
const value = jest.fn((_, arr: number[]) => arr.reverse())
const payload = [1, 2, 3]
store = createStore(
actionFactory({
test: [{ dispatch: 'standard/mockAction', value }],
}),
)
store.dispatch('test/test', payload)
expect(value).toBeCalledWith(payload)
expect(value).toBeCalledWith(expect.any(Object), payload)
expect(value).toReturnWith(payload.reverse())
expect(mockAction).toBeCalledWith<[ActionContext<any, any>, number[]]>(
expect.any(Object),
Expand Down Expand Up @@ -96,15 +96,15 @@ describe('actionFactory tests', () => {
)
})
it('commits mutation with function value', () => {
const value = jest.fn((arr: number[]) => arr.reverse())
const value = jest.fn((_, arr: number[]) => arr.reverse())
const payload = [1, 2, 3]
store = createStore(
actionFactory({
test: [{ commit: 'standard/MOCK_MUTATION', value }],
}),
)
store.dispatch('test/test', payload)
expect(value).toBeCalledWith(payload)
expect(value).toBeCalledWith(expect.any(Object), payload)
expect(value).toReturnWith(payload.reverse())
expect(MOCK_MUTATION).toBeCalledWith<[any, number[]]>(
expect.any(Object),
Expand Down Expand Up @@ -183,15 +183,15 @@ describe('mutationFactory tests', () => {
expect(() => store.commit('test/TEST')).toThrow()
})
it('sets state key with value(payload) when value is a function', () => {
const value = jest.fn((arr: number[]) => arr.reverse())
const value = jest.fn((_, arr: number[]) => arr.reverse())
const payload = [1, 2, 3]
let store = createStore(
mutationFactory({
TEST: [{ value, key: 'test' }],
}),
)
store.commit('test/TEST', payload)
expect(value).toBeCalledWith(payload)
expect(value).toBeCalledWith(expect.any(Object), payload)
// @ts-ignore
expect(store.state.test.test).toEqual(payload.reverse())
})
Expand Down
20 changes: 20 additions & 0 deletions license
@@ -0,0 +1,20 @@

Copyright (c) 2020 Na'aman Hirschfeld

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -33,7 +33,7 @@
"typescript"
],
"author": "Na'aman Hirschfeld",
"license": "ISC",
"license": "MIT",
"devDependencies": {
"@types/jest": "^25.2.1",
"@typescript-eslint/eslint-plugin": "^2.31.0",
Expand Down
6 changes: 3 additions & 3 deletions readme.md
@@ -1,6 +1,6 @@
# Vuex Factories

[![npm version](https://badge.fury.io/js/vuex-factories.svg)](https://badge.fury.io/js/vuex-factories) [![Build Status](https://travis-ci.org/Goldziher/vuex-factories.svg?branch=master)](https://travis-ci.org/Goldziher/vuex-factories) [![Coverage Status](https://coveralls.io/repos/github/Goldziher/vuex-factories/badge.svg?branch=master)](https://coveralls.io/github/Goldziher/vuex-factories?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/bd2f46045503d6e836aa/maintainability)](https://codeclimate.com/github/Goldziher/vuex-factories/maintainability)
[![npm version](https://badge.fury.io/js/vuex-factories.svg)](https://badge.fury.io/js/vuex-factories) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Build Status](https://travis-ci.org/Goldziher/vuex-factories.svg?branch=master)](https://travis-ci.org/Goldziher/vuex-factories) [![Coverage Status](https://coveralls.io/repos/github/Goldziher/vuex-factories/badge.svg?branch=master)](https://coveralls.io/github/Goldziher/vuex-factories?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/bd2f46045503d6e836aa/maintainability)](https://codeclimate.com/github/Goldziher/vuex-factories/maintainability)

This package offers an action factory and a mutation factory meant to reduce some of the boilerplate of writing vuex modules.

Expand Down Expand Up @@ -36,7 +36,7 @@ Commit and dispatch are strings of the type used by vuex, i.e. for a mutation it

Value in turn can take two different forms:

1. value can be a function. In this case, the commit or dispatch in the given option will be passed `value(payload)` as the payload.
1. value can be a function. In this case, the commit or dispatch in the given option will be passed `value(context, payload)` as the payload.
2. value can be any other kind of value. _note: if specified value for a given option will take precedence over a payload, even if payload is passed to the action_

| Option | Use | Example |
Expand Down Expand Up @@ -143,7 +143,7 @@ mutationFactory({

```

Each mutation option **must include** a key prop, which represents a state key, e.g. "users". It can also include a value prop. The value prop here acts exactly like it does for actionFactory, that is - it can be either a function, in which case it will be called with the payload as its parameter, or it can be a default value.
Each mutation option **must include** a key prop, which represents a state key, e.g. "users". It can also include a value prop. The value prop here acts exactly like it does for actionFactory, that is - it can be either a function, in which case it will be called with the state and payload as arguments: `value(state, payload)`, or it can be a default value of another type.

The use case for the mutation factory follows similar logic - reducing unnecessary boilerplate. For example:

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -20,7 +20,7 @@ export function actionFactory<S = any, R = any>(
} else {
const actionPayload =
typeof value === 'function'
? value(payload)
? value(context, payload)
: typeof value !== 'undefined'
? value
: payload
Expand Down Expand Up @@ -66,7 +66,7 @@ export function mutationFactory<S = any>(mutations: {
state,
key,
typeof value === 'function'
? value(payload)
? value(state, payload)
: typeof value !== 'undefined'
? value
: payload,
Expand Down

0 comments on commit ec02814

Please sign in to comment.