Skip to content

Instantly share code, notes, and snippets.

View agualis's full-sized avatar
🐿️
🔴 ✅ ♻️

Alberto Gualis agualis

🐿️
🔴 ✅ ♻️
View GitHub Profile
@agualis
agualis / AddRealTimeItem.spec.js
Created October 7, 2018 17:18
Test what happens when a new item is added to a list
describe('When new item is added in real time', ()=> {
it('ENSURE_ACTIVE_ITEMS action is dispatched', async () => {
const dispatchSpy = jest.spyOn(store, 'dispatch')
wrapper = await renderComponent('top')
expect(dispatchSpy).toHaveBeenCalledWith('ENSURE_ACTIVE_ITEMS')
})
@agualis
agualis / ItemView.spec.js
Created October 3, 2018 11:15
Test ItemView component
import { mount } from '@vue/test-utils'
import { resolvePromises } from '../../../test/test-utils'
import { createStore } from '../../store'
import { fakeItem } from '../../../test/fake-data'
import ItemView from '../ItemView'
let wrapper, store
describe('ItemView.vue', () => {
@agualis
agualis / UserViewDispatchTest.spec.js
Last active October 3, 2018 11:04
Example testing vuex action
it('Calls the action to fetch the user by id', async () => {
const dispatchSpy = jest.spyOn(store, 'dispatch')
await renderComponent(route)
expect(dispatchSpy).toHaveBeenCalledWith('FETCH_USER', { id: fakeUser.id })
expect(dispatchSpy.mock.calls.length).toBe(1)
})
@agualis
agualis / FilterTest.spec.js
Last active October 3, 2018 10:10
Vue Filter test
it('Renders time since creation', async () => {
fakeUser.created = new Date('September 07 2018')/1000
Date.now = jest.fn(() => new Date('September 09 2018'))
// We extracted a function to avoid repeating the same mount code in all the tests
wrapper = await renderComponent(route)
expect(wrapper.text()).toContain('2 days ago')
})
@agualis
agualis / UserView-async.spec.js
Created September 15, 2018 16:01
Asynchronous version of UserView test
import { mount } from '@vue/test-utils'
import { fakeUser } from '../../../test/fake-data'
import { resolvePromises } from '../../../test/test-utils'
import { createStore } from '../../store'
import UserView from '../UserView'
describe('UserView.vue', () => {
it('Renders user title', async () => {
// Creating a fake route
@agualis
agualis / UserView.spec.js
Last active October 3, 2018 09:13
User View test
import { mount } from '@vue/test-utils'
import { fakeUser } from '../../../test/fake-data'
import { createStore } from '../../store'
import UserView from '../UserView'
describe('UserView.vue', () => {
it('Renders user title', () => {
// Creating a fake route
const route = userRoute(fakeUser.id)
@agualis
agualis / action-patterns.spec.js
Created November 18, 2017 18:00
action-patterns.spec.js
import { rejectedPromise, resolvedPromise } from '../../../../../test/helpers'
import { retrieveFilmsAction } from '../films-actions'
import { queryAction } from '../pattern-actions'
describe('REQUEST/SUCCESS/ERROR actions', () => {
let action
let commitSpy
beforeEach(() => {
commitSpy = jest.fn()
import { retrieveFilms } from '../../repositories/films-repository'
import { FETCH_FILMS_ERROR, FETCH_FILMS_REQUEST, FETCH_FILMS_SUCCESS } from './films-mutations'
export function retrieveFilmsActionUsingQueryAction() {
return queryAction(retrieveFilms, FETCH_FILMS_REQUEST, FETCH_FILMS_SUCCESS, FETCH_FILMS_ERROR).run
}
export function queryAction(command, REQUEST, SUCCESS, ERROR) {
return { run }
// retrieveFilms is function that queries the Studio Ghibli backend and returns a promise with the list of movies
// or throws an error if the request failed
import { retrieveFilms } from '../../repositories/films-repository'
export function buildRetrieveFilmsAction() {
return retrieveFilmsAction(retrieveFilms).run
}
export function retrieveFilmsAction(retrieveFilms) {
return { run }
let action
let commitSpy
beforeEach(() => {
commitSpy = jest.fn()
})
describe('Retrieving films action', () => {
it('finishes with success', async () => {
const FILMS = 'ANY FILMS'