@@ -135,10 +135,6 @@ describe("<MatrixChat />", () => {
135
135
} ;
136
136
const getComponent = ( props : Partial < ComponentProps < typeof MatrixChat > > = { } ) =>
137
137
render ( < MatrixChat { ...defaultProps } { ...props } /> ) ;
138
- let localStorageSetSpy = jest . spyOn ( localStorage . __proto__ , "setItem" ) ;
139
- let localStorageGetSpy = jest . spyOn ( localStorage . __proto__ , "getItem" ) . mockReturnValue ( undefined ) ;
140
- let localStorageClearSpy = jest . spyOn ( localStorage . __proto__ , "clear" ) ;
141
- let sessionStorageSetSpy = jest . spyOn ( sessionStorage . __proto__ , "setItem" ) ;
142
138
143
139
// make test results readable
144
140
filterConsole ( "Failed to parse localStorage object" ) ;
@@ -180,10 +176,6 @@ describe("<MatrixChat />", () => {
180
176
unstable_features : { } ,
181
177
versions : [ "v1.1" ] ,
182
178
} ) ;
183
- localStorageSetSpy = jest . spyOn ( localStorage . __proto__ , "setItem" ) ;
184
- localStorageGetSpy = jest . spyOn ( localStorage . __proto__ , "getItem" ) . mockReturnValue ( undefined ) ;
185
- localStorageClearSpy = jest . spyOn ( localStorage . __proto__ , "clear" ) ;
186
- sessionStorageSetSpy = jest . spyOn ( sessionStorage . __proto__ , "setItem" ) ;
187
179
188
180
jest . spyOn ( StorageManager , "idbLoad" ) . mockReset ( ) ;
189
181
jest . spyOn ( StorageManager , "idbSave" ) . mockResolvedValue ( undefined ) ;
@@ -194,6 +186,8 @@ describe("<MatrixChat />", () => {
194
186
195
187
afterEach ( ( ) => {
196
188
jest . clearAllMocks ( ) ;
189
+ localStorage . clear ( ) ;
190
+ sessionStorage . clear ( ) ;
197
191
} ) ;
198
192
199
193
it ( "should render spinner while app is loading" , ( ) => {
@@ -208,16 +202,13 @@ describe("<MatrixChat />", () => {
208
202
mx_access_token : accessToken ,
209
203
} ,
210
204
} ;
211
- const mockLocalStorage : Record < string , string > = {
212
- mx_hs_url : serverConfig . hsUrl ,
213
- mx_is_url : serverConfig . isUrl ,
214
- mx_access_token : accessToken ,
215
- mx_user_id : userId ,
216
- mx_device_id : deviceId ,
217
- } ;
218
205
219
206
beforeEach ( ( ) => {
220
- localStorageGetSpy . mockImplementation ( ( key : unknown ) => mockLocalStorage [ key as string ] || "" ) ;
207
+ localStorage . setItem ( "mx_hs_url" , serverConfig . hsUrl ) ;
208
+ localStorage . setItem ( "mx_is_url" , serverConfig . isUrl ) ;
209
+ localStorage . setItem ( "mx_access_token" , accessToken ) ;
210
+ localStorage . setItem ( "mx_user_id" , userId ) ;
211
+ localStorage . setItem ( "mx_device_id" , deviceId ) ;
221
212
222
213
jest . spyOn ( StorageManager , "idbLoad" ) . mockImplementation ( async ( table , key ) => {
223
214
const safeKey = Array . isArray ( key ) ? key [ 0 ] : key ;
@@ -521,17 +512,14 @@ describe("<MatrixChat />", () => {
521
512
522
513
describe ( "with a soft-logged-out session" , ( ) => {
523
514
const mockidb : Record < string , Record < string , string > > = { } ;
524
- const mockLocalStorage : Record < string , string > = {
525
- mx_hs_url : serverConfig . hsUrl ,
526
- mx_is_url : serverConfig . isUrl ,
527
- mx_access_token : accessToken ,
528
- mx_user_id : userId ,
529
- mx_device_id : deviceId ,
530
- mx_soft_logout : "true" ,
531
- } ;
532
515
533
516
beforeEach ( ( ) => {
534
- localStorageGetSpy . mockImplementation ( ( key : unknown ) => mockLocalStorage [ key as string ] || "" ) ;
517
+ localStorage . setItem ( "mx_hs_url" , serverConfig . hsUrl ) ;
518
+ localStorage . setItem ( "mx_is_url" , serverConfig . isUrl ) ;
519
+ localStorage . setItem ( "mx_access_token" , accessToken ) ;
520
+ localStorage . setItem ( "mx_user_id" , userId ) ;
521
+ localStorage . setItem ( "mx_device_id" , deviceId ) ;
522
+ localStorage . setItem ( "mx_soft_logout" , "true" ) ;
535
523
536
524
mockClient . loginFlows . mockResolvedValue ( { flows : [ { type : "m.login.password" } ] } ) ;
537
525
@@ -727,14 +715,6 @@ describe("<MatrixChat />", () => {
727
715
loginToken,
728
716
} ;
729
717
730
- const mockLocalStorage : Record < string , string > = {
731
- mx_sso_hs_url : serverConfig . hsUrl ,
732
- mx_sso_is_url : serverConfig . isUrl ,
733
- // these are only going to be set during login
734
- mx_hs_url : serverConfig . hsUrl ,
735
- mx_is_url : serverConfig . isUrl ,
736
- } ;
737
-
738
718
let loginClient ! : ReturnType < typeof getMockClientWithEventEmitter > ;
739
719
const userId = "@alice:server.org" ;
740
720
const deviceId = "test-device-id" ;
@@ -746,17 +726,18 @@ describe("<MatrixChat />", () => {
746
726
} ;
747
727
748
728
beforeEach ( ( ) => {
729
+ localStorage . setItem ( "mx_sso_hs_url" , serverConfig . hsUrl ) ;
730
+ localStorage . setItem ( "mx_sso_is_url" , serverConfig . isUrl ) ;
749
731
loginClient = getMockClientWithEventEmitter ( getMockClientMethods ( ) ) ;
750
732
// this is used to create a temporary client during login
751
733
jest . spyOn ( MatrixJs , "createClient" ) . mockReturnValue ( loginClient ) ;
752
734
753
735
loginClient . login . mockClear ( ) . mockResolvedValue ( clientLoginResponse ) ;
754
-
755
- localStorageGetSpy . mockImplementation ( ( key : unknown ) => mockLocalStorage [ key as string ] || "" ) ;
756
736
} ) ;
757
737
758
738
it ( "should show an error dialog when no homeserver is found in local storage" , async ( ) => {
759
- localStorageGetSpy . mockReturnValue ( undefined ) ;
739
+ localStorage . removeItem ( "mx_sso_hs_url" ) ;
740
+ const localStorageGetSpy = jest . spyOn ( localStorage . __proto__ , "getItem" ) ;
760
741
getComponent ( { realQueryParams } ) ;
761
742
762
743
expect ( localStorageGetSpy ) . toHaveBeenCalledWith ( "mx_sso_hs_url" ) ;
@@ -830,12 +811,15 @@ describe("<MatrixChat />", () => {
830
811
) ;
831
812
} ) ;
832
813
it ( "should clear storage" , async ( ) => {
814
+ const localStorageClearSpy = jest . spyOn ( localStorage . __proto__ , "clear" ) ;
815
+
833
816
getComponent ( { realQueryParams } ) ;
834
817
835
818
await flushPromises ( ) ;
836
819
837
820
// just check we called the clearStorage function
838
821
expect ( loginClient . clearStores ) . toHaveBeenCalled ( ) ;
822
+ expect ( localStorage . getItem ( "mx_sso_hs_url" ) ) . toBe ( null ) ;
839
823
expect ( localStorageClearSpy ) . toHaveBeenCalled ( ) ;
840
824
} ) ;
841
825
@@ -844,17 +828,17 @@ describe("<MatrixChat />", () => {
844
828
845
829
await flushPromises ( ) ;
846
830
847
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_hs_url" , serverConfig . hsUrl ) ;
848
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_user_id" , userId ) ;
849
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_has_access_token" , "true" ) ;
850
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_device_id" , deviceId ) ;
831
+ expect ( localStorage . getItem ( "mx_hs_url" ) ) . toEqual ( serverConfig . hsUrl ) ;
832
+ expect ( localStorage . getItem ( "mx_user_id" ) ) . toEqual ( userId ) ;
833
+ expect ( localStorage . getItem ( "mx_has_access_token" ) ) . toEqual ( "true" ) ;
834
+ expect ( localStorage . getItem ( "mx_device_id" ) ) . toEqual ( deviceId ) ;
851
835
} ) ;
852
836
853
837
it ( "should set fresh login flag in session storage" , async ( ) => {
838
+ const sessionStorageSetSpy = jest . spyOn ( sessionStorage . __proto__ , "setItem" ) ;
854
839
getComponent ( { realQueryParams } ) ;
855
840
856
841
await flushPromises ( ) ;
857
-
858
842
expect ( sessionStorageSetSpy ) . toHaveBeenCalledWith ( "mx_fresh_login" , "true" ) ;
859
843
} ) ;
860
844
@@ -873,7 +857,7 @@ describe("<MatrixChat />", () => {
873
857
874
858
await flushPromises ( ) ;
875
859
876
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_hs_url" , hsUrlFromWk ) ;
860
+ expect ( localStorage . getItem ( "mx_hs_url" ) ) . toEqual ( hsUrlFromWk ) ;
877
861
} ) ;
878
862
879
863
it ( "should continue to post login setup when no session is found in local storage" , async ( ) => {
@@ -902,14 +886,6 @@ describe("<MatrixChat />", () => {
902
886
const deviceId = "test-device-id" ;
903
887
const accessToken = "test-access-token-from-oidc" ;
904
888
905
- const mockLocalStorage : Record < string , string > = {
906
- // these are only going to be set during login
907
- mx_hs_url : homeserverUrl ,
908
- mx_is_url : identityServerUrl ,
909
- mx_user_id : userId ,
910
- mx_device_id : deviceId ,
911
- } ;
912
-
913
889
const tokenResponse : BearerTokenResponse = {
914
890
access_token : accessToken ,
915
891
refresh_token : "def456" ,
@@ -950,7 +926,6 @@ describe("<MatrixChat />", () => {
950
926
jest . spyOn ( logger , "error" ) . mockClear ( ) ;
951
927
jest . spyOn ( logger , "log" ) . mockClear ( ) ;
952
928
953
- localStorageGetSpy . mockImplementation ( ( key : unknown ) => mockLocalStorage [ key as string ] || "" ) ;
954
929
loginClient . whoami . mockResolvedValue ( {
955
930
user_id : userId ,
956
931
device_id : deviceId ,
@@ -1047,6 +1022,7 @@ describe("<MatrixChat />", () => {
1047
1022
} ) ;
1048
1023
1049
1024
it ( "should not store clientId or issuer" , async ( ) => {
1025
+ const sessionStorageSetSpy = jest . spyOn ( sessionStorage . __proto__ , "setItem" ) ;
1050
1026
getComponent ( { realQueryParams } ) ;
1051
1027
1052
1028
await flushPromises ( ) ;
@@ -1058,7 +1034,6 @@ describe("<MatrixChat />", () => {
1058
1034
1059
1035
describe ( "when login succeeds" , ( ) => {
1060
1036
beforeEach ( ( ) => {
1061
- localStorageGetSpy . mockImplementation ( ( key : unknown ) => mockLocalStorage [ key as string ] || "" ) ;
1062
1037
jest . spyOn ( StorageManager , "idbLoad" ) . mockImplementation (
1063
1038
async ( _table : string , key : string | string [ ] ) => ( key === "mx_access_token" ? accessToken : null ) ,
1064
1039
) ;
@@ -1072,19 +1047,19 @@ describe("<MatrixChat />", () => {
1072
1047
1073
1048
await flushPromises ( ) ;
1074
1049
1075
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_hs_url" , homeserverUrl ) ;
1076
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_user_id" , userId ) ;
1077
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_has_access_token" , "true" ) ;
1078
- expect ( localStorageSetSpy ) . toHaveBeenCalledWith ( "mx_device_id" , deviceId ) ;
1050
+ expect ( localStorage . getItem ( "mx_hs_url" ) ) . toEqual ( homeserverUrl ) ;
1051
+ expect ( localStorage . getItem ( "mx_user_id" ) ) . toEqual ( userId ) ;
1052
+ expect ( localStorage . getItem ( "mx_has_access_token" ) ) . toEqual ( "true" ) ;
1053
+ expect ( localStorage . getItem ( "mx_device_id" ) ) . toEqual ( deviceId ) ;
1079
1054
} ) ;
1080
1055
1081
1056
it ( "should store clientId and issuer in session storage" , async ( ) => {
1082
1057
getComponent ( { realQueryParams } ) ;
1083
1058
1084
1059
await flushPromises ( ) ;
1085
1060
1086
- expect ( sessionStorageSetSpy ) . toHaveBeenCalledWith ( "mx_oidc_client_id" , clientId ) ;
1087
- expect ( sessionStorageSetSpy ) . toHaveBeenCalledWith ( "mx_oidc_token_issuer" , issuer ) ;
1061
+ expect ( sessionStorage . getItem ( "mx_oidc_client_id" ) ) . toEqual ( clientId ) ;
1062
+ expect ( sessionStorage . getItem ( "mx_oidc_token_issuer" ) ) . toEqual ( issuer ) ;
1088
1063
} ) ;
1089
1064
1090
1065
it ( "should set logged in and start MatrixClient" , async ( ) => {
@@ -1104,7 +1079,7 @@ describe("<MatrixChat />", () => {
1104
1079
homeserverUrl +
1105
1080
" softLogout: " +
1106
1081
false ,
1107
- " freshLogin: " + false ,
1082
+ " freshLogin: " + true ,
1108
1083
) ;
1109
1084
1110
1085
// client successfully started
0 commit comments