UNPKG

overmind

Version:
132 lines 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _1 = require("./"); describe('REHYDRATE', () => { test('should allow rehydration', () => { expect.assertions(1); const state = { foo: 'bar', }; (0, _1.rehydrate)(state, { foo: 'bar2', }); expect(state).toEqual({ foo: 'bar2', }); }); test('should allow rehydration by mutations', () => { expect.assertions(1); const state = { foo: 'bar', }; (0, _1.rehydrate)(state, [ { method: 'set', args: ['bar2'], hasChangedValue: false, path: 'foo', revert: () => { }, }, ]); expect(state).toEqual({ foo: 'bar2', }); }); test.only('should allow rehydration of single class value', () => { expect.assertions(1); class User { constructor() { this.name = 'Bob'; } toJSON() { return { [_1.SERIALIZE]: true, name: this.name, }; } static fromJSON(json) { return Object.assign(new User(), json); } } const state = { user: null, user2: new User(), }; (0, _1.rehydrate)(state, { user: { name: 'Bob2', }, user2: { name: 'Bob2', }, }, { user: User.fromJSON, user2: User.fromJSON, }); expect(state).toEqual({ user: { name: 'Bob2', }, user2: { name: 'Bob2', }, }); }); test('should allow rehydration of array', () => { expect.assertions(2); class User { constructor() { this.name = 'Bob'; } static fromJSON(json) { return Object.assign(new User(), json); } } const state = { users: [], }; (0, _1.rehydrate)(state, { users: [ { name: 'Bob2', }, { name: 'Bob3', }, ], }, { users: User.fromJSON, }); expect(state.users[0]).toBeInstanceOf(User); expect(state.users[1]).toBeInstanceOf(User); }); test('should allow rehydration of dictionary', () => { expect.assertions(2); class User { constructor() { this.name = 'Bob'; } static fromJSON(json) { return Object.assign(new User(), json); } } const state = { users: {}, }; (0, _1.rehydrate)(state, { users: { id1: { name: 'Bob2', }, id2: { name: 'Bob3', }, }, }, { users: User.fromJSON, }); expect(state.users.id1).toBeInstanceOf(User); expect(state.users.id2).toBeInstanceOf(User); }); }); //# sourceMappingURL=rehydrate.test.js.map