Skip to content

Commit

Permalink
Support nested-only testValue defs, with explicit undefined opt-out
Browse files Browse the repository at this point in the history
  • Loading branch information
zchsh committed Mar 6, 2021
1 parent 3a407c2 commit 4c41c38
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
17 changes: 12 additions & 5 deletions testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ 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 build a testValue for the object
const hasBaseValue = typeof memo[name] === 'object'
const hasValueSet = prop.hasOwnProperty('testValue')
// in most cases, we'll recurse and build the testValue
if (hasBaseValue || !hasValueSet) {
// if the base value was not set, we need to initialize it
if (typeof memo[name] === 'undefined') memo[name] = {}
// we override the base testValue with nested testValues
Object.assign(memo[name], getTestValues(prop.properties))
} else if (typeof memo[name] === 'undefined') {
// do nothing, there is no test value and thats ok
} else if (hasValueSet && typeof prop.testValue === 'undefined') {
// in some cases, prop authors explicitly set testValue: undefined.
// in these cases, we should not recurse and construct a testValue
} else {
// in all other cases we should throw an error
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
5 changes: 3 additions & 2 deletions testing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test('nested values with an array containing a monotype', () => {
expect(res.base[0]).toBe('nested-string')
})

test('no test values used at all', () => {
test('no test values used at all, when explicitly passing undefined', () => {
const res = getTestValues({
base: {
properties: {
Expand All @@ -67,6 +67,7 @@ test('no test values used at all', () => {
},
},
},
testValue: undefined,
},
})
expect(res).toEqual({})
Expand Down Expand Up @@ -140,5 +141,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 4c41c38

Please sign in to comment.