jest.mock("../../services/certificatesAPI");

import {
  act,
  cleanup,
  fireEvent,
  render,
  screen,
  waitFor,
} from "@testing-library/react";
import { useState } from "react";

import {
  CertificateProvider,
  useCertificateContext,
} from "./CertificateContext";

describe("CertificateContext", () => {
  beforeEach(() => {
    const TestComp = () => {
      const { createCert } = useCertificateContext();
      const [result, setResult] = useState(null);
      const testCreate = async (data) => {
        const x = await createCert(data);
        setResult(JSON.stringify(x));
      };

      return (
        <>
          <div data-testid="output">{result}</div>
          <button
            data-testid="createCertTBOX"
            onClick={async () => {
              await testCreate({
                common_name: "11111111111111111",
                type: "TBOX",
              });
            }}
          />
          <button
            data-testid="createCertICC"
            onClick={async () => {
              await testCreate({
                common_name: "11111111111111111",
                type: "ICC",
              });
            }}
          />
          <button
            data-testid="createCertAftersales"
            onClick={async () => {
              await testCreate({
                common_name: "11111111111111112",
                type: "Aftersales",
              });
            }}
          />
        </>
      );
    };

    render(
      <CertificateProvider>
        <TestComp />
      </CertificateProvider>
    );
  });

  afterEach(() => {
    cleanup();
  });

  it("TBOX certificate", async () => {
    await act(async () => {
      fireEvent.click(screen.getByTestId("createCertTBOX"));
      await waitFor(() =>
        expect(screen.getByTestId("output").innerHTML).toBe(
          '{"private_key":"-----BEGIN RSA PRIVATE KEY-----DATA-----END RSA PRIVATE KEY-----","public_key":"-----BEGIN CERTIFICATE-----DATA-----END CERTIFICATE-----","serial_number":"00:76:d2:ca:86:35:8c:88:52:fd:94:8e:55:d3:b6:a8:2e:73:68:00","type":"TBOX"}'
        )
      );
    });
  });

  it("ICC certificate", async () => {
    await act(async () => {
      fireEvent.click(screen.getByTestId("createCertICC"));
      await waitFor(() =>
        expect(screen.getByTestId("output").innerHTML).toBe(
          '{"private_key":"-----BEGIN RSA PRIVATE KEY-----DATA-----END RSA PRIVATE KEY-----","public_key":"-----BEGIN CERTIFICATE-----DATA-----END CERTIFICATE-----","serial_number":"00:76:d2:ca:86:35:8c:88:52:fd:94:8e:55:d3:b6:a8:2e:73:68:00","type":"ICC"}'
        )
      );
    });
  });

  it("Aftersales certificate", async () => {
    await act(async () => {
      fireEvent.click(screen.getByTestId("createCertAftersales"));
      await waitFor(() =>
        expect(screen.getByTestId("output").innerHTML).toBe(
          '{"private_key":"-----BEGIN RSA PRIVATE KEY-----DATA-----END RSA PRIVATE KEY-----","public_key":"-----BEGIN CERTIFICATE-----DATA-----END CERTIFICATE-----","serial_number":"00:76:d2:ca:86:35:8c:88:52:fd:94:8e:55:d3:b6:a8:2e:73:68:00","type":"Aftersales"}'
        )
      );
    });
  });
});
