All files / src/toArgs setDefaultValues.test.js

0% Statements 0/29
100% Branches 0/0
0% Functions 0/5
0% Lines 0/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                                                                                                                                                                                             
const {setDefaultValues}     = require('./setDefaultValues')
const {invalidDefaultValues} = require('../errors')
 
test('setDefaultValues works as expected', () => {
  const opts = [
    {key: 'name', types: ['string'], args: ['--name'], defaultValues: ['Logan']},
    {key: 'numStr', types: ['number', 'string'], args: ['--ns'], defaultValues: ['42', 'string']},
    {
      key: 'ask',
      types: null,
      args: ['ask'],
      opts: [
        {key: 'question', types: ['string'], args: ['-q']},
        {key: 'jokingly', types: [], args: ['-j'], defaultValues: {type: 'flag', count: 1}}
      ],
      defaultValues: {jokingly: 'foo'}
    },
    {key: 'brisk', types: [], args: ['--brisk'], defaultValues: {type: 'flag', count: 1}},
  ]
 
  const {errs, args} = setDefaultValues({opts})
 
  const expArgs = {
    _: [],
    name: 'Logan',
    numStr: ['42', 'string'],
    ask: {
      jokingly: 'foo'
    },
    brisk: {type: 'flag', count: 1}
  }
 
  const expErrs = []
 
  expect(args).toStrictEqual(expArgs)
  expect(errs).toStrictEqual(expErrs)
})
 
test('setDefaultValues reports an error if it has the wrong format', () => {
  const name     = {key: 'name', types: ['string'], args: ['--name'], defaultValues: []}
  const birthday = {key: 'birthday', types: ['string'], args: ['-b'], defaultValues: 'unknown'}
 
  const opts = [
    name,
    birthday,
    {
      key: 'ask',
      args: ['ask'],
      opts: [
        {key: 'question', types: ['string'], args: ['-q']},
        {key: 'jokingly', types: [], args: ['-j'], defaultValues: {type: 'flag', count: 1}
      }],
      defaultValues: {jokingly: 'foo'}
    }
  ]
 
  const {errs, args} = setDefaultValues({opts})
 
  const expArgs = {
    _: [],
    ask: {
      jokingly: 'foo'
    }
  }
 
  const expErrs = [
    invalidDefaultValues({defaultValues: [], option: name}),
    invalidDefaultValues({defaultValues: 'unknown', option: birthday})
  ]
 
  expect(args).toStrictEqual(expArgs)
  expect(errs).toStrictEqual(expErrs)
})
 
test('setDefaultValues works if opts is undefined', () => {
  const obj = {}
 
  const {args} = setDefaultValues(obj)
 
  expect(args).toStrictEqual({_: []})
})
 
test('setDefaultValues works if input is undefined', () => {
  const {args} = setDefaultValues()
 
  expect(args).toStrictEqual({_: []})
})
 
test('setDefaultValues passes on errors', () => {
  const ERRS = ['foo']
 
  const {errs} = setDefaultValues({errs: ERRS})
 
  expect(errs).toStrictEqual(ERRS)
})