Skip to content

Commit 6e095a1

Browse files
authored
Merge pull request #41 from webxdc/wofwca/cache-storage-test
feat: add `CacheStorage` test
2 parents fb1e9c5 + 5122610 commit 6e095a1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

js/storage.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,47 @@ function testIndexedDB(output) {
7272
}
7373
}
7474

75+
/**
76+
* @param {HTMLElement} output
77+
*/
78+
async function testCacheStorage(output) {
79+
output.appendChild(h("h3", {}, "CacheStorage"));
80+
81+
const appendErr = (err) => {
82+
output.append(h("strong", {class: "red"}, "WARNING: "), "CacheStorage is not supported." + err);
83+
}
84+
if (!globalThis.caches) {
85+
appendErr(`globalThis.caches is ${globalThis.caches}`);
86+
return;
87+
}
88+
89+
try {
90+
const cache = await caches.open("test-cache");
91+
const key = 'test-url';
92+
const res1 = await cache.match(key);
93+
const initialValue = parseInt(
94+
res1 != undefined ? await res1.text() : "0"
95+
);
96+
await cache.put(key, new Response(initialValue + 1));
97+
98+
const res2 = await cache.match(key);
99+
if (res2 == undefined) {
100+
appendErr('Failed to fetch the value that we just stored');
101+
}
102+
const retreived2 = await res2.text()
103+
104+
output.append("Counter: ", h("span", {}, retreived2));
105+
} catch (err) {
106+
appendErr(err);
107+
return;
108+
}
109+
}
110+
75111
window.addEventListener("load", () => {
76112
let div = h("div", {class: "container"});
77113
testStorage(div, "localStorage");
78114
testStorage(div, "sessionStorage");
79115
testIndexedDB(div);
116+
testCacheStorage(div);
80117
document.getElementById("storage-output").append(createHeader("Storage"), div);
81118
});

0 commit comments

Comments
 (0)