Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for testValue definitions in nested props only #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' } })
})