@virtuous/conductor
Version:
907 lines (783 loc) • 32.5 kB
JavaScript
;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _index = _interopRequireDefault(require("./index"));
var _Stack = _interopRequireDefault(require("../Stack"));
var _emitter = _interopRequireDefault(require("../emitter"));
var constants = _interopRequireWildcard(require("../constants"));
var errors = _interopRequireWildcard(require("./errors"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var pathname1 = '/myroute/123';
var pattern1 = '/myroute/:id';
var dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(function () {
return 123456;
});
describe('Conductor', function () {
beforeEach(function () {
_Stack["default"].constructor();
_index["default"].constructor();
_index["default"].register(pattern1);
});
describe('constructor()', function () {
it('should set an initial stack entry', function () {
var _stack$first = _Stack["default"].first(),
_stack$first2 = _slicedToArray(_stack$first, 2),
entry = _stack$first2[1];
expect(_Stack["default"].getAll().size).toBe(1);
expect(entry.pathname).toBe(pathname1);
});
it('should make use of a given custom history', function () {
// Get the initial first id.
var _stack$first3 = _Stack["default"].first(),
_stack$first4 = _slicedToArray(_stack$first3, 1),
id = _stack$first4[0];
var history = _index["default"].history;
_index["default"].constructor();
var newHistory = _index["default"].history;
_index["default"].register(pattern1); // Get the new first id.
var _stack$first5 = _Stack["default"].first(),
_stack$first6 = _slicedToArray(_stack$first5, 1),
newId = _stack$first6[0];
expect(history).not.toEqual(newHistory);
expect(_Stack["default"].getAll().size).toBe(1);
expect(id).not.toBe(newId);
});
});
describe('register()', function () {
it('should correctly register a pattern with a matching function', function () {
expect(_typeof(_index["default"].patterns[pattern1].match)).toBe('function');
});
it('should correctly register a pattern with a transform function', function () {
/**
* @returns {Object}
*/
var transform = function transform() {
return {};
};
_index["default"].register('/test', transform);
expect(_typeof(_index["default"].patterns['/test'].transform)).toBe('function');
});
it('should not register with missing pattern', function () {
expect(_index["default"].register).toThrowError(errors.EMISSINGPATTERN);
});
it('should not register with invalid type', function () {
expect(function () {
return _index["default"].register(123);
}).toThrowError(errors.EINVALIDPATTERN);
});
it('should update initial entry when matching pattern is registered', function () {
_index["default"].constructor();
_index["default"].register(pattern1, function (_ref) {
var params = _ref.params;
return {
params: _objectSpread({}, params, {
transformed: true
})
};
});
var _stack$first7 = _Stack["default"].first(),
_stack$first8 = _slicedToArray(_stack$first7, 2),
route = _stack$first8[1];
expect(route.pattern).toEqual(pattern1);
expect(route.params).toEqual({
id: '123',
transformed: true
});
});
});
describe('push()', function () {
it('should resolve correctly', function () {
var params = {
pathname: "".concat(pathname1, "?s=phrase"),
state: {
test: 123
}
};
var willCallback = jest.fn();
var didCallback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_PUSH, willCallback);
_emitter["default"].once(constants.EVENT_DID_PUSH, didCallback);
return _index["default"].push(params).then(function (result) {
expect(_index["default"].routeIndex).toBe(1);
var _stack$last = _Stack["default"].last(),
_stack$last2 = _slicedToArray(_stack$last, 2),
route = _stack$last2[1];
expect(route.pathname).toBe(pathname1);
expect(route.query).toEqual({
s: 'phrase'
});
expect(route.state).toEqual({
test: 123
});
expect(willCallback).toHaveBeenCalledWith(result);
expect(didCallback).toHaveBeenCalledWith(result);
expect(result.prev.constructor.name === 'Route').toBeTruthy();
expect(result.next.constructor.name === 'Route').toBeTruthy();
expect(_index["default"].history.location.pathname).toBe("".concat(pathname1, "?s=phrase"));
});
});
it('should transform the route when pushed',
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee() {
var transform, _stack$last3, _stack$last4, route;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
/**
* @param {Object} route The route object.
* @returns {Object}
*/
transform = function transform(route) {
return {
params: {
test: route.query.search
},
state: {
searchActive: !!route.query.search
}
};
};
_index["default"].register('/test', transform);
_context.next = 4;
return _index["default"].push({
pathname: '/test?search=hello'
});
case 4:
_stack$last3 = _Stack["default"].last(), _stack$last4 = _slicedToArray(_stack$last3, 2), route = _stack$last4[1];
expect(route.pathname).toBe('/test');
expect(route.params).toEqual({
test: 'hello'
});
expect(route.state).toEqual({
searchActive: true
});
case 8:
case "end":
return _context.stop();
}
}
}, _callee);
})));
it('should remove all forward routes from the stack',
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee2() {
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return _index["default"].push({
pathname: '/myroute/456'
});
case 2:
_context2.next = 4;
return _index["default"].push({
pathname: '/myroute/789'
});
case 4:
expect(_Stack["default"].getAll().size).toBe(3);
_context2.next = 7;
return _index["default"].pop({
steps: 2
});
case 7:
_context2.next = 9;
return _index["default"].push({
pathname: '/myroute/abc'
});
case 9:
expect(_index["default"].routeIndex).toBe(1);
expect(_Stack["default"].getAll().size).toBe(2);
expect(_Stack["default"].getByIndex(1).pathname).toBe('/myroute/abc');
case 12:
case "end":
return _context2.stop();
}
}
}, _callee2);
})));
it('should reject when params are missing', function () {
return _index["default"].push()["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EPARAMSMISSING));
});
});
it('should reject when params are empty', function () {
return _index["default"].push({})["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EPARAMSEMPTY));
});
});
it('should not emit willPush event', function () {
var params = {
pathname: pathname1,
emitBefore: false
};
var callback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_PUSH, callback);
return _index["default"].push(params).then(function () {
expect(callback).not.toHaveBeenCalled();
});
});
it('should not emit didPush event', function () {
var params = {
pathname: pathname1,
emitAfter: false
};
var callback = jest.fn();
_emitter["default"].once(constants.EVENT_DID_PUSH, callback);
return _index["default"].push(params).then(function () {
expect(callback).not.toHaveBeenCalled();
});
});
it('should push when a non-matching pathname was given', function (done) {
var params = {
pathname: '/not-registered'
};
return _index["default"].push(params).then(
/*#__PURE__*/
function () {
var _ref5 = _asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee3(_ref4) {
var next;
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
next = _ref4.next;
expect(next.pathname).toBe('/not-registered');
expect(next.pattern).toBeNull();
expect(next.transform).toBeNull(); // Pop to remove the non-matching route for the next set of tests.
_context3.next = 6;
return _index["default"].pop();
case 6:
done();
case 7:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
return function (_x) {
return _ref5.apply(this, arguments);
};
}());
});
});
describe('pop()', function () {
it('should resolve correctly',
/*#__PURE__*/
function () {
var _ref6 = _asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee4(done) {
var willCallback, didCallback;
return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
willCallback = jest.fn();
didCallback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_POP, willCallback);
_emitter["default"].once(constants.EVENT_DID_POP, didCallback);
_context4.next = 6;
return _index["default"].push({
pathname: '/myroute/456'
});
case 6:
_index["default"].pop().then(function (result) {
var route = _Stack["default"].getByIndex(0);
expect(_Stack["default"].getAll().size).toBe(2);
expect(_index["default"].routeIndex).toBe(0);
expect(route).toEqual(result.next);
expect(willCallback).toHaveBeenCalledWith(result);
expect(didCallback).toHaveBeenCalledWith(result);
expect(_index["default"].history.location.pathname).toBe(pathname1);
done();
});
case 7:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
return function (_x2) {
return _ref6.apply(this, arguments);
};
}());
it('should pop multiple routes',
/*#__PURE__*/
function () {
var _ref7 = _asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee5(done) {
return _regenerator["default"].wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return _index["default"].push({
pathname: '/myroute/456'
});
case 2:
_context5.next = 4;
return _index["default"].push({
pathname: '/myroute/789'
});
case 4:
_index["default"].pop({
steps: 2
}).then(function (result) {
var currentRoute = _Stack["default"].getByIndex(_index["default"].routeIndex);
expect(currentRoute).toBe(result.next);
done();
});
case 5:
case "end":
return _context5.stop();
}
}
}, _callee5);
}));
return function (_x3) {
return _ref7.apply(this, arguments);
};
}());
it('should not pop when steps is negative',
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee6() {
return _regenerator["default"].wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return _index["default"].push({
pathname: '/myroute/456'
});
case 2:
_context6.next = 4;
return _index["default"].pop({
steps: -3
})["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EINVALIDSTEPS));
});
case 4:
case "end":
return _context6.stop();
}
}
}, _callee6);
})));
it('should clamp when steps is larger than the stack',
/*#__PURE__*/
function () {
var _ref9 = _asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee7(done) {
return _regenerator["default"].wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return _index["default"].push({
pathname: '/myroute/456'
});
case 2:
_context7.next = 4;
return _index["default"].push({
pathname: '/myroute/789'
});
case 4:
_index["default"].pop({
steps: 5
}).then(function (result) {
expect(result.next).toBe(_Stack["default"].getByIndex(0));
done();
});
case 5:
case "end":
return _context7.stop();
}
}
}, _callee7);
}));
return function (_x4) {
return _ref9.apply(this, arguments);
};
}());
it('should merge given state to incoming route',
/*#__PURE__*/
function () {
var _ref10 = _asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee8(done) {
var state;
return _regenerator["default"].wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return _index["default"].push({
pathname: '/myroute/456',
state: {
test: 123
}
});
case 2:
_context8.next = 4;
return _index["default"].push({
pathname: '/myroute/789'
});
case 4:
state = {
test: 456
};
_index["default"].pop({
state: state
}).then(function () {
var currentRoute = _Stack["default"].getByIndex(_index["default"].routeIndex);
expect(currentRoute.state).toEqual(state);
done();
});
case 6:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
return function (_x5) {
return _ref10.apply(this, arguments);
};
}());
it('should not emit willPop event',
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee9() {
var callback;
return _regenerator["default"].wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
callback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_POP, callback);
_context9.next = 4;
return _index["default"].push({
pathname: '/myroute/456'
});
case 4:
return _context9.abrupt("return", _index["default"].pop({
emitBefore: false
}).then(function () {
expect(callback).not.toHaveBeenCalled();
}));
case 5:
case "end":
return _context9.stop();
}
}
}, _callee9);
})));
it('should not emit didPop event',
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee10() {
var callback;
return _regenerator["default"].wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
callback = jest.fn();
_emitter["default"].once(constants.EVENT_DID_POP, callback);
_context10.next = 4;
return _index["default"].push({
pathname: '/myroute/456'
});
case 4:
return _context10.abrupt("return", _index["default"].pop({
emitAfter: false
}).then(function () {
expect(callback).not.toHaveBeenCalled();
}));
case 5:
case "end":
return _context10.stop();
}
}
}, _callee10);
})));
});
describe('replace()', function () {
it('should replace correctly', function (done) {
var willCallback = jest.fn();
var didCallback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_REPLACE, willCallback);
_emitter["default"].once(constants.EVENT_DID_REPLACE, didCallback);
_index["default"].replace({
pathname: '/myroute/456',
state: {
test: 123
}
}).then(function (result) {
var _stack$first9 = _Stack["default"].first(),
_stack$first10 = _slicedToArray(_stack$first9, 2),
route = _stack$first10[1];
expect(_Stack["default"].getAll().size).toBe(1);
expect(route.pathname).toBe('/myroute/456');
expect(route.state).toEqual({
test: 123
});
expect(willCallback).toHaveBeenCalledWith(result);
expect(didCallback).toHaveBeenCalledWith(result);
done();
});
});
it('should reject when params are missing', function () {
return _index["default"].replace()["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EPARAMSMISSING));
});
});
it('should reject when params are empty', function () {
return _index["default"].replace({})["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EPARAMSEMPTY));
});
});
it('should not emit willPush event', function () {
var params = {
pathname: pathname1,
emitBefore: false
};
var callback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_REPLACE, callback);
return _index["default"].replace(params).then(function () {
expect(callback).not.toHaveBeenCalled();
});
});
it('should not emit didPush event', function () {
var params = {
pathname: pathname1,
emitAfter: false
};
var callback = jest.fn();
_emitter["default"].once(constants.EVENT_DID_REPLACE, callback);
return _index["default"].replace(params).then(function () {
expect(callback).not.toHaveBeenCalled();
});
});
it('should reject when pathname cannot be matched', function () {
var params = {
pathname: pathname1
};
return _index["default"].replace(params)["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EINVALIDPATHNAME));
});
});
});
describe('reset()', function () {
it('should correctly reset to the first route', function (done) {
var _stack$first11 = _Stack["default"].first(),
_stack$first12 = _slicedToArray(_stack$first11, 2),
firstRoute = _stack$first12[1];
var willCallback = jest.fn();
var didCallback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_RESET, willCallback);
_emitter["default"].once(constants.EVENT_DID_RESET, didCallback);
_index["default"].push({
pathname: '/myroute/456'
});
_index["default"].push({
pathname: '/myroute/789'
});
var prevRoute = _Stack["default"].getByIndex(_index["default"].routeIndex);
var state = {
reset: true
};
_index["default"].reset(state).then(function (result) {
expect(_index["default"].history.location.pathname).toBe(pathname1);
expect(firstRoute).toBe(result.next);
expect(result.next.state).toEqual(state);
expect(prevRoute).toBe(result.prev);
expect(willCallback).toHaveBeenCalledWith(result);
expect(didCallback).toHaveBeenCalledWith(result);
done();
});
});
it('should not reset when there is only one route', function (done) {
var willCallback = jest.fn();
var didCallback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_RESET, willCallback);
_emitter["default"].once(constants.EVENT_DID_RESET, didCallback);
_index["default"].reset().then(function () {
expect(willCallback).toHaveBeenCalled();
expect(didCallback).toHaveBeenCalled();
done();
});
});
});
describe('resetTo()', function () {
it('should correctly reset to the specified route',
/*#__PURE__*/
function () {
var _ref13 = _asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee11(done) {
var willCallback, didCallback, previous;
return _regenerator["default"].wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
willCallback = jest.fn();
didCallback = jest.fn();
_emitter["default"].once(constants.EVENT_WILL_RESET, willCallback);
_emitter["default"].once(constants.EVENT_DID_RESET, didCallback);
_context11.next = 6;
return _index["default"].push({
pathname: '/myroute/456'
});
case 6:
previous = _index["default"].getCurrentRoute();
_index["default"].resetTo('/myroute/789').then(function (result) {
var _stack$first13 = _Stack["default"].first(),
_stack$first14 = _slicedToArray(_stack$first13, 2),
route = _stack$first14[1];
expect(route).toBe(result.next);
expect(previous).toBe(result.prev);
expect(route.pathname).toBe('/myroute/789'); // Should contain the newly pushed route and the new first route.
expect(_Stack["default"].getAll().size).toBe(2);
expect(willCallback).toHaveBeenCalledWith(result);
expect(didCallback).toHaveBeenCalledWith(result);
done();
});
case 8:
case "end":
return _context11.stop();
}
}
}, _callee11);
}));
return function (_x6) {
return _ref13.apply(this, arguments);
};
}());
it('should not reset when pathname is missing', function () {
return _index["default"].resetTo()["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EMISSINGPATHNAME));
});
});
it('should not reset to non-matching pathname', function () {
return _index["default"].resetTo('/invalid/path')["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EINVALIDPATHNAME));
});
});
});
describe('update()', function () {
it('should correctly update/override a route`s state',
/*#__PURE__*/
_asyncToGenerator(
/*#__PURE__*/
_regenerator["default"].mark(function _callee12() {
var _stack$last5, _stack$last6, id, route, callback, state;
return _regenerator["default"].wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
_stack$last5 = _Stack["default"].last(), _stack$last6 = _slicedToArray(_stack$last5, 2), id = _stack$last6[0], route = _stack$last6[1];
callback = jest.fn();
_emitter["default"].once(constants.EVENT_UPDATE, callback);
state = {
test: 123
};
_context12.next = 6;
return _index["default"].update(id, state);
case 6:
expect(route.state).toEqual(state);
expect(route.updated).toEqual(dateNowSpy());
expect(callback).toHaveBeenCalledWith(route);
_context12.next = 11;
return _index["default"].update(id, {
test: 456
});
case 11:
expect(route.state).toEqual({
test: 456
});
case 12:
case "end":
return _context12.stop();
}
}
}, _callee12);
})));
it('should reject when id is missing', function () {
_index["default"].update()["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EPARAMSINVALID));
});
});
it('should reject when state is missing', function () {
_index["default"].update(12345)["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EPARAMSINVALID));
});
});
it('should reject when state is empty', function () {
_index["default"].update(12345, {})["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EPARAMSINVALID));
});
});
it('should reject when id doesn`t match a route', function () {
_index["default"].update(12345, {
test: 123
})["catch"](function (error) {
return expect(error).toEqual(new Error(errors.EINVALIDID));
});
});
});
describe('match()', function () {
it('should match a pathname correctly', function () {
expect(_index["default"].match('/myroute/123')).toBe(pattern1);
});
it('should not match a stranger pathname', function () {
expect(_index["default"].match('/test/123')).toBe(false);
});
it('should not match when no pathname is given', function () {
expect(_index["default"].match()).toBe(false);
});
});
describe('matches()', function () {
it('should match a pathname correctly', function () {
expect(_index["default"].matches(pattern1, '/myroute/123')).toBe(true);
});
it('should return false when there is a mismatch', function () {
expect(_index["default"].matches(pattern1, '/test/123')).toBe(false);
});
it('should not match a stranger pattern', function () {
expect(_index["default"].matches('/hello/:id', '/myroute/123')).toBe(false);
});
it('should not match when pattern is missing', function () {
expect(_index["default"].matches()).toBe(false);
});
it('should not match when params are missing', function () {
expect(_index["default"].matches(pattern1)).toBe(false);
});
});
});