diff --git a/src/components/chat_message_list/chat_message_list.js b/src/components/chat_message_list/chat_message_list.js index 8197e813a..4babea5a7 100644 --- a/src/components/chat_message_list/chat_message_list.js +++ b/src/components/chat_message_list/chat_message_list.js @@ -69,7 +69,8 @@ const ChatMessageList = { if (newerItem.type === 'date') { chatItem.messageChainId = uniqueId() } else if (newerItem.type === 'message') { - const newerUser = newerItem.data.account_id || newerItem.data.user.id + const newerUser = + newerItem.data.account_id || newerItem.data.user.id const olderUser = message.account_id || message.user.id if (newerUser !== olderUser) { chatItem.messageChainId = uniqueId() @@ -111,18 +112,21 @@ const ChatMessageList = { this.$emit('replyRequested', message) }, getPreviousItem(index) { - let result + let result = null - this.chatItems.slice(0, index).reverse().some((item) => { - const isMessage = item.type === 'message' - if (isMessage) { - result = item - } - return isMessage - }) + this.chatItems + .slice(0, index) + .reverse() + .some((item) => { + const isMessage = item.type === 'message' + if (isMessage) { + result = item + } + return isMessage + }) return result - } + }, }, } diff --git a/src/components/chat_message_list/chat_message_list.spec.js b/src/components/chat_message_list/chat_message_list.spec.js index 39892254c..53e14b2a7 100644 --- a/src/components/chat_message_list/chat_message_list.spec.js +++ b/src/components/chat_message_list/chat_message_list.spec.js @@ -2,29 +2,29 @@ import { shallowMount } from '@vue/test-utils' import ChatMessageList from './chat_message_list.vue' - describe('ChatMessageList', () => { describe('computed.chatItems', () => { it('Inserts date separators', () => { const component = shallowMount(ChatMessageList, { props: { - messages: [{ - id: '1', - idempotency_key: '1', - account_id: 'Alice', - created_at: new Date('2020-06-22T20:00:00.000Z'), - }, { - id: '2', - idempotency_key: '2', - account_id: 'Alice', - created_at: new Date('2020-06-22T20:01:00.000Z'), - }, { - id: '3', - idempotency_key: '3', - account_id: 'Alice', - created_at: new Date('2020-06-23T20:00:00.000Z'), - }] - } + messages: [ + { + id: '0', + account_id: 'Alice', + created_at: new Date('2020-06-22T20:00:00.000Z'), + }, + { + id: '1', + account_id: 'Alice', + created_at: new Date('2020-06-22T20:01:00.000Z'), + }, + { + id: '2', + account_id: 'Alice', + created_at: new Date('2020-06-23T20:00:00.000Z'), + }, + ], + }, }) expect(component.vm.chatItems.map((i) => i.type)).to.eql([ @@ -39,13 +39,14 @@ describe('ChatMessageList', () => { const component = shallowMount(ChatMessageList, { props: { headerDate: true, - messages: [{ - id: '1', - idempotency_key: '1', - account_id: 'Alice', - created_at: new Date('2020-06-23T20:00:00.000Z'), - }] - } + messages: [ + { + id: '0', + account_id: 'Alice', + created_at: new Date('2020-06-23T20:00:00.000Z'), + }, + ], + }, }) expect(component.vm.chatItems.map((i) => i.type)).to.eql([ @@ -57,23 +58,24 @@ describe('ChatMessageList', () => { it('Inserts time separators if messages were sent with considerable delay (5 minutes)', () => { const component = shallowMount(ChatMessageList, { props: { - messages: [{ - id: '1', - idempotency_key: '1', - account_id: 'Alice', - created_at: new Date('2020-06-22T20:00:00.000Z'), - }, { - id: '2', - idempotency_key: '2', - account_id: 'Alice', - created_at: new Date('2020-06-22T20:06:00.000Z'), - }, { - id: '3', - idempotency_key: '3', - account_id: 'Alice', - created_at: new Date('2020-06-23T20:00:00.000Z'), - }] - } + messages: [ + { + id: '0', + account_id: 'Alice', + created_at: new Date('2020-06-22T20:00:00.000Z'), + }, + { + id: '1', + account_id: 'Alice', + created_at: new Date('2020-06-22T20:06:00.000Z'), + }, + { + id: '2', + account_id: 'Alice', + created_at: new Date('2020-06-23T20:00:00.000Z'), + }, + ], + }, }) expect(component.vm.chatItems.map((i) => i.type)).to.eql([ @@ -95,39 +97,44 @@ describe('ChatMessageList', () => { it('Groups message chains by time and author', () => { const component = shallowMount(ChatMessageList, { props: { - messages: [{ - id: '1', - idempotency_key: '1', - account_id: 'Alice', - created_at: new Date('2020-06-22T20:00:00.000Z'), - }, { - id: '2', - idempotency_key: '2', - account_id: 'Alice', - created_at: new Date('2020-06-22T20:06:00.000Z'), - }, { - id: '3', - idempotency_key: '3', - account_id: 'Alice', - created_at: new Date('2020-06-23T20:00:00.000Z'), - }, { - id: '4', - account_id: 'Bob', - created_at: new Date('2020-06-23T20:01:00.000Z'), - }, { - id: '5', - account_id: 'Bob', - created_at: new Date('2020-06-23T20:02:00.000Z'), - }, { - id: '6', - account_id: 'Bob', - created_at: new Date('2020-06-23T20:03:00.000Z'), - }, { - id: '7', - account_id: 'Eve', - created_at: new Date('2020-06-23T20:04:00.000Z'), - }] - } + messages: [ + { + id: '0', + account_id: 'Alice', + created_at: new Date('2020-06-22T20:00:00.000Z'), + }, + { + id: '1', + account_id: 'Alice', + created_at: new Date('2020-06-22T20:06:00.000Z'), + }, + { + id: '2', + account_id: 'Alice', + created_at: new Date('2020-06-23T20:00:00.000Z'), + }, + { + id: '3', + account_id: 'Bob', + created_at: new Date('2020-06-23T20:01:00.000Z'), + }, + { + id: '4', + account_id: 'Bob', + created_at: new Date('2020-06-23T20:02:00.000Z'), + }, + { + id: '5', + account_id: 'Bob', + created_at: new Date('2020-06-23T20:03:00.000Z'), + }, + { + id: '6', + account_id: 'Eve', + created_at: new Date('2020-06-23T20:04:00.000Z'), + }, + ], + }, }) // Type check @@ -158,10 +165,80 @@ describe('ChatMessageList', () => { // Unique ID is randomly generated so we have to compare data against itself // Two messages from Bob next to each other - expect(component.vm.chatItems[5].messageChainId).to.eql(component.vm.chatItems[6].messageChainId) + expect(component.vm.chatItems[5].messageChainId).to.eql( + component.vm.chatItems[6].messageChainId, + ) // Message from Even right after Bob - expect(component.vm.chatItems[7].messageChainId).to.not.eql(component.vm.chatItems[8].messageChainId) + expect(component.vm.chatItems[7].messageChainId).to.not.eql( + component.vm.chatItems[8].messageChainId, + ) + }) + }) + describe('methods.getPreviousItem', () => { + describe('Finds correct previous meaningful (non-separator) message in the chatlist', () => { + let component + + beforeEach(() => { + component = shallowMount(ChatMessageList, { + props: { + messages: [ + { + id: '0', + account_id: 'Alice', + created_at: new Date('2020-06-22T20:00:00.000Z'), + }, + { + // Separator + id: '1', // 2 + account_id: 'Alice', + created_at: new Date('2020-06-22T20:06:00.000Z'), + }, + { + // Separator + id: '2', // 4 + account_id: 'Alice', + created_at: new Date('2020-06-23T20:00:00.000Z'), + }, + { + id: '3', // 5 + account_id: 'Bob', + created_at: new Date('2020-06-23T20:01:00.000Z'), + }, + { + id: '4', // 6 + account_id: 'Bob', + created_at: new Date('2020-06-23T20:02:00.000Z'), + }, + { + id: '5', // 7 + account_id: 'Bob', + created_at: new Date('2020-06-23T20:03:00.000Z'), + }, + { + id: '6', // 8 + account_id: 'Eve', + created_at: new Date('2020-06-23T20:04:00.000Z'), + }, + ], + }, + }) + }) + + it('Directly next to each other', () => { + const correct = component.vm.chatItems[6] + expect(component.vm.getPreviousItem(7)).to.eql(correct) + }) + + it('Across separator', () => { + const correct = component.vm.chatItems[2] + expect(component.vm.getPreviousItem(4)).to.eql(correct) + }) + + it('Returns null if no previous item exist', () => { + const correct = null + expect(component.vm.getPreviousItem(0)).to.eql(correct) + }) }) }) })