test: user + pagination ok, rest WIP

This commit is contained in:
ogerly 2020-04-22 15:06:55 +02:00
parent 6e934ed6b2
commit cccb6cec83
2 changed files with 64 additions and 22 deletions

View File

@ -1,14 +1,20 @@
import { config, mount } from '@vue/test-utils' import { config, mount } from '@vue/test-utils'
import Vuex from 'vuex' import Vuex from 'vuex'
import SearchResults from './SearchResults' import SearchResults from './SearchResults'
import { post } from '~/components/PostTeaser/PostTeaser.story'
import { user } from '~/components/UserTeaser/UserTeaser.story' import helpers from '~/storybook/helpers'
helpers.init()
const localVue = global.localVue const localVue = global.localVue
localVue.directive('scrollTo', jest.fn())
config.stubs['client-only'] = '<span><slot /></span>' config.stubs['client-only'] = '<span><slot /></span>'
config.stubs['nuxt-link'] = '<span><slot /></span>' config.stubs['nuxt-link'] = '<span><slot /></span>'
describe('SearchResults', () => { describe('SearchResults', () => {
let mocks, getters, propsData, wrapper let mocks, getters, propsData, wrapper
const Wrapper = () => { const Wrapper = () => {
@ -44,37 +50,74 @@ describe('SearchResults', () => {
}) })
}) })
describe('contains posts', () => {
describe('contains users less as 25 results', () => {
beforeEach(() => { beforeEach(() => {
console.log wrapper.setData( { users: helpers.fakeUser(1), userCount:1, activeTab: 'User' })
wrapper.setData({ posts: [post], activeTab: 'Post' })
}) })
it('renders post-teaser component', () => { it('renders pagination', () => {
expect(wrapper.find('.post-teaser').exists()).toBe(true) expect(wrapper.find('.pagination-buttons').exists()).toBe(true)
}) })
})
describe('contains users', () => { it('show NOT pagination', () => {
expect(wrapper.find('.pagination-buttons').attributes().style).toBe('display: none;')
})
describe('contains users more as 25 results', () => {
beforeEach(() => { beforeEach(() => {
wrapper.setData({ users: [user], activeTab: 'User' }) wrapper.setData( { users: helpers.fakeUser(52), userCount:52, activeTab: 'User' })
}) })
it('renders user-list', () => { it('renders user-list pagination', () => {
expect(wrapper.find('.user-list').exists()).toBe(true) expect(wrapper.find('.user-list').exists()).toBe(true)
}) })
})
describe('switchTab', () => { it('renders pagination', () => {
beforeEach(() => { expect(wrapper.find('.pagination-buttons').exists()).toBe(true)
wrapper.setData({ posts: [post], users: [user], activeTab: 'Post' })
wrapper.find('.tab-navigation').vm.$emit('switchTab', 'User')
}) })
it('switches activeTab when event is emitted', () => { it('show pagination', () => {
expect(wrapper.find('.user-list').exists()).toBe(true)
expect(wrapper.find('.pagination-buttons').attributes().style).toBe('')
}) })
}) })
})
/*
describe('contains posts', () => {
beforeEach(() => {
console.log
wrapper.setData({ posts: [post], activeTab: 'Post' })
})
it('renders post-teaser component', () => {
expect(wrapper.find('.post-teaser').exists()).toBe(true)
})
})
*/
/*
describe('switchTab', () => {
beforeEach(() => {
wrapper.setData({ posts: [post], users: [user], activeTab: 'Post' })
wrapper.find('.tab-navigation').vm.$emit('switchTab', 'User')
})
it('switches activeTab when event is emitted', () => {
expect(wrapper.find('.user-list').exists()).toBe(true)
})
})
*/
}) })
}) })
}) })

View File

@ -145,7 +145,6 @@ export default {
postsOffset: 0, postsOffset: 0,
usersOffset: 0, usersOffset: 0,
hashtagsOffset: 0, hashtagsOffset: 0,
searchCount: 0,
} }
}, },
computed: { computed: {
@ -198,6 +197,9 @@ export default {
hasMoreHashtags() { hasMoreHashtags() {
return (this.hashtagPage + 1) * this.pageSize < this.hashtagCount return (this.hashtagPage + 1) * this.pageSize < this.hashtagCount
}, },
searchCount() {
return this.postCount + this.userCount + this.hashtagCount
}
}, },
methods: { methods: {
clearPage() { clearPage() {
@ -252,7 +254,6 @@ export default {
update({ searchPosts }) { update({ searchPosts }) {
this.posts = searchPosts.posts this.posts = searchPosts.posts
this.postCount = searchPosts.postCount this.postCount = searchPosts.postCount
this.searchCount = this.postCount + this.userCount + this.hashtagCount
this.clearPage() this.clearPage()
if (this.postCount > 0) this.activeTab = 'Post' if (this.postCount > 0) this.activeTab = 'Post'
}, },
@ -276,7 +277,6 @@ export default {
update({ searchUsers }) { update({ searchUsers }) {
this.users = searchUsers.users this.users = searchUsers.users
this.userCount = searchUsers.userCount this.userCount = searchUsers.userCount
this.searchCount = this.postCount + this.userCount + this.hashtagCount
this.clearPage() this.clearPage()
if (this.postCount === 0 && this.userCount > 0) this.activeTab = 'User' if (this.postCount === 0 && this.userCount > 0) this.activeTab = 'User'
}, },
@ -300,7 +300,6 @@ export default {
update({ searchHashtags }) { update({ searchHashtags }) {
this.hashtags = searchHashtags.hashtags this.hashtags = searchHashtags.hashtags
this.hashtagCount = searchHashtags.hashtagCount this.hashtagCount = searchHashtags.hashtagCount
this.searchCount = this.postCount + this.userCount + this.hashtagCount
this.clearPage() this.clearPage()
if (this.postCount === 0 && this.userCount === 0 && this.hashtagCount > 0) if (this.postCount === 0 && this.userCount === 0 && this.hashtagCount > 0)
this.activeTab = 'Hashtag' this.activeTab = 'Hashtag'