From 24caa2cfcd8da73d82d163ca3cb8b9bdb945dd4b Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Thu, 13 Dec 2018 05:00:01 +0300 Subject: [PATCH] Add generateProfileLink --- src/components/chat_panel/chat_panel.js | 5 +++++ src/components/chat_panel/chat_panel.vue | 2 +- .../user_profile_link_generator.js | 10 ++++++++++ .../user_profile_link_generator.spec.js | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/services/user_profile_link_generator/user_profile_link_generator.js create mode 100644 test/unit/specs/services/user_profile_link_generator/user_profile_link_generator.spec.js diff --git a/src/components/chat_panel/chat_panel.js b/src/components/chat_panel/chat_panel.js index d8736d17..959b5c3a 100644 --- a/src/components/chat_panel/chat_panel.js +++ b/src/components/chat_panel/chat_panel.js @@ -1,3 +1,5 @@ +import generateProfileLink from 'src/services/user_profile_link_generator/user_profile_link_generator' + const chatPanel = { data () { return { @@ -9,6 +11,9 @@ const chatPanel = { computed: { messages () { return this.$store.state.chat.messages + }, + userProfileLink (user) { + return generateProfileLink(user, user.name) } }, methods: { diff --git a/src/components/chat_panel/chat_panel.vue b/src/components/chat_panel/chat_panel.vue index e3671818..182ea347 100644 --- a/src/components/chat_panel/chat_panel.vue +++ b/src/components/chat_panel/chat_panel.vue @@ -13,7 +13,7 @@
- + {{message.author.username}}
diff --git a/src/services/user_profile_link_generator/user_profile_link_generator.js b/src/services/user_profile_link_generator/user_profile_link_generator.js new file mode 100644 index 00000000..88e0ec38 --- /dev/null +++ b/src/services/user_profile_link_generator/user_profile_link_generator.js @@ -0,0 +1,10 @@ +const generateProfileLink = (user, name = '') => { + const baseLinkParams = { name: 'user-profile' } + const { id } = user + + return { ...baseLinkParams, params: (isExternal(user) ? { id } : { name }) } +} + +const isExternal = ({screen_name}) => (screen_name.indexOf('@') > 0) + +export default generateProfileLink diff --git a/test/unit/specs/services/user_profile_link_generator/user_profile_link_generator.spec.js b/test/unit/specs/services/user_profile_link_generator/user_profile_link_generator.spec.js new file mode 100644 index 00000000..dbb2ba9d --- /dev/null +++ b/test/unit/specs/services/user_profile_link_generator/user_profile_link_generator.spec.js @@ -0,0 +1,15 @@ +import generateProfileLink from 'src/services/user_profile_link_generator/user_profile_link_generator' + +describe('generateProfileLink', () => { + it('returns obj for local user', () => { + const user = { screen_name: 'john' } + + expect(generateProfileLink(user, 'jack')).to.eql({ name: 'user-profile', params: { name: 'jack' } }) + }) + + it('returns obj for external user', () => { + const user = { screen_name: 'john@domain', id: 1 } + + expect(generateProfileLink(user)).to.eql({ name: 'user-profile', params: { id: 1 } }) + }) +})