From faaa15e822770de1b80ba5d847542e47a3672724 Mon Sep 17 00:00:00 2001 From: Elias Mulhall Date: Fri, 17 Aug 2018 16:05:50 -0400 Subject: [PATCH] update tests for optional --- test/json-decode.test.ts | 47 ++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/test/json-decode.test.ts b/test/json-decode.test.ts index b78a808..a5f644b 100644 --- a/test/json-decode.test.ts +++ b/test/json-decode.test.ts @@ -399,12 +399,12 @@ describe('dict', () => { }); describe('optional', () => { - describe('decoding an interface with optional fields', () => { - interface User { - id: number; - isDog?: boolean; - } + interface User { + id: number; + isDog?: boolean; + } + describe('decoding an interface with optional fields', () => { const decoder = object({ id: number(), isDog: optional(boolean()) @@ -426,6 +426,36 @@ describe('optional', () => { }); }); }); + + it('supports map', () => { + const decoder = object({ + id: number(), + isDog: optional(number()).map(num => num !== 0) + }); + + expect(decoder.run({id: 1, isDog: 0})).toEqual({ok: true, result: {id: 1, isDog: false}}); + expect(decoder.run({id: 1, isDog: 77})).toEqual({ok: true, result: {id: 1, isDog: true}}); + expect(decoder.run({id: 1})).toEqual({ok: true, result: {id: 1}}); + }); + + it('supports andThen', () => { + const decoder = object({ + id: number(), + isDog: optional(string()).andThen( + dogName => + dogName.toLowerCase()[0] === 'd' + ? succeed(true) + : fail(`${dogName} is not a dog, all dog names start with 'D'`) + ) + }); + + expect(decoder.run({id: 1, isDog: 'Doug'})).toEqual({ok: true, result: {id: 1, isDog: true}}); + expect(decoder.run({id: 1, isDog: 'Wanda'})).toMatchObject({ + ok: false, + error: {message: "Wanda is not a dog, all dog names start with 'D'"} + }); + expect(decoder.run({id: 1})).toEqual({ok: true, result: {id: 1}}); + }); }); describe('oneOf', () => { @@ -531,7 +561,7 @@ describe('withDefault', () => { }); describe('valueAt', () => { - describe('decode an value', () => { + describe('decode a value accessed from a path', () => { it('can decode a single object field', () => { const decoder = valueAt(['a'], string()); expect(decoder.run({a: 'boots', b: 'cats'})).toEqual({ok: true, result: 'boots'}); @@ -573,7 +603,10 @@ describe('valueAt', () => { }); describe('decode an optional field', () => { - const decoder = valueAt(['a', 'b', 'c'], oneOf(string(), constant(undefined))); + const decoder: Decoder = valueAt( + ['a', 'b', 'c'], + union(string(), constant(undefined)) + ); it('fails when the path does not exist', () => { const error = decoder.run({a: {x: 'cats'}});