Repository URL to install this package:
|
Version:
1.3.2 ▾
|
global.window = global
import { getTyped } from '../src'
const example = {
eh: {
name: 'eh',
moose: 'true',
falsish: 'false',
canada: [10, 20],
price: '10.1',
nested: {
nil: undefined,
},
},
}
describe('composition', () => {
it('should work with getTyped', () => {
const {
// each of these defaults to the empty version if it's null or undefined
//
// plus it fixes "stringy primitives" to the type
// "true" => true, "10" => 10
//
// array('eh.nothere') => []
array,
// oneOf('eh.nothere', 'eh.canada') => [10, 20]
oneOf,
// obj('eh.moose') => {}
// obj('eh') => {moose: ...}
obj,
// float('eh.price') => 10.10
float,
// boolean('eh.moose') => true
// boolean('eh.nothere') => false
boolean,
// boolean('eh.nothere') => ''
// boolean('eh.name') => 'eh'
string,
// raw
get,
} = getTyped(example)
expect(array('eh.nothere')).toEqual([])
expect(obj('eh')).toEqual({ ...example.eh })
expect(boolean('eh.moose')).toEqual(true)
expect(boolean('eh.falsish')).toEqual(false)
expect(string('eh.nothere')).toEqual('')
expect(string('eh.name')).toEqual('eh')
expect(get('eh.nested.nil')).toEqual(undefined)
expect(string('eh.nested.nil')).toEqual('')
const transformed = {
name: string('eh.name'),
list: array('eh.nothere'),
nothere: string('eh.nothere'),
moose: boolean('eh.moose'),
falsish: boolean('eh.falsish'),
obj: obj('eh'),
nil: get('eh.nested.nil'),
}
expect(transformed).toMatchSnapshot()
})
it.skip('should work for oneOf(1, 2)', () => {
const { oneOf } = getTyped(example)
expect(oneOf('eh.nothere', 'eh.canada')).toEqual([10, 20])
})
it.skip('should work for floats', () => {
const { float } = getTyped(example)
expect(float('eh.price')).toEqual('10.10')
})
it.skip('should work .get({}, fallback)', () => {
const { get } = getTyped(example)
expect(get('eh.nested.nil', '')).toEqual('')
})
})