feat: Rewrite automations/methodsMixin to a composable (#9956)

Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
Sivin Varghese
2024-08-27 12:30:08 +05:30
committed by GitHub
parent f82ec3b885
commit bc6420019f
12 changed files with 1220 additions and 970 deletions

View File

@@ -1,806 +0,0 @@
import allLanguages from '../../../dashboard/components/widgets/conversation/advancedFilterItems/languages.js';
import allCountries from '../../../shared/constants/countries.js';
export const customAttributes = [
{
id: 1,
attribute_display_name: 'Signed Up At',
attribute_display_type: 'date',
attribute_description: 'This is a test',
attribute_key: 'signed_up_at',
attribute_values: [],
attribute_model: 'conversation_attribute',
default_value: null,
created_at: '2022-01-26T08:06:39.470Z',
updated_at: '2022-01-26T08:06:39.470Z',
},
{
id: 2,
attribute_display_name: 'Prime User',
attribute_display_type: 'checkbox',
attribute_description: 'Test',
attribute_key: 'prime_user',
attribute_values: [],
attribute_model: 'contact_attribute',
default_value: null,
created_at: '2022-01-26T08:07:29.664Z',
updated_at: '2022-01-26T08:07:29.664Z',
},
{
id: 3,
attribute_display_name: 'Test',
attribute_display_type: 'text',
attribute_description: 'Test',
attribute_key: 'test',
attribute_values: [],
attribute_model: 'conversation_attribute',
default_value: null,
created_at: '2022-01-26T08:07:58.325Z',
updated_at: '2022-01-26T08:07:58.325Z',
},
{
id: 4,
attribute_display_name: 'Link',
attribute_display_type: 'link',
attribute_description: 'Test',
attribute_key: 'link',
attribute_values: [],
attribute_model: 'conversation_attribute',
default_value: null,
created_at: '2022-02-07T07:31:51.562Z',
updated_at: '2022-02-07T07:31:51.562Z',
},
{
id: 5,
attribute_display_name: 'My List',
attribute_display_type: 'list',
attribute_description: 'This is a sample list',
attribute_key: 'my_list',
attribute_values: ['item1', 'item2', 'item3'],
attribute_model: 'conversation_attribute',
default_value: null,
created_at: '2022-02-21T20:31:34.175Z',
updated_at: '2022-02-21T20:31:34.175Z',
},
{
id: 6,
attribute_display_name: 'My Check',
attribute_display_type: 'checkbox',
attribute_description: 'Test Checkbox',
attribute_key: 'my_check',
attribute_values: [],
attribute_model: 'conversation_attribute',
default_value: null,
created_at: '2022-02-21T20:31:53.385Z',
updated_at: '2022-02-21T20:31:53.385Z',
},
{
id: 7,
attribute_display_name: 'ConList',
attribute_display_type: 'list',
attribute_description: 'This is a test list\n',
attribute_key: 'conlist',
attribute_values: ['Hello', 'Test', 'Test2'],
attribute_model: 'contact_attribute',
default_value: null,
created_at: '2022-02-28T12:58:05.005Z',
updated_at: '2022-02-28T12:58:05.005Z',
},
{
id: 8,
attribute_display_name: 'asdf',
attribute_display_type: 'link',
attribute_description: 'This is a some text',
attribute_key: 'asdf',
attribute_values: [],
attribute_model: 'contact_attribute',
default_value: null,
created_at: '2022-04-21T05:48:16.168Z',
updated_at: '2022-04-21T05:48:16.168Z',
},
];
export const emptyAutomation = {
name: null,
description: null,
event_name: 'conversation_created',
conditions: [
{
attribute_key: 'status',
filter_operator: 'equal_to',
values: '',
query_operator: 'and',
},
],
actions: [
{
action_name: 'assign_team',
action_params: [],
},
],
};
export const filterAttributes = [
{
key: 'status',
name: 'Status',
attributeI18nKey: 'STATUS',
inputType: 'multi_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'browser_language',
name: 'Browser Language',
attributeI18nKey: 'BROWSER_LANGUAGE',
inputType: 'search_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'country_code',
name: 'Country',
attributeI18nKey: 'COUNTRY_NAME',
inputType: 'search_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'referer',
name: 'Referrer Link',
attributeI18nKey: 'REFERER_LINK',
inputType: 'plain_text',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
{ value: 'contains', label: 'Contains' },
{ value: 'does_not_contain', label: 'Does not contain' },
],
},
{
key: 'inbox_id',
name: 'Inbox',
attributeI18nKey: 'INBOX',
inputType: 'multi_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'conversation_custom_attribute',
name: 'Conversation Custom Attributes',
disabled: true,
},
{
key: 'signed_up_at',
name: 'Signed Up At',
inputType: 'date',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
{ value: 'is_present', label: 'Is present' },
{ value: 'is_not_present', label: 'Is not present' },
{ value: 'is_greater_than', label: 'Is greater than' },
{ value: 'is_less_than', label: 'Is less than' },
],
},
{
key: 'test',
name: 'Test',
inputType: 'plain_text',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
{ value: 'is_present', label: 'Is present' },
{ value: 'is_not_present', label: 'Is not present' },
],
},
{
key: 'link',
name: 'Link',
inputType: 'plain_text',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'my_list',
name: 'My List',
inputType: 'search_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'my_check',
name: 'My Check',
inputType: 'search_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'contact_custom_attribute',
name: 'Contact Custom Attributes',
disabled: true,
},
{
key: 'prime_user',
name: 'Prime User',
inputType: 'search_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'conlist',
name: 'ConList',
inputType: 'search_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
{
key: 'asdf',
name: 'asdf',
inputType: 'plain_text',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
];
export const automation = {
id: 164,
account_id: 1,
name: 'Attachment',
description: 'Yo',
event_name: 'conversation_created',
conditions: [
{
values: [{ id: 'open', name: 'Open' }],
attribute_key: 'status',
filter_operator: 'equal_to',
query_operator: 'and',
},
],
actions: [{ action_name: 'send_attachment', action_params: [59] }],
created_on: 1652717181,
active: true,
files: [
{
id: 50,
automation_rule_id: 164,
file_type: 'image/jpeg',
account_id: 1,
file_url:
'http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBRQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--965b4c27f4c5e47c526f0f38266b25417b72e5dd/pfp.jpeg',
blob_id: 59,
filename: 'pfp.jpeg',
},
],
};
export const agents = [
{
id: 1,
account_id: 1,
availability_status: 'online',
auto_offline: true,
confirmed: true,
email: 'john@acme.inc',
available_name: 'Fayaz',
name: 'Fayaz',
role: 'administrator',
thumbnail:
'https://www.gravatar.com/avatar/0d722ac7bc3b3c92c030d0da9690d981?d=404',
},
{
id: 5,
account_id: 1,
availability_status: 'offline',
auto_offline: true,
confirmed: true,
email: 'john@doe.com',
available_name: 'John',
name: 'John',
role: 'agent',
thumbnail:
'https://www.gravatar.com/avatar/6a6c19fea4a3676970167ce51f39e6ee?d=404',
},
];
export const booleanFilterOptions = [
{
id: true,
name: 'True',
},
{
id: false,
name: 'False',
},
];
export const teams = [
{
id: 1,
name: 'sales team',
description: 'This is our internal sales team',
allow_auto_assign: true,
account_id: 1,
is_member: true,
},
{
id: 2,
name: 'fayaz',
description: 'Test',
allow_auto_assign: true,
account_id: 1,
is_member: false,
},
];
export const campaigns = [];
export const contacts = [
{
additional_attributes: {},
availability_status: 'offline',
email: 'asd123123@asd.com',
id: 32,
name: 'asd123123',
phone_number: null,
identifier: null,
thumbnail:
'https://www.gravatar.com/avatar/46000d9a1eef3e24a02ca9d6c2a8f494?d=404',
custom_attributes: {},
conversations_count: 5,
last_activity_at: 1650519706,
},
{
additional_attributes: {},
availability_status: 'offline',
email: 'barry_allen@a.com',
id: 29,
name: 'barry_allen',
phone_number: null,
identifier: null,
thumbnail:
'https://www.gravatar.com/avatar/ab5ff99efa3bc1f74db1dc2885f9e2ce?d=404',
custom_attributes: {},
conversations_count: 1,
last_activity_at: 1643728899,
},
];
export const inboxes = [
{
id: 1,
avatar_url: '',
channel_id: 1,
name: 'Acme Support',
channel_type: 'Channel::WebWidget',
greeting_enabled: false,
greeting_message: '',
working_hours_enabled: false,
enable_email_collect: true,
csat_survey_enabled: true,
sender_name_type: 0,
enable_auto_assignment: true,
out_of_office_message:
'We are unavailable at the moment. Leave a message we will respond once we are back.',
working_hours: [
{
day_of_week: 0,
closed_all_day: true,
open_hour: null,
open_minutes: null,
close_hour: null,
close_minutes: null,
open_all_day: false,
},
{
day_of_week: 1,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 2,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 3,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 4,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 5,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 6,
closed_all_day: true,
open_hour: null,
open_minutes: null,
close_hour: null,
close_minutes: null,
open_all_day: false,
},
],
timezone: 'America/Los_Angeles',
callback_webhook_url: null,
allow_messages_after_resolved: true,
widget_color: '#1f93ff',
website_url: 'https://acme.inc',
hmac_mandatory: false,
welcome_title: '',
welcome_tagline: '',
web_widget_script:
'\n <script>\n (function(d,t) {\n var BASE_URL="http://localhost:3000";\n var g=d.createElement(t),s=d.getElementsByTagName(t)[0];\n g.src=BASE_URL+"/packs/js/sdk.js";\n g.defer = true;\n g.async = true;\n s.parentNode.insertBefore(g,s);\n g.onload=function(){\n window.chatwootSDK.run({\n websiteToken: \'yZ7USzaEs7hrwUAHLGwjbxJ1\',\n baseUrl: BASE_URL\n })\n }\n })(document,"script");\n </script>\n ',
website_token: 'yZ7USzaEs7hrwUAHLGwjbxJ1',
selected_feature_flags: ['attachments', 'emoji_picker', 'end_conversation'],
reply_time: 'in_a_few_minutes',
hmac_token: 'rRJW1BHu4aFMMey4SE7tWr8A',
pre_chat_form_enabled: false,
pre_chat_form_options: {
pre_chat_fields: [
{
name: 'emailAddress',
type: 'email',
label: 'Email Id',
enabled: false,
required: true,
field_type: 'standard',
},
{
name: 'fullName',
type: 'text',
label: 'Full name',
enabled: false,
required: false,
field_type: 'standard',
},
{
name: 'phoneNumber',
type: 'text',
label: 'Phone number',
enabled: false,
required: false,
field_type: 'standard',
},
],
pre_chat_message: 'Share your queries or comments here.',
},
continuity_via_email: true,
phone_number: null,
},
{
id: 2,
avatar_url: '',
channel_id: 1,
name: 'Email',
channel_type: 'Channel::Email',
greeting_enabled: false,
greeting_message: null,
working_hours_enabled: false,
enable_email_collect: true,
csat_survey_enabled: false,
enable_auto_assignment: true,
out_of_office_message: null,
working_hours: [
{
day_of_week: 0,
closed_all_day: true,
open_hour: null,
open_minutes: null,
close_hour: null,
close_minutes: null,
open_all_day: false,
},
{
day_of_week: 1,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 2,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 3,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 4,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 5,
closed_all_day: false,
open_hour: 9,
open_minutes: 0,
close_hour: 17,
close_minutes: 0,
open_all_day: false,
},
{
day_of_week: 6,
closed_all_day: true,
open_hour: null,
open_minutes: null,
close_hour: null,
close_minutes: null,
open_all_day: false,
},
],
timezone: 'UTC',
callback_webhook_url: null,
allow_messages_after_resolved: true,
widget_color: null,
website_url: null,
hmac_mandatory: null,
welcome_title: null,
welcome_tagline: null,
web_widget_script: null,
website_token: null,
selected_feature_flags: null,
reply_time: null,
phone_number: null,
forward_to_email: '9ae8ebb96c7f2d6705009f5add6d1a2d@false',
email: 'fayaz@chatwoot.com',
imap_login: '',
imap_password: '',
imap_address: '',
imap_port: 0,
imap_enabled: false,
imap_enable_ssl: true,
smtp_login: '',
smtp_password: '',
smtp_address: '',
smtp_port: 0,
smtp_enabled: false,
smtp_domain: '',
smtp_enable_ssl_tls: false,
smtp_enable_starttls_auto: true,
smtp_openssl_verify_mode: 'none',
smtp_authentication: 'login',
},
];
export const labels = [
{
id: 2,
title: 'testlabel',
},
{
id: 1,
title: 'snoozes',
},
];
export const statusFilterOptions = [
{ id: 'open', name: 'Open' },
{ id: 'resolved', name: 'Resolved' },
{ id: 'pending', name: 'Pending' },
{ id: 'snoozed', name: 'Snoozed' },
{ id: 'all', name: 'All' },
];
export const languages = allLanguages;
export const countries = allCountries;
export const MESSAGE_CONDITION_VALUES = [
{
id: 'incoming',
name: 'Incoming Message',
},
{
id: 'outgoing',
name: 'Outgoing Message',
},
];
export const automationToSubmit = {
name: 'Fayaz',
description: 'Hello',
event_name: 'conversation_created',
conditions: [
{
attribute_key: 'status',
filter_operator: 'equal_to',
values: [{ id: 'open', name: 'Open' }],
query_operator: 'and',
custom_attribute_type: '',
},
],
actions: [
{ action_name: 'add_label', action_params: [{ id: 2, name: 'testlabel' }] },
],
};
export const savedAutomation = {
id: 165,
account_id: 1,
name: 'Fayaz',
description: 'Hello',
event_name: 'conversation_created',
conditions: [
{
values: ['open'],
attribute_key: 'status',
filter_operator: 'equal_to',
},
],
actions: [
{
action_name: 'add_label',
action_params: [2],
},
],
created_on: 1652776043,
active: true,
};
export const contactAttrs = [
{
key: 'contact_list',
name: 'Contact List',
inputType: 'search_select',
filterOperators: [
{
value: 'equal_to',
label: 'Equal to',
},
{
value: 'not_equal_to',
label: 'Not equal to',
},
],
},
];
export const conversationAttrs = [
{
key: 'text_attr',
name: 'Text Attr',
inputType: 'plain_text',
filterOperators: [
{
value: 'equal_to',
label: 'Equal to',
},
{
value: 'not_equal_to',
label: 'Not equal to',
},
{
value: 'is_present',
label: 'Is present',
},
{
value: 'is_not_present',
label: 'Is not present',
},
],
},
];
export const expectedOutputForCustomAttributeGenerator = [
{
key: 'conversation_custom_attribute',
name: 'Conversation Custom Attributes',
disabled: true,
},
{
key: 'text_attr',
name: 'Text Attr',
inputType: 'plain_text',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
{ value: 'is_present', label: 'Is present' },
{ value: 'is_not_present', label: 'Is not present' },
],
},
{
key: 'contact_custom_attribute',
name: 'Contact Custom Attributes',
disabled: true,
},
{
key: 'contact_list',
name: 'Contact List',
inputType: 'search_select',
filterOperators: [
{ value: 'equal_to', label: 'Equal to' },
{ value: 'not_equal_to', label: 'Not equal to' },
],
},
];
export const slaPolicies = [
{
id: 1,
account_id: 1,
name: 'Low',
first_response_time_threshold: 60,
next_response_time_threshold: 120,
resolution_time_threshold: 240,
created_at: '2022-01-26T08:06:39.470Z',
updated_at: '2022-01-26T08:06:39.470Z',
},
{
id: 2,
account_id: 1,
name: 'Medium',
first_response_time_threshold: 30,
next_response_time_threshold: 60,
resolution_time_threshold: 120,
created_at: '2022-01-26T08:06:39.470Z',
updated_at: '2022-01-26T08:06:39.470Z',
},
{
id: 3,
account_id: 1,
name: 'High',
first_response_time_threshold: 15,
next_response_time_threshold: 30,
resolution_time_threshold: 60,
created_at: '2022-01-26T08:06:39.470Z',
updated_at: '2022-01-26T08:06:39.470Z',
},
{
id: 4,
account_id: 1,
name: 'Urgent',
first_response_time_threshold: 5,
next_response_time_threshold: 10,
resolution_time_threshold: 20,
created_at: '2022-01-26T08:06:39.470Z',
updated_at: '2022-01-26T08:06:39.470Z',
},
];

View File

@@ -1,323 +0,0 @@
import * as helpers from 'dashboard/helper/automationHelper';
import {
OPERATOR_TYPES_1,
OPERATOR_TYPES_3,
OPERATOR_TYPES_4,
} from 'dashboard/routes/dashboard/settings/automation/operators';
import {
customAttributes,
labels,
automation,
contactAttrs,
conversationAttrs,
expectedOutputForCustomAttributeGenerator,
} from './automationFixtures';
import { AUTOMATIONS } from 'dashboard/routes/dashboard/settings/automation/constants';
describe('automationMethodsMixin', () => {
it('getCustomAttributeInputType returns the attribute input type', () => {
expect(helpers.getCustomAttributeInputType('date')).toEqual('date');
expect(helpers.getCustomAttributeInputType('date')).not.toEqual(
'some_random_value'
);
expect(helpers.getCustomAttributeInputType('text')).toEqual('plain_text');
expect(helpers.getCustomAttributeInputType('list')).toEqual(
'search_select'
);
expect(helpers.getCustomAttributeInputType('checkbox')).toEqual(
'search_select'
);
expect(helpers.getCustomAttributeInputType('some_random_text')).toEqual(
'plain_text'
);
});
it('isACustomAttribute returns the custom attribute value if true', () => {
expect(
helpers.isACustomAttribute(customAttributes, 'signed_up_at')
).toBeTruthy();
expect(helpers.isACustomAttribute(customAttributes, 'status')).toBeFalsy();
});
it('getCustomAttributeListDropdownValues returns the attribute dropdown values', () => {
const myListValues = [
{
id: 'item1',
name: 'item1',
},
{
id: 'item2',
name: 'item2',
},
{
id: 'item3',
name: 'item3',
},
];
expect(
helpers.getCustomAttributeListDropdownValues(customAttributes, 'my_list')
).toEqual(myListValues);
});
it('isCustomAttributeCheckbox checks if attribute is a checkbox', () => {
expect(
helpers.isCustomAttributeCheckbox(customAttributes, 'prime_user')
.attribute_display_type
).toEqual('checkbox');
expect(
helpers.isCustomAttributeCheckbox(customAttributes, 'my_check')
.attribute_display_type
).toEqual('checkbox');
expect(
helpers.isCustomAttributeCheckbox(customAttributes, 'my_list')
).not.toEqual('checkbox');
});
it('isCustomAttributeList checks if attribute is a list', () => {
expect(
helpers.isCustomAttributeList(customAttributes, 'my_list')
.attribute_display_type
).toEqual('list');
});
it('getOperatorTypes returns the correct custom attribute operators', () => {
expect(helpers.getOperatorTypes('list')).toEqual(OPERATOR_TYPES_1);
expect(helpers.getOperatorTypes('text')).toEqual(OPERATOR_TYPES_3);
expect(helpers.getOperatorTypes('number')).toEqual(OPERATOR_TYPES_1);
expect(helpers.getOperatorTypes('link')).toEqual(OPERATOR_TYPES_1);
expect(helpers.getOperatorTypes('date')).toEqual(OPERATOR_TYPES_4);
expect(helpers.getOperatorTypes('checkbox')).toEqual(OPERATOR_TYPES_1);
expect(helpers.getOperatorTypes('some_random')).toEqual(OPERATOR_TYPES_1);
});
it('generateConditionOptions returns expected conditions options array', () => {
const testConditions = [
{
id: 123,
title: 'Fayaz',
email: 'test@test.com',
},
{
title: 'John',
id: 324,
email: 'test@john.com',
},
];
const expectedConditions = [
{
id: 123,
name: 'Fayaz',
},
{
id: 324,
name: 'John',
},
];
expect(helpers.generateConditionOptions(testConditions)).toEqual(
expectedConditions
);
});
it('getActionOptions returns expected actions options array', () => {
const expectedOptions = [
{
id: 'testlabel',
name: 'testlabel',
},
{
id: 'snoozes',
name: 'snoozes',
},
];
expect(helpers.getActionOptions({ labels, type: 'add_label' })).toEqual(
expectedOptions
);
});
it('getConditionOptions returns expected conditions options', () => {
const testOptions = [
{
id: 'open',
name: 'Open',
},
{
id: 'resolved',
name: 'Resolved',
},
{
id: 'pending',
name: 'Pending',
},
{
id: 'snoozed',
name: 'Snoozed',
},
{
id: 'all',
name: 'All',
},
];
const expectedOptions = [
{
id: 'open',
name: 'Open',
},
{
id: 'resolved',
name: 'Resolved',
},
{
id: 'pending',
name: 'Pending',
},
{
id: 'snoozed',
name: 'Snoozed',
},
{
id: 'all',
name: 'All',
},
];
expect(
helpers.getConditionOptions({
customAttributes,
campaigns: [],
statusFilterOptions: testOptions,
type: 'status',
})
).toEqual(expectedOptions);
});
it('getFileName returns the correct file name', () => {
expect(
helpers.getFileName(automation.actions[0], automation.files)
).toEqual('pfp.jpeg');
});
it('getDefaultConditions returns the resp default condition model', () => {
const messageCreatedModel = [
{
attribute_key: 'message_type',
filter_operator: 'equal_to',
values: '',
query_operator: 'and',
custom_attribute_type: '',
},
];
const genericConditionModel = [
{
attribute_key: 'status',
filter_operator: 'equal_to',
values: '',
query_operator: 'and',
custom_attribute_type: '',
},
];
expect(helpers.getDefaultConditions('message_created')).toEqual(
messageCreatedModel
);
expect(helpers.getDefaultConditions()).toEqual(genericConditionModel);
});
it('getDefaultActions returns the resp default action model', () => {
const genericActionModel = [
{
action_name: 'assign_agent',
action_params: [],
},
];
expect(helpers.getDefaultActions()).toEqual(genericActionModel);
});
it('filterCustomAttributes filters the raw custom attributes', () => {
const filteredAttributes = [
{ key: 'signed_up_at', name: 'Signed Up At', type: 'date' },
{ key: 'prime_user', name: 'Prime User', type: 'checkbox' },
{ key: 'test', name: 'Test', type: 'text' },
{ key: 'link', name: 'Link', type: 'link' },
{ key: 'my_list', name: 'My List', type: 'list' },
{ key: 'my_check', name: 'My Check', type: 'checkbox' },
{ key: 'conlist', name: 'ConList', type: 'list' },
{ key: 'asdf', name: 'asdf', type: 'link' },
];
expect(helpers.filterCustomAttributes(customAttributes)).toEqual(
filteredAttributes
);
});
it('getStandardAttributeInputType returns the resp default action model', () => {
expect(
helpers.getStandardAttributeInputType(
AUTOMATIONS,
'message_created',
'message_type'
)
).toEqual('search_select');
expect(
helpers.getStandardAttributeInputType(
AUTOMATIONS,
'conversation_created',
'status'
)
).toEqual('multi_select');
expect(
helpers.getStandardAttributeInputType(
AUTOMATIONS,
'conversation_updated',
'referer'
)
).toEqual('plain_text');
});
it('generateAutomationPayload returns the resp default action model', () => {
const testPayload = {
name: 'Test',
description: 'This is a test',
event_name: 'conversation_created',
conditions: [
{
attribute_key: 'status',
filter_operator: 'equal_to',
values: [{ id: 'open', name: 'Open' }],
query_operator: 'and',
},
],
actions: [
{
action_name: 'add_label',
action_params: [{ id: 2, name: 'testlabel' }],
},
],
};
const expectedPayload = {
name: 'Test',
description: 'This is a test',
event_name: 'conversation_created',
conditions: [
{
attribute_key: 'status',
filter_operator: 'equal_to',
values: ['open'],
},
],
actions: [
{
action_name: 'add_label',
action_params: [2],
},
],
};
expect(helpers.generateAutomationPayload(testPayload)).toEqual(
expectedPayload
);
});
it('isCustomAttribute returns the resp default action model', () => {
const attrs = helpers.filterCustomAttributes(customAttributes);
expect(helpers.isCustomAttribute(attrs, 'my_list')).toBeTruthy();
expect(helpers.isCustomAttribute(attrs, 'my_check')).toBeTruthy();
expect(helpers.isCustomAttribute(attrs, 'signed_up_at')).toBeTruthy();
expect(helpers.isCustomAttribute(attrs, 'link')).toBeTruthy();
expect(helpers.isCustomAttribute(attrs, 'prime_user')).toBeTruthy();
expect(helpers.isCustomAttribute(attrs, 'hello')).toBeFalsy();
});
it('generateCustomAttributes generates and returns correct condition attribute', () => {
expect(
helpers.generateCustomAttributes(
conversationAttrs,
contactAttrs,
'Conversation Custom Attributes',
'Contact Custom Attributes'
)
).toEqual(expectedOutputForCustomAttributeGenerator);
});
});

View File

@@ -1,450 +0,0 @@
import methodsMixin from '../../../dashboard/mixins/automations/methodsMixin';
import {
automation,
customAttributes,
agents,
booleanFilterOptions,
teams,
labels,
statusFilterOptions,
campaigns,
contacts,
inboxes,
languages,
countries,
slaPolicies,
MESSAGE_CONDITION_VALUES,
automationToSubmit,
savedAutomation,
} from './automationFixtures';
import {
AUTOMATIONS,
AUTOMATION_ACTION_TYPES,
} from '../../../dashboard/routes/dashboard/settings/automation/constants.js';
import { createWrapper, createLocalVue } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
const localVue = createLocalVue();
localVue.use(Vuex);
// Vuelidate required to test submit method
const createComponent = (
mixins,
data,
// eslint-disable-next-line default-param-last
computed = {},
// eslint-disable-next-line default-param-last
methods = {},
validations
) => {
const Component = {
render() {},
mixins,
data,
computed,
methods,
validations,
};
const Constructor = Vue.extend(Component);
const vm = new Constructor().$mount();
return createWrapper(vm);
};
const generateComputedProperties = () => {
return {
statusFilterOptions() {
return statusFilterOptions;
},
agents() {
return agents;
},
customAttributes() {
return customAttributes;
},
labels() {
return labels;
},
teams() {
return teams;
},
booleanFilterOptions() {
return booleanFilterOptions;
},
campaigns() {
return campaigns;
},
contacts() {
return contacts;
},
inboxes() {
return inboxes;
},
languages() {
return languages;
},
countries() {
return countries;
},
slaPolicies() {
return slaPolicies;
},
MESSAGE_CONDITION_VALUES() {
return MESSAGE_CONDITION_VALUES;
},
};
};
describe('automationMethodsMixin', () => {
it('getFileName returns the correct file name', () => {
const data = () => {
return {};
};
const wrapper = createComponent([methodsMixin], data);
expect(
wrapper.vm.getFileName(automation.actions[0], automation.files)
).toEqual(automation.files[0].filename);
});
it('getAttributes returns all attributes', () => {
const data = () => {
return {
automationTypes: AUTOMATIONS,
};
};
const wrapper = createComponent([methodsMixin], data);
expect(wrapper.vm.getAttributes('conversation_created')).toEqual(
AUTOMATIONS.conversation_created.conditions
);
});
it('getAttributes returns all respective attributes', () => {
const data = () => {
return {
allCustomAttributes: customAttributes,
automationTypes: AUTOMATIONS,
automation,
};
};
const wrapper = createComponent([methodsMixin], data);
expect(wrapper.vm.getInputType('status')).toEqual('multi_select');
expect(wrapper.vm.getInputType('my_list')).toEqual('search_select');
});
it('getOperators returns all respective operators', () => {
const data = () => {
return {
allCustomAttributes: customAttributes,
automationTypes: AUTOMATIONS,
automation,
};
};
const wrapper = createComponent([methodsMixin], data);
expect(wrapper.vm.getOperators('status')).toEqual(
AUTOMATIONS.conversation_created.conditions[0].filterOperators
);
});
it('getAutomationType returns the correct automationType', () => {
const data = () => {
return {
automationTypes: AUTOMATIONS,
automation,
};
};
const wrapper = createComponent([methodsMixin], data);
expect(wrapper.vm.getAutomationType('status')).toEqual(
AUTOMATIONS[automation.event_name].conditions[0]
);
});
it('getConditionDropdownValues returns respective condition dropdown values', () => {
const computed = generateComputedProperties();
const data = () => {
return {
allCustomAttributes: customAttributes,
};
};
const wrapper = createComponent([methodsMixin], data, computed);
expect(wrapper.vm.getConditionDropdownValues('status')).toEqual(
statusFilterOptions
);
expect(wrapper.vm.getConditionDropdownValues('team_id')).toEqual(teams);
expect(wrapper.vm.getConditionDropdownValues('assignee_id')).toEqual(
agents
);
expect(wrapper.vm.getConditionDropdownValues('contact')).toEqual(contacts);
expect(wrapper.vm.getConditionDropdownValues('inbox_id')).toEqual(inboxes);
expect(wrapper.vm.getConditionDropdownValues('campaigns')).toEqual(
campaigns
);
expect(wrapper.vm.getConditionDropdownValues('browser_language')).toEqual(
languages
);
expect(wrapper.vm.getConditionDropdownValues('country_code')).toEqual(
countries
);
expect(wrapper.vm.getConditionDropdownValues('message_type')).toEqual(
MESSAGE_CONDITION_VALUES
);
});
it('appendNewCondition appends a new condition to the automation data property', () => {
const condition = {
attribute_key: 'status',
filter_operator: 'equal_to',
values: '',
query_operator: 'and',
custom_attribute_type: '',
};
const data = () => {
return {
automation,
};
};
const wrapper = createComponent([methodsMixin], data);
wrapper.vm.appendNewCondition();
expect(automation.conditions[automation.conditions.length - 1]).toEqual(
condition
);
});
it('appendNewAction appends a new condition to the automation data property', () => {
const action = {
action_name: 'assign_agent',
action_params: [],
};
const data = () => {
return {
automation,
};
};
const wrapper = createComponent([methodsMixin], data);
wrapper.vm.appendNewAction();
expect(automation.actions[automation.actions.length - 1]).toEqual(action);
});
it('removeFilter removes the given condition in the automation', () => {
const data = () => {
return {
automation,
};
};
const wrapper = createComponent([methodsMixin], data);
wrapper.vm.removeFilter(0);
expect(automation.conditions.length).toEqual(1);
});
it('removeAction removes the given action in the automation', () => {
const data = () => {
return {
automation,
};
};
const wrapper = createComponent([methodsMixin], data);
wrapper.vm.removeAction(0);
expect(automation.actions.length).toEqual(1);
});
it('resetFilter resets the current automation conditions', () => {
const data = () => {
return {
automation: automationToSubmit,
automationTypes: AUTOMATIONS,
};
};
const conditionAfterReset = {
attribute_key: 'status',
filter_operator: 'equal_to',
values: '',
query_operator: 'and',
custom_attribute_type: '',
};
const wrapper = createComponent([methodsMixin], data);
wrapper.vm.resetFilter(0, automationToSubmit.conditions[0]);
expect(automation.conditions[0]).toEqual(conditionAfterReset);
});
it('showUserInput returns boolean value based on the operator type', () => {
const data = () => {
return {};
};
const wrapper = createComponent([methodsMixin], data);
expect(wrapper.vm.showUserInput('is_present')).toBeFalsy();
expect(wrapper.vm.showUserInput('is_not_present')).toBeFalsy();
expect(wrapper.vm.showUserInput('equal_to')).toBeTruthy();
expect(wrapper.vm.showUserInput('not_equal_to')).toBeTruthy();
});
it('showActionInput returns boolean value based on the action type', () => {
const data = () => {
return {
automationActionTypes: AUTOMATION_ACTION_TYPES,
};
};
const wrapper = createComponent([methodsMixin], data);
expect(wrapper.vm.showActionInput('send_email_to_team')).toBeFalsy();
expect(wrapper.vm.showActionInput('send_message')).toBeFalsy();
expect(wrapper.vm.showActionInput('send_webhook_event')).toBeTruthy();
expect(wrapper.vm.showActionInput('resolve_conversation')).toBeFalsy();
expect(wrapper.vm.showActionInput('add_label')).toBeTruthy();
});
it('resetAction resets the action to default state', () => {
const data = () => {
return {
automation,
};
};
const wrapper = createComponent([methodsMixin], data);
wrapper.vm.resetAction(0);
expect(automation.actions[0].action_params).toEqual([]);
});
it('manifestConditions resets the action to default state', () => {
const data = () => {
return {
automation: {},
allCustomAttributes: customAttributes,
automationTypes: AUTOMATIONS,
};
};
const methods = {
getConditionDropdownValues() {
return statusFilterOptions;
},
};
const manifestedConditions = [
{
values: [
{
id: 'open',
name: 'Open',
},
],
attribute_key: 'status',
filter_operator: 'equal_to',
query_operator: 'and',
},
];
const wrapper = createComponent([methodsMixin], data, {}, methods);
expect(wrapper.vm.manifestConditions(savedAutomation)).toEqual(
manifestedConditions
);
});
it('generateActionsArray return the manifested actions array', () => {
const data = () => {
return {
automationActionTypes: AUTOMATION_ACTION_TYPES,
};
};
const computed = {
agents() {
return agents;
},
labels() {
return labels;
},
teams() {
return teams;
},
};
const methods = {
getActionDropdownValues() {
return [
{
id: 2,
name: 'testlabel',
},
{
id: 1,
name: 'snoozes',
},
];
},
};
const testAction = {
action_name: 'add_label',
action_params: [2],
};
const expectedActionArray = [
{
id: 2,
name: 'testlabel',
},
];
const wrapper = createComponent([methodsMixin], data, computed, methods);
expect(wrapper.vm.generateActionsArray(testAction)).toEqual(
expectedActionArray
);
});
it('manifestActions manifest the received action and generate the correct array', () => {
const data = () => {
return {
automation: {},
allCustomAttributes: customAttributes,
automationTypes: AUTOMATIONS,
};
};
const methods = {
generateActionsArray() {
return [
{
id: 2,
name: 'testlabel',
},
];
},
};
const expectedActions = [
{
action_name: 'add_label',
action_params: [
{
id: 2,
name: 'testlabel',
},
],
},
];
const wrapper = createComponent([methodsMixin], data, {}, methods);
expect(wrapper.vm.manifestActions(savedAutomation)).toEqual(
expectedActions
);
});
it('getActionDropdownValues returns Action dropdown Values', () => {
const data = () => {
return {};
};
const computed = {
agents() {
return agents;
},
labels() {
return labels;
},
teams() {
return teams;
},
slaPolicies() {
return slaPolicies;
},
};
const expectedActionDropdownValues = [
{ id: 'testlabel', name: 'testlabel' },
{ id: 'snoozes', name: 'snoozes' },
];
const wrapper = createComponent([methodsMixin], data, computed);
expect(wrapper.vm.getActionDropdownValues('add_label')).toEqual(
expectedActionDropdownValues
);
});
});