Skip to content

Commit 0ca792d

Browse files
committed
Add support for testValue definitions in nested props only
1 parent 3a407c2 commit 0ca792d

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

testing.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ function getTestValues(propsSpec) {
3131
}
3232
}
3333
} else if (typeof prop.properties === 'object') {
34-
// if properties is an object, and the `testValue` is also an object,
35-
// we can recurse and assign the results directly
36-
if (typeof memo[name] === 'object') {
34+
// if properties is an object, we can recurse and assign the results directly
35+
const hasBaseValue = typeof memo[name] === 'object'
36+
const hasUndefinedBase = typeof memo[name] === 'undefined'
37+
if (hasBaseValue || hasUndefinedBase) {
38+
// the base object could have an undefined test value, if so, we set up an empty object
39+
if (hasUndefinedBase) memo[name] = {}
3740
Object.assign(memo[name], getTestValues(prop.properties))
38-
} else if (typeof memo[name] === 'undefined') {
39-
// do nothing, there is no test value and thats ok
4041
} else {
42+
// otherwise we should throw an error - testValues should match the properties type
4143
throw new Error(
4244
`The property "${name}" has a "properties" key, which requires an array or object type, but its "testValue" is ${JSON.stringify(
4345
memo[name]

testing.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test('no test values used at all', () => {
6969
},
7070
},
7171
})
72-
expect(res).toEqual({})
72+
expect(res).toEqual({ base: { array: [{}], object: {} } })
7373
})
7474

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

0 commit comments

Comments
 (0)