11import { readable , writable } from '@amadeus-it-group/tansu' ;
2- import { ChangeDetectionStrategy , Component , NgZone , effect } from '@angular/core' ;
2+ import { ChangeDetectionStrategy , Component , effect } from '@angular/core' ;
33import { TestBed } from '@angular/core/testing' ;
4- import { beforeEach , describe , expect , it } from 'vitest' ;
4+ import { describe , expect , it } from 'vitest' ;
55import { toAngularSignal , toAngularWritableSignal } from './stores' ;
66
77describe ( 'toAngularSignals' , ( ) => {
8- let log : string [ ] = [ ] ;
9-
10- beforeEach ( ( ) => {
11- log = [ ] ;
12- } ) ;
13-
14- const createZoneCheckFn =
15- < T extends any [ ] , R > ( name : string , fn : ( ...args : T ) => R ) =>
16- ( ...args : T ) : R => {
17- log . push ( `begin ${ name } , ngZone = ${ NgZone . isInAngularZone ( ) } ` ) ;
18- try {
19- return fn ( ...args ) ;
20- } finally {
21- log . push ( `end ${ name } , ngZone = ${ NgZone . isInAngularZone ( ) } ` ) ;
22- }
23- } ;
24-
258 it ( '[toAngularSignal] works synchronously' , ( ) => {
269 const tansuStore = writable ( 1 ) ;
2710 const signal = TestBed . runInInjectionContext ( ( ) => toAngularSignal ( tansuStore ) ) ;
@@ -33,51 +16,38 @@ describe('toAngularSignals', () => {
3316 expect ( signal ( ) ) . toBe ( 2 ) ; // no change as the subscription was ended
3417 } ) ;
3518
19+ it ( '[toAngularSignal] subscribes and unsubscribes' , ( ) => {
20+ const log : string [ ] = [ ] ;
21+ const tansuStore = readable ( 0 as number , {
22+ onUse : ( set ) => {
23+ log . push ( 'onUse' ) ;
24+ set ( 1 ) ;
25+ return ( ) => {
26+ log . push ( 'destroy' ) ;
27+ } ;
28+ } ,
29+ } ) ;
30+ const signal = TestBed . runInInjectionContext ( ( ) => toAngularSignal ( tansuStore ) ) ;
31+ expect ( signal ( ) ) . toBe ( 1 ) ;
32+ TestBed . resetTestingModule ( ) ;
33+ expect ( log ) . toStrictEqual ( [ 'onUse' , 'destroy' ] ) ;
34+ } ) ;
35+
3636 it ( '[toAngularWritableSignal] works synchronously' , ( ) => {
3737 const tansuStore = writable ( 1 ) ;
3838 const signal = TestBed . runInInjectionContext ( ( ) => toAngularWritableSignal ( tansuStore ) ) ;
3939 expect ( signal ( ) ) . toBe ( 1 ) ;
4040 tansuStore . set ( 2 ) ;
4141 expect ( signal ( ) ) . toBe ( 2 ) ;
42- const ngZone = TestBed . inject ( NgZone ) ;
43- ngZone . run ( ( ) => {
44- signal . set ( 3 ) ;
45- } ) ;
42+ signal . set ( 3 ) ;
4643 expect ( tansuStore . get ( ) ) . toBe ( 3 ) ;
47- ngZone . run ( ( ) => {
48- signal . set ( 4 ) ;
49- } ) ;
44+ signal . set ( 4 ) ;
5045 expect ( tansuStore . get ( ) ) . toBe ( 4 ) ;
5146 TestBed . resetTestingModule ( ) ; // this ends the subscription
5247 tansuStore . set ( 5 ) ;
5348 expect ( signal ( ) ) . toBe ( 4 ) ; // no change as the subscription was ended
5449 } ) ;
5550
56- it ( '[toAngularSignal] subscribes and unsubscribes outside Angular zone' , ( ) => {
57- const ngZone = TestBed . inject ( NgZone ) ;
58- const tansuStore = readable ( 0 as number , {
59- onUse : createZoneCheckFn ( 'onUse' , ( set ) => {
60- set ( 1 ) ;
61- return createZoneCheckFn ( 'destroy' , ( ) => { } ) ;
62- } ) ,
63- } ) ;
64- ngZone . run (
65- createZoneCheckFn ( 'ngZone.run' , ( ) => {
66- const signal = TestBed . runInInjectionContext ( ( ) => toAngularSignal ( tansuStore ) ) ;
67- expect ( signal ( ) ) . toBe ( 1 ) ;
68- TestBed . resetTestingModule ( ) ;
69- } ) ,
70- ) ;
71- expect ( log ) . toStrictEqual ( [
72- 'begin ngZone.run, ngZone = true' ,
73- 'begin onUse, ngZone = false' ,
74- 'end onUse, ngZone = false' ,
75- 'begin destroy, ngZone = false' ,
76- 'end destroy, ngZone = false' ,
77- 'end ngZone.run, ngZone = true' ,
78- ] ) ;
79- } ) ;
80-
8151 @Component ( {
8252 selector : '[auMyTestWithoutEffect]' ,
8353 changeDetection : ChangeDetectionStrategy . OnPush ,
@@ -107,50 +77,19 @@ describe('toAngularSignals', () => {
10777 }
10878
10979 for ( const MyComponent of [ MyTestWithEffectComponent , MyTestWithoutEffectComponent ] ) {
110- it ( `[toAngularSignal] works in ${ MyComponent . name } (inside Angular zone)` , async ( ) => {
111- TestBed . configureTestingModule ( {
112- imports : [ MyComponent ] ,
113- } ) ;
114- const fixture = TestBed . createComponent ( MyComponent ) ;
115- fixture . autoDetectChanges ( ) ;
116- await fixture . whenStable ( ) ;
117- expect ( fixture . nativeElement . textContent ) . toBe ( '1' ) ;
118- if ( MyComponent === MyTestWithEffectComponent ) {
119- // eslint-disable-next-line vitest/no-conditional-expect
120- expect ( fixture . componentInstance . changes ) . toStrictEqual ( [ 1 ] ) ;
121- }
122- const zone = TestBed . inject ( NgZone ) ;
123- expect ( NgZone . isInAngularZone ( ) ) . toBeFalsy ( ) ;
124- zone . run ( ( ) => {
125- expect ( NgZone . isInAngularZone ( ) ) . toBeTruthy ( ) ;
126- fixture . componentInstance . myStore . set ( 2 ) ;
127- fixture . componentInstance . myStore . set ( 3 ) ;
128- } ) ;
129- await fixture . whenStable ( ) ;
130- expect ( fixture . nativeElement . textContent ) . toBe ( '3' ) ;
131- if ( MyComponent === MyTestWithEffectComponent ) {
132- // eslint-disable-next-line vitest/no-conditional-expect
133- expect ( fixture . componentInstance . changes ) . toStrictEqual ( [ 1 , 3 ] ) ;
134- }
135- fixture . destroy ( ) ;
136- } ) ;
137-
138- it ( `[toAngularSignal] works in ${ MyComponent . name } (outside Angular zone)` , async ( ) => {
80+ it ( `[toAngularSignal] works in ${ MyComponent . name } ` , async ( ) => {
13981 const fixture = TestBed . createComponent ( MyComponent ) ;
14082 fixture . autoDetectChanges ( ) ;
14183 await fixture . whenStable ( ) ;
14284 expect ( fixture . nativeElement . textContent ) . toBe ( '1' ) ;
14385 if ( MyComponent === MyTestWithEffectComponent ) {
144- // eslint-disable-next-line vitest/no-conditional-expect
14586 expect ( fixture . componentInstance . changes ) . toStrictEqual ( [ 1 ] ) ;
14687 }
147- expect ( NgZone . isInAngularZone ( ) ) . toBeFalsy ( ) ;
14888 fixture . componentInstance . myStore . set ( 2 ) ;
14989 fixture . componentInstance . myStore . set ( 3 ) ;
15090 await fixture . whenStable ( ) ;
15191 expect ( fixture . nativeElement . textContent ) . toBe ( '3' ) ;
15292 if ( MyComponent === MyTestWithEffectComponent ) {
153- // eslint-disable-next-line vitest/no-conditional-expect
15493 expect ( fixture . componentInstance . changes ) . toStrictEqual ( [ 1 , 3 ] ) ;
15594 }
15695 fixture . destroy ( ) ;
0 commit comments