Skip to content

Commit

Permalink
added tests for the new vue components
Browse files Browse the repository at this point in the history
  • Loading branch information
CODE-MNA committed Mar 16, 2024
1 parent a029450 commit 8cfa9db
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { mount } from '@vue/test-utils';
import ChatMessageDisplay from './ChatMessageDisplay.vue';

describe('ChatMessageDisplayVue', () => {
it('renders valid message', () => {
const message = {
sender: 'John',
message: 'Hello, world!',
UTC_timestamp: '2022-03-17T12:00:00Z',
};
const wrapper = mount(ChatMessageDisplay, {
props: { message },
});

expect(wrapper.find('.message-container').exists()).toBe(true);
expect(wrapper.find('.sender-name').text()).toBe(message.sender);
expect(wrapper.find('.message-content').text()).toBe(message.message);
expect(wrapper.find('.timestamp').text()).toBe(message.UTC_timestamp);
});


// Add more test cases for other error scenarios...

});
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
import { mount, shallowMount } from '@vue/test-utils';
import ChatMessagesContainer from './ChatMessagesContainer.vue';
import { ChatMessage } from '@models/ChatMessage';

describe('ChatMessagesContainer', () => {
it('renders all messages', () => {
// Mock messages
const messages: ChatMessage[] = [{
sender:"System",
message:"Hello user, this is default message!",
UTC_timestamp: "2021-3-5"
},
{
sender:"User",
message:"Hello system, this is nice!",
UTC_timestamp: "2021-3-5"
},
{
sender:"Buddy",
message:"Let's play football!",
UTC_timestamp: "2021-3-5"
}
];

describe("ChatContainer", () => {
// Mount the component with props
const wrapper = mount(ChatMessagesContainer, {
props: {
messageListProp: messages,
},
});

it("Contains chat messages",()=>{

})
})
// Assert that all messages are rendered
const messageComponents = wrapper.findAllComponents({ name: 'ChatMessageDisplay' });
expect(messageComponents.length).toBe(messages.length);

expect(messageComponents.forEach(com => {
expect(com.text()).toContain(com.props().message.sender)
expect(com.text()).toContain(com.props().message.message)
expect(com.text()).toContain(com.props().message.UTC_timestamp)
}))



});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<script setup lang="ts">
import { ChatMessage } from '@models/ChatMessage';
import ChatMessageDisplayVue from '../ChatMessageDisplay/ChatMessageDisplay.vue';
import ChatMessageDisplay from '../ChatMessageDisplay/ChatMessageDisplay.vue';
defineProps<{messageListProp: ChatMessage[]}>()
</script>

<template>
<div class="container">
<ChatMessageDisplayVue
<ChatMessageDisplay
v-for="(item, index) in messageListProp"
:message="item"
:index="index"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ChatSender.spec.ts
import { VueWrapper, mount } from '@vue/test-utils';
import ChatSender from './ChatSender.vue';

describe('ChatSender', () => {
let wrapper: VueWrapper<InstanceType<typeof ChatSender>>;

beforeEach(() => {
wrapper = mount(ChatSender,{
data(){
return{
currentMessage:""
}
}
});
});

afterEach(() => {
wrapper.unmount();
});

it('renders input field and send button', () => {
const input = wrapper.find('input[type="text"]');
const button = wrapper.find('button');

expect(input.exists()).toBe(true);
expect(button.exists()).toBe(true);
});

it('emits "messageSend" event with current message when Send button is clicked', async () => {
const message = 'Hello, world!';
const input = wrapper.find('input[type="text"]');
await input.setValue(message);

const button = wrapper.find('button');
await button.trigger('click');

expect(wrapper.emitted('messageSend')).toBeTruthy();
const eventData : any = wrapper.emitted('messageSend');
expect(eventData).toBeTruthy();
expect(eventData[0][0]).toEqual({
sender: 'default',
message,
UTC_timestamp: expect.any(String),
});
});



});

0 comments on commit 8cfa9db

Please sign in to comment.