import url from './url';

function setLocation(href) {
    Object.defineProperty(window.location, 'href', {
        writable: true,
        value: href,
    });
    Object.defineProperty(window.location, 'pathname', {
        writable: true,
        value: href.substring(href.indexOf("/florence")),
    });
}
setLocation('http://publishing.onsdigital.co.uk/florence/datasets');

jest.mock('../utilities/log', () => {
    return {
        add: function() {
            // do nothing
        },
        eventTypes: {
            unexpectedRuntimeError: ""
        }
    }
});

jest.mock('../utilities/notifications', () => {
    return {
        add: function() {
            // do nothing
        }
    }
});

afterEach(() => {
    setLocation('http://publishing.onsdigital.co.uk/florence/datasets');
});

describe("'../' should remove one route from the path", () => {
    it("'../' will go up one level", () => {
        expect(url.resolve("../")).toBe("/florence");
    })

    it("'../../' will go up two levels", () => {
        setLocation('http://publishing.onsdigital.co.uk/florence/datasets/an-id-12345/metadata');
        expect(url.resolve("../../")).toBe("/florence/datasets");
    })
    it("'../../../teams' will go up three levels and to the teams path", () => {
        setLocation('http://publishing.onsdigital.co.uk/florence/datasets/an-id-12345/metadata');
        expect(url.resolve("../../../teams")).toBe("/florence/teams");
    })
});

test("Routes relative to the root are prefixed with '/florence'", () => {
    expect(url.resolve('/teams')).toBe("/florence/teams");
    expect(url.resolve('/')).toBe("/florence");
});

test("Replacing the last route at the root of the app still prefixes the path with '/florence'", () => {
    setLocation('http://publishing.onsdigital.co.uk/florence');
    expect(url.resolve("datasets")).toBe("/florence/datasets");
})

test("Path from current location replaces the last route with the new path", () => {
    expect(url.resolve("teams")).toBe("/florence/teams");
})

test("Slugged URL with leading slash produces correct slug without leading dash", () => {
    expect(url.slug("/economy/grossdomesticproduct/bulletins/gdp/jan2017")).toBe("economy-grossdomesticproduct-bulletins-gdp-jan2017");
});

test("Slugged URL without leading slash produces correct slug", () => {
    expect(url.slug("economy/grossdomesticproduct/bulletins/gdp/jan2017")).toBe("economy-grossdomesticproduct-bulletins-gdp-jan2017");
});

describe("Resolved URLs include query parameters", () => {
    it("includes queries on routes from the root path", () => {
        expect(url.resolve("/datasets/my-dataset/editions/current/versions/1?collection=my-collection")).toBe("/florence/datasets/my-dataset/editions/current/versions/1?collection=my-collection");
    });
    
    it("includes queries on relative routes", () => {
        setLocation('http://publishing.onsdigital.co.uk/florence/datasets/my-dataset-id');
        expect(url.resolve("../?collection=my-collection")).toBe("/florence/datasets?collection=my-collection");
    });

    it("excludes queries when the caller passes in the correct argument", () => {
        setLocation('http://publishing.onsdigital.co.uk/florence/datasets/my-dataset-id?collection=my-collection-id');
        expect(url.resolve("/collections")).toBe("/florence/collections");
    });
});
@@ -44,24 +44,60 @@
 ion(href) %7B%0A
+    console.log(href.indexOf(%22?%22));%0A
     Object.d
@@ -337,16 +337,192 @@
     %7D);%0A
+    Object.defineProperty(window.location, 'search', %7B%0A        writable: true,%0A        value: href.substring(href.indexOf(%22?%22) %3E= 0 ? href.indexOf(%22?%22) : href.length),%0A    %7D);%0A
 %7D%0AsetLoc
@@ -3518,17 +3518,51 @@
 lections
-%22
+?collection=my-collection-id%22, true
 )).toBe(

Fix url exclude parameters test to actually test functionality correctly