diff --git a/webapp/components/PostCard.vue b/webapp/components/PostCard/index.vue similarity index 88% rename from webapp/components/PostCard.vue rename to webapp/components/PostCard/index.vue index 07f548fb0..87d70b720 100644 --- a/webapp/components/PostCard.vue +++ b/webapp/components/PostCard/index.vue @@ -76,6 +76,8 @@ import HcUser from '~/components/User' import ContentMenu from '~/components/ContentMenu' import { randomBytes } from 'crypto' +import RemoveLinks from '~/mixins/removeLinks.js' +import { mapGetters } from 'vuex' export default { name: 'HcPostCard', @@ -90,18 +92,16 @@ export default { } }, computed: { + ...mapGetters({ + user: 'auth/user' + }), excerpt() { - // remove all links from excerpt to prevent issues with the serounding link - let excerpt = this.post.contentExcerpt.replace(/(.+)<\/a>/gim, '$1') - // do not display content that is only linebreaks - if (excerpt.replace(/
/gim, '').trim() === '') { - excerpt = '' - } - - return excerpt + return RemoveLinks.methods.removeLinks(this.post.contentExcerpt) }, isAuthor() { - return this.$store.getters['auth/user'].id === this.post.author.id + const { author } = this.post + if (!author) return false + return this.user.id === this.post.author.id } }, methods: { diff --git a/webapp/components/PostCard/spec.js b/webapp/components/PostCard/spec.js new file mode 100644 index 000000000..451326ed6 --- /dev/null +++ b/webapp/components/PostCard/spec.js @@ -0,0 +1,60 @@ +import { config, mount, createLocalVue, RouterLinkStub } from '@vue/test-utils' +import PostCard from '.' +import Styleguide from '@human-connection/styleguide' +import Vuex from 'vuex' + +const localVue = createLocalVue() + +localVue.use(Vuex) +localVue.use(Styleguide) + +config.stubs['no-ssr'] = '' +config.stubs['v-popover'] = '' + +describe('PostCard', () => { + let wrapper + let stubs + let mocks + let propsData + let getters + + beforeEach(() => { + propsData = {} + stubs = { + NuxtLink: RouterLinkStub + } + mocks = { + $t: jest.fn() + } + getters = { + 'auth/user': () => { + return {} + } + } + }) + + const Wrapper = () => { + const store = new Vuex.Store({ + getters + }) + return mount(PostCard, { + stubs, + mocks, + propsData, + store, + localVue + }) + } + + describe('given a post', () => { + beforeEach(() => { + propsData.post= { + title: "It's a title" + } + }) + + it('renders title', () => { + expect(Wrapper().text()).toContain("It's a title") + }) + }) +}) diff --git a/webapp/mixins/removeLinks.js b/webapp/mixins/removeLinks.js new file mode 100644 index 000000000..4dfb3ce31 --- /dev/null +++ b/webapp/mixins/removeLinks.js @@ -0,0 +1,15 @@ +export default { + methods: { + removeLinks(content){ + if (!content) return '' + // remove all links from excerpt to prevent issues with the surrounding link + let excerpt = content.replace(/(.+)<\/a>/gim, '$1') + // do not display content that is only linebreaks + if (excerpt.replace(/
/gim, '').trim() === '') { + excerpt = '' + } + + return excerpt + } + } +} diff --git a/webapp/pages/index.vue b/webapp/pages/index.vue index f5d7e03ba..26ff6296d 100644 --- a/webapp/pages/index.vue +++ b/webapp/pages/index.vue @@ -34,7 +34,7 @@