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.
98 lines • 2.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const deduper_1 = require("./deduper");
const nock_1 = __importDefault(require("nock"));
describe("Deduper", () => {
beforeEach(() => {
nock_1.default.disableNetConnect();
});
it("should be defined", () => {
expect(deduper_1.Deduper).toBeDefined();
});
it("should return a deduper instance", async () => {
const deduper = deduper_1.getDeduper("12345");
expect(deduper).toBeInstanceOf(deduper_1.Deduper);
});
it("detect changes", async () => {
const fakeStorage = {
async load() {
return {
1: "an_old_fake_hash",
};
},
};
const deduper = new deduper_1.Deduper(fakeStorage);
await deduper.load();
//@ts-ignore
deduper.getTimestamp = () => "fake_timestamp";
//@ts-ignore
deduper.hash = () => "fake_hash";
const changes = await deduper.findChanges([
{ id: 1, foo: "bar" },
{ id: 2 },
]);
const created = [
{
id: "2.fake_timestamp",
_id: 2,
_hash: "fake_hash",
},
];
const updated = [
{
id: "1.fake_timestamp",
foo: "bar",
_id: 1,
_hash: "fake_hash",
},
];
expect(changes).toEqual({
all: [...created, ...updated],
created,
updated,
});
});
it("should persist changes", async () => {
const fakeStorage = {
save: jest.fn(),
};
const deduper = new deduper_1.Deduper(fakeStorage);
//@ts-ignore
deduper.cache = {
item1: "YtgnKwYsbpXJZwGO2ZUBQQ==",
};
await deduper.persistChanges([
{
id: "item1",
name: "foo",
},
{
id: "item2",
name: "bar",
},
]);
expect(fakeStorage.save).toHaveBeenCalledWith({
item1: "5LkFSjD3MWK7bOMeo1f5xg==",
item2: "OWD/UUTp9eoycPHl+01Q3Q==",
});
});
it("should initialize the deduper", async () => {
const fakeStorage = {
save: jest.fn(),
};
const deduper = new deduper_1.Deduper(fakeStorage);
await deduper.initialize([
{
id: "item1",
name: "foo",
},
]);
expect(fakeStorage.save).toHaveBeenCalledWith({
item1: "5LkFSjD3MWK7bOMeo1f5xg==",
});
});
});
//# sourceMappingURL=deduper.spec.js.map