@@ -2,6 +2,7 @@ import * as React from 'react';
22import { Bonn , Field , FieldProps , FormProps , Listener } from '../src/bonn' ;
33import { Form } from '../src/business/form' ;
44import { mount } from 'enzyme' ;
5+ import { FieldListenerRepositoryImpl } from '../src/business/callback_repository' ;
56
67describe ( 'Bonn' , function ( ) {
78
@@ -288,8 +289,56 @@ describe('Bonn', function () {
288289
289290 /* Then */
290291 expect ( result . text ( ) ) . not . toContain ( 'Foutje' ) ;
291- } )
292+ } ) ;
293+
294+ it ( 'should unsubscribe its listeners when component is unmounted' , function ( ) {
295+ /* Given */
296+ const fieldListenerRepositoryImpl = new FieldListenerRepositoryImpl ( ) ;
297+ const form = new Form ( fieldListenerRepositoryImpl ) ;
298+
299+ class MyField extends React . Component < FieldProps , { } > {
300+ render ( ) {
301+ return < div >
302+ < input name = "field" />
303+ { this . props . validationError }
304+ </ div >
305+ }
306+ }
307+ const Component = Field < { } > ( MyField ) ;
308+
309+ /* When */
310+ const result = mount ( < Component name = "field" form = { form } /> ) ;
311+ result . unmount ( ) ;
292312
313+ /* Then */
314+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'field' ) ) . not . toBeUndefined ( ) ;
315+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'field' ) . length ) . toBe ( 0 ) ;
316+ } ) ;
317+
318+ it ( 'should leave listeners that are listening to the same field' , function ( ) {
319+ /* Given */
320+ const fieldListenerRepositoryImpl = new FieldListenerRepositoryImpl ( ) ;
321+ const form = new Form ( fieldListenerRepositoryImpl ) ;
322+
323+ class MyField extends React . Component < FieldProps , { } > {
324+ render ( ) {
325+ return < div >
326+ < input name = "field" />
327+ { this . props . validationError }
328+ </ div >
329+ }
330+ }
331+ const Component = Field < { } > ( MyField ) ;
332+
333+ /* When */
334+ const first = mount ( < Component name = "field" form = { form } /> ) ;
335+ first . unmount ( ) ;
336+ const second = mount ( < Component name = "field" form = { form } /> ) ;
337+
338+ /* Then */
339+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'field' ) ) . not . toBeUndefined ( ) ;
340+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'field' ) . length ) . toBe ( 1 ) ;
341+ } ) ;
293342 } ) ;
294343
295344 describe ( 'Listener' , function ( ) {
@@ -349,6 +398,54 @@ describe('Bonn', function () {
349398 expect ( numRendersForComponent ) . toBe ( 2 ) ;
350399 } ) ;
351400
401+ it ( 'should unsubscribe its listeners when component is unmounted' , function ( ) {
402+ /* Given */
403+ const fieldListenerRepositoryImpl = new FieldListenerRepositoryImpl ( ) ;
404+ const form = new Form ( fieldListenerRepositoryImpl ) ;
405+
406+ class MyComponent extends React . Component < FormProps , { } > {
407+ render ( ) {
408+ return < div > </ div >
409+ }
410+ }
411+
412+ /* When */
413+ const Component = Listener < { } > ( MyComponent , [ 'field' ] ) ;
414+
415+ /* When */
416+ const result = mount ( < Component form = { form } /> ) ;
417+ result . unmount ( ) ;
418+
419+ /* Then */
420+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'field' ) ) . not . toBeUndefined ( ) ;
421+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'field' ) . length ) . toBe ( 0 ) ;
422+ } ) ;
423+
424+ it ( 'should unsubscribe multiple listeners when component is unmounted' , function ( ) {
425+ /* Given */
426+ const fieldListenerRepositoryImpl = new FieldListenerRepositoryImpl ( ) ;
427+ const form = new Form ( fieldListenerRepositoryImpl ) ;
428+
429+ class MyComponent extends React . Component < FormProps , { } > {
430+ render ( ) {
431+ return < div > </ div >
432+ }
433+ }
434+
435+ /* When */
436+ const Component = Listener < { } > ( MyComponent , [ 'field' , 'other' ] ) ;
437+
438+ /* When */
439+ const result = mount ( < Component form = { form } /> ) ;
440+ result . unmount ( ) ;
441+
442+ /* Then */
443+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'field' ) ) . not . toBeUndefined ( ) ;
444+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'other' ) ) . not . toBeUndefined ( ) ;
445+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'field' ) . length ) . toBe ( 0 ) ;
446+ expect ( fieldListenerRepositoryImpl . getFieldListenersForFieldName ( 'other' ) . length ) . toBe ( 0 ) ;
447+ } ) ;
448+
352449 } ) ;
353450
354451} ) ;
0 commit comments