Add intergation tests for Contents/Home

This commit is contained in:
AkiraFukushima 2019-01-19 21:41:25 +09:00
parent 079c4aa785
commit 7b4ddac7cb
2 changed files with 115 additions and 4 deletions

View File

@ -0,0 +1,113 @@
import Mastodon from 'megalodon'
import { createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import Home from '~/src/renderer/store/TimelineSpace/Contents/Home'
jest.genMockFromModule('megalodon')
jest.mock('megalodon')
const state = {
lazyLoading: false,
heading: true,
timeline: [],
unreadTimeline: [],
filter: '',
showReblogs: true,
showReplies: true
}
const initState = {
namespaced: true,
state: state,
actions: Home.actions,
mutations: Home.mutations
}
const timelineState = {
namespaced: true,
state: {
account: {
accessToken: 'token',
baseURL: 'http://localhost'
}
}
}
describe('Home', () => {
let store
let localVue
beforeEach(() => {
localVue = createLocalVue()
localVue.use(Vuex)
store = new Vuex.Store({
modules: {
Home: initState,
TimelineSpace: timelineState
}
})
Mastodon.mockClear()
})
describe('fetchTimeline', () => {
it('should be updated', async () => {
const mockClient = {
get: () => {
return new Promise((resolve, reject) => {
resolve({
data: [
{ id: 1 },
{ id: 2 }
]
})
})
}
}
Mastodon.mockImplementation(() => mockClient)
const statuses = await store.dispatch('Home/fetchTimeline')
expect(statuses).toEqual([
{ id: 1 },
{ id: 2 }
])
expect(store.state.Home.timeline).toEqual([
{ id: 1 },
{ id: 2 }
])
})
})
describe('lazyFetchTimeline', () => {
describe('last is null', () => {
it('should not be updated', async () => {
const result = await store.dispatch('Home/lazyFetchTimeline', null)
expect(result).toEqual(null)
})
})
describe('success', () => {
it('should be updated', async () => {
const mockClient = {
get: () => {
return new Promise((resolve, reject) => {
resolve({
data: [
{ id: 19 },
{ id: 18 }
]
})
})
}
}
Mastodon.mockImplementation(() => mockClient)
await store.dispatch('Home/lazyFetchTimeline', { id: 20 })
expect(store.state.Home.lazyLoading).toEqual(false)
expect(store.state.Home.timeline).toEqual([
{ id: 1 },
{ id: 2 },
{ id: 19 },
{ id: 18 }
])
})
})
})
})

View File

@ -79,7 +79,7 @@ const Home = {
}
},
actions: {
fetchTimeline ({ state, commit, rootState }, account) {
fetchTimeline ({ state, commit, rootState }) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
@ -104,13 +104,11 @@ const Home = {
)
return client.get('/timelines/home', { max_id: last.id, limit: 40 })
.then(res => {
commit('changeLazyLoading', false)
commit('insertTimeline', res.data)
return res.data
})
.catch(err => {
.finally(() => {
commit('changeLazyLoading', false)
throw err
})
}
}