Skip to content

Commit

Permalink
Add a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Nov 16, 2024
1 parent 9bdcd2b commit b90af12
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
34 changes: 24 additions & 10 deletions test/QueueTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const test = require("ava");
const fs = require("fs");
const Cache = require("../");
const RemoteAssetCache = require("../src/RemoteAssetCache");

Expand Down Expand Up @@ -46,16 +45,16 @@ test("Double Fetch async function (dry run)", async (t) => {
return Promise.resolve(expected);
};

let ac1 = Cache(fetch, {
dryRun: true,
requestId: "fetch-1",
});
let ac2 = Cache(fetch, {
dryRun: true,
requestId: "fetch-2",
});
let ac1 = Cache(fetch, {
dryRun: true,
requestId: "fetch-1",
});
let ac2 = Cache(fetch, {
dryRun: true,
requestId: "fetch-2",
});

// Make sure we only fetch once!
// two distinct fetches
t.not(ac1, ac2);

let result1 = await ac1;
Expand All @@ -65,3 +64,18 @@ let ac2 = Cache(fetch, {
t.deepEqual(result1, expected);
t.deepEqual(result2, expected);
});

test("Double Fetch 404 errors should only fetch once", async (t) => {
let ac1 = Cache("https://httpstat.us/404", {
dryRun: true,
});
let ac2 = Cache("https://httpstat.us/404", {
dryRun: true,
});

// Make sure we only fetch once!
t.is(ac1, ac2);

await t.throwsAsync(async () => await ac1);
await t.throwsAsync(async () => await ac2);
});
5 changes: 3 additions & 2 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ test("type: parsed-xml", async (t) => {
t.is(xml.children.length, 1)
});

test("raw: true, cache miss", async (t) => {
test("returnType: response, cache miss", async (t) => {
let feedUrl = "https://www.11ty.dev/blog/feed.xml";
let ac = new RemoteAssetCache(feedUrl, ".cache", {
type: "xml",
Expand All @@ -365,7 +365,7 @@ test("raw: true, cache miss", async (t) => {
});


test("raw: true, cache hit", async (t) => {
test("returnType: response, cache hit", async (t) => {
let feedUrl = "https://www.11ty.dev/blog/feed.xml";
let ac = new RemoteAssetCache(feedUrl, ".cache", {
type: "xml",
Expand All @@ -384,3 +384,4 @@ test("raw: true, cache hit", async (t) => {

t.is(response.cache, "hit");
});

0 comments on commit b90af12

Please sign in to comment.