UNPKG

react-redux-fetch

Version:

A declarative and customizable way to fetch data for React components and manage that data in the Redux state

126 lines (107 loc) 3.19 kB
'use strict'; var _chai = require('chai'); var _chai2 = _interopRequireDefault(_chai); var _seamlessImmutable = require('seamless-immutable'); var _seamlessImmutable2 = _interopRequireDefault(_seamlessImmutable); var _actionTypes = require('../constants/actionTypes'); var _createAddToListAction = require('./createAddToListAction'); var _createAddToListAction2 = _interopRequireDefault(_createAddToListAction); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } _chai2.default.should(); describe('createAddToListAction()', function () { var state = (0, _seamlessImmutable2.default)({ pending: true, fulfilled: false, value: [{ id: 1, text: 'old text' }, { id: 2, text: 'old text' }, { id: 3, text: 'old text' }] }); describe('existing item', function () { var actionUpdate = { type: _actionTypes.FETCH.for('get').FULFILL, key: 'myList', value: { id: 2, text: 'new text' }, request: { meta: { addToList: { idName: 'id', id: 2 } } } }; it('should replace the existing item', function () { var action = (0, _createAddToListAction2.default)(state, actionUpdate); action.value.should.eql([{ id: 1, text: 'old text' }, { id: 2, text: 'new text' }, { id: 3, text: 'old text' }]); }); }); describe('new item', function () { var actionNew = { type: _actionTypes.FETCH.for('get').FULFILL, key: 'myList', value: { id: 4, text: 'new text' }, request: { meta: { addToList: { idName: 'id', id: 2 } } } }; it('should add the new item', function () { var action = (0, _createAddToListAction2.default)(state, actionNew); action.value.should.eql([{ id: 1, text: 'old text' }, { id: 2, text: 'old text' }, { id: 3, text: 'old text' }, { id: 4, text: 'new text' }]); }); }); describe('deep path', function () { var deepState = { pending: true, fulfilled: false, value: { prop1: 'a', prop2: { a: 1, b: 2 }, prop3: { prop4: 'b', prop5: [{ id: 1, text: 'old text' }, { id: 2, text: 'old text' }, { id: 3, text: 'old text' }] } } }; var deepAction = { type: _actionTypes.FETCH.for('get').FULFILL, key: 'myList', value: { prop3: { prop5: { id: 4, text: 'new text' } } }, request: { meta: { addToList: { path: 'prop3.prop5', idName: 'id', id: 2 } } } }; it('only replaces the correct leaf', function () { var action = (0, _createAddToListAction2.default)(deepState, deepAction); action.value.should.eql({ prop1: 'a', prop2: { a: 1, b: 2 }, prop3: { prop4: 'b', prop5: [{ id: 1, text: 'old text' }, { id: 2, text: 'old text' }, { id: 3, text: 'old text' }, { id: 4, text: 'new text' }] } }); }); }); });