mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 17:36:44 +00:00
add81b9c04
## Description: This PR will fix #1204 Reloading the page during a game will rejoin with the same clientID, so the player can resume, even if they have to catch up from the start. It will use the localStorage to remember the clientID. ## Please complete the following: - [X] I have added screenshots for all UI updates - [X] I process any text displayed to the user through translateText() and I've added it to the en.json file - [X] I have added relevant tests to the test directory - [X] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: WoodyDRN --------- Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { getClientID } from "../../src/core/Util";
|
|
|
|
describe("Util", () => {
|
|
class InMemoryLocalStorage {
|
|
private readonly store = new Map<string, string>();
|
|
getItem(key: string): string | null {
|
|
return this.store.has(key) ? this.store.get(key)! : null;
|
|
}
|
|
setItem(key: string, value: string): void {
|
|
this.store.set(key, String(value));
|
|
}
|
|
removeItem(key: string): void {
|
|
this.store.delete(key);
|
|
}
|
|
clear(): void {
|
|
this.store.clear();
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
(globalThis as any).localStorage = new InMemoryLocalStorage();
|
|
});
|
|
|
|
test("creates and persists a new client", () => {
|
|
expect((globalThis as any).localStorage.getItem("client_id")).toBeNull();
|
|
|
|
const id = getClientID("testGameID");
|
|
|
|
expect(typeof id).toBe("string");
|
|
expect(id).toMatch(/^[0-9a-zA-Z]{8}$/);
|
|
|
|
const stored = (globalThis as any).localStorage.getItem("client_id");
|
|
expect(stored).toBe(id);
|
|
});
|
|
|
|
test("creates two games and make sure only last one is updated", () => {
|
|
const id1 = getClientID("testGameID1");
|
|
const id2 = getClientID("testGameID2");
|
|
|
|
expect(id1).not.toBe(id2);
|
|
|
|
const stored = (globalThis as any).localStorage.getItem("client_id");
|
|
expect(stored).toBe(id2);
|
|
});
|
|
|
|
test("creates two games with same game id, make sure the id stays the same", () => {
|
|
const id1 = getClientID("testGameID1");
|
|
const id2 = getClientID("testGameID1");
|
|
|
|
expect(id1).toBe(id2);
|
|
|
|
const stored = (globalThis as any).localStorage.getItem("client_id");
|
|
expect(stored).toBe(id1);
|
|
});
|
|
});
|