File tree Expand file tree Collapse file tree 2 files changed +37
-3
lines changed Expand file tree Collapse file tree 2 files changed +37
-3
lines changed Original file line number Diff line number Diff line change @@ -13,13 +13,18 @@ function loadServiceEventHandlers<
13
13
params : Ref < Params | undefined | null > ,
14
14
data : Ref < M [ ] > ,
15
15
) : ( ) => void {
16
- const onCreated = ( item : M ) : void => {
16
+ const onCreated = ( createdItem : M ) : void => {
17
17
// ignore items not matching the query or when no params are set
18
- if ( ! params . value || ! sift ( params . value . query ) ( item ) ) {
18
+ if ( ! params . value || ! sift ( params . value . query ) ( createdItem ) ) {
19
19
return ;
20
20
}
21
21
22
- data . value = [ ...data . value , item ] ;
22
+ // ignore items that already exist
23
+ if ( data . value . find ( ( item ) => getId ( createdItem ) === getId ( item ) ) !== undefined ) {
24
+ return ;
25
+ }
26
+
27
+ data . value = [ ...data . value , createdItem ] ;
23
28
} ;
24
29
25
30
const onRemoved = ( item : M ) : void => {
Original file line number Diff line number Diff line change @@ -427,6 +427,35 @@ describe('Find composition', () => {
427
427
expect ( findComposition && findComposition . data . value ) . not . toContainEqual ( additionalTestModel ) ;
428
428
} ) ;
429
429
430
+ it ( 'should ignore "create" events when item already exists' , async ( ) => {
431
+ expect . assertions ( 2 ) ;
432
+
433
+ // given
434
+ const emitter = eventHelper ( ) ;
435
+ const feathersMock = {
436
+ service : ( ) => ( {
437
+ find : jest . fn ( ( ) => [ additionalTestModel ] ) ,
438
+ on : emitter . on ,
439
+ off : jest . fn ( ) ,
440
+ } ) ,
441
+ on : jest . fn ( ) ,
442
+ off : jest . fn ( ) ,
443
+ } as unknown as Application ;
444
+ const useFind = useFindOriginal ( feathersMock ) ;
445
+ let findComposition = null as UseFind < TestModel > | null ;
446
+ mountComposition ( ( ) => {
447
+ findComposition = useFind ( 'testModels' ) ;
448
+ } ) ;
449
+ await nextTick ( ) ;
450
+
451
+ // when
452
+ emitter . emit ( 'created' , additionalTestModel ) ;
453
+
454
+ // then
455
+ expect ( findComposition ) . toBeTruthy ( ) ;
456
+ expect ( findComposition && findComposition . data . value ) . toHaveLength ( 1 ) ;
457
+ } ) ;
458
+
430
459
it ( 'should listen to "patch" events' , async ( ) => {
431
460
expect . assertions ( 2 ) ;
432
461
You can’t perform that action at this time.
0 commit comments