1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Switch to a Map for convertQueryDictToStringRecord

This commit is contained in:
Andy Balaam
2023-01-06 14:16:21 +00:00
parent ca98d9ff11
commit b1566ee540

View File

@ -70,15 +70,12 @@ jest.mock("../../src/webrtc/call", () => ({
// Utility function to ease the transition from our QueryDict type to a string record // Utility function to ease the transition from our QueryDict type to a string record
// which we can use to stringify with URLSearchParams // which we can use to stringify with URLSearchParams
function convertQueryDictToStringRecord(queryDict?: QueryDict): Record<string, string> { function convertQueryDictToStringRecord(queryDict?: QueryDict): Map<string, string> {
if (!queryDict) { if (!queryDict) {
return {}; return new Map();
} }
return Object.entries(queryDict).reduce((resultant, [key, value]) => { return new Map(Object.entries(queryDict).map(([k, v]) => [k, String(v)]));
resultant[key] = String(value);
return resultant;
}, {} as Record<string, string>);
} }
type HttpLookup = { type HttpLookup = {
@ -103,28 +100,37 @@ type WrappedRoom = Room & {
describe("convertQueryDictToStringRecord", () => { describe("convertQueryDictToStringRecord", () => {
it("returns an empty map when dict is undefined", () => { it("returns an empty map when dict is undefined", () => {
expect(convertQueryDictToStringRecord(undefined)).toEqual({}); expect(convertQueryDictToStringRecord(undefined)).toEqual(new Map());
}); });
it("converts an empty QueryDict to an empty map", () => { it("converts an empty QueryDict to an empty map", () => {
expect(convertQueryDictToStringRecord({})).toEqual({}); expect(convertQueryDictToStringRecord({})).toEqual(new Map());
}); });
it("converts a QueryDict of strings to the equivalent map", () => { it("converts a QueryDict of strings to the equivalent map", () => {
expect(convertQueryDictToStringRecord({ a: "b", c: "d" })).toEqual({ a: "b", c: "d" }); expect(convertQueryDictToStringRecord({ a: "b", c: "d" })).toEqual(
new Map([
["a", "b"],
["c", "d"],
]),
);
}); });
it("converts the values of the supplied QueryDict to strings", () => { it("converts the values of the supplied QueryDict to strings", () => {
expect(convertQueryDictToStringRecord({ arr: ["b", "c"], num: 45, boo: true, und: undefined })).toEqual({ expect(convertQueryDictToStringRecord({ arr: ["b", "c"], num: 45, boo: true, und: undefined })).toEqual(
arr: "b,c", new Map([
num: "45", ["arr", "b,c"],
boo: "true", ["num", "45"],
und: "undefined", ["boo", "true"],
}); ["und", "undefined"],
]),
);
}); });
it("produces sane URLSearchParams conversions", () => { it("produces sane URLSearchParams conversions", () => {
expect(new URLSearchParams(convertQueryDictToStringRecord({ a: "b", c: "d" })).toString()).toEqual("a=b&c=d"); expect(new URLSearchParams(Array.from(convertQueryDictToStringRecord({ a: "b", c: "d" }))).toString()).toEqual(
"a=b&c=d",
);
}); });
}); });
@ -248,10 +254,12 @@ describe("MatrixClient", function () {
return Promise.resolve(next.data); return Promise.resolve(next.data);
} }
const receivedRequestQueryString = new URLSearchParams(convertQueryDictToStringRecord(queryParams)).toString(); const receivedRequestQueryString = new URLSearchParams(
Array.from(convertQueryDictToStringRecord(queryParams)),
).toString();
const receivedRequestDebugString = `${method} ${prefix}${path}${receivedRequestQueryString}`; const receivedRequestDebugString = `${method} ${prefix}${path}${receivedRequestQueryString}`;
const expectedQueryString = new URLSearchParams( const expectedQueryString = new URLSearchParams(
convertQueryDictToStringRecord(next.expectQueryParams), Array.from(convertQueryDictToStringRecord(next.expectQueryParams)),
).toString(); ).toString();
const expectedRequestDebugString = `${next.method} ${next.prefix ?? ""}${next.path}${expectedQueryString}`; const expectedRequestDebugString = `${next.method} ${next.prefix ?? ""}${next.path}${expectedQueryString}`;
// If you're seeing this then you forgot to handle at least 1 pending request. // If you're seeing this then you forgot to handle at least 1 pending request.