mirror of
https://github.com/status-im/sourcecred.git
synced 2025-02-16 14:37:31 +00:00
discourse: factor out snapshotTestUtil (#1423)
I want to have the reference tests depend on real snapshotted data. Therefore, I'm factoring out the utilities for interacting with the snapshot data out of fetch.test.js and into snapshotTestUtil.js Test plan: `yarn test` still passes.
This commit is contained in:
parent
eed115a995
commit
4e0d884283
src/plugins/discourse
@ -1,34 +1,10 @@
|
||||
// @flow
|
||||
|
||||
import deepFreeze from "deep-freeze";
|
||||
import {Fetcher, type DiscourseFetchOptions} from "./fetch";
|
||||
import base64url from "base64url";
|
||||
import path from "path";
|
||||
import fs from "fs-extra";
|
||||
import {Fetcher} from "./fetch";
|
||||
import {options, snapshotFetcher} from "./mockSnapshotFetcher";
|
||||
|
||||
describe("plugins/discourse/fetch", () => {
|
||||
const options: DiscourseFetchOptions = deepFreeze({
|
||||
apiKey: "FAKE_KEY",
|
||||
apiUsername: "credbot",
|
||||
serverUrl: "https://sourcecred-test.discourse.group",
|
||||
});
|
||||
|
||||
describe("snapshot testing", () => {
|
||||
async function snapshotFetch(
|
||||
url: string | Request | URL
|
||||
): Promise<Response> {
|
||||
const snapshotDir = "src/plugins/discourse/snapshots";
|
||||
const filename = base64url(url);
|
||||
const file = path.join(snapshotDir, filename);
|
||||
if (await fs.exists(file)) {
|
||||
const contents = await fs.readFile(file);
|
||||
return new Response(contents, {status: 200});
|
||||
} else {
|
||||
throw new Error(`couldn't load snapshot for ${file}`);
|
||||
}
|
||||
}
|
||||
const snapshotFetcher = () => new Fetcher(options, snapshotFetch, 0);
|
||||
|
||||
it("loads LatestTopicId from snapshot", async () => {
|
||||
const topicId = await snapshotFetcher().latestTopicId();
|
||||
expect(topicId).toMatchInlineSnapshot(`21`);
|
||||
|
26
src/plugins/discourse/mockSnapshotFetcher.js
Normal file
26
src/plugins/discourse/mockSnapshotFetcher.js
Normal file
@ -0,0 +1,26 @@
|
||||
// @flow
|
||||
|
||||
import deepFreeze from "deep-freeze";
|
||||
import base64url from "base64url";
|
||||
import path from "path";
|
||||
import fs from "fs-extra";
|
||||
import {Fetcher, type DiscourseFetchOptions} from "./fetch";
|
||||
|
||||
export const options: DiscourseFetchOptions = deepFreeze({
|
||||
apiKey: "FAKE_KEY",
|
||||
apiUsername: "credbot",
|
||||
serverUrl: "https://sourcecred-test.discourse.group",
|
||||
});
|
||||
|
||||
async function snapshotFetch(url: string | Request | URL): Promise<Response> {
|
||||
const snapshotDir = "src/plugins/discourse/snapshots";
|
||||
const filename = base64url(url);
|
||||
const file = path.join(snapshotDir, filename);
|
||||
if (await fs.exists(file)) {
|
||||
const contents = await fs.readFile(file);
|
||||
return new Response(contents, {status: 200});
|
||||
} else {
|
||||
throw new Error(`couldn't load snapshot for ${file}`);
|
||||
}
|
||||
}
|
||||
export const snapshotFetcher = () => new Fetcher(options, snapshotFetch, 0);
|
Loading…
x
Reference in New Issue
Block a user