Skip to content

Commit

Permalink
Merge remote-tracking branch 'australiangreens/integration/civicrm' i…
Browse files Browse the repository at this point in the history
…nto stage-main-12-2
  • Loading branch information
crayolakat authored and codygordon committed Jul 4, 2022
1 parent f04bfee commit 8658b11
Show file tree
Hide file tree
Showing 20 changed files with 2,166 additions and 0 deletions.
109 changes: 109 additions & 0 deletions __test__/extensions/action-handlers/civicrm-addtag.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { when } from "jest-when";
import {
validateActionHandler,
validateActionHandlerWithClientChoices
} from "../../../src/extensions/action-handlers";

import * as HandlerToTest from "../../../src/extensions/action-handlers/civicrm-addtag";
import { getConfig, hasConfig } from "../../../src/server/api/lib/config";
import { searchTags } from "../../../src/extensions/contact-loaders/civicrm/util";
import { getCacheLength } from "../../../src/extensions/contact-loaders/civicrm/getcachelength";
import { CIVICRM_ACTION_HANDLER_ADDTAG } from "../../../src/extensions/contact-loaders/civicrm/const";

jest.mock("../../../src/server/api/lib/config");
jest.mock("../../../src/extensions/contact-loaders/civicrm/util");
jest.mock("../../../src/extensions/contact-loaders/civicrm/getcachelength");

describe("civicrm-addtag", () => {
beforeEach(async () => {
when(hasConfig)
.calledWith("CIVICRM_API_KEY")
.mockReturnValue(true);
when(hasConfig)
.calledWith("CIVICRM_SITE_KEY")
.mockReturnValue(true);
when(hasConfig)
.calledWith("CIVICRM_API_URL")
.mockReturnValue(true);
});

afterEach(async () => {
jest.restoreAllMocks();
});

it("passes validation, and comes with standard action handler functionality", async () => {
expect(() => validateActionHandler(HandlerToTest)).not.toThrowError();
expect(() =>
validateActionHandlerWithClientChoices(HandlerToTest)
).not.toThrowError();
expect(HandlerToTest.name).toEqual(CIVICRM_ACTION_HANDLER_ADDTAG);
expect(HandlerToTest.displayName()).toEqual("Add tag to CiviCRM contact");
expect(await HandlerToTest.processDeletedQuestionResponse()).toEqual(
undefined
);
expect(HandlerToTest.clientChoiceDataCacheKey({ id: 1 })).toEqual("1");
});

describe("civicrm-addtag available()", () => {
it("is available if the civicrm contact loader is available", async () => {
when(getConfig)
.calledWith("CONTACT_LOADERS")
.mockReturnValue("civicrm");
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_ADDTAG)
.mockReturnValue(1800);
expect(await HandlerToTest.available({ id: 1 })).toEqual({
result: true,
expiresSeconds: 1800
});
});

it("is not available if the civicrm contact loader is not available", async () => {
when(getConfig)
.calledWith("CONTACT_LOADERS")
.mockReturnValue("");
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_ADDTAG)
.mockReturnValue(1800);
expect(await HandlerToTest.available({ id: 1 })).toEqual({
result: false,
expiresSeconds: 1800
});
});
});

describe("civicrm-addtag getClientChoiceData()", () => {
it("returns successful data when data is available", async () => {
const theTagData = [
{ id: "2", name: "Company" },
{ id: "3", name: "Government Entity" },
{ id: "4", name: "Major Donor" },
{ id: "1", name: "Non-profit" },
{ id: "5", name: "Volunteer" }
];

when(searchTags).mockResolvedValue(theTagData);
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_ADDTAG)
.mockReturnValue(7200);
expect(await HandlerToTest.getClientChoiceData({ id: 1 })).toEqual({
data:
'{"items":[{"name":"Company","details":"2"},{"name":"Government Entity","details":"3"},{"name":"Major Donor","details":"4"},{"name":"Non-profit","details":"1"},{"name":"Volunteer","details":"5"}]}',
expiresSeconds: 7200
});
});

it("returns successful data when data is empty", async () => {
const theTagData = [];

when(searchTags).mockResolvedValue(theTagData);
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_ADDTAG)
.mockReturnValue(7200);
expect(await HandlerToTest.getClientChoiceData({ id: 1 })).toEqual({
data: '{"items":[]}',
expiresSeconds: 7200
});
});
});
});
112 changes: 112 additions & 0 deletions __test__/extensions/action-handlers/civicrm-addtogroup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { when } from "jest-when";
import {
validateActionHandler,
validateActionHandlerWithClientChoices
} from "../../../src/extensions/action-handlers";
import { searchGroups } from "../../../src/extensions/contact-loaders/civicrm/util";
import { getCacheLength } from "../../../src/extensions/contact-loaders/civicrm/getcachelength";
import * as HandlerToTest from "../../../src/extensions/action-handlers/civicrm-addtogroup";
import { getConfig, hasConfig } from "../../../src/server/api/lib/config";
import { CIVICRM_ACTION_HANDLER_ADDGROUP } from "../../../src/extensions/contact-loaders/civicrm/const";

jest.mock("../../../src/server/api/lib/config");
jest.mock("../../../src/extensions/contact-loaders/civicrm/util");
jest.mock("../../../src/extensions/contact-loaders/civicrm/getcachelength");

describe("civicrm-addtogroup", () => {
beforeEach(async () => {
when(hasConfig)
.calledWith("CIVICRM_API_KEY")
.mockReturnValue(true);
when(hasConfig)
.calledWith("CIVICRM_SITE_KEY")
.mockReturnValue(true);
when(hasConfig)
.calledWith("CIVICRM_API_URL")
.mockReturnValue(true);
});

afterEach(async () => {
jest.restoreAllMocks();
});

it("passes validation, and comes with standard action handler functionality", async () => {
expect(() => validateActionHandler(HandlerToTest)).not.toThrowError();
expect(() =>
validateActionHandlerWithClientChoices(HandlerToTest)
).not.toThrowError();
expect(HandlerToTest.name).toEqual(CIVICRM_ACTION_HANDLER_ADDGROUP);
expect(HandlerToTest.displayName()).toEqual("Add to CiviCRM group");
expect(await HandlerToTest.processDeletedQuestionResponse()).toEqual(
undefined
);
expect(HandlerToTest.clientChoiceDataCacheKey({ id: 1 })).toEqual("1");
});

describe("civicrm-addtogroup available()", () => {
it("is available if the civicrm contact loader is available", async () => {
when(getConfig)
.calledWith("CONTACT_LOADERS")
.mockReturnValue("civicrm");
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_ADDGROUP)
.mockReturnValue(1800);
expect(await HandlerToTest.available({ id: 1 })).toEqual({
result: true,
expiresSeconds: 1800
});
});

it("is not available if the civicrm contact loader is not available", async () => {
when(getConfig)
.calledWith("CONTACT_LOADERS")
.mockReturnValue("");
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_ADDGROUP)
.mockReturnValue(1800);
expect(await HandlerToTest.available({ id: 1 })).toEqual({
result: false,
expiresSeconds: 1800
});
});
});

describe("civicrm-addtag getClientChoiceData()", () => {
it("returns successful data when data is available", async () => {
const theGroupData = [
{ id: "2", title: "Administrators" },
{ id: "3", title: "Volunteers" },
{ id: "4", title: "Donors" },
{ id: "1", title: "Newsletter Subscribers" },
{ id: "5", title: "Volunteer" }
];

when(searchGroups)
.calledWith("")
.mockResolvedValue(theGroupData);
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_ADDGROUP)
.mockReturnValue(7200);
expect(await HandlerToTest.getClientChoiceData({ id: 1 })).toEqual({
data:
'{"items":[{"name":"Administrators","details":"2"},{"name":"Volunteers","details":"3"},{"name":"Donors","details":"4"},{"name":"Newsletter Subscribers","details":"1"},{"name":"Volunteer","details":"5"}]}',
expiresSeconds: 7200
});
});

it("returns successful data when data is empty", async () => {
const theGroupData = [];

when(searchGroups)
.calledWith("")
.mockResolvedValue(theGroupData);
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_ADDGROUP)
.mockReturnValue(7200);
expect(await HandlerToTest.getClientChoiceData({ id: 1 })).toEqual({
data: '{"items":[]}',
expiresSeconds: 7200
});
});
});
});
118 changes: 118 additions & 0 deletions __test__/extensions/action-handlers/civicrm-registerevent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { when } from "jest-when";
import {
validateActionHandler,
validateActionHandlerWithClientChoices
} from "../../../src/extensions/action-handlers";
import { searchEvents } from "../../../src/extensions/contact-loaders/civicrm/util";
import { getCacheLength } from "../../../src/extensions/contact-loaders/civicrm/getcachelength";
import * as HandlerToTest from "../../../src/extensions/action-handlers/civicrm-registerevent";
import { getConfig, hasConfig } from "../../../src/server/api/lib/config";
import {
CIVICRM_ACTION_HANDLER_REGISTEREVENT,
CIVICRM_CONTACT_LOADER
} from "../../../src/extensions/contact-loaders/civicrm/const";

jest.mock("../../../src/server/api/lib/config");
jest.mock("../../../src/extensions/contact-loaders/civicrm/util");
jest.mock("../../../src/extensions/contact-loaders/civicrm/getcachelength");

describe("civicrm-registerevent", () => {
beforeEach(async () => {
when(hasConfig)
.calledWith("CIVICRM_API_KEY")
.mockReturnValue(true);
when(hasConfig)
.calledWith("CIVICRM_SITE_KEY")
.mockReturnValue(true);
when(hasConfig)
.calledWith("CIVICRM_API_URL")
.mockReturnValue(true);
});

afterEach(async () => {
jest.restoreAllMocks();
});

it("passes validation, and comes with standard action handler functionality", async () => {
when(getCacheLength)
.calledWith(CIVICRM_CONTACT_LOADER)
.mockReturnValue(7200);
expect(() => validateActionHandler(HandlerToTest)).not.toThrowError();
expect(() =>
validateActionHandlerWithClientChoices(HandlerToTest)
).not.toThrowError();
expect(HandlerToTest.name).toEqual(CIVICRM_ACTION_HANDLER_REGISTEREVENT);
expect(HandlerToTest.displayName()).toEqual(
"Register contact for CiviCRM event"
);
expect(await HandlerToTest.processDeletedQuestionResponse()).toEqual(
undefined
);
expect(HandlerToTest.clientChoiceDataCacheKey({ id: 1 })).toEqual("1");
});

describe("civicrm-registerevent available()", () => {
it("is available if the civicrm contact loader is available", async () => {
when(getConfig)
.calledWith("CONTACT_LOADERS")
.mockReturnValue("civicrm");
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_REGISTEREVENT)
.mockReturnValue(1800);
expect(await HandlerToTest.available({ id: 1 })).toEqual({
result: true,
expiresSeconds: 1800
});
});

it("is not available if the civicrm contact loader is not available", async () => {
when(getConfig)
.calledWith("CONTACT_LOADERS")
.mockReturnValue("");
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_REGISTEREVENT)
.mockReturnValue(1800);
expect(await HandlerToTest.available({ id: 1 })).toEqual({
result: false,
expiresSeconds: 1800
});
});
});

describe("civicrm-registerevent getClientChoiceData()", () => {
it("returns successful data when data is available", async () => {
const theEventData = [
{
id: "2",
title: "Company",
event_title: "Demo Event",
default_role_id: "1",
start_date: "2021-11-01 16:00:00"
}
];

when(searchEvents).mockResolvedValue(theEventData);
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_REGISTEREVENT)
.mockReturnValue(7200);
expect(await HandlerToTest.getClientChoiceData({ id: 1 })).toEqual({
data:
'{"items":[{"name":"Company (2021-11-01)","details":"{\\"id\\":\\"2\\",\\"role_id\\":\\"1\\"}"}]}',
expiresSeconds: 7200
});
});

it("returns successful data when data is empty", async () => {
const theEventData = [];

when(searchEvents).mockResolvedValue(theEventData);
when(getCacheLength)
.calledWith(CIVICRM_ACTION_HANDLER_REGISTEREVENT)
.mockReturnValue(7200);
expect(await HandlerToTest.getClientChoiceData({ id: 1 })).toEqual({
data: '{"items":[]}',
expiresSeconds: 7200
});
});
});
});
Loading

0 comments on commit 8658b11

Please sign in to comment.