@@ -715,11 +715,26 @@ describe('AtlasUserData', function () {
715
715
716
716
context ( 'AtlasUserData.updateAttributes' , function ( ) {
717
717
it ( 'updates data successfully' , async function ( ) {
718
- const putResponse = { name : 'Updated Name' , hasDarkMode : false } ;
719
- authenticatedFetchStub . resolves ( mockResponse ( putResponse ) ) ;
720
- getResourceUrlStub . resolves (
721
- 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj/test-id'
722
- ) ;
718
+ const getResponse = {
719
+ data : JSON . stringify ( { name : 'Original Name' , hasDarkMode : true } ) ,
720
+ } ;
721
+ const putResponse = { } ;
722
+
723
+ authenticatedFetchStub
724
+ . onFirstCall ( )
725
+ . resolves ( mockResponse ( getResponse ) )
726
+ . onSecondCall ( )
727
+ . resolves ( mockResponse ( putResponse ) ) ;
728
+
729
+ getResourceUrlStub
730
+ . onFirstCall ( )
731
+ . resolves (
732
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
733
+ )
734
+ . onSecondCall ( )
735
+ . resolves (
736
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj/test-id'
737
+ ) ;
723
738
724
739
const userData = getAtlasUserData ( ) ;
725
740
const result = await userData . updateAttributes ( 'test-id' , {
@@ -729,20 +744,42 @@ describe('AtlasUserData', function () {
729
744
730
745
expect ( result ) . equals ( true ) ;
731
746
732
- expect ( authenticatedFetchStub ) . to . have . been . calledOnce ;
733
- const [ url , options ] = authenticatedFetchStub . firstCall . args ;
734
- expect ( url ) . to . equal (
747
+ expect ( authenticatedFetchStub ) . to . have . been . calledTwice ;
748
+
749
+ const [ getUrl , getOptions ] = authenticatedFetchStub . firstCall . args ;
750
+ expect ( getUrl ) . to . equal (
751
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
752
+ ) ;
753
+ expect ( getOptions . method ) . to . equal ( 'GET' ) ;
754
+
755
+ const [ putUrl , putOptions ] = authenticatedFetchStub . secondCall . args ;
756
+ expect ( putUrl ) . to . equal (
735
757
'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj/test-id'
736
758
) ;
737
- expect ( options . method ) . to . equal ( 'PUT' ) ;
738
- expect ( options . headers [ 'Content-Type' ] ) . to . equal ( 'application/json' ) ;
759
+ expect ( putOptions . method ) . to . equal ( 'PUT' ) ;
760
+ expect ( putOptions . headers [ 'Content-Type' ] ) . to . equal ( 'application/json' ) ;
739
761
} ) ;
740
762
741
763
it ( 'returns false when response is not ok' , async function ( ) {
742
- authenticatedFetchStub . resolves ( mockResponse ( { } , false , 400 ) ) ;
743
- getResourceUrlStub . resolves (
744
- 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
745
- ) ;
764
+ const getResponse = {
765
+ data : JSON . stringify ( { name : 'Original Name' , hasDarkMode : true } ) ,
766
+ } ;
767
+
768
+ authenticatedFetchStub
769
+ . onFirstCall ( )
770
+ . resolves ( mockResponse ( getResponse ) )
771
+ . onSecondCall ( )
772
+ . resolves ( mockResponse ( { } , false , 400 ) ) ;
773
+
774
+ getResourceUrlStub
775
+ . onFirstCall ( )
776
+ . resolves (
777
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
778
+ )
779
+ . onSecondCall ( )
780
+ . resolves (
781
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj/test-id'
782
+ ) ;
746
783
747
784
const userData = getAtlasUserData ( ) ;
748
785
@@ -753,11 +790,26 @@ describe('AtlasUserData', function () {
753
790
} ) ;
754
791
755
792
it ( 'uses custom serializer for request body' , async function ( ) {
756
- const putResponse = { name : 'Updated' } ;
757
- authenticatedFetchStub . resolves ( mockResponse ( putResponse ) ) ;
758
- getResourceUrlStub . resolves (
759
- 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
760
- ) ;
793
+ const getResponse = {
794
+ data : JSON . stringify ( { name : 'Original Name' , hasDarkMode : true } ) ,
795
+ } ;
796
+ const putResponse = { } ;
797
+
798
+ authenticatedFetchStub
799
+ . onFirstCall ( )
800
+ . resolves ( mockResponse ( getResponse ) )
801
+ . onSecondCall ( )
802
+ . resolves ( mockResponse ( putResponse ) ) ;
803
+
804
+ getResourceUrlStub
805
+ . onFirstCall ( )
806
+ . resolves (
807
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
808
+ )
809
+ . onSecondCall ( )
810
+ . resolves (
811
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj/test-id'
812
+ ) ;
761
813
762
814
const userData = new AtlasUserData (
763
815
getTestSchema ( ) ,
@@ -773,8 +825,10 @@ describe('AtlasUserData', function () {
773
825
774
826
await userData . updateAttributes ( 'test-id' , { name : 'Updated' } ) ;
775
827
776
- const [ , options ] = authenticatedFetchStub . firstCall . args ;
777
- expect ( options . body as string ) . to . equal ( 'custom:{"name":"Updated"}' ) ;
828
+ const [ , putOptions ] = authenticatedFetchStub . secondCall . args ;
829
+ expect ( putOptions . body as string ) . to . equal (
830
+ 'custom:{"name":"Updated","hasDarkMode":true,"hasWebSupport":false}'
831
+ ) ;
778
832
} ) ;
779
833
} ) ;
780
834
@@ -826,17 +880,39 @@ describe('AtlasUserData', function () {
826
880
} ) ;
827
881
828
882
it ( 'constructs URL correctly for update operation' , async function ( ) {
829
- const putResponse = { data : { name : 'Updated' } } ;
830
- authenticatedFetchStub . resolves ( mockResponse ( putResponse ) ) ;
831
- getResourceUrlStub . resolves (
832
- 'cluster-connection.cloud.mongodb.com/FavoriteQueries/org123/proj456/item789'
833
- ) ;
883
+ const getResponse = {
884
+ data : JSON . stringify ( { name : 'Original' , hasDarkMode : true } ) ,
885
+ } ;
886
+ const putResponse = { } ;
887
+
888
+ authenticatedFetchStub
889
+ . onFirstCall ( )
890
+ . resolves ( mockResponse ( getResponse ) )
891
+ . onSecondCall ( )
892
+ . resolves ( mockResponse ( putResponse ) ) ;
893
+
894
+ getResourceUrlStub
895
+ . onFirstCall ( )
896
+ . resolves (
897
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/org123/proj456'
898
+ )
899
+ . onSecondCall ( )
900
+ . resolves (
901
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/org123/proj456/item789'
902
+ ) ;
834
903
835
904
const userData = getAtlasUserData ( { } , 'org123' , 'proj456' ) ;
836
905
await userData . updateAttributes ( 'item789' , { name : 'Updated' } ) ;
837
906
838
- const [ url ] = authenticatedFetchStub . firstCall . args ;
839
- expect ( url ) . to . equal (
907
+ expect ( authenticatedFetchStub ) . to . have . been . calledTwice ;
908
+
909
+ const [ getUrl ] = authenticatedFetchStub . firstCall . args ;
910
+ expect ( getUrl ) . to . equal (
911
+ 'cluster-connection.cloud.mongodb.com/FavoriteQueries/org123/proj456'
912
+ ) ;
913
+
914
+ const [ putUrl ] = authenticatedFetchStub . secondCall . args ;
915
+ expect ( putUrl ) . to . equal (
840
916
'cluster-connection.cloud.mongodb.com/FavoriteQueries/org123/proj456/item789'
841
917
) ;
842
918
} ) ;
0 commit comments