@@ -3,6 +3,7 @@ import { expect } from 'chai';
33import { act , createRenderer , screen } from '@mui/internal-test-utils' ;
44import { ReactStore } from './ReactStore' ;
55import { useRefWithInit } from '../useRefWithInit' ;
6+ import { createSelector } from './createSelector' ;
67
78type TestState = { value : number ; label : string } ;
89
@@ -238,6 +239,8 @@ describe('ReactStore', () => {
238239 parent : ( state : ChildState ) => state . parent ,
239240 } ;
240241
242+ const localCountSelector = createSelector ( ( state : ChildState ) => state . count ) ;
243+
241244 const parentStore = new ReactStore < ParentState , Record < string , never > , typeof parentSelectors > (
242245 { count : 0 } ,
243246 undefined ,
@@ -274,8 +277,8 @@ describe('ReactStore', () => {
274277 store . state . parent ?. set ( 'count' , newCount ) ;
275278 } ;
276279
277- childStore . observeState ( 'parent' , onParentUpdated ) ;
278- childStore . observeState ( 'count' , onCountUpdated ) ;
280+ childStore . observeSelector ( 'parent' , onParentUpdated ) ;
281+ childStore . observeSelector ( localCountSelector , onCountUpdated ) ;
279282
280283 function Test ( ) {
281284 const count = childStore . useState ( 'count' ) ;
@@ -314,158 +317,6 @@ describe('ReactStore', () => {
314317 expect ( childStore . select ( 'count' ) ) . to . equal ( 15 ) ;
315318 expect ( output . textContent ) . to . equal ( '15' ) ;
316319 } ) ;
317-
318- describe ( 'observeState' , ( ) => {
319- it ( 'calls listener immediately with current value on subscription' , ( ) => {
320- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
321- const calls : Array < { newValue : number ; oldValue : number } > = [ ] ;
322-
323- store . observeState ( 'value' , ( newValue , oldValue ) => {
324- calls . push ( { newValue, oldValue } ) ;
325- } ) ;
326-
327- expect ( calls ) . to . have . lengthOf ( 1 ) ;
328- expect ( calls [ 0 ] ) . to . deep . equal ( { newValue : 5 , oldValue : 5 } ) ;
329- } ) ;
330-
331- it ( 'calls listener when observed state key changes' , ( ) => {
332- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
333- const calls : Array < { newValue : number ; oldValue : number } > = [ ] ;
334-
335- store . observeState ( 'value' , ( newValue , oldValue ) => {
336- calls . push ( { newValue, oldValue } ) ;
337- } ) ;
338-
339- store . set ( 'value' , 10 ) ;
340- store . set ( 'value' , 15 ) ;
341-
342- expect ( calls ) . to . have . lengthOf ( 3 ) ;
343- expect ( calls [ 1 ] ) . to . deep . equal ( { newValue : 10 , oldValue : 5 } ) ;
344- expect ( calls [ 2 ] ) . to . deep . equal ( { newValue : 15 , oldValue : 10 } ) ;
345- } ) ;
346-
347- it ( 'does not call listener when non-observed state keys change' , ( ) => {
348- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
349- const calls : Array < { newValue : number ; oldValue : number } > = [ ] ;
350-
351- store . observeState ( 'value' , ( newValue , oldValue ) => {
352- calls . push ( { newValue, oldValue } ) ;
353- } ) ;
354-
355- store . set ( 'label' , 'updated' ) ;
356-
357- expect ( calls ) . to . have . lengthOf ( 1 ) ; // Only initial call
358- } ) ;
359-
360- it ( 'does not call listener when value is set to same value (Object.is comparison)' , ( ) => {
361- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
362- const calls : Array < { newValue : number ; oldValue : number } > = [ ] ;
363-
364- store . observeState ( 'value' , ( newValue , oldValue ) => {
365- calls . push ( { newValue, oldValue } ) ;
366- } ) ;
367-
368- store . set ( 'value' , 5 ) ;
369-
370- expect ( calls ) . to . have . lengthOf ( 1 ) ; // Only initial call
371- } ) ;
372-
373- it ( 'provides the store instance to the listener' , ( ) => {
374- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
375- let receivedStore ! : ReactStore < TestState > ;
376-
377- store . observeState ( 'value' , ( _ , __ , storeArg ) => {
378- receivedStore = storeArg ;
379- } ) ;
380-
381- expect ( receivedStore ) . to . equal ( store ) ;
382- } ) ;
383-
384- it ( 'returns an unsubscribe function that stops observing' , ( ) => {
385- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
386- const calls : Array < { newValue : number ; oldValue : number } > = [ ] ;
387-
388- const unsubscribe = store . observeState ( 'value' , ( newValue , oldValue ) => {
389- calls . push ( { newValue, oldValue } ) ;
390- } ) ;
391-
392- store . set ( 'value' , 10 ) ;
393- expect ( calls ) . to . have . lengthOf ( 2 ) ;
394-
395- unsubscribe ( ) ;
396-
397- store . set ( 'value' , 15 ) ;
398- expect ( calls ) . to . have . lengthOf ( 2 ) ; // No new calls after unsubscribe
399- } ) ;
400-
401- it ( 'supports multiple observers on the same key' , ( ) => {
402- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
403- const calls1 : number [ ] = [ ] ;
404- const calls2 : number [ ] = [ ] ;
405-
406- store . observeState ( 'value' , ( newValue ) => {
407- calls1 . push ( newValue ) ;
408- } ) ;
409-
410- store . observeState ( 'value' , ( newValue ) => {
411- calls2 . push ( newValue ) ;
412- } ) ;
413-
414- store . set ( 'value' , 10 ) ;
415-
416- expect ( calls1 ) . to . deep . equal ( [ 5 , 10 ] ) ;
417- expect ( calls2 ) . to . deep . equal ( [ 5 , 10 ] ) ;
418- } ) ;
419-
420- it ( 'supports observers on different keys' , ( ) => {
421- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
422- const valueCalls : number [ ] = [ ] ;
423- const labelCalls : string [ ] = [ ] ;
424-
425- store . observeState ( 'value' , ( newValue ) => {
426- valueCalls . push ( newValue ) ;
427- } ) ;
428-
429- store . observeState ( 'label' , ( newValue ) => {
430- labelCalls . push ( newValue ) ;
431- } ) ;
432-
433- store . set ( 'value' , 10 ) ;
434- store . set ( 'label' , 'updated' ) ;
435-
436- expect ( valueCalls ) . to . deep . equal ( [ 5 , 10 ] ) ;
437- expect ( labelCalls ) . to . deep . equal ( [ 'initial' , 'updated' ] ) ;
438- } ) ;
439-
440- it ( 'tracks changes made through update method' , ( ) => {
441- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
442- const calls : Array < { newValue : number ; oldValue : number } > = [ ] ;
443-
444- store . observeState ( 'value' , ( newValue , oldValue ) => {
445- calls . push ( { newValue, oldValue } ) ;
446- } ) ;
447-
448- store . update ( { value : 20 , label : 'updated' } ) ;
449-
450- expect ( calls ) . to . have . lengthOf ( 2 ) ;
451- expect ( calls [ 1 ] ) . to . deep . equal ( { newValue : 20 , oldValue : 5 } ) ;
452- } ) ;
453-
454- it ( 'tracks changes made through setState method' , ( ) => {
455- const store = new ReactStore < TestState > ( { value : 5 , label : 'initial' } ) ;
456- const calls : Array < { newValue : number ; oldValue : number } > = [ ] ;
457-
458- store . observeState ( 'value' , ( newValue , oldValue ) => {
459- calls . push ( { newValue, oldValue } ) ;
460- } ) ;
461-
462- store . setState ( { value : 25 , label : 'new' } ) ;
463-
464- expect ( calls ) . to . have . lengthOf ( 2 ) ;
465- expect ( calls [ 1 ] ) . to . deep . equal ( { newValue : 25 , oldValue : 5 } ) ;
466- } ) ;
467- } ) ;
468-
469320 describe ( 'observeSelector' , ( ) => {
470321 type CounterState = { count : number ; multiplier : number } ;
471322 const selectors = {
0 commit comments