-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
190 additions
and
43 deletions.
There are no files selected for viewing
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
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
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
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,71 @@ | ||
import { postFeedbackToGcNotify } from "../../lib/notify/postFeedbackToGcNotify"; | ||
import handler from "../../pages/api/submit-feedback"; | ||
import { createMocks } from "node-mocks-http"; | ||
|
||
jest.mock("../../lib/notify/postFeedbackToGcNotify"); | ||
|
||
describe("Feeback widget api tests", () => { | ||
beforeEach(() => { | ||
postFeedbackToGcNotify.mockRestore(); | ||
}); | ||
|
||
it("it should post to GC Notify", async () => { | ||
postFeedbackToGcNotify.mockReturnValue( | ||
new Promise((resolve) => resolve({ ok: true })) | ||
); | ||
const { req, res } = createMocks({ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: { | ||
page: "/home", | ||
"what-was-wrong": "input-field-name", | ||
}, | ||
}); | ||
await handler(req, res); | ||
expect(res._getStatusCode()).toBe(200); | ||
expect(JSON.parse(res._getData())).toEqual({ | ||
page: "/home", | ||
"what-was-wrong": "input-field-name", | ||
}); | ||
}); | ||
|
||
it("it should error if required field isn't included", async () => { | ||
const { req, res } = createMocks({ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: { | ||
page: "/home", | ||
}, | ||
}); | ||
await handler(req, res); | ||
expect(res._getStatusCode()).toBe(400); | ||
expect(JSON.parse(res._getData())).toEqual({ | ||
message: "required field missing", | ||
}); | ||
}); | ||
|
||
it("it should error if there was a bad post request", async () => { | ||
postFeedbackToGcNotify.mockReturnValue( | ||
new Promise((resolve) => resolve({ ok: false })) | ||
); | ||
const { req, res } = createMocks({ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: { | ||
page: "/home", | ||
"what-was-wrong": "input-field-name", | ||
}, | ||
}); | ||
await handler(req, res); | ||
expect(res._getStatusCode()).toBe(500); | ||
expect(JSON.parse(res._getData())).toEqual({ | ||
message: "something went wrong", | ||
}); | ||
}); | ||
}); |
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
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
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,22 @@ | ||
export async function postFeedbackToGcNotify(data) { | ||
console.log(process.env.NOTIFY_FEEDBACK_TEMPLATE_ID); | ||
console.log(process.env.THANK_YOU_EMAIL); | ||
console.log(process.env.NOTIFY_API_KEY); | ||
return await fetch( | ||
`${process.env.NOTIFY_BASE_API_URL}/v2/notifications/email`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `ApiKey-v1 ${process.env.NOTIFY_API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
email_address: process.env.THANK_YOU_EMAIL, | ||
template_id: process.env.NOTIFY_FEEDBACK_TEMPLATE_ID, | ||
personalisation: { | ||
...data, | ||
}, | ||
}), | ||
} | ||
); | ||
} |
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
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
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 |
---|---|---|
@@ -1,5 +1,21 @@ | ||
import { postFeedbackToGcNotify } from "../../lib/notify/postFeedbackToGcNotify"; | ||
|
||
export default async function handler(req, res) { | ||
const data = req.body; | ||
|
||
res.status(200).json(data); | ||
if (!data["what-was-wrong"]) { | ||
res.status(400).json({ message: "required field missing" }); | ||
} else { | ||
try { | ||
let r = await postFeedbackToGcNotify(data); | ||
|
||
if (r.ok) { | ||
res.status(200).json(data); | ||
} else { | ||
throw new Exception("bad request"); | ||
} | ||
} catch (e) { | ||
res.status(500).json({ message: "something went wrong" }); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.