1.2.0 • Published 6 years ago

@molgenis/molgenis-vue-test-utils v1.2.0

Weekly downloads
21
License
LGPL-3.0
Repository
github
Last release
6 years ago

molgenis-vue-test-utils

Testing utilities for Vue and Vuex Javascript unit tests

Installation

NPM

npm install @molgenis/molgenis-vue-test-utils --save

Yarn

yarn add @molgenis/molgenis-vue-test-utils

Usage

Import in your HTML

<script src="path/to/molgenis-vue-test-utils.js"></script>

Import as ES6 module

import utils from '@molgenis/molgenis-vue-test-utils'

utils.testAction(...)

CommonJS import

const utils = require('@molgenis/molgenis-vue-test-utils/dist/molgenis-vue-test-utils.js')

utils.testAction(...)

Examples

// actions.js

import api from 'whatever-you-use-as-api'
export const GET_A_NICE_RESPONSE = '__GET_A_NICE_RESPONSE__'

export default {
  [GET_A_NICE_RESPONSE] ({commit}, id) {
    api.get('/api/response/' + id).then(response => {
      commit(SET_RESPONSE, response)
    }
  }
}
 
// actions.spec.js
 
import td from 'testdouble'
import api from 'whatever-you-use-as-api'
import { testAction } from '@molgenis/molgenis-js-test-utils'
import actions from 'store/actions'
 
it('call an api, and call a mutation with the response', done => {
  const response = 'got a nice response'
  
  const get = td.function('api.get')
  td.when(get('/api/response/my_id')).thenResolve(response)
  td.replace(api, 'get', get)
 
  const options = {
    payload: 'my_id',
    expectedMutations: [
      {type: SET_RESPONSE, payload: response}
    ]
  }
  testAction(actions.__GET_NICE_RESPONSE__, options, done)
})

Methods

MethodDescription
utils.testAction(action, options, done)Test a Vuex action

Options

The options object that can be supplied to the testAction function can contain the following parameters

ParameterDescriptionDefault value
payloada JS object or string or number to pass to the action being testednull
statethe state used by the action being tested{}
expectedMutationsan array of object{type:..., payload:...} describing all the mutations that are committed by the action being tested[]
expectedActionsan array of object{type:..., payload:...} describing all the actions that are dispatched by the action being tested[]
gettersan object containing getter functions as keys, and their expected return value as value. Use this to mock getters in your actions{}

Contributing

This project uses Yarn for development, uses Mocha for testing and is compiled with Rollup

To get started: yarn install

To build: yarn build

To test: yarn test

To test with coverage: yarn test:cover

To get coverage: yarn coveralls

To lint: yarn lint

To debug: first add node-inspector: npm install -g node-inspector then run: yarn debug