test: refactor commandsManager usage in unit tests for clarity

This commit is contained in:
Carlos Santos 2025-05-14 16:39:29 +02:00
parent a2f1ea71d2
commit b52eab4bca
2 changed files with 18 additions and 6 deletions

View File

@ -2,12 +2,15 @@ import { describe, it, expect, jest } from '@jest/globals';
import { OpenViduMeet } from '../../src/components/OpenViduMeet';
import '../../src/index';
import { WebComponentCommand } from '../../src/models/command.model';
import { CommandsManager } from '../../src/components/CommandsManager';
describe('OpenViduMeet Web Component Commands', () => {
let component: OpenViduMeet;
let commandsManager: CommandsManager;
beforeEach(() => {
component = document.createElement('openvidu-meet') as OpenViduMeet;
commandsManager = component['commandsManager'] as CommandsManager;
document.body.appendChild(component);
});
@ -20,21 +23,27 @@ describe('OpenViduMeet Web Component Commands', () => {
it('should update allowedOrigin when setAllowedOrigin is called', () => {
const testOrigin = 'https://test-origin.com';
// Set allowed origin
(component as any).commandsManager.setAllowedOrigin(testOrigin);
commandsManager.setAllowedOrigin(testOrigin);
expect(commandsManager['allowedOrigin']).toBe(testOrigin);
// Check if it was updated
expect((component as any).commandsManager.allowedOrigin).toBe(testOrigin);
});
it('should call commandsManager.sendMessage when leaveRoom is called', () => {
const spy = jest.spyOn(component['commandsManager'], 'sendMessage');
it('should call commandsManager.leaveRoom when leaveRoom is called', () => {
const meetSpy = jest.spyOn(component['commandsManager'], 'leaveRoom');
const spy = jest.spyOn(commandsManager, 'sendMessage' as keyof CommandsManager);
component.leaveRoom();
expect(meetSpy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({ command: WebComponentCommand.LEAVE_ROOM });
});
it('should call commandsManager.sendMessage when endMeeting is called', () => {
const spy = jest.spyOn(component['commandsManager'], 'sendMessage');
const spy = jest.spyOn(commandsManager, 'sendMessage' as keyof CommandsManager);
component.endMeeting();
expect(spy).toHaveBeenCalledWith({ command: WebComponentCommand.END_MEETING });
});

View File

@ -3,12 +3,15 @@ import { OpenViduMeet } from '../../src/components/OpenViduMeet';
import '../../src/index';
import { WebComponentCommand } from '../../src/models/command.model';
import { WEBCOMPONENT_ROOM_URL } from '../config';
import { CommandsManager } from '../../src/components/CommandsManager';
describe('OpenViduMeet Event Handling', () => {
let component: OpenViduMeet;
let commandsManager: CommandsManager;
beforeEach(() => {
component = document.createElement('openvidu-meet') as OpenViduMeet;
commandsManager = component['commandsManager'] as CommandsManager;
document.body.appendChild(component);
});
@ -33,7 +36,7 @@ describe('OpenViduMeet Event Handling', () => {
});
it('should call sendMessage when READY event is received', () => {
const sendMessageSpy = jest.spyOn(component['commandsManager'], 'sendMessage');
const sendMessageSpy = jest.spyOn(commandsManager, 'sendMessage' as keyof CommandsManager);
// Mock a message event
const readyEvent = new MessageEvent('message', {