|
| 1 | +import axios from "axios" |
| 2 | +import { APIKeyResponse, getApiKey, login, LoginResponse, logout } from "./api" |
| 3 | + |
| 4 | +// Mock the axios module so that no real network requests are made, but rather |
| 5 | +// we swap in a resolved or rejected value |
| 6 | +// |
| 7 | +// See: https://jestjs.io/docs/mock-functions#mocking-modules |
| 8 | +jest.mock("axios") |
| 9 | + |
| 10 | +describe("api.ts", () => { |
| 11 | + describe("login", () => { |
| 12 | + it("should return LoginResponse", async () => { |
| 13 | + // given |
| 14 | + const loginResponse: LoginResponse = { |
| 15 | + session_token: "abc_123_test", |
| 16 | + } |
| 17 | + const axiosMockPost = jest.fn().mockImplementationOnce(() => { |
| 18 | + return Promise.resolve({ data: loginResponse }) |
| 19 | + }) |
| 20 | + axios.post = axiosMockPost |
| 21 | + |
| 22 | + // when |
| 23 | + const result = await login("test", "123") |
| 24 | + |
| 25 | + // then |
| 26 | + expect(axiosMockPost).toHaveBeenCalled() |
| 27 | + expect(result).toStrictEqual(loginResponse) |
| 28 | + }) |
| 29 | + |
| 30 | + it("should throw an error on 401", async () => { |
| 31 | + // given |
| 32 | + // ..ensure that we await our expect assertion in async/await test |
| 33 | + expect.assertions(1) |
| 34 | + const expectedError = { |
| 35 | + message: "Validation failed", |
| 36 | + errors: [{ field: "email", code: "email" }], |
| 37 | + } |
| 38 | + const axiosMockPost = jest.fn().mockImplementationOnce(() => { |
| 39 | + return Promise.reject(expectedError) |
| 40 | + }) |
| 41 | + axios.post = axiosMockPost |
| 42 | + |
| 43 | + try { |
| 44 | + await login("test", "123") |
| 45 | + } catch (error) { |
| 46 | + expect(error).toStrictEqual(expectedError) |
| 47 | + } |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe("logout", () => { |
| 52 | + it("should return without erroring", async () => { |
| 53 | + // given |
| 54 | + const axiosMockPost = jest.fn().mockImplementationOnce(() => { |
| 55 | + return Promise.resolve() |
| 56 | + }) |
| 57 | + axios.post = axiosMockPost |
| 58 | + |
| 59 | + // when |
| 60 | + await logout() |
| 61 | + |
| 62 | + // then |
| 63 | + expect(axiosMockPost).toHaveBeenCalled() |
| 64 | + }) |
| 65 | + |
| 66 | + it("should throw an error on 500", async () => { |
| 67 | + // given |
| 68 | + // ..ensure that we await our expect assertion in async/await test |
| 69 | + expect.assertions(1) |
| 70 | + const expectedError = { |
| 71 | + message: "Failed to logout.", |
| 72 | + } |
| 73 | + const axiosMockPost = jest.fn().mockImplementationOnce(() => { |
| 74 | + return Promise.reject(expectedError) |
| 75 | + }) |
| 76 | + axios.post = axiosMockPost |
| 77 | + |
| 78 | + try { |
| 79 | + await logout() |
| 80 | + } catch (error) { |
| 81 | + expect(error).toStrictEqual(expectedError) |
| 82 | + } |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + describe("getApiKey", () => { |
| 87 | + it("should return APIKeyResponse", async () => { |
| 88 | + // given |
| 89 | + const apiKeyResponse: APIKeyResponse = { |
| 90 | + key: "abc_123_test", |
| 91 | + } |
| 92 | + const axiosMockPost = jest.fn().mockImplementationOnce(() => { |
| 93 | + return Promise.resolve({ data: apiKeyResponse }) |
| 94 | + }) |
| 95 | + axios.post = axiosMockPost |
| 96 | + |
| 97 | + // when |
| 98 | + const result = await getApiKey() |
| 99 | + |
| 100 | + // then |
| 101 | + expect(axiosMockPost).toHaveBeenCalled() |
| 102 | + expect(result).toStrictEqual(apiKeyResponse) |
| 103 | + }) |
| 104 | + |
| 105 | + it("should throw an error on 401", async () => { |
| 106 | + // given |
| 107 | + // ..ensure that we await our expect assertion in async/await test |
| 108 | + expect.assertions(1) |
| 109 | + const expectedError = { |
| 110 | + message: "No Cookie!", |
| 111 | + } |
| 112 | + const axiosMockPost = jest.fn().mockImplementationOnce(() => { |
| 113 | + return Promise.reject(expectedError) |
| 114 | + }) |
| 115 | + axios.post = axiosMockPost |
| 116 | + |
| 117 | + try { |
| 118 | + await getApiKey() |
| 119 | + } catch (error) { |
| 120 | + expect(error).toStrictEqual(expectedError) |
| 121 | + } |
| 122 | + }) |
| 123 | + }) |
| 124 | +}) |
0 commit comments