all files / modules/__tests__/ Location-test.js

100% Statements 137/137
100% Branches 0/0
100% Functions 69/69
100% Lines 137/137
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254              31×                                                                                                                                                                                                                            
/* jshint -W058 */
const expect = require("expect");
const Location = require("../Location");
 
describe("an empty Location", function () {
    let location;
    beforeEach(function () {
        location = new Location();
    });
 
    it("has the correct href", function () {
        expect(location.href).toEqual("/");
    });
});
 
describe("a fully-specified Location", function () {
    let location;
    beforeEach(function () {
        location = new Location("http://user:pass@example.com:5000/the/path?the=query");
    });
 
    it("has the correct href", function () {
        expect(location.href).toEqual("http://user:pass@example.com:5000/the/path?the=query");
    });
 
    it("has the correct protocol", function () {
        expect(location.protocol).toEqual("http:");
    });
 
    it("has the correct auth", function () {
        expect(location.auth).toEqual("user:pass");
    });
 
    it("has the correct host", function () {
        expect(location.host).toEqual("example.com:5000");
    });
 
    it("has the correct hostname", function () {
        expect(location.hostname).toEqual("example.com");
    });
 
    it("has the correct port", function () {
        expect(location.port).toEqual("5000");
    });
 
    it("has the correct pathname", function () {
        expect(location.pathname).toEqual("/the/path");
    });
 
    it("has the correct path", function () {
        expect(location.path).toEqual("/the/path?the=query");
    });
 
    it("has the correct search", function () {
        expect(location.search).toEqual("?the=query");
    });
 
    it("has the correct queryString", function () {
        expect(location.queryString).toEqual("the=query");
    });
 
    it("has the correct query", function () {
        expect(location.query).toEqual({the: "query"});
    });
 
    describe("with http: protocol on the standard port", function () {
        it("leaves the port # out of host", function () {
            const location = new Location("http://example.com:80/the/path");
            expect(location.host).toEqual("example.com");
        });
    });
 
    describe("with http: protocol on a non-standard port", function () {
        it("includes the port # in host", function () {
            const location = new Location("http://example.com:8080/the/path");
            expect(location.host).toEqual("example.com:8080");
        });
    });
 
    describe("with https: protocol on the standard port", function () {
        it("leaves the port # out of host", function () {
            const location = new Location("https://example.com:443/the/path");
            expect(location.host).toEqual("example.com");
        });
    });
 
    describe("with https: protocol on a non-standard port", function () {
        it("includes the port # in host", function () {
            const location = new Location("https://example.com:5000/the/path");
            expect(location.host).toEqual("example.com:5000");
        });
    });
 
    describe("when the href is set", function () {
        it("has the correct href", function () {
            location.href = "https://user:pass@example.net/another/path?another=query";
            expect(location.href).toEqual("https://user:pass@example.net/another/path?another=query");
        });
    });
 
    describe("when the protocol is set", function () {
        it("has the correct href", function () {
            location.protocol = "https:";
            expect(location.href).toEqual("https://user:pass@example.com:5000/the/path?the=query");
        });
    });
 
    describe("when the hostname is set", function () {
        it("has the correct href", function () {
            location.hostname = "example.net";
            expect(location.href).toEqual("http://user:pass@example.net:5000/the/path?the=query");
        });
    });
 
    describe("when the host is set", function () {
        describe("with a port", function () {
            it("has the correct href", function () {
                location.host = "example.net:8080";
                expect(location.href).toEqual("http://user:pass@example.net:8080/the/path?the=query");
            });
        });
 
        describe("without a port", function () {
            it("has the correct href", function () {
                location.host = "example.net";
                expect(location.href).toEqual("http://user:pass@example.net/the/path?the=query");
            });
        });
    });
 
    describe("when the port is set", function () {
        it("has the correct href", function () {
            location.port = 6000;
            expect(location.href).toEqual("http://user:pass@example.com:6000/the/path?the=query");
        });
    });
 
    describe("when the pathname is set", function () {
        it("has the correct href", function () {
            location.pathname = "/another/path";
            expect(location.href).toEqual("http://user:pass@example.com:5000/another/path?the=query");
        });
    });
 
    describe("when the path is set", function () {
        describe("with a search", function () {
            it("has the correct href", function () {
                location.path = "/another/path?another=query";
                expect(location.href).toEqual("http://user:pass@example.com:5000/another/path?another=query");
            });
        });
 
        describe("without a search", function () {
            it("has the correct href", function () {
                location.path = "/another/path";
                expect(location.href).toEqual("http://user:pass@example.com:5000/another/path");
            });
        });
    });
 
    describe("when the search is set", function () {
        it("has the correct href", function () {
            location.search = "?another=query";
            expect(location.href).toEqual("http://user:pass@example.com:5000/the/path?another=query");
        });
    });
 
    describe("when the queryString is set", function () {
        it("has the correct href", function () {
            location.queryString = "another=query";
            expect(location.href).toEqual("http://user:pass@example.com:5000/the/path?another=query");
        });
    });
 
    describe("when the query is set", function () {
        it("has the correct href", function () {
            location.query = {another: "query"};
            expect(location.href).toEqual("http://user:pass@example.com:5000/the/path?another=query");
        });
    });
 
    describe("when appending another location", function () {
        beforeEach(function () {
            location = location.concat("https://example.org/more/path?more=query");
        });
 
        it("uses the new protocol", function () {
            expect(location.protocol).toEqual("https:");
        });
 
        it("uses the new host", function () {
            expect(location.host).toEqual("example.org");
        });
 
        it("has the correct pathname", function () {
            expect(location.pathname).toEqual("/the/path/more/path");
        });
 
        it("has the correct query", function () {
            expect(location.query).toEqual({the: "query", more: "query"});
        });
    });
});
 
describe("a Location with only a path", function () {
    let location;
    beforeEach(function () {
        location = new Location("/the/path?the=query");
    });
 
    it("has no protocol", function () {
        expect(location.protocol).toBe(null);
    });
 
    it("has no hostname", function () {
        expect(location.hostname).toBe(null);
    });
 
    it("has no port", function () {
        expect(location.port).toBe(null);
    });
 
    it("has no host", function () {
        expect(location.host).toBe(null);
    });
 
    it("has the correct pathname", function () {
        expect(location.pathname).toEqual("/the/path");
    });
 
    it("has the correct query", function () {
        expect(location.query).toEqual({the: "query"});
    });
});
 
describe("a Location with no search", function () {
    let location;
    beforeEach(function () {
        location = new Location("/the/path");
    });
 
    it("has an empty search", function () {
        expect(location.search).toEqual("");
    });
 
    it("has an empty queryString", function () {
        expect(location.queryString).toEqual("");
    });
 
    it("has an empty query", function () {
        expect(location.query).toEqual({});
    });
});