Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CFE-454] - Tests of utils #564

Merged
merged 14 commits into from
Nov 29, 2024
Merged
58 changes: 58 additions & 0 deletions src/utils/__tests__/array.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, it, expect } from 'vitest';
import { removeDuplicatedItems } from '../array';

describe('removeDuplicatedItems', () => {
it('should remove duplicated items based on the default key (uuid)', () => {
const array = [
{ uuid: '1', name: 'John' },
{ uuid: '2', name: 'Jane' },
{ uuid: '1', name: 'John Duplicate' },
{ uuid: '3', name: 'Bob' },
];

const result = removeDuplicatedItems(array);

expect(result).toEqual([
{ uuid: '1', name: 'John' },
{ uuid: '2', name: 'Jane' },
{ uuid: '3', name: 'Bob' },
]);
});

it('should remove duplicated items based on a custom key', () => {
const array = [
{ id: 'a', name: 'John' },
{ id: 'b', name: 'Jane' },
{ id: 'a', name: 'John Duplicate' },
{ id: 'c', name: 'Bob' },
];

const result = removeDuplicatedItems(array, 'id');

expect(result).toEqual([
{ id: 'a', name: 'John' },
{ id: 'b', name: 'Jane' },
{ id: 'c', name: 'Bob' },
]);
});

it('should return the original array if there are no duplicates', () => {
const array = [
{ uuid: '1', name: 'John' },
{ uuid: '2', name: 'Jane' },
{ uuid: '3', name: 'Bob' },
];

const result = removeDuplicatedItems(array);

expect(result).toEqual(array);
});

it('should return an empty array if input is empty', () => {
const array = [];

const result = removeDuplicatedItems(array);

expect(result).toEqual([]);
});
});
37 changes: 37 additions & 0 deletions src/utils/__tests__/callUnnnicAlert.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import unnnic from '@weni/unnnic-system';
import callUnnnicAlert from '../callUnnnicAlert.js';

describe('callUnnnicAlert', () => {
beforeEach(() => {
document.body.innerHTML = `
<div class="mobile-home__main"></div>
`;

vi.spyOn(unnnic, 'unnnicCallAlert').mockImplementation(() => {});
});

it('should call unnnicCallAlert with the correct props and containerRef', () => {
const props = { type: 'success', message: 'Alert message' };

callUnnnicAlert(props);

expect(unnnic.unnnicCallAlert).toHaveBeenCalledWith({
...props,
containerRef: document.querySelector('.mobile-home__main'),
});
});

it('should handle missing .mobile-home__main normally', () => {
document.querySelector('.mobile-home__main').remove();

const props = { type: 'error', message: 'Another alert' };

callUnnnicAlert(props);

expect(unnnic.unnnicCallAlert).toHaveBeenCalledWith({
...props,
containerRef: null,
});
});
});
110 changes: 110 additions & 0 deletions src/utils/__tests__/chats.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { resetChats, formatContactName } from '../chats';
import { useDiscussions } from '@/store/modules/chats/discussions';
import { useDiscussionMessages } from '@/store/modules/chats/discussionMessages';
import { useRoomMessages } from '@/store/modules/chats/roomMessages';
import { useRooms } from '@/store/modules/chats/rooms';

vi.mock('@/store/modules/chats/discussions', () => ({
useDiscussions: vi.fn(),
}));

vi.mock('@/store/modules/chats/discussionMessages', () => ({
useDiscussionMessages: vi.fn(),
}));

vi.mock('@/store/modules/chats/roomMessages', () => ({
useRoomMessages: vi.fn(),
}));

vi.mock('@/store/modules/chats/rooms', () => ({
useRooms: vi.fn(),
}));

describe('resetChats', () => {
const mockResetDiscussionMessages = vi.fn();
const mockResetRoomMessages = vi.fn();
const mockSetActiveDiscussion = vi.fn();
const mockSetActiveRoom = vi.fn();

beforeEach(() => {
vi.clearAllMocks();

useDiscussionMessages.mockReturnValue({
resetDiscussionMessages: mockResetDiscussionMessages,
});

useRoomMessages.mockReturnValue({
resetRoomMessages: mockResetRoomMessages,
});

useDiscussions.mockReturnValue({
setActiveDiscussion: mockSetActiveDiscussion,
});

useRooms.mockReturnValue({
setActiveRoom: mockSetActiveRoom,
});
});

it('should call the correct store methods to reset chats', async () => {
await resetChats();

expect(mockResetDiscussionMessages).toHaveBeenCalledOnce();
expect(mockResetRoomMessages).toHaveBeenCalledOnce();
expect(mockSetActiveDiscussion).toHaveBeenCalledWith(null);
expect(mockSetActiveRoom).toHaveBeenCalledWith(null);
});
});

describe('formatContactName', () => {
it('should format contact name with service chat and protocol', () => {
const room = {
service_chat: 'ServiceChatName',
protocol: 'Protocol123',
contact: { name: 'John Doe' },
};

const result = formatContactName(room);

expect(result).toBe('ServiceChatName | Protocol123 | John Doe');
});

it('should format contact name with only service chat', () => {
const room = {
service_chat: 'ServiceChatName',
contact: { name: 'John Doe' },
};

const result = formatContactName(room);

expect(result).toBe('ServiceChatName | John Doe');
});

it('should format contact name with only protocol', () => {
const room = {
protocol: 'Protocol123',
contact: { name: 'John Doe' },
};

const result = formatContactName(room);

expect(result).toBe('Protocol123 | John Doe');
});

it('should return only contact name if service chat and protocol are missing', () => {
const room = {
contact: { name: 'John Doe' },
};

const result = formatContactName(room);

expect(result).toBe('John Doe');
});

it('should handle missing room object normally', () => {
const result = formatContactName(null);

expect(result).toBe('');
});
});
162 changes: 162 additions & 0 deletions src/utils/__tests__/config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
getToken,
setToken,
getProject,
setProject,
setStatus,
} from '@/utils/config';

let mockSessionStorage;
let mockLocalStorage;

describe('Config storage', () => {
beforeEach(() => {
mockSessionStorage = {};
mockLocalStorage = {};

vi.stubGlobal('sessionStorage', {
getItem: vi.fn((key) => mockSessionStorage[key] || null),
setItem: vi.fn((key, value) => {
mockSessionStorage[key] = value;
}),
clear: vi.fn(() => {
Object.keys(mockSessionStorage).forEach(
(key) => delete mockSessionStorage[key],
);
}),
});

vi.stubGlobal('localStorage', {
getItem: vi.fn((key) => mockLocalStorage[key] || null),
setItem: vi.fn((key, value) => {
mockLocalStorage[key] = value;
}),
clear: vi.fn(() => {
Object.keys(mockLocalStorage).forEach(
(key) => delete mockLocalStorage[key],
);
}),
});
});

afterEach(() => {
vi.restoreAllMocks();
});

describe('Token', () => {
it('should get the token from sessionStorage first, then localStorage', () => {
mockSessionStorage['WENICHATS_API_TOKEN'] = 'session-token';
mockLocalStorage['WENICHATS_API_TOKEN'] = 'local-token';

const token = getToken();

expect(sessionStorage.getItem).toHaveBeenCalledWith(
'WENICHATS_API_TOKEN',
);
expect(localStorage.getItem).not.toHaveBeenCalled();
expect(token).toBe('session-token');
});

it('should fall back to localStorage if sessionStorage does not have the token', () => {
mockLocalStorage['WENICHATS_API_TOKEN'] = 'local-token';

const token = getToken();

expect(sessionStorage.getItem).toHaveBeenCalledWith(
'WENICHATS_API_TOKEN',
);
expect(localStorage.getItem).toHaveBeenCalledWith('WENICHATS_API_TOKEN');
expect(token).toBe('local-token');
});

it('should return an empty string if no token is found', () => {
const token = getToken();

expect(token).toBe('');
});

it('should set the token in both sessionStorage and localStorage', async () => {
const token = 'new-token';

await setToken(token);

expect(sessionStorage.setItem).toHaveBeenCalledWith(
'WENICHATS_API_TOKEN',
token,
);
expect(localStorage.setItem).toHaveBeenCalledWith(
'WENICHATS_API_TOKEN',
token,
);
expect(mockSessionStorage['WENICHATS_API_TOKEN']).toBe(token);
expect(mockLocalStorage['WENICHATS_API_TOKEN']).toBe(token);
});
});

describe('Project', () => {
it('should get the project from sessionStorage first, then localStorage', () => {
mockSessionStorage['WENICHATS_PROJECT_UUID'] = 'session-project';
mockLocalStorage['WENICHATS_PROJECT_UUID'] = 'local-project';

const project = getProject();

expect(sessionStorage.getItem).toHaveBeenCalledWith(
'WENICHATS_PROJECT_UUID',
);
expect(localStorage.getItem).not.toHaveBeenCalled();
expect(project).toBe('session-project');
});

it('should fall back to localStorage if sessionStorage does not have the project', () => {
mockLocalStorage['WENICHATS_PROJECT_UUID'] = 'local-project';

const project = getProject();

expect(sessionStorage.getItem).toHaveBeenCalledWith(
'WENICHATS_PROJECT_UUID',
);
expect(localStorage.getItem).toHaveBeenCalledWith(
'WENICHATS_PROJECT_UUID',
);
expect(project).toBe('local-project');
});

it('should return an empty string if no project is found', () => {
const project = getProject();

expect(project).toBe('');
});

it('should set the project in both sessionStorage and localStorage', async () => {
const projectUuid = 'new-project-uuid';

await setProject(projectUuid);

expect(sessionStorage.setItem).toHaveBeenCalledWith(
'WENICHATS_PROJECT_UUID',
projectUuid,
);
expect(localStorage.setItem).toHaveBeenCalledWith(
'WENICHATS_PROJECT_UUID',
projectUuid,
);
expect(mockSessionStorage['WENICHATS_PROJECT_UUID']).toBe(projectUuid);
expect(mockLocalStorage['WENICHATS_PROJECT_UUID']).toBe(projectUuid);
});
});

describe('Status', () => {
it('should set the status in sessionStorage only', async () => {
const status = 'online';

await setStatus(status);

expect(sessionStorage.setItem).toHaveBeenCalledWith(
'statusAgent',
status,
);
expect(mockSessionStorage['statusAgent']).toBe(status);
});
});
});
Loading