Merge pull request #4047 from Ocelot-Social-Community/3505-display-social-media-favicons-on-profile

feat: 🔥 Displaying Social Media Favicons On Profile Page
This commit is contained in:
Mogge 2020-12-22 13:06:39 +01:00 committed by GitHub
commit cc9c70dbe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 149 additions and 28 deletions

View File

@ -0,0 +1,97 @@
import { config, mount } from '@vue/test-utils'
import SocialMedia from './SocialMedia.vue'
config.stubs['ds-space'] = '<span><slot /></span>'
config.stubs['ds-text'] = '<span><slot /></span>'
describe('SocialMedia.vue', () => {
let propsData
let mocks
beforeEach(() => {
propsData = {}
mocks = {
$t: jest.fn(),
}
})
describe('mount', () => {
const Wrapper = () => {
return mount(SocialMedia, { propsData, mocks })
}
describe('socialMedia card title', () => {
beforeEach(() => {
propsData.userName = 'Jenny Rostock'
propsData.user = {
socialMedia: [
{
id: 'ee1e8ed6-fbef-4bcf-b411-a12926f2ea1e',
url: 'https://www.instagram.com/nimitbhargava',
__typename: 'SocialMedia',
},
],
}
})
it('renders socialMedia card title', () => {
Wrapper()
expect(mocks.$t).toHaveBeenCalledWith('profile.socialMedia')
})
})
describe('socialMedia links', () => {
let wrapper
beforeEach(() => {
propsData.userName = 'Jenny Rostock'
propsData.user = {
socialMedia: [
{
id: 'ee1e8ed6-fbef-4bcf-b411-a12926f2ea1e',
url: 'https://www.instagram.com/nimitbhargava',
__typename: 'SocialMedia',
},
{
id: 'dc91aecb-3289-47d0-8770-4b24eb24fd9c',
url: 'https://www.facebook.com/NimitBhargava',
__typename: 'SocialMedia',
},
{
id: 'db1dc400-9303-4b43-9451-87dcac13b913',
url: 'https://www.youtube.com/channel/UCu3GiKBFn5I07V9hBxF2CRA',
__typename: 'SocialMedia',
},
],
}
// Now assign wrapper
wrapper = Wrapper()
})
it('shows 3 social media links', () => {
expect(wrapper.findAll('a')).toHaveLength(3)
})
it('renders a social media link', () => {
const link = wrapper.findAll('a').at(0)
expect(link.attributes('href')).toEqual('https://www.instagram.com/nimitbhargava')
})
it('shows the first favicon', () => {
const favicon = wrapper.findAll('a').at(0).find('img')
expect(favicon.attributes('src')).toEqual('https://www.instagram.com/favicon.ico')
})
it('shows the second favicon', () => {
const favicon = wrapper.findAll('a').at(1).find('img')
expect(favicon.attributes('src')).toEqual('https://www.facebook.com/favicon.ico')
})
it('shows the last favicon', () => {
const favicon = wrapper.findAll('a').at(-1).find('img')
expect(favicon.attributes('src')).toEqual('https://www.youtube.com/favicon.ico')
})
})
})
})

View File

@ -0,0 +1,49 @@
<template>
<ds-space v-if="user.socialMedia && user.socialMedia.length" margin="large">
<base-card class="social-media-bc">
<ds-space margin="x-small">
<ds-text tag="h5" color="soft">
{{ $t('profile.socialMedia') }} {{ userName | truncate(15) }}?
</ds-text>
<template>
<ds-space v-for="link in socialMediaLinks()" :key="link.id" margin="x-small">
<a :href="link.url" target="_blank">
<img :src="link.favicon" alt="Link:" height="22" width="22" />
{{ link.username }}
</a>
</ds-space>
</template>
</ds-space>
</base-card>
</ds-space>
</template>
<script>
export default {
name: 'social-media',
props: {
userName: {},
user: {},
},
methods: {
socialMediaLinks() {
const { socialMedia = [] } = this.user
return socialMedia.map((socialMedia) => {
const { url } = socialMedia
const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n])?(?:www\.)?([^:/\n?]+)/g)
const [domain] = matches || []
const favicon = domain ? `${domain}/favicon.ico` : null
const username = url.split('/').pop()
return { url, username, favicon }
})
},
},
}
</script>
<style scoped>
.social-media-bc {
position: relative;
height: auto;
}
</style>

View File

@ -103,23 +103,7 @@
type="following"
@fetchAllConnections="fetchAllConnections"
/>
<ds-space v-if="user.socialMedia && user.socialMedia.length" margin="large">
<base-card style="position: relative; height: auto">
<ds-space margin="x-small">
<ds-text tag="h5" color="soft">
{{ $t('profile.socialMedia') }} {{ userName | truncate(15) }}?
</ds-text>
<template>
<ds-space v-for="link in socialMediaLinks" :key="link.username" margin="x-small">
<a :href="link.url" target="_blank">
<user-avatar :image="link.favicon" />
{{ link.username }}
</a>
</ds-space>
</template>
</ds-space>
</base-card>
</ds-space>
<social-media :user-name="userName" :user="user" />
</ds-flex-item>
<ds-flex-item :width="{ base: '100%', sm: 3, md: 5, lg: 3 }">
@ -243,6 +227,7 @@ import { muteUser, unmuteUser } from '~/graphql/settings/MutedUsers'
import { blockUser, unblockUser } from '~/graphql/settings/BlockedUsers'
import PostMutations from '~/graphql/PostMutations'
import UpdateQuery from '~/components/utils/UpdateQuery'
import SocialMedia from '~/components/SocialMedia/SocialMedia'
const tabToFilterMapping = ({ tab, id }) => {
return {
@ -254,6 +239,7 @@ const tabToFilterMapping = ({ tab, id }) => {
export default {
components: {
SocialMedia,
PostTeaser,
HcFollowButton,
HcCountTo,
@ -292,17 +278,6 @@ export default {
user() {
return this.User ? this.User[0] : {}
},
socialMediaLinks() {
const { socialMedia = [] } = this.user
return socialMedia.map((socialMedia) => {
const { url } = socialMedia
const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n])?(?:www\.)?([^:/\n?]+)/g)
const [domain] = matches || []
const favicon = domain ? `${domain}/favicon.ico` : null
const username = url.split('/').pop()
return { url, username, favicon }
})
},
userName() {
const { name } = this.user || {}
return name || this.$t('profile.userAnonym')