-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for ngpvan-optout message-handler
- Loading branch information
1 parent
cd4a8ac
commit e73268c
Showing
1 changed file
with
143 additions
and
0 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
__test__/extensions/message-handlers/ngpvan-optout.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) | ||
}); | ||
}); |