88 any ,
99} from '@metamask/superstruct' ;
1010
11+ import { nullUnion } from './jsx' ;
1112import {
1213 enumValue ,
1314 literal ,
@@ -18,6 +19,7 @@ import {
1819import type { BoxElement } from '../jsx' ;
1920import { Footer , Icon , Text , Button , Box } from '../jsx' ;
2021import {
22+ BoldStruct ,
2123 BoxStruct ,
2224 FieldStruct ,
2325 FooterStruct ,
@@ -67,6 +69,14 @@ describe('typedUnion', () => {
6769 BoxStruct ,
6870 typedUnion ( [ TextStruct , FieldStruct ] ) ,
6971 ] ) ;
72+ const stringUnion = nullUnion ( [
73+ string ( ) ,
74+ typedUnion ( [ TextStruct , BoldStruct ] ) ,
75+ ] ) ;
76+ const nonTypedObjectUnion = nullUnion ( [
77+ typedUnion ( [ TextStruct , BoldStruct ] ) ,
78+ object ( { type : literal ( 'bar' ) } ) ,
79+ ] ) ;
7080
7181 it ( 'validates strictly the part of the union that matches the type' , ( ) => {
7282 // @ts -expect-error Invalid props.
@@ -86,6 +96,23 @@ describe('typedUnion', () => {
8696 ) ;
8797 } ) ;
8898
99+ it ( 'validates when nested in a union including primitives' , ( ) => {
100+ // @ts -expect-error Invalid props.
101+ const result = validate ( Text ( { } ) , stringUnion ) ;
102+
103+ expect ( result [ 0 ] ?. message ) . toBe (
104+ 'Expected the value to satisfy a union of `string | union`, but received: [object Object]' ,
105+ ) ;
106+ } ) ;
107+
108+ it ( 'validates when nested in a union including non-typed objects' , ( ) => {
109+ const result = validate ( { type : 'abc' } , nonTypedObjectUnion ) ;
110+
111+ expect ( result [ 0 ] ?. message ) . toBe (
112+ 'Expected the value to satisfy a union of `union | object`, but received: [object Object]' ,
113+ ) ;
114+ } ) ;
115+
89116 it ( 'validates refined elements' , ( ) => {
90117 const refinedUnionStruct = typedUnion ( [ BoxStruct , FooterStruct ] ) ;
91118 const result = validate (
0 commit comments