- Notifications
You must be signed in to change notification settings - Fork2.1k
Unit test route components#339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
agualis wants to merge13 commits intovuejs:masterChoose a base branch fromagualis:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
ad8543f
Add babel test presets
agualisc0ccf67
Add jest config file
agualisb0b0902
Add fake-api
agualis2d707a4
Add fake data
agualisaae397f
Add method to resolve promises under test
agualis269c680
Add global jest-setup
agualisdde5059
Add test dependencies
agualis3ab56ae
Add UserView component unit tests
agualisd7e347c
Add ItemView component unit tests
agualis907f347
Add ListView Component unit tests
agualis49bc18d
Avoid using 8888 port in dev environment
agualisdce5839
Avoid cross-env in dev script
agualisc3492b1
force ENV_NODE=test when runninghg jest
agualisFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
12 changes: 10 additions & 2 deletions.babelrc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletionsjest.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
"moduleFileExtensions": [ | ||
"js", | ||
"json", | ||
// tell Jest to handle *.vue files | ||
"vue" | ||
], | ||
"transform": { | ||
// process js with babel-jest | ||
"^.+\\.js$": "<rootDir>/node_modules/babel-jest", | ||
// process *.vue files with vue-jest | ||
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest" | ||
}, | ||
// support the same @ -> src alias mapping in source code | ||
"moduleNameMapper": { | ||
"^@/(.*)$": "<rootDir>/src/$1", | ||
'../api': '<rootDir>/src/api/__mocks__/fake-api.js' | ||
}, | ||
// serializer for snapshots | ||
"snapshotSerializers": [ | ||
"<rootDir>/node_modules/jest-serializer-vue" | ||
], | ||
"setupTestFrameworkScriptFile": "<rootDir>/src/jest-setup.js" | ||
} |
9 changes: 8 additions & 1 deletionpackage.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletionssrc/api/__mocks__/fake-api.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { addedItemId, fakeItemList, fakeUser, newItemListAfterAddingNewItem } from '../../../test/fake-data' | ||
export function fetchIdsByType(type) { | ||
return Promise.resolve(Object.keys(fakeItemList)) | ||
} | ||
export function fetchItem (id) { | ||
return Promise.resolve(fakeItemList[id]) | ||
} | ||
export function watchList (type, cb) { | ||
cb(newItemListAfterAddingNewItem) | ||
} | ||
export function fetchItems (ids) { | ||
return Promise.all(ids.map(id => fetchItem(id))) | ||
} | ||
export function fetchUser (id) { | ||
if (id === fakeUser.id) return Promise.resolve(fakeUser) | ||
return Promise.reject('User not found') | ||
} | ||
9 changes: 9 additions & 0 deletionssrc/jest-setup.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Vue from 'vue' | ||
import * as filters from './util/filters' | ||
// We would extract this to a function that would be reused by both app.js and jest-setup but, | ||
// we didn't want to change original production code | ||
Object.keys(filters).forEach(key => { | ||
Vue.filter(key, filters[key]) | ||
}) | ||
108 changes: 108 additions & 0 deletionssrc/views/__tests__/CreateListView.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { mount } from '@vue/test-utils' | ||
import { addedItemId, fakeItemList, newItemListAfterAddingNewItem } from '../../../test/fake-data' | ||
import { resolvePromises } from '../../../test/test-utils' | ||
import { createStore } from '../../store' | ||
import CreateListView from '../CreateListView' | ||
const PAGE_TYPES = ['top', 'new', 'show', 'ask', 'job'] | ||
let wrapper, store, routerSpy | ||
describe('CreateListView.vue', () => { | ||
beforeEach(()=> { | ||
store = createStore() | ||
routerSpy = jest.fn() | ||
}) | ||
it('shows number of available pages', async () => { | ||
const page = 1 | ||
wrapper = await renderComponent('top', page) | ||
expect(wrapper.find('.news-list-nav').text()).toBe('< prev 1/3 more >') | ||
}) | ||
it('shows current page in paginator', async () => { | ||
const currentPage = 2 | ||
wrapper = await renderComponent('top', currentPage) | ||
expect(wrapper.find('.news-list-nav').text()).toBe('< prev 2/3 more >') | ||
}) | ||
PAGE_TYPES.forEach(async (type) => { | ||
it('calls FETCH_LIST_DATA action for page ' + type, async () => { | ||
const dispatchSpy = jest.spyOn(store, 'dispatch') | ||
wrapper = await renderComponent(type) | ||
expect(dispatchSpy).toHaveBeenCalledWith('FETCH_LIST_DATA', {"type": type}) | ||
expect(dispatchSpy.mock.calls.length).toBe(3) | ||
}) | ||
}) | ||
it('loads 20 items', async () => { | ||
wrapper = await renderComponent('top') | ||
expect(wrapper.findAll('.news-item')).toHaveLength(20) | ||
}) | ||
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') | ||
}) | ||
it('The new list is set', async () => { | ||
const commitSpy = jest.spyOn(store, 'commit') | ||
wrapper = await renderComponent('top') | ||
expect(commitSpy).toHaveBeenCalledWith('SET_LIST', {"ids": newItemListAfterAddingNewItem, "type": "top"}) | ||
expect(wrapper.text()).toContain(fakeItemList[addedItemId].title) | ||
}) | ||
it('The title of the new added item is rendered', async () => { | ||
wrapper = await renderComponent('top') | ||
expect(wrapper.text()).toContain(fakeItemList[addedItemId].title) | ||
}) | ||
}) | ||
}) | ||
async function renderComponent(type, page) { | ||
const $route = { | ||
path: '/some/path', | ||
params: { page } | ||
} | ||
store.state.route = $route | ||
const mixin = { | ||
beforeMount: function () { | ||
this.$root = { | ||
_isMounted: true | ||
} | ||
} | ||
} | ||
const wrapper = mount(CreateListView(type), { store, | ||
propsData: { | ||
type: 'type', | ||
}, | ||
mocks: { | ||
$route, | ||
$bar: { start: jest.fn(), finish: jest.fn() } | ||
}, | ||
stubs: ['router-link'], | ||
mixins: [mixin] | ||
}) | ||
wrapper.vm.$options.asyncData({ store }) | ||
await resolvePromises() | ||
return wrapper | ||
} | ||
64 changes: 64 additions & 0 deletionssrc/views/__tests__/ItemView.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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', () => { | ||
beforeEach(()=> { | ||
store = createStore() | ||
}) | ||
it('Renders item title', async () => { | ||
wrapper = await renderComponent(fakeItem.id) | ||
expect(wrapper.text()).toContain(fakeItem.title) | ||
}) | ||
it('Renders item host', async () => { | ||
fakeItem.url = 'https://www.fake.domain.com/link/fake-uri' | ||
wrapper = await renderComponent(fakeItem.id) | ||
expect(wrapper.text()).toContain('fake.domain.com') | ||
}) | ||
it('Renders item user', async () => { | ||
wrapper = await renderComponent(fakeItem.id) | ||
expect(wrapper.text()).toContain('| by ' + fakeItem.by) | ||
}) | ||
it('Calls the action to fetch the item by id', async () => { | ||
const dispatchSpy = jest.spyOn(store, 'dispatch') | ||
wrapper = renderComponent(fakeItem.id) | ||
expect(dispatchSpy).toHaveBeenCalledWith('FETCH_ITEMS', { ids: [fakeItem.id] }) | ||
}) | ||
it('Calls the action to fetch the comments by id', async () => { | ||
const dispatchSpy = jest.spyOn(store, 'dispatch') | ||
wrapper = await renderComponent(fakeItem.id) | ||
expect(dispatchSpy).toHaveBeenCalledWith('FETCH_ITEMS', { ids: fakeItem.kids }) | ||
}) | ||
}) | ||
async function renderComponent(id) { | ||
const route = { | ||
path: '/item', | ||
params: { id } | ||
} | ||
store.state.route = route | ||
const wrapper = mount(ItemView, { store, | ||
mocks: { | ||
$route: route, | ||
}, | ||
stubs: ['router-link'] | ||
}) | ||
wrapper.vm.$options.asyncData({ store, route }) | ||
await resolvePromises() | ||
return wrapper | ||
} | ||
55 changes: 55 additions & 0 deletionssrc/views/__tests__/UserView.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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' | ||
let wrapper, store, route | ||
describe('UserView.vue', () => { | ||
beforeEach(() => { | ||
route = userRoute(fakeUser.id) | ||
store = createStore() | ||
}) | ||
it('Renders user id', async () => { | ||
wrapper = await renderComponent(route) | ||
expect(wrapper.text()).toContain(`User : ${fakeUser.id}`) | ||
}) | ||
it('Renders time since creation', async () => { | ||
fakeUser.created = new Date('September 07 2018')/1000 | ||
Date.now = jest.fn(() => new Date('September 09 2018')) | ||
wrapper = await renderComponent(route) | ||
expect(wrapper.text()).toContain('2 days ago') | ||
}) | ||
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) | ||
}) | ||
}) | ||
const userRoute = (id) => ({ | ||
path: '/user', | ||
params: { id } | ||
}) | ||
const renderComponent = async route => { | ||
const wrapper = mount(UserView, { store, | ||
mocks: { | ||
$route: route, | ||
} | ||
}) | ||
wrapper.vm.$options.asyncData({ store, route }) | ||
await resolvePromises() | ||
return wrapper | ||
} |
17 changes: 17 additions & 0 deletionstest/fake-data.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import news from './fake-news.json' | ||
const arrayToObject = (array) => | ||
array.reduce((obj, item) => { | ||
obj[item.id] = item | ||
return obj | ||
}, {}) | ||
export const fakeItemList = arrayToObject(news) | ||
const anItemId = 17944752 | ||
export const fakeItem = fakeItemList[anItemId] | ||
export const fakeUser = { id: 17944752} | ||
export const addedItemId = 17938548 | ||
export const newItemListAfterAddingNewItem = [ 1, 2, 3, 4, 5, addedItemId, 6, 7, 8, 9 ] |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.