all files / modules/utils/__tests__/ compileRoute-test.js

100% Statements 35/35
100% Branches 0/0
100% Functions 13/13
100% Lines 35/35
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                                            
const assert = require("assert");
const expect = require("expect");
const compileRoute = require("../compileRoute");
 
describe("compileRoute", function () {
    let keys;
    beforeEach(function () {
        keys = [];
    });
 
    describe("when a pattern contains named keys", function () {
        it("populates the keys array with the values", function () {
            compileRoute("/users/:userID/posts/:postID", keys);
            expect(keys).toEqual(["userID", "postID"]);
        });
    });
 
    describe("when a pattern contains *s", function () {
        it("has splat keys", function () {
            compileRoute("/files/*.*", keys);
            expect(keys).toEqual(["splat", "splat"]);
        });
 
        it("matches correctly", function () {
            const re = compileRoute("/files/*.*", keys);
            assert(re.exec("/files/fun.jpg"));
            assert(!re.exec("/files/fun"));
        });
    });
 
    describe("a pattern with ()", function () {
        it("has the correct keys", function () {
            compileRoute("/users/(:userID)", keys);
            expect(keys).toEqual(["userID"]);
        });
 
        it("matches correctly", function () {
            const re = compileRoute("/users/(:userID)", keys);
            assert(!re.exec("/users/5"));
            assert(re.exec("/users/(5)"));
        });
    });
 
    describe("a pattern with ^ and $", function () {
        it("has the correct keys", function () {
            compileRoute("/user$/^:userID", keys);
            expect(keys).toEqual(["userID"]);
        });
 
        it("matches correctly", function () {
            const re = compileRoute("/user$/^:userID", keys);
            assert(!re.exec("/user$/5"));
            assert(re.exec("/user$/^5"));
        });
    });
});