mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-24 20:55:49 +00:00
Use localStorage with Web Storage API (#23172)
This commit is contained in:
52
test/test_helper/local-storage-fallback.test.ts
Normal file
52
test/test_helper/local-storage-fallback.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { describe, test, expect, beforeEach } from "vitest";
|
||||
import { FallbackStorage } from "./local-storage-fallback";
|
||||
|
||||
describe("FallbackStorage", () => {
|
||||
let storage;
|
||||
|
||||
beforeEach(() => {
|
||||
storage = new FallbackStorage();
|
||||
});
|
||||
|
||||
test("should set and get an item", () => {
|
||||
storage.setItem("key1", "value1");
|
||||
expect(storage.getItem("key1")).toBe("value1");
|
||||
});
|
||||
|
||||
test("should return null for non-existing item", () => {
|
||||
expect(storage.getItem("nonExistingKey")).toBeNull();
|
||||
});
|
||||
|
||||
test("should remove an item", () => {
|
||||
storage.setItem("key2", "value2");
|
||||
storage.removeItem("key2");
|
||||
expect(storage.getItem("key2")).toBeNull();
|
||||
});
|
||||
|
||||
test("should clear all items", () => {
|
||||
storage.setItem("key3", "value3");
|
||||
storage.setItem("key4", "value4");
|
||||
storage.clear();
|
||||
expect(storage.getItem("key3")).toBeNull();
|
||||
expect(storage.getItem("key4")).toBeNull();
|
||||
});
|
||||
|
||||
test("should return the correct key for an index", () => {
|
||||
storage.setItem("key5", "value5");
|
||||
storage.setItem("key6", "value6");
|
||||
expect(storage.key(0)).toBe("key5");
|
||||
expect(storage.key(1)).toBe("key6");
|
||||
});
|
||||
|
||||
test("should throw TypeError if key method is called without arguments", () => {
|
||||
expect(() => storage.key()).toThrow(TypeError);
|
||||
});
|
||||
|
||||
test("should return the correct length", () => {
|
||||
storage.setItem("key7", "value7");
|
||||
storage.setItem("key8", "value8");
|
||||
expect(storage.length).toBe(2);
|
||||
storage.removeItem("key7");
|
||||
expect(storage.length).toBe(1);
|
||||
});
|
||||
});
|
||||
38
test/test_helper/local-storage-fallback.ts
Normal file
38
test/test_helper/local-storage-fallback.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export class FallbackStorage implements Storage {
|
||||
private valuesMap = new Map();
|
||||
|
||||
getItem(key) {
|
||||
const stringKey = String(key);
|
||||
if (this.valuesMap.has(key)) {
|
||||
return String(this.valuesMap.get(stringKey));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
setItem(key, val) {
|
||||
this.valuesMap.set(String(key), String(val));
|
||||
}
|
||||
|
||||
removeItem(key) {
|
||||
this.valuesMap.delete(key);
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.valuesMap.clear();
|
||||
}
|
||||
|
||||
key(i) {
|
||||
if (arguments.length === 0) {
|
||||
// this is a TypeError implemented on Chrome, Firefox throws Not enough arguments to Storage.key.
|
||||
throw new TypeError(
|
||||
"Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present."
|
||||
);
|
||||
}
|
||||
const arr = Array.from(this.valuesMap.keys());
|
||||
return arr[i];
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.valuesMap.size;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user