Add: button to load all remaining connections

This commit is contained in:
Raphael Beer 2020-03-18 23:49:30 +01:00
parent a873aac44d
commit f339ed2c7f
No known key found for this signature in database
GPG Key ID: C1AC5E018B25EF11
3 changed files with 341 additions and 278 deletions

View File

@ -8,7 +8,7 @@ config.stubs['client-only'] = '<span><slot /></span>'
config.stubs['ds-space'] = '<span><slot /></span>' config.stubs['ds-space'] = '<span><slot /></span>'
config.stubs['nuxt-link'] = '<span><slot /></span>' config.stubs['nuxt-link'] = '<span><slot /></span>'
let user let user, additionalConnections
describe('FollowList.vue', () => { describe('FollowList.vue', () => {
let store, mocks, getters, propsData let store, mocks, getters, propsData
@ -34,24 +34,43 @@ describe('FollowList.vue', () => {
}) })
describe('given a user with connections', () => { describe('given a user with connections', () => {
it('shows the users following', () => { ;['following', 'followedBy'].forEach(type =>
const wrapper = mount(FollowList, { describe(`and type=${type}`, () => {
let wrapper
let queryMock
beforeAll(() => {
queryMock = jest.fn().mockResolvedValue({
data: { User: [{ [type]: additionalConnections[type] }] },
})
wrapper = mount(FollowList, {
store, store,
propsData: { ...propsData, type: 'following' }, propsData: { ...propsData, type: type },
mocks, mocks: {
...mocks,
$apollo: {
query: queryMock,
},
},
localVue, localVue,
}) })
expect(wrapper.findAll('.user-teaser').length).toEqual(user.following.length)
}) })
it('shows the users being followed', () => {
const wrapper = mount(FollowList, { it(`shows the users ${type}`, () => {
store, expect(wrapper.findAll('.user-teaser').length).toEqual(user[type].length)
propsData: { ...propsData, type: 'followedBy' },
mocks,
localVue,
}) })
expect(wrapper.findAll('.user-teaser').length).toEqual(user.followedBy.length) it(`has a button to load all remaining users ${type}`, async () => {
wrapper.find('button').trigger('click')
await wrapper.vm.$nextTick()
expect(queryMock).toHaveBeenCalledWith({
query: wrapper.vm.queries[type],
variables: { id: user.id },
}) })
expect(wrapper.vm.connections.length).toBe(user[`${type}Count`])
})
}),
)
}) })
}) })
}) })
@ -137,7 +156,8 @@ user = {
], ],
} }
const additionalFollowedBy = [ additionalConnections = {
followedBy: [
{ {
id: '9f305e7a-ae5a-4e22-8269-8b6899af674f', id: '9f305e7a-ae5a-4e22-8269-8b6899af674f',
name: 'Hugh Harris Sr.', name: 'Hugh Harris Sr.',
@ -263,9 +283,9 @@ const additionalFollowedBy = [
name: 'Dolores Wilkinson V', name: 'Dolores Wilkinson V',
slug: 'dolores-wilkinson-v', slug: 'dolores-wilkinson-v',
}, },
] ],
const additionalFollowing = [ following: [
{ {
id: '9011b4fd-feec-4b74-b6d0-e0a41b6658e6', id: '9011b4fd-feec-4b74-b6d0-e0a41b6658e6',
name: 'Lela Kautzer', name: 'Lela Kautzer',
@ -386,4 +406,5 @@ const additionalFollowing = [
name: 'Isaac Purdy MD', name: 'Isaac Purdy MD',
slug: 'isaac-purdy-md', slug: 'isaac-purdy-md',
}, },
] ],
}

View File

@ -12,14 +12,14 @@
<user-teaser :user="follow" /> <user-teaser :user="follow" />
</client-only> </client-only>
</ds-space> </ds-space>
<ds-space v-if="this.user.followedByCount - this.connections.length" margin="small"> <ds-space v-if="this.counts[this.type] - this.connections.length" margin="small">
<ds-text size="small" color="soft"> <base-button @click="fetchConnections" size="small" color="softer">
{{ {{
$t('profile.network.andMore', { $t('profile.network.andMore', {
number: this.user.followedByCount - this.connections.length, number: this.counts[this.type] - this.connections.length,
}) })
}} }}
</ds-text> </base-button>
</ds-space> </ds-space>
</template> </template>
<template v-else> <template v-else>
@ -34,8 +34,7 @@
import uniqBy from 'lodash/uniqBy' import uniqBy from 'lodash/uniqBy'
import UserAvatar from '~/components/_new/generic/UserAvatar/UserAvatar' import UserAvatar from '~/components/_new/generic/UserAvatar/UserAvatar'
import UserTeaser from '~/components/UserTeaser/UserTeaser' import UserTeaser from '~/components/UserTeaser/UserTeaser'
import { followedByQuery, followingQuery } from '~/graphql/User'
let expanded = false
export default { export default {
name: 'FollowerList', name: 'FollowerList',
@ -50,6 +49,14 @@ export default {
data() { data() {
return { return {
connections: this.user[this.type], connections: this.user[this.type],
queries: {
followedBy: followedByQuery,
following: followingQuery,
},
counts: {
followedBy: this.user.followedByCount,
following: this.user.followingCount,
},
} }
}, },
computed: { computed: {
@ -62,6 +69,16 @@ export default {
uniq(items, field = 'id') { uniq(items, field = 'id') {
return uniqBy(items, field) return uniqBy(items, field)
}, },
async fetchConnections() {
const query = this.queries[this.type]
const { data } = await this.$apollo.query({
query: this.queries[this.type],
variables: { id: this.user.id },
// neither result nor update are being called when defined here (?)
})
const connections = data.User[0][this.type]
this.connections = this.connections.concat(connections)
},
}, },
} }
</script> </script>

View File

@ -283,3 +283,28 @@ export const currentUserQuery = gql`
} }
} }
` `
export const followedByQuery = gql`
query($id: ID!) {
User(id: $id) {
followedBy(offset: 7) {
id
slug
name
}
}
}
`
export const followingQuery = gql`
query($id: ID!) {
User(id: $id) {
following(offset: 7) {
id
slug
name
}
}
}
`