Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 523e691

Browse files
authored
MatrixChat-test: stop mocking localStorage (#11419)
We have a perfectly good localStorage impl; no need to do a half-arsed mock of it.
1 parent 6b14ecf commit 523e691

File tree

1 file changed

+34
-59
lines changed

1 file changed

+34
-59
lines changed

test/components/structures/MatrixChat-test.tsx

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ describe("<MatrixChat />", () => {
135135
};
136136
const getComponent = (props: Partial<ComponentProps<typeof MatrixChat>> = {}) =>
137137
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");
142138

143139
// make test results readable
144140
filterConsole("Failed to parse localStorage object");
@@ -180,10 +176,6 @@ describe("<MatrixChat />", () => {
180176
unstable_features: {},
181177
versions: ["v1.1"],
182178
});
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");
187179

188180
jest.spyOn(StorageManager, "idbLoad").mockReset();
189181
jest.spyOn(StorageManager, "idbSave").mockResolvedValue(undefined);
@@ -194,6 +186,8 @@ describe("<MatrixChat />", () => {
194186

195187
afterEach(() => {
196188
jest.clearAllMocks();
189+
localStorage.clear();
190+
sessionStorage.clear();
197191
});
198192

199193
it("should render spinner while app is loading", () => {
@@ -208,16 +202,13 @@ describe("<MatrixChat />", () => {
208202
mx_access_token: accessToken,
209203
},
210204
};
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-
};
218205

219206
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);
221212

222213
jest.spyOn(StorageManager, "idbLoad").mockImplementation(async (table, key) => {
223214
const safeKey = Array.isArray(key) ? key[0] : key;
@@ -521,17 +512,14 @@ describe("<MatrixChat />", () => {
521512

522513
describe("with a soft-logged-out session", () => {
523514
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-
};
532515

533516
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");
535523

536524
mockClient.loginFlows.mockResolvedValue({ flows: [{ type: "m.login.password" }] });
537525

@@ -727,14 +715,6 @@ describe("<MatrixChat />", () => {
727715
loginToken,
728716
};
729717

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-
738718
let loginClient!: ReturnType<typeof getMockClientWithEventEmitter>;
739719
const userId = "@alice:server.org";
740720
const deviceId = "test-device-id";
@@ -746,17 +726,18 @@ describe("<MatrixChat />", () => {
746726
};
747727

748728
beforeEach(() => {
729+
localStorage.setItem("mx_sso_hs_url", serverConfig.hsUrl);
730+
localStorage.setItem("mx_sso_is_url", serverConfig.isUrl);
749731
loginClient = getMockClientWithEventEmitter(getMockClientMethods());
750732
// this is used to create a temporary client during login
751733
jest.spyOn(MatrixJs, "createClient").mockReturnValue(loginClient);
752734

753735
loginClient.login.mockClear().mockResolvedValue(clientLoginResponse);
754-
755-
localStorageGetSpy.mockImplementation((key: unknown) => mockLocalStorage[key as string] || "");
756736
});
757737

758738
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");
760741
getComponent({ realQueryParams });
761742

762743
expect(localStorageGetSpy).toHaveBeenCalledWith("mx_sso_hs_url");
@@ -830,12 +811,15 @@ describe("<MatrixChat />", () => {
830811
);
831812
});
832813
it("should clear storage", async () => {
814+
const localStorageClearSpy = jest.spyOn(localStorage.__proto__, "clear");
815+
833816
getComponent({ realQueryParams });
834817

835818
await flushPromises();
836819

837820
// just check we called the clearStorage function
838821
expect(loginClient.clearStores).toHaveBeenCalled();
822+
expect(localStorage.getItem("mx_sso_hs_url")).toBe(null);
839823
expect(localStorageClearSpy).toHaveBeenCalled();
840824
});
841825

@@ -844,17 +828,17 @@ describe("<MatrixChat />", () => {
844828

845829
await flushPromises();
846830

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);
851835
});
852836

853837
it("should set fresh login flag in session storage", async () => {
838+
const sessionStorageSetSpy = jest.spyOn(sessionStorage.__proto__, "setItem");
854839
getComponent({ realQueryParams });
855840

856841
await flushPromises();
857-
858842
expect(sessionStorageSetSpy).toHaveBeenCalledWith("mx_fresh_login", "true");
859843
});
860844

@@ -873,7 +857,7 @@ describe("<MatrixChat />", () => {
873857

874858
await flushPromises();
875859

876-
expect(localStorageSetSpy).toHaveBeenCalledWith("mx_hs_url", hsUrlFromWk);
860+
expect(localStorage.getItem("mx_hs_url")).toEqual(hsUrlFromWk);
877861
});
878862

879863
it("should continue to post login setup when no session is found in local storage", async () => {
@@ -902,14 +886,6 @@ describe("<MatrixChat />", () => {
902886
const deviceId = "test-device-id";
903887
const accessToken = "test-access-token-from-oidc";
904888

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-
913889
const tokenResponse: BearerTokenResponse = {
914890
access_token: accessToken,
915891
refresh_token: "def456",
@@ -950,7 +926,6 @@ describe("<MatrixChat />", () => {
950926
jest.spyOn(logger, "error").mockClear();
951927
jest.spyOn(logger, "log").mockClear();
952928

953-
localStorageGetSpy.mockImplementation((key: unknown) => mockLocalStorage[key as string] || "");
954929
loginClient.whoami.mockResolvedValue({
955930
user_id: userId,
956931
device_id: deviceId,
@@ -1047,6 +1022,7 @@ describe("<MatrixChat />", () => {
10471022
});
10481023

10491024
it("should not store clientId or issuer", async () => {
1025+
const sessionStorageSetSpy = jest.spyOn(sessionStorage.__proto__, "setItem");
10501026
getComponent({ realQueryParams });
10511027

10521028
await flushPromises();
@@ -1058,7 +1034,6 @@ describe("<MatrixChat />", () => {
10581034

10591035
describe("when login succeeds", () => {
10601036
beforeEach(() => {
1061-
localStorageGetSpy.mockImplementation((key: unknown) => mockLocalStorage[key as string] || "");
10621037
jest.spyOn(StorageManager, "idbLoad").mockImplementation(
10631038
async (_table: string, key: string | string[]) => (key === "mx_access_token" ? accessToken : null),
10641039
);
@@ -1072,19 +1047,19 @@ describe("<MatrixChat />", () => {
10721047

10731048
await flushPromises();
10741049

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);
10791054
});
10801055

10811056
it("should store clientId and issuer in session storage", async () => {
10821057
getComponent({ realQueryParams });
10831058

10841059
await flushPromises();
10851060

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);
10881063
});
10891064

10901065
it("should set logged in and start MatrixClient", async () => {
@@ -1104,7 +1079,7 @@ describe("<MatrixChat />", () => {
11041079
homeserverUrl +
11051080
" softLogout: " +
11061081
false,
1107-
" freshLogin: " + false,
1082+
" freshLogin: " + true,
11081083
);
11091084

11101085
// client successfully started

0 commit comments

Comments
 (0)