import React from 'react';
import { shallow } from 'enzyme';
import { getObjFromQueryParams, buildTemplateNameDescription } from '../v2common';

describe('Test v2common container', () => {
  it('test getObjFromQueryParams', () => {
    expect(getObjFromQueryParams("?channel=WHATSAPP&mode=CREATE")).toEqual({
        channel: 'WHATSAPP',
        mode: 'CREATE',
    });
  });
});

describe('buildTemplateNameDescription', () => {
  it('returns undefined when templateName is empty string', () => {
    expect(buildTemplateNameDescription('Template name', '')).toBeUndefined();
  });

  it('returns undefined when templateName is null', () => {
    expect(buildTemplateNameDescription('Template name', null)).toBeUndefined();
  });

  it('returns undefined when templateName is undefined', () => {
    expect(buildTemplateNameDescription('Template name', undefined)).toBeUndefined();
  });

  it('renders a span wrapper when templateName is provided', () => {
    const result = buildTemplateNameDescription('Template name', 'Welcome 01');
    const wrapper = shallow(result);
    expect(wrapper.type()).toBe('span');
  });

  it('renders label text in the first CapLabelInline with correct class', () => {
    const result = buildTemplateNameDescription('Template name', 'Welcome 01');
    const wrapper = shallow(result);
    const firstLabel = wrapper.childAt(0);
    expect(firstLabel.prop('className')).toBe('notification-template-label');
    expect(firstLabel.prop('children')).toBe('Template name');
  });

  it('renders template name in the second CapLabelInline with correct class', () => {
    const result = buildTemplateNameDescription('Template name', 'Welcome 01');
    const wrapper = shallow(result);
    const secondLabel = wrapper.childAt(1);
    expect(secondLabel.prop('className')).toBe('notification-template-name');
    expect(secondLabel.prop('children')).toBe('Welcome 01');
  });

  it('both CapLabelInline elements have type="label1"', () => {
    const result = buildTemplateNameDescription('Template name', 'Welcome 01');
    const wrapper = shallow(result);
    wrapper.children().forEach((node) => {
      expect(node.prop('type')).toBe('label1');
    });
  });
});
