Skip to content

Commit

Permalink
Tests for WhatsApp message and template logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsynge committed Feb 4, 2025
1 parent ee9f9f0 commit 8fa20af
Show file tree
Hide file tree
Showing 5 changed files with 740 additions and 0 deletions.
208 changes: 208 additions & 0 deletions src/lib/comps/forms/whatsapp/messages/builder/actions/actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import { describe, it, expect } from 'vitest';
import * as actionsFunctions from '$lib/comps/forms/whatsapp/messages/builder/actions/actions';

describe('createWhatsAppMessageAction', () => {
const buttonId = '9f4d2b16-3a72-4bfb-bc6e-d6fbcbb6c582';
const alternativeButtonId = 'c3a1f9d7-5b8e-4e1b-92f5-8a2d7c3e64f1';
const messageId = '3f4e3c27-3a72-32fd-ed2e-fffbffe6c432'; //random uuid

it('should append a new action when an array exists', () => {
const actions = {
[buttonId]: [
{
type: 'send_whatsapp_message' as 'send_whatsapp_message',
message_id: messageId
}
]
};
const result = actionsFunctions.createWhatsAppMessageAction(buttonId, actions);
expect(result[buttonId]).toHaveLength(2);
expect(result[buttonId][0]).toEqual({
type: 'send_whatsapp_message',
message_id: messageId
});
expect(result[buttonId][1]).toEqual({
type: 'send_whatsapp_message',
message_id: ''
});
});

it('should append a new buttonId if there is an existing buttonId with actions', () => {
const actions = {
[buttonId]: [
{
type: 'send_whatsapp_message' as 'send_whatsapp_message',
message_id: messageId
}
]
};
const result = actionsFunctions.createWhatsAppMessageAction(alternativeButtonId, actions);
expect(result[buttonId]).toHaveLength(1);
expect(result[buttonId][0]).toEqual({
type: 'send_whatsapp_message',
message_id: messageId
});
expect(result[alternativeButtonId]).toHaveLength(1);
expect(result[alternativeButtonId][0]).toEqual({
type: 'send_whatsapp_message',
message_id: ''
});
});

it('should create a new array if id does not exist', () => {
const actions = {};
const result = actionsFunctions.createWhatsAppMessageAction(buttonId, actions);
expect(result[buttonId]).toHaveLength(1);
expect(result[buttonId][0]).toEqual({
type: 'send_whatsapp_message',
message_id: ''
});
});

it('should not mutate the original actions object', () => {
const actions = {
[buttonId]: [
{
type: 'send_whatsapp_message' as 'send_whatsapp_message',
message_id: messageId
}
]
};
const copy = structuredClone(actions); // Deep copy for comparison

actionsFunctions.createWhatsAppMessageAction(buttonId, actions);

expect(actions).toEqual(copy); // Ensure original object is unchanged
});

it('should handle an existing id with a non-array value gracefully', () => {
const actions = { [buttonId]: null as any }; // Simulating unexpected value
const result = actionsFunctions.createWhatsAppMessageAction(buttonId, actions);
expect(result[buttonId]).toEqual([{ type: 'send_whatsapp_message', message_id: '' }]);
});

it('should not modify other ids in the actions object', () => {
const actions = {
[buttonId]: [],
[alternativeButtonId]: [
{ type: 'send_whatsapp_message' as 'send_whatsapp_message', message_id: messageId }
]
};
const result = actionsFunctions.createWhatsAppMessageAction(buttonId, actions);

expect(result[alternativeButtonId]).toEqual(actions[alternativeButtonId]); // Ensure user2's actions remain unchanged
});
});

describe('createEventRegistrationAction', () => {
const buttonId = '9f4d2b16-3a72-4bfb-bc6e-d6fbcbb6c582';
const alternativeButtonId = 'c3a1f9d7-5b8e-4e1b-92f5-8a2d7c3e64f1';
const eventId = 1234;

it('should append a new action when an array exists', () => {
const actions = {
[buttonId]: [
{
type: 'register_for_event' as 'register_for_event',
event_id: eventId
}
]
};
const result = actionsFunctions.createEventRegistrationAction(buttonId, actions);
expect(result[buttonId]).toHaveLength(2);
expect(result[buttonId][0]).toEqual({
type: 'register_for_event',
event_id: eventId
});
expect(result[buttonId][1]).toEqual({
type: 'register_for_event',
event_id: 0
});
});

it('should append a new buttonId if there is an existing buttonId with actions', () => {
const actions = {
[buttonId]: [
{
type: 'register_for_event' as 'register_for_event',
event_id: eventId
}
]
};
const result = actionsFunctions.createEventRegistrationAction(alternativeButtonId, actions);
expect(result[buttonId]).toHaveLength(1);
expect(result[buttonId][0]).toEqual({
type: 'register_for_event',
event_id: eventId
});
expect(result[alternativeButtonId]).toHaveLength(1);
expect(result[alternativeButtonId][0]).toEqual({
type: 'register_for_event',
event_id: 0
});
});

it('should create a new array if id does not exist', () => {
const actions = {};
const result = actionsFunctions.createEventRegistrationAction(buttonId, actions);
expect(result[buttonId]).toHaveLength(1);
expect(result[buttonId][0]).toEqual({
type: 'register_for_event',
event_id: 0
});
});

it('should not mutate the original actions object', () => {
const actions = {
[buttonId]: [
{
type: 'register_for_event' as 'register_for_event',
event_id: eventId
}
]
};
const copy = structuredClone(actions); // Deep copy for comparison

actionsFunctions.createEventRegistrationAction(buttonId, actions);

expect(actions).toEqual(copy); // Ensure original object is unchanged
});

it('should handle an existing id with a non-array value gracefully', () => {
const actions = { [buttonId]: null as any }; // Simulating unexpected value
const result = actionsFunctions.createEventRegistrationAction(buttonId, actions);
expect(result[buttonId]).toEqual([{ type: 'register_for_event', event_id: 0 }]);
});

it('should not modify other ids in the actions object', () => {
const actions = {
[buttonId]: [],
[alternativeButtonId]: [
{ type: 'register_for_event' as 'register_for_event', event_id: eventId }
]
};
const result = actionsFunctions.createEventRegistrationAction(buttonId, actions);

expect(result[alternativeButtonId]).toEqual(actions[alternativeButtonId]); // Ensure user2's actions remain unchanged
});
});

describe('updateEventRegistrationAction', () => {
it('should update event_id if action type is "register_for_event"', () => {
const action = { type: 'register_for_event' as 'register_for_event', event_id: 1 };
const updatedAction = actionsFunctions.updateEventRegistrationAction(action, 42);

expect(updatedAction).toEqual({ ...action, event_id: 42 });
});

it('should return the original action if type is not "register_for_event"', () => {
const action = {
type: 'send_whatsapp_message' as 'send_whatsapp_message',
message_id: 'randomUUID',
otherProp: 'test'
};
const updatedAction = actionsFunctions.updateEventRegistrationAction(action, 42);

expect(updatedAction).toBe(action);
});
});
133 changes: 133 additions & 0 deletions src/lib/comps/forms/whatsapp/messages/builder/actions/buttons.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { describe, it, expect, vi } from 'vitest';
import {
addButton,
removeButton
} from '$lib/comps/forms/whatsapp/messages/builder/actions/buttons';

const mockedUUID = '9f4d2b16-3a72-4bfb-bc6e-d6fbcbb6c582';
vi.mock('uuid', () => ({
v4: () => mockedUUID
}));

describe('addButton', () => {
it('should add a button to an interactive message', () => {
const message = {
type: 'interactive' as 'interactive',
interactive: {
type: 'button' as 'button',
body: { text: 'Message' },
action: { buttons: [] }
}
};
expect(addButton(message)).toEqual({
...message,
interactive: {
...message.interactive,
action: {
buttons: [{ type: 'reply', id: mockedUUID, title: '[Button title]' }]
}
}
});
});

it('should convert a text message to an interactive message with a button', () => {
const message = { type: 'text' as 'text', text: { body: 'Hello', preview_url: false } };
expect(addButton(message)).toEqual({
...message,
type: 'interactive',
interactive: {
body: { text: 'Hello' },
type: 'button',
action: { buttons: [{ type: 'reply', id: mockedUUID, title: '[Button title]' }] }
}
});
});

it('should convert an image message to an interactive message with a button', () => {
const message = {
type: 'image' as 'image',
image: { caption: 'Look!', link: 'https://example.com/image.jpg' }
};
expect(addButton(message)).toEqual({
...message,
type: 'interactive',
interactive: {
body: { text: 'Look!' },
header: { type: 'image', image: { link: 'https://example.com/image.jpg' } },
type: 'button',
action: { buttons: [{ type: 'reply', id: mockedUUID, title: '[Button title]' }] }
}
});
});

it('should return the original message if type is not interactive, text, or image', () => {
const message = { type: 'video' as 'video', video: { link: 'https://example.com/video.mp4' } };
expect(addButton(message)).toBe(message);
});
});

describe('removeButton', () => {
it('should remove a button from an interactive message', () => {
const message = {
type: 'interactive' as 'interactive',
interactive: {
type: 'button' as 'button',
body: { text: 'Hello' },
action: {
buttons: [
{ type: 'reply' as 'reply', id: mockedUUID, title: 'Button 1' },
{ type: 'reply' as 'reply', id: mockedUUID, title: 'Button 2' }
]
}
}
};

expect(removeButton(message, 0)).toEqual({
...message,
interactive: {
...message.interactive,
action: {
buttons: [{ type: 'reply', id: mockedUUID, title: 'Button 2' }]
}
}
});
});

it('should convert an interactive message with one button into a text message', () => {
const message = {
type: 'interactive' as 'interactive',
interactive: {
type: 'button' as 'button',
body: { text: 'Hello' },
action: { buttons: [{ type: 'reply' as 'reply', id: mockedUUID, title: 'Button 1' }] }
}
};

expect(removeButton(message, 0)).toEqual({
type: 'text',
text: { body: 'Hello', preview_url: false }
});
});

it('should convert an interactive message with an image and one button into an image message', () => {
const message = {
type: 'interactive' as 'interactive',
interactive: {
type: 'button' as 'button',
body: { text: 'Hello' },
header: { type: 'image' as 'image', image: { link: 'https://example.com/image.jpg' } },
action: { buttons: [{ type: 'reply' as 'reply', id: '1', title: 'Button 1' }] }
}
};

expect(removeButton(message, 0)).toEqual({
type: 'image',
image: { link: 'https://example.com/image.jpg', caption: 'Hello' }
});
});

it('should return the original message if it is not interactive', () => {
const message = { type: 'text' as 'text', text: { body: 'Hello', preview_url: false } };
expect(removeButton(message, 0)).toBe(message);
});
});
Loading

0 comments on commit 8fa20af

Please sign in to comment.