@bbc/spartacus
Version:
162 lines (123 loc) • 6.93 kB
JavaScript
;
var _reactRouterConfig = require('react-router-config');
var reactRouterConfig = _interopRequireWildcard(_reactRouterConfig);
var _ = require('.');
var _2 = _interopRequireDefault(_);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /*
* © Jordan Tart https://github.com/jtart
* https://github.com/jtart/react-universal-app
*/
describe('loadInitialData', function () {
var data = 'Some data!';
var match = { match: true };
describe('no matched route', function () {
it('should return an empty object', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
reactRouterConfig.matchRoutes = jest.fn().mockReturnValue([]);
_context.prev = 1;
_context.next = 4;
return (0, _2.default)('url', []);
case 4:
_context.next = 9;
break;
case 6:
_context.prev = 6;
_context.t0 = _context['catch'](1);
expect(_context.t0).toMatchSnapshot();
case 9:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[1, 6]]);
})));
});
describe('no getInitialData function on route', function () {
it('should return an empty object', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
var initialData;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
reactRouterConfig.matchRoutes = jest.fn().mockReturnValue([{ route: { route: 'data' }, match: { match: true } }]);
_context2.next = 3;
return (0, _2.default)('url', ['url']);
case 3:
initialData = _context2.sent;
expect(initialData).toEqual({});
case 5:
case 'end':
return _context2.stop();
}
}
}, _callee2, undefined);
})));
});
describe('getInitialData function on route', function () {
it('should call getInitialData with passed ctx and return value', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
var route, initialData;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
route = {
getInitialData: jest.fn().mockImplementation(function () {
return Promise.resolve(data);
})
};
reactRouterConfig.matchRoutes = jest.fn().mockReturnValue([{ route: route, match: match }]);
_context3.next = 4;
return (0, _2.default)('url', ['url']);
case 4:
initialData = _context3.sent;
expect(route.getInitialData).toHaveBeenCalledTimes(1);
expect(route.getInitialData).toHaveBeenCalledWith({ match: match });
expect(initialData).toEqual(data);
case 8:
case 'end':
return _context3.stop();
}
}
}, _callee3, undefined);
})));
describe('error on getInitialData call', function () {
it('should throw an error', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
var error, route;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
error = 'Error!';
route = {
getInitialData: jest.fn().mockImplementation(function () {
return Promise.reject(error);
})
};
reactRouterConfig.matchRoutes = jest.fn().mockReturnValue([{ route: route, match: match }]);
_context4.prev = 3;
_context4.next = 6;
return (0, _2.default)(route, {});
case 6:
_context4.next = 11;
break;
case 8:
_context4.prev = 8;
_context4.t0 = _context4['catch'](3);
expect(_context4.t0).toEqual(error);
case 11:
expect(route.getInitialData).toHaveBeenCalledTimes(1);
case 12:
case 'end':
return _context4.stop();
}
}
}, _callee4, undefined, [[3, 8]]);
})));
});
});
});