import React from "react";
import { shallow } from "enzyme";
import { Link } from "react-router";
import { createMockUser } from "../../utilities/tests/test-utils";
import NavBar from "./NavBar";

const notLoggedUser = createMockUser();
const authenticatedUser = createMockUser("user@test.com", true, true, "ADMIN");
const authenticatedViewer = createMockUser("user@test.com", true, true, "VIEWER");

const defaultProps = {
    config: {
        enableDatasetImport: false,
    },
    user: notLoggedUser,
    rootPath: "/florence",
    location: {},
};

const withPreviewNavProps = {
    ...defaultProps,
    location: {
        ...defaultProps.location,
        pathname: "/florence/collections/foo-1234/preview",
    },
    workingOn: {
        id: "foo-1234",
        name: "foo",
    },
};

const NavbarItems = ["Collections", "Users and access", "Teams", "Interactives", "Teams", "Security", "Sign out"];

describe("NavBar", () => {
    describe("when user is not authenticated", () => {
        it("should render only one link to Sign in", () => {
            const component = shallow(<NavBar {...defaultProps} />);

            expect(component.hasClass("global-nav__list")).toBe(true);
            expect(component.find(Link)).toHaveLength(1);
            expect(component.find("Link[to='/florence/login']").exists()).toBe(true);
        });
    });

    describe("when user is authenticated as Admin", () => {
        it("should render navigation with links", () => {
            const component = shallow(<NavBar {...defaultProps} user={authenticatedUser} />);
            const nav = component.find(Link);

            expect(component.hasClass("global-nav__list")).toBe(true);
            expect(component.find(Link)).toHaveLength(NavbarItems.length);
            nav.forEach((n, i) => expect(n.getElement().props.children).toBe(NavbarItems[i]));
        });

        it("should not render Sign in link", () => {
            const component = shallow(<NavBar {...defaultProps} user={authenticatedUser} />);
            expect(component.hasClass("sign-in")).toBe(false);
        });

        it("should not display Datasets", () => {
            const component = shallow(<NavBar {...defaultProps} user={authenticatedUser} />);
            expect(component.find("Link[to='/florence/uploads/data']").exists()).toBe(false);
        });

        describe("when enableNewSignIn feature flag is enabled", () => {
            const props = {
                ...defaultProps,
                config: {
                    ...defaultProps.config,
                    enableNewSignIn: true,
                },
            };
            const component = shallow(<NavBar {...props} user={authenticatedUser} />);
            it("Preview teams option should be present", () => {
                const link = component.find("Link[to='/florence/groups']");
                expect(link.getElement().props.children[0].includes("Preview teams"));
            });
        });

        describe("when enabled dataset import", () => {
            it("should display Datasets", () => {
                const props = {
                    ...defaultProps,
                    user: authenticatedUser,
                    config: {
                        ...defaultProps.config,
                        enableDatasetImport: true,
                    },
                };
                const component = shallow(<NavBar {...props} />);
                expect(component.find("Link[to='/florence/uploads/data']").exists()).toBe(true);
            });
        });

        describe("when enabled dataset import", () => {
            it("should display Datasets", () => {
                const props = {
                    ...defaultProps,
                    user: authenticatedUser,
                    config: {
                        ...defaultProps.config,
                        enableDatasetImport: true,
                    },
                };
                const component = shallow(<NavBar {...props} />);
                expect(component.find("Link[to='/florence/uploads/data']").exists()).toBe(true);
            });
        });
        describe("when on collections", () => {
            it("should display Working On: ", () => {
                const props = {
                    ...defaultProps,
                    user: authenticatedUser,
                    location: {
                        pathname: "/florence/collections/foo-1234",
                    },
                    workingOn: {
                        id: "foo-1234",
                        name: "foo",
                    },
                };
                const wrapper = shallow(<NavBar {...props} />);
                const link = wrapper.find("Link[to='/florence/collections/foo-1234']");

                link.getElement().props.children[0].includes("Working on:");
                link.getElement().props.children[0].includes("foo");
            });
        });
    });

    describe("when user is authenticated as Viewer", () => {
        it("should render navigation with links", () => {
            const NavbarItems = ["Collections", "Sign out"];
            const component = shallow(<NavBar {...defaultProps} user={authenticatedViewer} />);
            const nav = component.find(Link);

            expect(component.hasClass("global-nav__list")).toBe(true);
            expect(component.find(Link)).toHaveLength(NavbarItems.length);
            nav.forEach((n, i) => expect(n.getElement().props.children).toBe(NavbarItems[i]));
        });
        describe("when on collections url", () => {
            it("should render PreviewNav component", () => {
                const component = shallow(<NavBar {...withPreviewNavProps} user={authenticatedViewer} />);
                expect(component.find("Connect(PreviewNav)")).toHaveLength(1);
            });
        });
    });
});

@@ -826,16 +826,32 @@
 ions%22, %22
+Interactives%22, %22
 Users an
@@ -871,45 +871,8 @@
 eams
-%22, %22Interactives%22, %22Teams%22, %22Security
 %22, %22
@@ -2474,123 +2474,25 @@
     
-config: %7B%0A                    ...defaultProps.config,%0A                    enableNewSignIn: true,%0A                %7D,
+isNewSignIn: true
 %0A   
@@ -2814,24 +2814,106 @@
 w teams%22));%0A
+                expect(link.getElement().props.children%5B0%5D.includes(%22Security%22));%0A
             

update test NavBar