import * as React from "react";
import { render, screen, fireEvent, cleanup } from "@testing-library/react";
import { FormItemName } from "..";

const testId = "FormBuilder-Attributes-FormItemName";
const props = {};

describe("Rendering", () => {
  afterEach(cleanup);

  it(`Should render in the dom`, () => {
    render(<FormItemName {...props} />);
    expect(screen.queryByTestId(testId)).toBeTruthy();
  });
});

describe("Events", () => {
  afterEach(cleanup);

  it(`Should trigger onChange when the "name" field updates`, () => {
    const onChange = jest.fn();
    render(<FormItemName {...props} onChange={onChange} />);
    fireEvent.change(screen.getByTestId(testId).querySelector("input"), {
      target: { value: "Test" },
    });
    expect(onChange).toBeCalled();
  });

  it(`Should trigger onBlur when the "name" field loses focus`, () => {
    const onBlur = jest.fn();
    render(<FormItemName {...props} onBlur={onBlur} />);
    fireEvent.blur(screen.getByTestId(testId).querySelector("input"));
    expect(onBlur).toBeCalled();
  });
});
