move tests to tests folder
This commit is contained in:
parent
1c7be5b663
commit
fd17bb535b
2 changed files with 0 additions and 0 deletions
244
test/unit/specs/components/chat_message_list.spec.js
Normal file
244
test/unit/specs/components/chat_message_list.spec.js
Normal file
|
|
@ -0,0 +1,244 @@
|
|||
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: '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([
|
||||
'message',
|
||||
'message',
|
||||
'date',
|
||||
'message',
|
||||
])
|
||||
})
|
||||
|
||||
it('Inserts date header if needed', () => {
|
||||
const component = shallowMount(ChatMessageList, {
|
||||
props: {
|
||||
headerDate: true,
|
||||
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([
|
||||
'date',
|
||||
'message',
|
||||
])
|
||||
})
|
||||
|
||||
it('Inserts time separators if messages were sent with considerable delay (5 minutes)', () => {
|
||||
const component = shallowMount(ChatMessageList, {
|
||||
props: {
|
||||
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([
|
||||
'message',
|
||||
'date',
|
||||
'message',
|
||||
'date',
|
||||
'message',
|
||||
])
|
||||
expect(component.vm.chatItems.map((i) => i.isTime)).to.eql([
|
||||
undefined,
|
||||
true,
|
||||
undefined,
|
||||
false,
|
||||
undefined,
|
||||
])
|
||||
})
|
||||
|
||||
it('Groups message chains by time and author', () => {
|
||||
const component = shallowMount(ChatMessageList, {
|
||||
props: {
|
||||
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
|
||||
expect(component.vm.chatItems.map((i) => i.type)).to.eql([
|
||||
'message',
|
||||
'date',
|
||||
'message',
|
||||
'date',
|
||||
'message',
|
||||
'message',
|
||||
'message',
|
||||
'message',
|
||||
'message',
|
||||
])
|
||||
|
||||
// Chain head/Tail checks
|
||||
expect(component.vm.chatItems.map((i) => [i.isHead, i.isTail])).to.eql([
|
||||
[true, true],
|
||||
[undefined, undefined],
|
||||
[true, true],
|
||||
[undefined, undefined],
|
||||
[true, true],
|
||||
[true, false],
|
||||
[false, false],
|
||||
[false, true],
|
||||
[true, true],
|
||||
])
|
||||
|
||||
// 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,
|
||||
)
|
||||
|
||||
// Message from Even right after Bob
|
||||
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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
132
test/unit/specs/components/chat_view.spec.js
Normal file
132
test/unit/specs/components/chat_view.spec.js
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
import { shallowMount } from '@vue/test-utils'
|
||||
import { setActivePinia } from 'pinia'
|
||||
import { createTestingPinia } from '@pinia/testing'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import ChatView from './chat_view.vue'
|
||||
|
||||
const message1 = {
|
||||
id: '1',
|
||||
chat_id: 2,
|
||||
idempotency_key: '1',
|
||||
created_at: new Date('2020-06-22T18:45:53.000Z'),
|
||||
}
|
||||
|
||||
const message2 = {
|
||||
id: '2',
|
||||
chat_id: 2,
|
||||
idempotency_key: '2',
|
||||
account_id: '9vmRb29zLQReckr5ay',
|
||||
created_at: new Date('2020-06-22T18:45:56.000Z'),
|
||||
}
|
||||
|
||||
const message3 = {
|
||||
id: '3',
|
||||
chat_id: 2,
|
||||
idempotency_key: '3',
|
||||
account_id: '9vmRb29zLQReckr5ay',
|
||||
created_at: new Date('2020-07-22T18:45:59.000Z'),
|
||||
}
|
||||
|
||||
const global = {
|
||||
mocks: {
|
||||
$store: {
|
||||
state: {
|
||||
api: {},
|
||||
users: {}
|
||||
},
|
||||
},
|
||||
$route: {
|
||||
params: {
|
||||
recipient_id: 2,
|
||||
},
|
||||
},
|
||||
$router: {
|
||||
push: () => {}
|
||||
}
|
||||
},
|
||||
stubs: {
|
||||
FAIcon: true,
|
||||
},
|
||||
}
|
||||
|
||||
describe('ChatView methods', () => {
|
||||
let component
|
||||
beforeEach(() => {
|
||||
setActivePinia(createTestingPinia())
|
||||
component = shallowMount(ChatView, { global, props: { testMode: true } })
|
||||
component.vm.chat = { id: 2 }
|
||||
})
|
||||
|
||||
describe('addMessages', () => {
|
||||
it("Doesn't add duplicates", () => {
|
||||
component.vm.addMessages({ messages: [message1] })
|
||||
component.vm.addMessages({ messages: [message1] })
|
||||
expect(component.vm.messages.length).to.eql(1)
|
||||
|
||||
component.vm.addMessages({ messages: [message2] })
|
||||
expect(component.vm.messages.length).to.eql(2)
|
||||
})
|
||||
|
||||
it('Updates minId and lastMessage and newMessageCount', () => {
|
||||
component.vm.addMessages({ messages: [message1] })
|
||||
expect(component.vm.maxId).to.eql(message1.id)
|
||||
expect(component.vm.minId).to.eql(message1.id)
|
||||
expect(component.vm.newMessageCount).to.eql(1)
|
||||
|
||||
component.vm.addMessages({ messages: [message2] })
|
||||
expect(component.vm.maxId).to.eql(message2.id)
|
||||
expect(component.vm.minId).to.eql(message1.id)
|
||||
expect(component.vm.newMessageCount).to.eql(2)
|
||||
|
||||
component.vm.readChat()
|
||||
expect(component.vm.newMessageCount).to.eql(0)
|
||||
expect(component.vm.lastReadMessageId).to.eql(message2.id)
|
||||
|
||||
// Add message with higher id
|
||||
component.vm.addMessages({ messages: [message3] })
|
||||
expect(component.vm.newMessageCount).to.eql(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('deleteChatMessage', () => {
|
||||
it('Updates minId and lastMessage', () => {
|
||||
component.vm.addMessages({ messages: [message1] })
|
||||
component.vm.addMessages({ messages: [message2] })
|
||||
component.vm.addMessages({ messages: [message3] })
|
||||
|
||||
expect(component.vm.maxId).to.eql(message3.id)
|
||||
expect(component.vm.minId).to.eql(message1.id)
|
||||
|
||||
component.vm.deleteChatMessage({ messageId: message3.id })
|
||||
expect(component.vm.maxId).to.eql(message2.id)
|
||||
expect(component.vm.minId).to.eql(message1.id)
|
||||
|
||||
component.vm.deleteChatMessage({ messageId: message1.id })
|
||||
expect(component.vm.maxId).to.eql(message2.id)
|
||||
expect(component.vm.minId).to.eql(message2.id)
|
||||
})
|
||||
})
|
||||
|
||||
describe('cullOlder', () => {
|
||||
it('keeps 50 newest messages and messagesIndex matches', () => {
|
||||
for (let i = 100; i > 0; i--) {
|
||||
// Use decimal values with toFixed to hack together constant length predictable strings
|
||||
component.vm.addMessages({
|
||||
messages: [
|
||||
{
|
||||
...message1,
|
||||
id: 'a' + (i / 1000).toFixed(3),
|
||||
idempotency_key: i,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
component.vm.cullOlder()
|
||||
expect(component.vm.messages.length).to.eql(50)
|
||||
expect(component.vm.messages[0].id).to.eql('a0.051')
|
||||
expect(component.vm.minId).to.eql('a0.051')
|
||||
expect(component.vm.messages[49].id).to.eql('a0.100')
|
||||
expect(Object.keys(component.vm.messagesIndex).length).to.eql(50)
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue