@@ -569,4 +569,184 @@ describe('EntityCache', () => {
569
569
570
570
expect ( cache . gc ( ) ) . toEqual ( [ ] ) ;
571
571
} ) ;
572
+
573
+ it ( 'allows cache eviction' , ( ) => {
574
+ const { cache, query } = newBookAuthorCache ( ) ;
575
+
576
+ cache . writeQuery ( {
577
+ query,
578
+ data : {
579
+ book : {
580
+ __typename : "Book" ,
581
+ isbn : "031648637X" ,
582
+ title : "The Cuckoo's Calling" ,
583
+ author : {
584
+ __typename : "Author" ,
585
+ name : "Robert Galbraith" ,
586
+ } ,
587
+ } ,
588
+ } ,
589
+ } ) ;
590
+
591
+ expect ( cache . evict ( {
592
+ rootId : "Author:J.K. Rowling" ,
593
+ query,
594
+ } ) ) . toEqual ( {
595
+ success : false ,
596
+ } ) ;
597
+
598
+ const bookAuthorFragment = gql `
599
+ fragment BookAuthor on Book {
600
+ author {
601
+ name
602
+ }
603
+ }
604
+ ` ;
605
+
606
+ const fragmentResult = cache . readFragment ( {
607
+ id : "Book:031648637X" ,
608
+ fragment : bookAuthorFragment ,
609
+ } ) ;
610
+
611
+ expect ( fragmentResult ) . toEqual ( {
612
+ __typename : "Book" ,
613
+ author : {
614
+ __typename : "Author" ,
615
+ name : "Robert Galbraith" ,
616
+ } ,
617
+ } ) ;
618
+
619
+ cache . recordOptimisticTransaction ( proxy => {
620
+ proxy . writeFragment ( {
621
+ id : "Book:031648637X" ,
622
+ fragment : bookAuthorFragment ,
623
+ data : {
624
+ ...fragmentResult ,
625
+ author : {
626
+ __typename : "Author" ,
627
+ name : "J.K. Rowling" ,
628
+ } ,
629
+ } ,
630
+ } ) ;
631
+ } , "real name" ) ;
632
+
633
+ const snapshotWithBothNames = {
634
+ ROOT_QUERY : {
635
+ book : {
636
+ __ref : "Book:031648637X" ,
637
+ } ,
638
+ } ,
639
+ "Book:031648637X" : {
640
+ __typename : "Book" ,
641
+ author : {
642
+ __ref : "Author:J.K. Rowling" ,
643
+ } ,
644
+ title : "The Cuckoo's Calling" ,
645
+ } ,
646
+ "Author:Robert Galbraith" : {
647
+ __typename : "Author" ,
648
+ name : "Robert Galbraith" ,
649
+ } ,
650
+ "Author:J.K. Rowling" : {
651
+ __typename : "Author" ,
652
+ name : "J.K. Rowling" ,
653
+ } ,
654
+ } ;
655
+
656
+ expect ( cache . extract ( true ) ) . toEqual ( snapshotWithBothNames ) ;
657
+
658
+ expect ( cache . gc ( ) ) . toEqual ( [ ] ) ;
659
+
660
+ expect ( cache . retain ( 'Author:Robert Galbraith' ) ) . toBe ( 1 ) ;
661
+
662
+ expect ( cache . gc ( ) ) . toEqual ( [ ] ) ;
663
+
664
+ expect ( cache . evict ( {
665
+ rootId : 'Author:Robert Galbraith' ,
666
+ query,
667
+ } ) ) . toEqual ( {
668
+ success : true ,
669
+ } ) ;
670
+
671
+ expect ( cache . gc ( ) ) . toEqual ( [ ] ) ;
672
+
673
+ cache . removeOptimistic ( "real name" ) ;
674
+
675
+ expect ( cache . extract ( true ) ) . toEqual ( {
676
+ ROOT_QUERY : {
677
+ book : {
678
+ __ref : "Book:031648637X" ,
679
+ } ,
680
+ } ,
681
+ "Book:031648637X" : {
682
+ __typename : "Book" ,
683
+ author : {
684
+ __ref : "Author:Robert Galbraith" ,
685
+ } ,
686
+ title : "The Cuckoo's Calling" ,
687
+ } ,
688
+ "Author:Robert Galbraith" : {
689
+ __typename : "Author" ,
690
+ name : "Robert Galbraith" ,
691
+ } ,
692
+ } ) ;
693
+
694
+ cache . writeFragment ( {
695
+ id : "Book:031648637X" ,
696
+ fragment : bookAuthorFragment ,
697
+ data : {
698
+ ...fragmentResult ,
699
+ author : {
700
+ __typename : "Author" ,
701
+ name : "J.K. Rowling" ,
702
+ } ,
703
+ } ,
704
+ } ) ;
705
+
706
+ expect ( cache . extract ( true ) ) . toEqual ( snapshotWithBothNames ) ;
707
+
708
+ expect ( cache . retain ( "Author:Robert Galbraith" ) ) . toBe ( 2 ) ;
709
+
710
+ expect ( cache . gc ( ) ) . toEqual ( [ ] ) ;
711
+
712
+ expect ( cache . release ( "Author:Robert Galbraith" ) ) . toBe ( 1 ) ;
713
+ expect ( cache . release ( "Author:Robert Galbraith" ) ) . toBe ( 0 ) ;
714
+
715
+ expect ( cache . gc ( ) ) . toEqual ( [
716
+ "Author:Robert Galbraith" ,
717
+ ] ) ;
718
+
719
+ // If you're ever tempted to do this, you probably want to use cache.clear()
720
+ // instead, but evicting the ROOT_QUERY should work at least.
721
+ expect ( cache . evict ( {
722
+ rootId : "ROOT_QUERY" ,
723
+ query,
724
+ } ) ) . toEqual ( {
725
+ success : true ,
726
+ } ) ;
727
+
728
+ expect ( cache . extract ( true ) ) . toEqual ( {
729
+ "Book:031648637X" : {
730
+ __typename : "Book" ,
731
+ author : {
732
+ __ref : "Author:J.K. Rowling" ,
733
+ } ,
734
+ title : "The Cuckoo's Calling" ,
735
+ } ,
736
+ "Author:J.K. Rowling" : {
737
+ __typename : "Author" ,
738
+ name : "J.K. Rowling" ,
739
+ } ,
740
+ } ) ;
741
+
742
+ // The book has been retained a couple of times since we've written it
743
+ // directly, but J.K. has never been directly written.
744
+ expect ( cache . release ( "Book:031648637X" ) ) . toBe ( 1 ) ;
745
+ expect ( cache . release ( "Book:031648637X" ) ) . toBe ( 0 ) ;
746
+
747
+ expect ( cache . gc ( ) . sort ( ) ) . toEqual ( [
748
+ "Author:J.K. Rowling" ,
749
+ "Book:031648637X" ,
750
+ ] ) ;
751
+ } ) ;
572
752
} ) ;
0 commit comments