-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Cover error handling cases in API tests (1/2) (#2466)
- Loading branch information
1 parent
29889f8
commit 66e1bdc
Showing
27 changed files
with
600 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { CoreDomainClient } from "@opensystemslab/planx-core"; | ||
import { getClient } from "."; | ||
import { userContext } from "../modules/auth/middleware"; | ||
import { getJWT } from "../tests/mockJWT"; | ||
|
||
test("getClient() throws an error if a store is not set", () => { | ||
expect(() => getClient()).toThrow(); | ||
}); | ||
|
||
test("getClient() returns a client if store is set", () => { | ||
const getStoreMock = jest.spyOn(userContext, "getStore"); | ||
getStoreMock.mockReturnValue({ | ||
user: { | ||
sub: "123", | ||
jwt: getJWT({ role: "teamEditor" }), | ||
}, | ||
}); | ||
|
||
const client = getClient(); | ||
|
||
expect(client).toBeDefined(); | ||
expect(client).toBeInstanceOf(CoreDomainClient); | ||
}); |
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,39 @@ | ||
import { sendEmail } from "."; | ||
import { NotifyClient } from "notifications-node-client"; | ||
import { NotifyConfig } from "../../types"; | ||
|
||
jest.mock("notifications-node-client"); | ||
|
||
const TEST_EMAIL = "[email protected]"; | ||
const mockConfig: NotifyConfig = { | ||
personalisation: { | ||
teamName: "test", | ||
emailReplyToId: "test", | ||
helpEmail: "test", | ||
helpOpeningHours: "test", | ||
helpPhone: "test", | ||
}, | ||
}; | ||
|
||
describe("sendEmail", () => { | ||
it("throws an error if an invalid template is used", async () => { | ||
await expect( | ||
// @ts-expect-error Argument of type "invalidTemplate" is not assignable to parameter | ||
sendEmail("invalidTemplate", "[email protected]", {}), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it("throw an error if an error is thrown within sendEmail()", async () => { | ||
const mockNotifyClient = NotifyClient.mock.instances[0]; | ||
mockNotifyClient.sendEmail.mockRejectedValue(new Error()); | ||
await expect(sendEmail("save", TEST_EMAIL, mockConfig)).rejects.toThrow(); | ||
}); | ||
|
||
it("throw an error if the NotifyClient errors", async () => { | ||
const mockNotifyClient = NotifyClient.mock.instances[0]; | ||
mockNotifyClient.sendEmail.mockRejectedValue({ | ||
response: { data: { errors: ["Invalid email"] } }, | ||
}); | ||
await expect(sendEmail("save", TEST_EMAIL, mockConfig)).rejects.toThrow(); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.