import { mount } from 'enzyme';

import Highlight from './highlight';

describe('Typeahead input', () => {
  const highlighted = (node) => node.find('strong');
  const getTextFromNodes = (nodes) => nodes.reduce((value, node) => value + node.text(), '');

  it('highlights part of label that matches the query', () => {
    const query = 'test';
    const label = `this is a ${query} label`;
    const result = mount(<Highlight value={label} query={query} />);

    expect(highlighted(result).text()).toStrictEqual(query);

    expect(getTextFromNodes(result)).toBe(label);
  });

  it('does not change text if query is not present in it', () => {
    const query = 'test';
    const label = `this is a label`;
    const result = mount(<Highlight value={label} query={query} />);

    expect(result.text()).toBe(label);
  });

  it('highlights whole label that matches the query', () => {
    const query = 'test';
    const label = query;
    const result = mount(<Highlight value={label} query={query} />);

    expect(highlighted(result).text()).toStrictEqual(query);
  });
});
