@@ -2,37 +2,60 @@ import { from, toArray } from 'rxjs';
22import { tapOnce , tapOnceOnFirstTruthy } from './tap-once' ;
33
44describe ( tapOnce . name , ( ) => {
5- it ( 'should execute the function only once at the specified index' , ( done ) => {
5+ it ( 'should execute the function only once at the default index 0 ' , ( done ) => {
66 const tapFn = jest . fn ( ) ;
77 const in$ = from ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
8- const out$ = in$ . pipe ( tapOnce ( tapFn , 2 ) ) ;
8+ const out$ = in$ . pipe ( tapOnce ( tapFn ) ) ;
99
1010 out$ . pipe ( toArray ( ) ) . subscribe ( ( r ) => {
1111 expect ( r ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
1212 expect ( tapFn ) . toHaveBeenCalledTimes ( 1 ) ;
13- expect ( tapFn ) . toHaveBeenCalledWith ( 3 ) ;
13+ expect ( tapFn ) . toHaveBeenCalledWith ( 1 ) ;
1414 done ( ) ;
1515 } ) ;
1616 } ) ;
1717
18- it ( 'should execute the function only once at the default index 0 ' , ( done ) => {
18+ it ( 'should execute the function only once at the specified index' , ( done ) => {
1919 const tapFn = jest . fn ( ) ;
2020 const in$ = from ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
21- const out$ = in$ . pipe ( tapOnce ( tapFn ) ) ;
21+ const out$ = in$ . pipe ( tapOnce ( tapFn , 2 ) ) ;
2222
2323 out$ . pipe ( toArray ( ) ) . subscribe ( ( r ) => {
2424 expect ( r ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
2525 expect ( tapFn ) . toHaveBeenCalledTimes ( 1 ) ;
26- expect ( tapFn ) . toHaveBeenCalledWith ( 1 ) ;
26+ expect ( tapFn ) . toHaveBeenCalledWith ( 3 ) ;
2727 done ( ) ;
2828 } ) ;
2929 } ) ;
3030
3131 it ( 'should throw an error if tapIndex is negative' , ( ) => {
32- expect ( ( ) => tapOnce ( ( ) => { } , - 1 ) ) . toThrow (
32+ expect ( ( ) => tapOnce ( ( ) => void 0 , - 1 ) ) . toThrow (
3333 'tapIndex must be a non-negative integer' ,
3434 ) ;
3535 } ) ;
36+
37+ it ( 'should not share state across multiple subscriptions' , ( done ) => {
38+ const tapFn = jest . fn ( ) ;
39+ const in$ = from ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
40+ const out$ = in$ . pipe ( tapOnce ( tapFn , 1 ) ) ;
41+
42+ // First subscription
43+ out$ . pipe ( toArray ( ) ) . subscribe ( ( r1 ) => {
44+ expect ( r1 ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
45+ expect ( tapFn ) . toHaveBeenCalledTimes ( 1 ) ;
46+ expect ( tapFn ) . toHaveBeenCalledWith ( 2 ) ;
47+
48+ tapFn . mockClear ( ) ;
49+
50+ // Second subscription - should also trigger the tap function
51+ out$ . pipe ( toArray ( ) ) . subscribe ( ( r2 ) => {
52+ expect ( r2 ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
53+ expect ( tapFn ) . toHaveBeenCalledTimes ( 1 ) ;
54+ expect ( tapFn ) . toHaveBeenCalledWith ( 2 ) ;
55+ done ( ) ;
56+ } ) ;
57+ } ) ;
58+ } ) ;
3659} ) ;
3760
3861describe ( tapOnceOnFirstTruthy . name , ( ) => {
@@ -73,4 +96,27 @@ describe(tapOnceOnFirstTruthy.name, () => {
7396 done ( ) ;
7497 } ) ;
7598 } ) ;
99+
100+ it ( 'should not share state across multiple subscriptions' , ( done ) => {
101+ const tapFn = jest . fn ( ) ;
102+ const in$ = from ( [ 0 , null , false , 3 , 4 , 5 ] ) ;
103+ const out$ = in$ . pipe ( tapOnceOnFirstTruthy ( tapFn ) ) ;
104+
105+ // First subscription
106+ out$ . pipe ( toArray ( ) ) . subscribe ( ( r1 ) => {
107+ expect ( r1 ) . toEqual ( [ 0 , null , false , 3 , 4 , 5 ] ) ;
108+ expect ( tapFn ) . toHaveBeenCalledTimes ( 1 ) ;
109+ expect ( tapFn ) . toHaveBeenCalledWith ( 3 ) ;
110+
111+ tapFn . mockClear ( ) ;
112+
113+ // Second subscription - should also trigger the tap function
114+ out$ . pipe ( toArray ( ) ) . subscribe ( ( r2 ) => {
115+ expect ( r2 ) . toEqual ( [ 0 , null , false , 3 , 4 , 5 ] ) ;
116+ expect ( tapFn ) . toHaveBeenCalledTimes ( 1 ) ;
117+ expect ( tapFn ) . toHaveBeenCalledWith ( 3 ) ;
118+ done ( ) ;
119+ } ) ;
120+ } ) ;
121+ } ) ;
76122} ) ;
0 commit comments