test(phase22): add client utility behaviour coverage
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const vm = require("vm");
|
||||
const assert = require("assert");
|
||||
|
||||
const rootDir = path.resolve(__dirname, "..", "..");
|
||||
|
||||
const loadEndpointClientModule = (injected = {}) => {
|
||||
const filePath = path.join(
|
||||
rootDir,
|
||||
"actions",
|
||||
"clients",
|
||||
"endpointClient.js"
|
||||
);
|
||||
let source = fs.readFileSync(filePath, "utf8");
|
||||
|
||||
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
|
||||
source = source.replace(/export const\s+/g, "const ");
|
||||
source += "\nmodule.exports = { getJson, requestJson };\n";
|
||||
|
||||
const context = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
require,
|
||||
axios: () => {
|
||||
throw new Error("axios not injected");
|
||||
},
|
||||
...injected
|
||||
};
|
||||
|
||||
vm.runInNewContext(source, context, { filename: filePath });
|
||||
return context.module.exports;
|
||||
};
|
||||
|
||||
const loadRelayClientModule = (injected = {}) => {
|
||||
const filePath = path.join(rootDir, "actions", "clients", "relayClient.js");
|
||||
let source = fs.readFileSync(filePath, "utf8");
|
||||
|
||||
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
|
||||
source = source.replace(/export const\s+/g, "const ");
|
||||
source += "\nmodule.exports = { buildHashedQueryUrl };\n";
|
||||
|
||||
const context = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
require,
|
||||
axios: {
|
||||
get: async () => {
|
||||
throw new Error("axios.get not injected");
|
||||
}
|
||||
},
|
||||
hashAPIPath: () => "&hash=fallback",
|
||||
process: { env: {} },
|
||||
...injected
|
||||
};
|
||||
|
||||
vm.runInNewContext(source, context, { filename: filePath });
|
||||
return context.module.exports;
|
||||
};
|
||||
|
||||
const tests = [];
|
||||
const test = (name, fn) => tests.push({ name, fn });
|
||||
|
||||
test("clients/endpointClient getJson returns response.data from axios.get", async () => {
|
||||
const calls = [];
|
||||
const axios = {
|
||||
get: async (url, config) => {
|
||||
calls.push({ url, config });
|
||||
return { data: { ok: true } };
|
||||
}
|
||||
};
|
||||
|
||||
const mod = loadEndpointClientModule({ axios });
|
||||
const result = await mod.getJson("/x", { headers: { a: 1 } });
|
||||
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(result)), { ok: true });
|
||||
assert.strictEqual(calls.length, 1);
|
||||
assert.strictEqual(calls[0].url, "/x");
|
||||
assert.strictEqual(calls[0].config.headers.a, 1);
|
||||
});
|
||||
|
||||
test("clients/endpointClient requestJson returns response.data from axios(config)", async () => {
|
||||
const calls = [];
|
||||
const axios = async (config) => {
|
||||
calls.push(config);
|
||||
return { data: { saved: true } };
|
||||
};
|
||||
|
||||
const mod = loadEndpointClientModule({ axios });
|
||||
const result = await mod.requestJson({ method: "post", url: "/y" });
|
||||
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(result)), { saved: true });
|
||||
assert.strictEqual(calls.length, 1);
|
||||
assert.strictEqual(calls[0].method, "post");
|
||||
});
|
||||
|
||||
test("clients/relayClient buildHashedQueryUrl appends browser hash response", async () => {
|
||||
const calls = [];
|
||||
|
||||
const mod = loadRelayClientModule({
|
||||
axios: {
|
||||
get: async (url) => {
|
||||
calls.push(url);
|
||||
return { data: { hash: "&hash=abc" } };
|
||||
}
|
||||
},
|
||||
process: { env: {} }
|
||||
});
|
||||
|
||||
const result = await mod.buildHashedQueryUrl("/api/file/deleteblob?x=1");
|
||||
|
||||
assert.strictEqual(result, "/api/file/deleteblob?x=1&hash=abc");
|
||||
assert.strictEqual(calls.length, 1);
|
||||
assert.strictEqual(
|
||||
calls[0],
|
||||
"/api/endpoint/gethash_api?path=%2Fapi%2Ffile%2Fdeleteblob%3Fx%3D1"
|
||||
);
|
||||
});
|
||||
|
||||
test("clients/relayClient falls back to hashAPIPath on server when HASHKEY present", async () => {
|
||||
const mod = loadRelayClientModule({
|
||||
axios: {
|
||||
get: async () => {
|
||||
throw new Error("network unavailable");
|
||||
}
|
||||
},
|
||||
hashAPIPath: () => "&hash=server-fallback",
|
||||
process: { env: { HASHKEY: "secret" } }
|
||||
});
|
||||
|
||||
const result = await mod.buildHashedQueryUrl("/api/file/deleteblob?x=1");
|
||||
assert.strictEqual(result, "/api/file/deleteblob?x=1&hash=server-fallback");
|
||||
});
|
||||
|
||||
test("clients/relayClient rethrows when browser hash call fails and no HASHKEY", async () => {
|
||||
const expected = new Error("hash service down");
|
||||
|
||||
const mod = loadRelayClientModule({
|
||||
axios: {
|
||||
get: async () => {
|
||||
throw expected;
|
||||
}
|
||||
},
|
||||
process: { env: {} }
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
() => mod.buildHashedQueryUrl("/api/file/deleteblob?x=1"),
|
||||
(error) => error === expected
|
||||
);
|
||||
});
|
||||
|
||||
const run = async () => {
|
||||
let passed = 0;
|
||||
|
||||
for (const currentTest of tests) {
|
||||
await currentTest.fn();
|
||||
passed += 1;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Phase 22 client-utils tests passed (${passed}/${tests.length}).`
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = run;
|
||||
|
||||
if (require.main === module) {
|
||||
run().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user