Skip to content

Instantly share code, notes, and snippets.

@brandonaaskov
Created January 18, 2020 23:09
Show Gist options
  • Save brandonaaskov/564395532509e62455096f40df0354dc to your computer and use it in GitHub Desktop.
Save brandonaaskov/564395532509e62455096f40df0354dc to your computer and use it in GitHub Desktop.
Example file testing nuxt stores
import _ from "lodash"
import Vuex from "vuex"
import { createLocalVue } from "@vue/test-utils"
describe("store/movies", () => {
// ----------------------------------------------------
// focus on the code from here...
const localVue = createLocalVue()
localVue.use(Vuex)
let NuxtStore
let store
beforeAll(async () => {
// note the store will mutate across tests
const storePath = `${process.env.buildDir}/store.js`
NuxtStore = await import(storePath)
})
beforeEach(async () => {
store = await NuxtStore.createStore()
})
// ...to here is what matters
// ----------------------------------------------------
describe("disney", () => {
let disney
beforeEach(() => {
disney = store.getters['movies/disney']
})
test("getter is a function", () => {
expect(_.isArray(disney)).toBe(true)
})
test("should be 6 movies total", () => {
expect(disney.length).toBe(6)
})
})
describe('byStudio', () => {
let byStudio
beforeEach(() => {
byStudio = store.getters['movies/byStudio']
})
test('is a function', () => {
expect(_.isFunction(byStudio)).toBe(true)
})
test('matches what the disney getter returns', () => {
const movies = store.getters['movies/byStudio']('Disney')
const disney = store.getters['movies/disney']
expect(movies).toEqual(disney)
})
test('shows all other studios with one entry each', () => {
expect(byStudio('sony').length).toBe(1)
expect(byStudio('warner bros.').length).toBe(1)
expect(byStudio('universal').length).toBe(1)
expect(byStudio('beijing enlight').length).toBe(1)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment