import ModularUIResponse from "../../../modularui/ModularUIResponse";

import ApplicationModel from "../ApplicationModel";
import UserServicesModel from "../../user/UserServicesModel";

import mockWebapp from "./webapp.json";
import mockWebappContributions from "./webappContributions.json";
import { NotAllowedUriException } from "../../../exceptions";
import Href from "../../href/Href";

describe("application", () => {
  it("should be able to create an empty Application object", () => {
    const application = new ApplicationModel({});

    expect(application.type).toBe("Application");
    expect(application.label).toBe("");
    expect(application.tabs.size).toBe(0);
  });

  it("application model is applicable for contributions with resourcetype Application", () => {
    const data = ModularUIResponse.create({
      key: "Webapp",
      data: mockWebapp,
      contributions: mockWebappContributions,
    });
    expect(ApplicationModel.isApplicableModel(data)).toBeTruthy();
  });

  it("should create an application object with links an a label on a typical modular ui response", () => {
    const data = ModularUIResponse.create({
      key: "Webapp",
      data: mockWebapp,
      contributions: mockWebappContributions,
    });

    const application = new ApplicationModel(data);

    expect(application).toBeInstanceOf(ApplicationModel);
    expect(application.label).toBe("Webapp");
    expect(application.tabs.size).toBe(3);
    expect(application.getInitialChildModelLinks()).toHaveLength(1);
    expect(application.modelcatalog.label).toBe("Model catalog");

    application.userServices = null;
    expect(application.userServices).toBeNull();

    const userServicesModel = new UserServicesModel({});
    application.userServices = userServicesModel;
    expect(application.userServices).toStrictEqual(userServicesModel);
  });

  it("sets user service models as child models", () => {
    const application = new ApplicationModel({});

    application.addChildModels([]);
    expect(application.userServices).toBeNull();

    const userServicesModel = new UserServicesModel({
      data: {
        _links: {
          component: [
            {
              name: "Userdata",
              href: "/Login/userdata",
            },
          ],
        },
      },
    });
    application.addChildModels([userServicesModel]);
    expect(application.userServices).toStrictEqual(userServicesModel);

    expect(application.userHref).toStrictEqual(new Href("/Login/userdata"));
  });

  it("throws when userservices are called login and getInitialChildModelLinks is called", () => {
    const data = ModularUIResponse.create({
      key: "Webapp",
      data: {
        webapplication: {
          ...mockWebapp.webapplication,
          _links: {
            ...mockWebapp.webapplication._links,
            user_services: [{ href: "/login", name: "UserServices" }],
          },
        },
      },
      contributions: mockWebappContributions,
    });

    const application = new ApplicationModel(data);

    expect(() => {
      application.getInitialChildModelLinks();
    }).toThrow(NotAllowedUriException);

    expect(application.userHref).toBeNull();
  });
});
