Skip to content

Commit

Permalink
Add support for testValue definitions in nested props only
Browse files Browse the repository at this point in the history
  • Loading branch information
zchsh committed Mar 6, 2021
1 parent 3a407c2 commit 0ca792d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ function getTestValues(propsSpec) {
}
}
} else if (typeof prop.properties === 'object') {
// if properties is an object, and the `testValue` is also an object,
// we can recurse and assign the results directly
if (typeof memo[name] === 'object') {
// if properties is an object, we can recurse and assign the results directly
const hasBaseValue = typeof memo[name] === 'object'
const hasUndefinedBase = typeof memo[name] === 'undefined'
if (hasBaseValue || hasUndefinedBase) {
// the base object could have an undefined test value, if so, we set up an empty object
if (hasUndefinedBase) memo[name] = {}
Object.assign(memo[name], getTestValues(prop.properties))
} else if (typeof memo[name] === 'undefined') {
// do nothing, there is no test value and thats ok
} else {
// otherwise we should throw an error - testValues should match the properties type
throw new Error(
`The property "${name}" has a "properties" key, which requires an array or object type, but its "testValue" is ${JSON.stringify(
memo[name]
Expand Down
4 changes: 2 additions & 2 deletions testing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test('no test values used at all', () => {
},
},
})
expect(res).toEqual({})
expect(res).toEqual({ base: { array: [{}], object: {} } })
})

test('errors when a multi-type array has a test value at base and in properties', () => {
Expand Down Expand Up @@ -140,5 +140,5 @@ test('sub-properties define test values but base property is undefined', () => {
},
},
})
expect(res).toEqual({})
expect(res).toEqual({ base: { foo: 'nested' } })
})

0 comments on commit 0ca792d

Please sign in to comment.