npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vuex-test-helpers

v0.2.0

Published

A test library to construct mock Vuex stores

Downloads

7

Readme

Vuex test helpers

build-status

A helper library to construct mock Vuex stores for tests

Installation

$ npm install -D vuex-test-helpers

Dependencies

Vuex test helpers currently requires testdouble.js. Vuex test helpers uses td.function() to create mock actions and getters. You should install testdouble as a dev dependency in your project as well.

Example usage

Let's say we have a Vue component, MyComponent, that uses state data and an action from a Vuex store:

myComponent.vue

<template>
  <div>
    <h1>{{ message }}</h1>

    <button @click="loadData" id="fetch">Load awesome data!</button>

    <div v-if="superAwesomeData" id="super-awesome-data">
      {{ superAwesomeData }}
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'MyModule',
  data() {
    return {
      superAwesomeData: undefined
    }
  },
  computed: {
    ...mapState(['message'])
  },
  methods: {
    ...mapActions(['fetchData']),
    loadData() {
      this.fetchData()
        .then(data => {
          this.superAwesomeData = data
        })
    }
  }
}
</script>

MyComponent.spec.js

import MyComponent from '@/components/MyComponent'
import { shallow, createLocalVue } from '@vue/test-utils',
import { createMockStore } from 'vuex-test-helpers'
import Vuex from 'vuex'

describe('MyComponent', () => {
  let wrapper, subject, store

  beforeEach(() => {
    const localVue = createLocalVue()
    localVue.use(Vuex)

    { store } = createMockStore()
      .withState({ message: 'Hello world!' })
      .withActions(['fetchData'])

    wrapper = shallow(MyComponent, {
      localVue,
      store: new Vuex.Store(store)
    })

    subject = wrapper.vm
  })

  it('renders the message from the store', () => {
    expect(wrapper.find('h1').text()).to.equal('Hello world!')
    expect
  })

  describe('when the fetch button is clicked', () => {
    beforeEach(() => {
      td.when(store.actions.fetchData()).thenResolve('the super awesome data')
      wrapper.find('#fetch').trigger('click')
    })

    it('renders the response', () => {
      expect(wrapper.find('#super-awesome-data').text()).to.equal('the super awesome data')
    })
  })
})

API

Returns a MockStoreWrapper object which can be used to build a mock store.

const mockStore = createMockStore().store

Updates the working store with the given state object. If no state is provided, the store will default state to an empty object.

Returns a MockStoreWrapper so all methods are chainable.

const mockStore = createMockStore().withState({foo: 'bar'})

console.log(mockStore.store.state) // { foo: 'bar' }

Updates the working store with mock getters. If no getters are provided, the store will create a default empty getters object.

Returns a MockStoreWrapper so all methods are chainable.

const mockStore = createMockStore().withGetters(['myGetter'])

// in a spec
td.when(mockStore.store.getters.myGetter()).thenReturn('my getter value')

// in a view component that has mapped myGetter
console.log(this.myGetter) // my getter value

Updates the working store with mock actions. If no actions are provided, the store will create a default empty actions object.

Returns a MockStoreWrapper so all methods are chainable.

const mockStore = createMockStore().withActions(['myAction'])

// in a spec
td.when(mockStore.store.actions.myAction()).thenResolve('my action result')

// in a view component that has mapped myAction
this.myAction().then(result => {
  console.log(result) // my action result
})

Adds a module with the given name to the working store. If no name is provider a default, empty modules object is created. Use this method to add modules created with createMockModule to your store.

Returns a MockStoreWrapper so all methods are chainable.

const testModule = createMockModule('test').withState({foo: 'bar'})
const mockStore = createMockStore().withModule(testModule.name, testModule.module)
console.log(mockStore.store.modules) // { test: { state: { foo: 'bar' } } }

Returns a MockModuleWrapper object which can be used to build a mock module.

const mockModule = createMockModule('test')
console.log(mockModule.name) // test
console.log(mockModule.module) // {}

Updates the modules state with the given object. If no object is provided a default empty object is created.

Returns a MockModuleWrapper so all methods are chainable.

const testModule = createMockModule('test').withState({foo: 'bar'})
console.log(testModule.module.state) // { foo: 'bar' }

Creates mocked getters for the module. If no getters are provided a default empty getters object is created.

Returns a MockModuleWrapper so all methods are chainable.

const mockModule = createMockModule('test').withGetters(['myGetter'])

// in a spec
td.when(mockModule.module.getters.myGetter()).thenReturn('my getter value')

// in a view component that has mapped myGetter from the test module
console.log(this.myGetter) // my getter value

Creates mocked actions for the module. If no actions are provided a default empty actions object is created.

Returns a MockModuleWrapper so all methods are chainable.

const mockModule = createMockModule('test').withActions(['myAction'])

// in a spec
td.when(mockModule.module.actions.myAction()).thenResolve('my action result')

// in a view component that has mapped myAction from the test module
this.myAction().then(result => {
  console.log(result) // my action result
})

Adds a module with the given name to the current module. If no name is provider a default, empty modules object is created. You can use this method to create store structures that have nested modules infitely deep.

Returns a MockModuleWrapper so all methods are chainable.

const testModule = createMockModule('test').withState({foo: 'bar'})
const nestedModule = createMockModule('nested').withState({ nested: 'state' })

testModule.withModule(nestedModule.name, nestedModule.module)
console.log(testModule.module)
// {
//   state: { foo: 'bar' },
//   modules: {
//     nested: {
//       state: { nested: 'state' }
//     }
//   }
// }