1
1
import { it , describe , expect , vi } from 'vitest' ;
2
2
import { DataBind , DataComputed , DataEffect } from '@studiometa/ui' ;
3
- import { destroy , h , mount } from '#test-utils' ;
3
+ import { destroy , hConnected as h , mount } from '#test-utils' ;
4
4
5
5
describe ( 'The DataBind component' , ( ) => {
6
6
it ( 'should set the textContent of the root element' , ( ) => {
@@ -151,8 +151,12 @@ describe('The DataBind component', () => {
151
151
it ( 'should dispatch value to other instances' , async ( ) => {
152
152
const instance1 = new DataBind ( h ( 'div' , { dataOptionName : 'a' } , [ 'foo' ] ) ) ;
153
153
const instance2 = new DataBind ( h ( 'div' , { dataOptionName : 'a' } , [ 'foo' ] ) ) ;
154
- const instance3 = new DataComputed ( h ( 'div' , { dataOptionName : 'a' , dataOptionCompute : 'value + value' } , [ 'foofoo' ] ) ) ;
155
- const instance4 = new DataEffect ( h ( 'div' , { dataOptionName : 'a' , dataOptionEffect : 'target.id = value' , id : 'foo' } ) ) ;
154
+ const instance3 = new DataComputed (
155
+ h ( 'div' , { dataOptionName : 'a' , dataOptionCompute : 'value + value' } , [ 'foofoo' ] ) ,
156
+ ) ;
157
+ const instance4 = new DataEffect (
158
+ h ( 'div' , { dataOptionName : 'a' , dataOptionEffect : 'target.id = value' , id : 'foo' } ) ,
159
+ ) ;
156
160
157
161
await mount ( instance1 , instance2 , instance3 , instance4 ) ;
158
162
@@ -166,4 +170,38 @@ describe('The DataBind component', () => {
166
170
expect ( instance3 . value ) . toBe ( 'barbar' ) ;
167
171
expect ( instance4 . $el . id ) . toBe ( 'bar' ) ;
168
172
} ) ;
173
+
174
+ it ( 'should forget related instances not in the DOM anymore' , async ( ) => {
175
+ const fragment = new Document ( ) ;
176
+ const inputA = h ( 'input' , {
177
+ type : 'checkbox' ,
178
+ value : 'foo' ,
179
+ checked : '' ,
180
+ dataOptionName : 'checkbox[]' ,
181
+ } ) ;
182
+ const inputB = h ( 'input' , {
183
+ type : 'checkbox' ,
184
+ value : 'bar' ,
185
+ checked : '' ,
186
+ dataOptionName : 'checkbox[]' ,
187
+ } ) ;
188
+ fragment . append ( inputA , inputB ) ;
189
+
190
+ const instanceA = new DataBind ( inputA ) ;
191
+ const instanceB = new DataBind ( inputB ) ;
192
+
193
+ await mount ( instanceA , instanceB ) ;
194
+
195
+ expect ( inputA . isConnected ) . toBe ( true ) ;
196
+ expect ( inputB . isConnected ) . toBe ( true ) ;
197
+ expect ( instanceA . value ) . toEqual ( [ 'foo' , 'bar' ] ) ;
198
+
199
+ inputB . replaceWith (
200
+ h ( 'input' , { type : 'checkbox' , value : 'bar' , dataOptionName : 'checkbox[]' } ) ,
201
+ ) ;
202
+
203
+ expect ( inputA . isConnected ) . toBe ( true ) ;
204
+ expect ( inputB . isConnected ) . toBe ( false ) ;
205
+ expect ( instanceA . value ) . toEqual ( [ 'foo' ] ) ;
206
+ } ) ;
169
207
} ) ;
0 commit comments