UNPKG

@bbc/spartacus

Version:
197 lines (156 loc) 8.48 kB
'use strict'; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactRouterConfig = require('react-router-config'); var reactRouterConfig = _interopRequireWildcard(_reactRouterConfig); var _App = require('./App'); var _loadInitialData = require('../utilities/loadInitialData'); var loadInitialData = _interopRequireWildcard(_loadInitialData); 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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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('App', function () { var wrapper = void 0; var setStateSpy = void 0; var loadInitialDataSpy = void 0; var initialData = { data: 'Some initial data' }; reactRouterConfig.renderRoutes = jest.fn().mockReturnValue(_react2.default.createElement( 'h1', null, initialData.data )); beforeAll(function () { // eslint-disable-next-line no-undef wrapper = shallow(_react2.default.createElement(_App.App, { location: { pathname: 'pathnameOne' }, routes: [], initialData: initialData })); setStateSpy = jest.spyOn(wrapper.instance(), 'setState'); loadInitialDataSpy = jest.spyOn(loadInitialData, 'default'); }); it('should return rendered routes', function () { expect.assertions(4); expect(loadInitialDataSpy).not.toHaveBeenCalled(); expect(reactRouterConfig.renderRoutes).toHaveBeenCalledTimes(1); expect(reactRouterConfig.renderRoutes).toHaveBeenCalledWith([], { data: initialData, error: null, loading: false }); expect(wrapper).toMatchSnapshot(); }); describe('componentDidUpdate', function () { describe('same location', function () { it('should not call set state with new data', function () { wrapper.setProps({ location: { pathname: 'pathnameOne' } }); expect.assertions(2); expect(loadInitialDataSpy).not.toHaveBeenCalled(); expect(setStateSpy).not.toHaveBeenCalled(); }); }); describe('different location', function () { describe('rejected loadInitialData', function () { it('should set state to the error', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { var error; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: error = 'Error!'; loadInitialData.default = jest.fn().mockImplementation(function () { return Promise.reject(error); }); wrapper.setProps({ location: { pathname: 'pathnameThree' } }); _context.next = 5; return loadInitialData.default; case 5: expect.assertions(3); // why is state being called 3 times? expect(setStateSpy).toHaveBeenNthCalledWith(1, { data: null, error: null, loadInitialDataPromise: expect.any(Promise), loading: true }); expect(setStateSpy).toHaveBeenNthCalledWith(2, { data: null, error: error, loadInitialDataPromise: null, loading: false }); expect(setStateSpy).toHaveBeenNthCalledWith(2, { data: null, error: error, loadInitialDataPromise: null, loading: false }); case 9: case 'end': return _context.stop(); } } }, _callee, undefined); }))); }); describe('successful fetch of route, match, and initial props', function () { it('should call set state with new data', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { var pathname, data; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: pathname = 'pathnameFour'; data = 'Really cool data'; loadInitialData.default = jest.fn().mockImplementation(_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: return _context2.abrupt('return', data); case 1: case 'end': return _context2.stop(); } } }, _callee2, undefined); }))); wrapper.setProps({ location: { pathname: pathname } }); _context3.next = 6; return loadInitialData.default; case 6: expect.assertions(4); expect(loadInitialData.default).toHaveBeenCalledWith(pathname, []); // why is state being called 3 times? expect(setStateSpy).toHaveBeenNthCalledWith(4, { data: null, error: null, loadInitialDataPromise: expect.any(Promise), loading: true }); expect(setStateSpy).toHaveBeenNthCalledWith(5, { data: data, error: null, loadInitialDataPromise: null, loading: false }); expect(setStateSpy).toHaveBeenNthCalledWith(6, { data: data, error: null, loadInitialDataPromise: null, loading: false }); case 11: case 'end': return _context3.stop(); } } }, _callee3, undefined); }))); }); }); }); });