Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

4.3 KB
Raw Blame
import { useEditableAutomation } from '../useEditableAutomation';
import useAutomationValues from '../useAutomationValues';
import { generateAutomationPayload } from 'dashboard/helper/automationHelper';
import { AUTOMATIONS } from 'dashboard/routes/dashboard/settings/automation/constants';

vi.mock('../useAutomationValues');

describe('useEditableAutomation', () => {
  beforeEach(() => {
    useAutomationValues.mockReturnValue({
      getConditionDropdownValues: vi.fn(attributeKey => {
        if (attributeKey === 'private_note') {
          return [
            { id: true, name: 'True' },
            { id: false, name: 'False' },
          ];
        }

        return [];
      }),
      getActionDropdownValues: vi.fn(actionName => {
        if (actionName === 'assign_agent') {
          return [
            { id: 'nil', name: 'None' },
            {
              id: 'last_responding_agent',
              name: 'Last Responding Agent',
            },
            { id: 1, name: 'Agent 1' },
          ];
        }

        return [];
      }),
    });
  });

  it('rehydrates boolean conditions as a single selected option', () => {
    const automation = {
      event_name: 'message_created',
      conditions: [
        {
          attribute_key: 'private_note',
          filter_operator: 'equal_to',
          values: [false],
          query_operator: null,
        },
      ],
      actions: [],
    };
    const automationTypes = {
      message_created: {
        conditions: [{ key: 'private_note', inputType: 'search_select' }],
      },
    };

    const { formatAutomation } = useEditableAutomation();
    const result = formatAutomation(automation, [], automationTypes, []);

    expect(result.conditions).toEqual([
      {
        attribute_key: 'private_note',
        filter_operator: 'equal_to',
        values: { id: false, name: 'False' },
        query_operator: 'and',
      },
    ]);
  });

  it('rehydrates a long text condition as its text', () => {
    const automation = {
      event_name: 'message_created',
      conditions: [
        {
          attribute_key: 'captain_condition',
          filter_operator: 'detects',
          values: ['the customer wants a refund'],
          query_operator: null,
        },
      ],
      actions: [],
    };
    const automationTypes = {
      message_created: {
        conditions: [{ key: 'captain_condition', inputType: 'long_text' }],
      },
    };

    const { formatAutomation } = useEditableAutomation();
    const result = formatAutomation(automation, [], automationTypes, []);

    expect(result.conditions[0].values).toBe('the customer wants a refund');
  });

  it('rehydrates last responding agent as a selected action option', () => {
    const automation = {
      event_name: 'conversation_created',
      conditions: [],
      actions: [
        {
          action_name: 'assign_agent',
          action_params: ['last_responding_agent'],
        },
      ],
    };
    const automationActionTypes = [
      { key: 'assign_agent', inputType: 'search_select' },
    ];

    const { formatAutomation } = useEditableAutomation();
    const result = formatAutomation(automation, [], {}, automationActionTypes);

    expect(result.actions).toEqual([
      {
        action_name: 'assign_agent',
        action_params: [
          {
            id: 'last_responding_agent',
            name: 'Last Responding Agent',
          },
        ],
      },
    ]);
  });

  it.each([
    {
      description: 'multiple independent content values',
      values: ['hello', 'refund'],
    },
    {
      description: 'a single content value containing a comma',
      values: ['hello, world'],
    },
  ])('round trips $description without changing its values', ({ values }) => {
    const automation = {
      name: 'Content automation',
      description: 'Matches message content',
      event_name: 'message_created',
      conditions: [
        {
          attribute_key: 'content',
          filter_operator: 'contains',
          values,
          query_operator: null,
        },
      ],
      actions: [],
    };

    const { formatAutomation } = useEditableAutomation();
    const editableAutomation = formatAutomation(
      automation,
      [],
      AUTOMATIONS,
      []
    );
    const payload = generateAutomationPayload(editableAutomation);

    expect(payload.conditions[0].values).toEqual(values);
  });
});