@@ -900,7 +900,12 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
900
900
901
901
it ( 'selector can throw on update' , async ( ) => {
902
902
const store = createExternalStore ( { a : 'a' } ) ;
903
- const selector = state => state . a . toUpperCase ( ) ;
903
+ const selector = state => {
904
+ if ( typeof state . a !== 'string' ) {
905
+ throw new TypeError ( 'Malformed state' ) ;
906
+ }
907
+ return state . a . toUpperCase ( ) ;
908
+ } ;
904
909
905
910
function App ( ) {
906
911
const a = useSyncExternalStoreWithSelector (
@@ -927,15 +932,18 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
927
932
await act ( ( ) => {
928
933
store . set ( { } ) ;
929
934
} ) ;
930
- expect ( container . textContent ) . toEqual (
931
- "Cannot read property 'toUpperCase' of undefined" ,
932
- ) ;
935
+ expect ( container . textContent ) . toEqual ( 'Malformed state' ) ;
933
936
} ) ;
934
937
935
938
it ( 'isEqual can throw on update' , async ( ) => {
936
939
const store = createExternalStore ( { a : 'A' } ) ;
937
940
const selector = state => state . a ;
938
- const isEqual = ( left , right ) => left . a . trim ( ) === right . a . trim ( ) ;
941
+ const isEqual = ( left , right ) => {
942
+ if ( typeof left . a !== 'string' || typeof right . a !== 'string' ) {
943
+ throw new TypeError ( 'Malformed state' ) ;
944
+ }
945
+ return left . a . trim ( ) === right . a . trim ( ) ;
946
+ } ;
939
947
940
948
function App ( ) {
941
949
const a = useSyncExternalStoreWithSelector (
@@ -963,9 +971,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
963
971
await act ( ( ) => {
964
972
store . set ( { } ) ;
965
973
} ) ;
966
- expect ( container . textContent ) . toEqual (
967
- "Cannot read property 'trim' of undefined" ,
968
- ) ;
974
+ expect ( container . textContent ) . toEqual ( 'Malformed state' ) ;
969
975
} ) ;
970
976
} ) ;
971
977
} ) ;
0 commit comments