Skip to content

Commit bd7e15c

Browse files
Add coverage
1 parent f6e312c commit bd7e15c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

packages/snaps-sdk/src/internals/structs.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
any,
99
} from '@metamask/superstruct';
1010

11+
import { nullUnion } from './jsx';
1112
import {
1213
enumValue,
1314
literal,
@@ -18,6 +19,7 @@ import {
1819
import type { BoxElement } from '../jsx';
1920
import { Footer, Icon, Text, Button, Box } from '../jsx';
2021
import {
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

Comments
 (0)