z-deduper
Version:
This library will allow you to build a Custom Zapier deduper for your advanced polling triggers use cases in your Zapier app.
82 lines • 2.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const nock_1 = __importDefault(require("nock"));
const storage_1 = require("./storage");
const utils_1 = require("./utils");
const ZAP_ID = "12345";
function mock(zapId = ZAP_ID) {
const scope = nock_1.default(storage_1.ZAPIER_STORE_URL);
return {
getRecords(page) {
return scope
.get("/api/records")
.matchHeader("X-Secret", utils_1.generateKey(`${zapId}.${page}`));
},
postRecords(page, data) {
return scope
.post("/api/records", data)
.matchHeader("X-Secret", utils_1.generateKey(`${zapId}.${page}`));
},
};
}
describe("Storage", () => {
beforeEach(() => {
nock_1.default.disableNetConnect();
});
it("should be defined", () => {
expect(storage_1.Storage).toBeDefined();
});
it("should load all records from storage", async () => {
mock().getRecords(0).reply(200, {
__zapier_custom_deduper__: true,
"records.0": "eyJyZWMxIjoiZm9vIiw",
"records.1": "icmVjMiI6ImJhciIsIn",
total: 2,
});
mock().getRecords(1).reply(200, {
__zapier_custom_deduper__: true,
"records.2": "JlYzMiOiJiYXoifQ==",
});
const response = await new storage_1.Storage(ZAP_ID).load();
expect(response).toEqual({
rec1: "foo",
rec2: "bar",
rec3: "baz",
});
});
it("should save all records to storage", async () => {
const data = [
{
__zapier_custom_deduper__: true,
"records.0": "eyJyZWMxIjoiZm9vIiw",
"records.1": "icmVjMiI6ImJhciIsIn",
page: 0,
total: 2,
},
{
__zapier_custom_deduper__: true,
"records.2": "JlYzMiOiJiYXoifQ==",
page: 1,
total: 2,
},
];
mock().postRecords(0, data[0]).reply(200, data[0]);
mock().postRecords(1, data[1]).reply(200, data[1]);
const storage = new storage_1.Storage(ZAP_ID);
//@ts-ignore
storage.MAX_KEY_SIZE = 19;
//@ts-ignore
storage.MAX_KEYS = 2;
const records = {
rec1: "foo",
rec2: "bar",
rec3: "baz",
};
const response = await storage.save(records);
expect(response).toEqual(data);
});
});
//# sourceMappingURL=storage.spec.js.map