Skip to content

Commit

Permalink
add test for ngpvan-optout message-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
engelhartrueben committed Dec 9, 2024
1 parent cd4a8ac commit e73268c
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions __test__/extensions/message-handlers/ngpvan-optout.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
const Config = require("../../../src/server/api/lib/config");
const Van = require("../../../src/extensions/message-handlers/ngpvan-optout");
const VanAction = require("../../../src/extensions/action-handlers/ngpvan-action");

describe("extensions.message-handlers.ngpvan-optout", () => {
afterEach(async () => {
jest.restoreAllMocks();
});

describe("postMessageSave", () => {
let message;
let contact;
let organization;
let handlerContext;
let body;

beforeEach(async () => {
message = {
is_from_contact: false
};

contact = {
message_status: "needsMessage",
customFields: '{"vanid": 12345}',
cell: "(123)-456-7891"
};

organization = {
id: 1
};

handlerContext = {
autoOptOutReason: "stop"
}

// Custom body for this call - this is the expected structure
body = {
"canvassContext": {
"inputTypeId": 11, // API input
"phone": {
"dialingPrefix": "1",
"phoneNumber": "1234567891",
"smsOptInStatus": "O" // opt out status
}
},
"resultCodeId": 205
};

jest.spyOn(Config, "getConfig").mockReturnValue(undefined);
jest.spyOn(Van, "available").mockReturnValue(true);

jest.spyOn(VanAction, "postCanvassResponse").mockResolvedValue(null);
});

it("delegates to its dependencies and DOES call postCanvassResponse", async () => {
const result = await Van.postMessageSave({
contact,
handlerContext,
organization
});

expect(result).toEqual({});

// This also verifies that postCanvassResponse was only called once
expect(VanAction.postCanvassResponse.mock.calls).toEqual([
[
contact,
organization,
body
]
]);
});

describe("when the handler is not available", () => {
beforeEach(async () => {
Van.available.mockReturnValue(false);
});

it("returns an empty object and DOES NOT call postCanvassResponse", async () => {
const result = await Van.postMessageSave({
message,
contact,
organization
});
expect(result).toEqual({});
expect(VanAction.postCanvassResponse.mock.calls).toHaveLength(0);
});
});

describe("when contact is null or undefined", () => {
it("returns an empty object and DOES NOT call postCanvassResponse", async () => {
const result = await Van.postMessageSave({
message,
organization
});
expect(result).toEqual({});
expect(VanAction.postCanvassResponse.mock.calls).toHaveLength(0);
});
});

describe("when no VAN Id is inclued", () => {
beforeEach(async () => {
contact = {
...contact,
customFields: '{}'
};
});

it("returns an empty object and DOES NOT call postCanvassResponse", async () => {
const result = await Van.postMessageSave({
message,
contact,
organization,
handlerContext
});

expect(result).toEqual({});
expect(VanAction.postCanvassResponse.mock.calls).toHaveLength(0);
})
})

describe("when alternate VAN ID is included", () => {
beforeEach(async () => {
contact = {
...contact,
customFields: '{"VanID": 54321}'
};
});

it("still works and DOES call postCanvassResponse", async () => {
const result = await Van.postMessageSave({
message,
contact,
organization,
handlerContext
});

expect(result).toEqual({});
expect(VanAction.postCanvassResponse.mock.calls).toHaveLength(1);
})
})
});
});

0 comments on commit e73268c

Please sign in to comment.