additional test

This commit is contained in:
Henry Jameson 2026-07-21 16:42:01 +03:00
commit 0ce839531d
2 changed files with 168 additions and 87 deletions

View file

@ -69,7 +69,8 @@ const ChatMessageList = {
if (newerItem.type === 'date') { if (newerItem.type === 'date') {
chatItem.messageChainId = uniqueId() chatItem.messageChainId = uniqueId()
} else if (newerItem.type === 'message') { } 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 const olderUser = message.account_id || message.user.id
if (newerUser !== olderUser) { if (newerUser !== olderUser) {
chatItem.messageChainId = uniqueId() chatItem.messageChainId = uniqueId()
@ -111,18 +112,21 @@ const ChatMessageList = {
this.$emit('replyRequested', message) this.$emit('replyRequested', message)
}, },
getPreviousItem(index) { getPreviousItem(index) {
let result let result = null
this.chatItems.slice(0, index).reverse().some((item) => { this.chatItems
const isMessage = item.type === 'message' .slice(0, index)
if (isMessage) { .reverse()
result = item .some((item) => {
} const isMessage = item.type === 'message'
return isMessage if (isMessage) {
}) result = item
}
return isMessage
})
return result return result
} },
}, },
} }

View file

@ -2,29 +2,29 @@ import { shallowMount } from '@vue/test-utils'
import ChatMessageList from './chat_message_list.vue' import ChatMessageList from './chat_message_list.vue'
describe('ChatMessageList', () => { describe('ChatMessageList', () => {
describe('computed.chatItems', () => { describe('computed.chatItems', () => {
it('Inserts date separators', () => { it('Inserts date separators', () => {
const component = shallowMount(ChatMessageList, { const component = shallowMount(ChatMessageList, {
props: { props: {
messages: [{ messages: [
id: '1', {
idempotency_key: '1', id: '0',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-22T20:00:00.000Z'), created_at: new Date('2020-06-22T20:00:00.000Z'),
}, { },
id: '2', {
idempotency_key: '2', id: '1',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-22T20:01:00.000Z'), created_at: new Date('2020-06-22T20:01:00.000Z'),
}, { },
id: '3', {
idempotency_key: '3', id: '2',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-23T20:00:00.000Z'), created_at: new Date('2020-06-23T20:00:00.000Z'),
}] },
} ],
},
}) })
expect(component.vm.chatItems.map((i) => i.type)).to.eql([ expect(component.vm.chatItems.map((i) => i.type)).to.eql([
@ -39,13 +39,14 @@ describe('ChatMessageList', () => {
const component = shallowMount(ChatMessageList, { const component = shallowMount(ChatMessageList, {
props: { props: {
headerDate: true, headerDate: true,
messages: [{ messages: [
id: '1', {
idempotency_key: '1', id: '0',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-23T20:00:00.000Z'), created_at: new Date('2020-06-23T20:00:00.000Z'),
}] },
} ],
},
}) })
expect(component.vm.chatItems.map((i) => i.type)).to.eql([ 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)', () => { it('Inserts time separators if messages were sent with considerable delay (5 minutes)', () => {
const component = shallowMount(ChatMessageList, { const component = shallowMount(ChatMessageList, {
props: { props: {
messages: [{ messages: [
id: '1', {
idempotency_key: '1', id: '0',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-22T20:00:00.000Z'), created_at: new Date('2020-06-22T20:00:00.000Z'),
}, { },
id: '2', {
idempotency_key: '2', id: '1',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-22T20:06:00.000Z'), created_at: new Date('2020-06-22T20:06:00.000Z'),
}, { },
id: '3', {
idempotency_key: '3', id: '2',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-23T20:00:00.000Z'), created_at: new Date('2020-06-23T20:00:00.000Z'),
}] },
} ],
},
}) })
expect(component.vm.chatItems.map((i) => i.type)).to.eql([ expect(component.vm.chatItems.map((i) => i.type)).to.eql([
@ -95,39 +97,44 @@ describe('ChatMessageList', () => {
it('Groups message chains by time and author', () => { it('Groups message chains by time and author', () => {
const component = shallowMount(ChatMessageList, { const component = shallowMount(ChatMessageList, {
props: { props: {
messages: [{ messages: [
id: '1', {
idempotency_key: '1', id: '0',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-22T20:00:00.000Z'), created_at: new Date('2020-06-22T20:00:00.000Z'),
}, { },
id: '2', {
idempotency_key: '2', id: '1',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-22T20:06:00.000Z'), created_at: new Date('2020-06-22T20:06:00.000Z'),
}, { },
id: '3', {
idempotency_key: '3', id: '2',
account_id: 'Alice', account_id: 'Alice',
created_at: new Date('2020-06-23T20:00:00.000Z'), created_at: new Date('2020-06-23T20:00:00.000Z'),
}, { },
id: '4', {
account_id: 'Bob', id: '3',
created_at: new Date('2020-06-23T20:01:00.000Z'), 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: '4',
}, { account_id: 'Bob',
id: '6', created_at: new Date('2020-06-23T20:02:00.000Z'),
account_id: 'Bob', },
created_at: new Date('2020-06-23T20:03:00.000Z'), {
}, { id: '5',
id: '7', account_id: 'Bob',
account_id: 'Eve', created_at: new Date('2020-06-23T20:03:00.000Z'),
created_at: new Date('2020-06-23T20:04:00.000Z'), },
}] {
} id: '6',
account_id: 'Eve',
created_at: new Date('2020-06-23T20:04:00.000Z'),
},
],
},
}) })
// Type check // Type check
@ -158,10 +165,80 @@ describe('ChatMessageList', () => {
// Unique ID is randomly generated so we have to compare data against itself // Unique ID is randomly generated so we have to compare data against itself
// Two messages from Bob next to each other // 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 // 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)
})
}) })
}) })
}) })