import React from "react";
import { render, waitFor } from "@testing-library/react";

import CarUpdateStatusProgress from "../CarUpdateStatusProgress";
import useStyles from "../../useStyles";
import s from "./Statuses";
import addSnapshotSerializer from "../../../utils/snapshot";

const TestWrapper = ({ status }) => {
  const classes = useStyles();
  return <CarUpdateStatusProgress status={status} classes={classes} />;
};

const renderCarUpdateStatusProgress = async (status) => {
  const { container } = render(<TestWrapper status={status} />);
  await waitFor(() => { });
  return container;
};

describe("CarUpdateStatusProgress", () => {
  beforeAll(() => {
    addSnapshotSerializer(expect);
  });

  const tests = [
    {
      name: "manifest_received",
      status: {
        car_update_id: 297,
        msg: s.ManifestReceived,
        err: -6,
        extra_info: "",
      },
    },
    {
      name: "manifest_accepted",
      status: {
        car_update_id: 297,
        msg: s.ManifestAccepted,
        err: -7,
        extra_info: "",
      },
    },
    {
      name: "download_started",
      status: {
        car_update_id: 297,
        msg: s.DownloadStarted,
        err: -14,
        extra_info: "",
      },
    },
    {
      name: "download_start ECU",
      status: {
        car_update_id: 297,
        ecu: "ADAS",
        file_current: 0,
        file_total: 1264672,
        package_current: 0,
        package_total: 2529856,
        msg: s.DownloadStarted,
        err: 0,
      },
    },
    {
      name: "downloading",
      status: {
        car_update_id: 297,
        ecu: "ADAS",
        file_current: 1048576,
        file_total: 1264672,
        package_current: 1048576,
        package_total: 2529856,
        msg: s.Downloading,
        err: 0,
      },
    },
    {
      name: "download_complete",
      status: {
        car_update_id: 297,
        ecu: "ADAS",
        file_current: 1264672,
        file_total: 1264672,
        package_current: 1264672,
        package_total: 2529856,
        msg: s.DownloadCompleted,
        err: 0,
      },
    },
    {
      name: "package_download_complete",
      status: {
        car_update_id: 297,
        msg: s.PackageDownloadCompleted,
        err: -15,
        extra_info: "",
      },
    },
    {
      name: "download_error",
      status: {
        car_update_id: 1,
        ecu: "TEST",
        file_current: 0,
        file_total: 100,
        package_current: 0,
        package_total: 1000,
        msg: s.DownloadFailed,
        err: 0,
      },
    },
    {
      name: "installing",
      status: {
        car_update_id: 1,
        ecu: "TEST",
        installed: 5,
        total_files: 10,
        msg: s.Installing,
        err: 0,
      },
    },
    {
      name: "install_complete ECU",
      status: {
        car_update_id: 1,
        ecu: "TEST",
        installed: 10,
        total_files: 10,
        msg: s.InstallSucceeded,
        err: 0,
      },
    },
    {
      name: "install_error",
      status: {
        car_update_id: 1,
        ecu: "TEST",
        installed: 5,
        total_files: 10,
        msg: s.InstallFailed,
        err: 0,
      },
    },
    {
      name: "install_scheduled",
      status: {
        car_update_id: 1,
        ecu: "TEST",
        installed: 5,
        total_files: 10,
        msg: s.InstallSucceeded,
        err: 0,
      },
    },
    {
      name: "requirements_failed",
      status: {
        car_update_id: 1,
        ecu: "TEST",
        installed: 5,
        total_files: 10,
        msg: s.InstallFailed,
        err: 0,
      },
    },
  ];
  for (let i = 0, len = tests.length; i < len; i++) {
    const test = tests[i];

    it(`Render ${test.name}`, async () => {
      const container = await renderCarUpdateStatusProgress(test.status);
      expect(container).toMatchSnapshot();
    });
  }
});
