-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/fetch-jsonp' into develop
- Loading branch information
Showing
11 changed files
with
462 additions
and
81 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export type ErrorWithMessage = { | ||
message: string; | ||
}; | ||
|
||
export const isErrorWithMessage = (error: unknown): error is ErrorWithMessage => | ||
typeof error === "object" && | ||
error !== null && | ||
"message" in error && | ||
typeof (error as Record<string, unknown>).message === "string"; | ||
|
||
export const toErrorWithMessage = (maybeError: unknown): ErrorWithMessage => { | ||
if (isErrorWithMessage(maybeError)) return maybeError; | ||
|
||
try { | ||
return new Error(JSON.parse(JSON.stringify(maybeError))); | ||
} catch { | ||
// fallback in case there's an error stringifying the maybeError | ||
// like with circular references for example. | ||
return new Error(String(maybeError)); | ||
} | ||
}; | ||
|
||
export const getErrorMessage = (error: unknown) => | ||
toErrorWithMessage(error).message; |
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,6 @@ | ||
export const TEST_MAILCHIMP_URL = | ||
"https://example.us20.list-manage.com/subscribe/post?u=example&id=example"; | ||
|
||
export const TEST_EMAIL = "[email protected]"; | ||
|
||
export const TEST_MESSAGE = "TESTING Message"; |
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,27 @@ | ||
import { getErrorMessage } from "../src/errors"; | ||
|
||
describe("Error messages", () => { | ||
test("should get error message from error object", () => { | ||
const error = new Error("Test error"); | ||
expect(getErrorMessage(error)).toEqual(error.message); | ||
}); | ||
|
||
test("should get error message from error json object", () => { | ||
const errorJson = { message: "Test Error" }; | ||
expect(getErrorMessage(errorJson)).toEqual(errorJson.message); | ||
}); | ||
|
||
test("should return error message from error string", () => { | ||
const errorString = "Test Error"; | ||
expect(getErrorMessage(errorString)).toEqual(errorString); | ||
}); | ||
|
||
test("should return error message if error message has cyclic object value", () => { | ||
const jsonMock = jest.spyOn(JSON, "stringify"); | ||
jsonMock.mockImplementation(() => { | ||
throw new TypeError("cyclic object value"); | ||
}); | ||
const errorString = "Test Error"; | ||
expect(getErrorMessage(errorString)).toEqual("Test Error"); | ||
}); | ||
}); |
This file was deleted.
Oops, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import fetchJsonp from "fetch-jsonp"; | ||
import { useMailChimpForm } from "../../src"; | ||
import { TEST_EMAIL, TEST_MAILCHIMP_URL, TEST_MESSAGE } from "../const"; | ||
|
||
jest.mock("fetch-jsonp", () => | ||
jest.fn().mockImplementation(() => | ||
Promise.resolve({ | ||
ok: false, | ||
json: () => Promise.resolve({ msg: "TESTING Message" }), | ||
}) | ||
) | ||
); | ||
|
||
describe("handleSubmit mailchimp response shows not ok", () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
test("should receive failure status and message", async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useMailChimpForm(TEST_MAILCHIMP_URL) | ||
); | ||
act(() => { | ||
result.current.handleSubmit({ email: TEST_EMAIL }); | ||
}); | ||
await waitForNextUpdate(); | ||
expect(fetchJsonp).toHaveBeenCalled(); | ||
expect(result.current.loading).toBe(false); | ||
expect(result.current.success).toBe(false); | ||
expect(result.current.error).toBe(true); | ||
expect(result.current.message).toEqual(TEST_MESSAGE); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
tests/useMailChimpForm/handleSubmitFetchJsonpException.test.ts
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,31 @@ | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import fetchJsonp from "fetch-jsonp"; | ||
import { useMailChimpForm } from "../../src"; | ||
import { TEST_EMAIL, TEST_MAILCHIMP_URL, TEST_MESSAGE } from "../const"; | ||
|
||
jest.mock("fetch-jsonp", () => | ||
jest | ||
.fn() | ||
.mockImplementation(() => Promise.reject(new Error("TESTING Message"))) | ||
); | ||
|
||
describe("handleSubmit fetchJsonp throw errors", () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
test("should receive failure status and message", async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useMailChimpForm(TEST_MAILCHIMP_URL) | ||
); | ||
act(() => { | ||
result.current.handleSubmit({ email: TEST_EMAIL }); | ||
}); | ||
await waitForNextUpdate(); | ||
expect(fetchJsonp).toHaveBeenCalled(); | ||
expect(result.current.loading).toBe(false); | ||
expect(result.current.success).toBe(false); | ||
expect(result.current.error).toBe(true); | ||
expect(result.current.message).toEqual(TEST_MESSAGE); | ||
}); | ||
}); |
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,41 @@ | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import fetchJsonp from "fetch-jsonp"; | ||
import { useMailChimpForm } from "../../src"; | ||
import { TEST_EMAIL, TEST_MAILCHIMP_URL, TEST_MESSAGE } from "../const"; | ||
|
||
jest.mock("fetch-jsonp", () => | ||
jest.fn().mockImplementation(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve({ msg: "TESTING Message" }), | ||
}) | ||
) | ||
); | ||
|
||
describe("handleSubmit user subscribes successfully", () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
test("should receive success status and message", async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useMailChimpForm(TEST_MAILCHIMP_URL) | ||
); | ||
act(() => { | ||
result.current.handleSubmit({ email: TEST_EMAIL }); | ||
}); | ||
await waitForNextUpdate(); | ||
expect(fetchJsonp).toHaveBeenCalled(); | ||
expect(result.current.loading).toBe(false); | ||
expect(result.current.success).toBe(true); | ||
expect(result.current.error).toBe(false); | ||
expect(result.current.message).toEqual(TEST_MESSAGE); | ||
act(() => { | ||
result.current.reset(); | ||
}); | ||
expect(result.current.loading).toBe(false); | ||
expect(result.current.success).toBe(false); | ||
expect(result.current.error).toBe(false); | ||
expect(result.current.message).toEqual(""); | ||
}); | ||
}); |
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,28 @@ | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import { useFormFields, useMailChimpForm } from "../../src"; | ||
import { TEST_EMAIL, TEST_MAILCHIMP_URL } from "../const"; | ||
|
||
test("should return initial mailchimp form state", () => { | ||
const { result } = renderHook(() => useFormFields({ email: "" })); | ||
expect(result.current.fields).toEqual({ email: "" }); | ||
}); | ||
|
||
test("should return initial mailchimp form status", () => { | ||
const { result } = renderHook(() => useMailChimpForm(TEST_MAILCHIMP_URL)); | ||
expect(result.current.success).toBe(false); | ||
expect(result.current.error).toBe(false); | ||
expect(result.current.loading).toBe(false); | ||
expect(result.current.message).toBe(""); | ||
}); | ||
|
||
test("should return initial mailchimp form state", () => { | ||
const { result } = renderHook(() => useFormFields({ email: "" })); | ||
act(() => { | ||
result.current.handleFieldChange({ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
target: { id: "email", value: TEST_EMAIL }, | ||
}); | ||
}); | ||
expect(result.current.fields).toEqual({ email: TEST_EMAIL }); | ||
}); |