@@ -863,41 +863,60 @@ describe('ExtensionState', () => {
863863 } ) ;
864864
865865 describe ( 'clearAvatarCache' , ( ) => {
866- it ( 'Should clear all avatars from the cache and delete all avatars that are currently stored on the file system' , ( ) => {
866+ let spyOnReaddir : jest . SpyInstance , spyOnUnlink : jest . SpyInstance ;
867+ beforeAll ( ( ) => {
868+ spyOnReaddir = jest . spyOn ( fs , 'readdir' ) ;
869+ spyOnUnlink = jest . spyOn ( fs , 'unlink' ) ;
870+ } ) ;
871+
872+ it ( 'Should clear all avatars from the cache and delete all avatars that are currently stored on the file system' , async ( ) => {
873+ // Setup
867874 extensionContext . globalState . update . mockResolvedValueOnce ( null ) ;
868- const spyOnReaddir = jest . spyOn ( fs , 'readdir' ) ;
869875 spyOnReaddir . mockImplementationOnce ( ( _ , callback ) => callback ( null , [ 'file1.jpg' , 'file2.jpg' ] ) ) ;
870- const spyOnUnlink = jest . spyOn ( fs , 'unlink' ) ;
871- spyOnUnlink . mockImplementation ( ( _ , callback ) => callback ( null ) ) ;
876+ spyOnUnlink . mockImplementationOnce ( ( _ , callback ) => callback ( null ) ) ;
877+ spyOnUnlink . mockImplementationOnce ( ( _ , callback ) => callback ( null ) ) ;
872878
873879 // Run
874- extensionState . clearAvatarCache ( ) ;
880+ const result = await extensionState . clearAvatarCache ( ) ;
875881
876882 // Assert
883+ expect ( result ) . toBeNull ( ) ;
877884 expect ( extensionContext . globalState . update ) . toHaveBeenCalledWith ( 'avatarCache' , { } ) ;
878885 expect ( spyOnReaddir ) . toHaveBeenCalledTimes ( 1 ) ;
879- expect ( spyOnReaddir . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/path/to/globalStorage/avatars' ) ;
886+ expect ( spyOnReaddir ) . toHaveBeenNthCalledWith ( 1 , '/path/to/globalStorage/avatars' , expect . anything ( ) ) ;
880887 expect ( spyOnUnlink ) . toHaveBeenCalledTimes ( 2 ) ;
881- expect ( spyOnUnlink . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/path/to/globalStorage/avatars/file1.jpg' ) ;
882- expect ( spyOnUnlink . mock . calls [ 1 ] [ 0 ] ) . toBe ( '/path/to/globalStorage/avatars/file2.jpg' ) ;
888+ expect ( spyOnUnlink ) . toHaveBeenNthCalledWith ( 1 , '/path/to/globalStorage/avatars/file1.jpg' , expect . anything ( ) ) ;
889+ expect ( spyOnUnlink ) . toHaveBeenNthCalledWith ( 2 , '/path/to/globalStorage/avatars/file2.jpg' , expect . anything ( ) ) ;
883890 } ) ;
884891
885- it ( 'Should skip deleting avatars on the file system if they could not be listed from the file system' , ( ) => {
892+ it ( 'Should skip deleting avatars on the file system if they could not be listed from the file system' , async ( ) => {
893+ // Setup
886894 extensionContext . globalState . update . mockResolvedValueOnce ( null ) ;
887- const spyOnReaddir = jest . spyOn ( fs , 'readdir' ) ;
888895 spyOnReaddir . mockImplementationOnce ( ( _ , callback ) => callback ( new Error ( ) , [ 'file1.jpg' , 'file2.jpg' ] ) ) ;
889- const spyOnUnlink = jest . spyOn ( fs , 'unlink' ) ;
890- spyOnUnlink . mockImplementation ( ( _ , callback ) => callback ( null ) ) ;
891896
892897 // Run
893- extensionState . clearAvatarCache ( ) ;
898+ const result = await extensionState . clearAvatarCache ( ) ;
894899
895900 // Assert
901+ expect ( result ) . toBeNull ( ) ;
896902 expect ( extensionContext . globalState . update ) . toHaveBeenCalledWith ( 'avatarCache' , { } ) ;
897903 expect ( spyOnReaddir ) . toHaveBeenCalledTimes ( 1 ) ;
898- expect ( spyOnReaddir . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/path/to/globalStorage/avatars' ) ;
904+ expect ( spyOnReaddir ) . toHaveBeenNthCalledWith ( 1 , '/path/to/globalStorage/avatars' , expect . anything ( ) ) ;
899905 expect ( spyOnUnlink ) . toHaveBeenCalledTimes ( 0 ) ;
900906 } ) ;
907+
908+ it ( 'Shouldn\'t delete avatars on the file system if globalState.update rejects, and return the error message' , async ( ) => {
909+ // Setup
910+ extensionContext . globalState . update . mockRejectedValueOnce ( null ) ;
911+
912+ // Run
913+ const result = await extensionState . clearAvatarCache ( ) ;
914+
915+ // Assert
916+ expect ( result ) . toBe ( 'Visual Studio Code was unable to save the Git Graph Global State Memento.' ) ;
917+ expect ( extensionContext . globalState . update ) . toHaveBeenCalledWith ( 'avatarCache' , { } ) ;
918+ expect ( spyOnReaddir ) . not . toHaveBeenCalled ( ) ;
919+ } ) ;
901920 } ) ;
902921
903922 describe ( 'startCodeReview' , ( ) => {
0 commit comments