|
| 1 | +import { |
| 2 | + getUnserializable, |
| 3 | + throwIfUnserializable, |
| 4 | +} from '../../src/meta-reducers/utils'; |
| 5 | + |
| 6 | +describe('getUnserializable:', () => { |
| 7 | + describe('serializable value:', () => { |
| 8 | + it('should not throw', () => { |
| 9 | + expect(getUnserializable(1)).toBe(false); |
| 10 | + expect(getUnserializable(true)).toBe(false); |
| 11 | + expect(getUnserializable('string')).toBe(false); |
| 12 | + expect(getUnserializable([1, 2, 3])).toBe(false); |
| 13 | + expect(getUnserializable({})).toBe(false); |
| 14 | + expect( |
| 15 | + getUnserializable({ |
| 16 | + nested: { number: 1, undefined: undefined, null: null }, |
| 17 | + }) |
| 18 | + ).toBe(false); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('unserializable value:', () => { |
| 23 | + it('should throw', () => { |
| 24 | + class TestClass {} |
| 25 | + |
| 26 | + expect(getUnserializable()).toEqual({ value: undefined, path: ['root'] }); |
| 27 | + expect(getUnserializable(null)).toEqual({ value: null, path: ['root'] }); |
| 28 | + |
| 29 | + const date = new Date(); |
| 30 | + expect(getUnserializable({ date })).toEqual({ |
| 31 | + value: date, |
| 32 | + path: ['date'], |
| 33 | + }); |
| 34 | + expect(getUnserializable({ set: new Set([]) })).toEqual({ |
| 35 | + value: new Set([]), |
| 36 | + path: ['set'], |
| 37 | + }); |
| 38 | + expect(getUnserializable({ map: new Map([]) })).toEqual({ |
| 39 | + value: new Map([]), |
| 40 | + path: ['map'], |
| 41 | + }); |
| 42 | + expect(getUnserializable({ class: new TestClass() })).toEqual({ |
| 43 | + value: new TestClass(), |
| 44 | + path: ['class'], |
| 45 | + }); |
| 46 | + expect( |
| 47 | + getUnserializable({ |
| 48 | + nested: { valid: true, class: new TestClass(), alsoValid: '' }, |
| 49 | + valid: [3], |
| 50 | + }) |
| 51 | + ).toEqual({ value: new TestClass(), path: ['nested', 'class'] }); |
| 52 | + }); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('throwIfUnserializable', () => { |
| 57 | + describe('serializable', () => { |
| 58 | + it('should not throw an error', () => { |
| 59 | + expect(() => throwIfUnserializable(false, 'state')).not.toThrow(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('unserializable', () => { |
| 64 | + it('should throw an error', () => { |
| 65 | + expect(() => |
| 66 | + throwIfUnserializable({ path: ['root'], value: undefined }, 'state') |
| 67 | + ).toThrowError(`Detected unserializable state at "root"`); |
| 68 | + expect(() => |
| 69 | + throwIfUnserializable({ path: ['date'], value: new Date() }, 'action') |
| 70 | + ).toThrowError(`Detected unserializable action at "date"`); |
| 71 | + expect(() => |
| 72 | + throwIfUnserializable( |
| 73 | + { |
| 74 | + path: ['one', 'two', 'three'], |
| 75 | + value: new Date(), |
| 76 | + }, |
| 77 | + 'state' |
| 78 | + ) |
| 79 | + ).toThrowError(`Detected unserializable state at "one.two.three"`); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments