Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log errors from callback form #389

Merged
merged 1 commit into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 67 additions & 8 deletions src/Callback/Form.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,78 @@ import { render, fireEvent, waitFor } from "@testing-library/react-native"
import { postCallbackInfo } from "./callbackAPI"
import Form from "./Form"
import { CallbackStackScreens } from "../navigation"
import Logger from "../logger"
import { Alert } from "react-native"

jest.mock("./callbackAPI")
jest.mock("@react-navigation/native")
jest.mock("../logger.ts")
describe("Form", () => {
it("navigates to the success screen on a requested call back", async () => {
const navigateSpy = jest.fn()
;(useNavigation as jest.Mock).mockReturnValueOnce({ navigate: navigateSpy })
;(postCallbackInfo as jest.Mock).mockResolvedValueOnce({ kind: "success" })
const { getByLabelText } = render(<Form />)
describe("on a successful call back requested", () => {
it("navigates to the success screen", async () => {
const navigateSpy = jest.fn()
;(useNavigation as jest.Mock).mockReturnValueOnce({
navigate: navigateSpy,
})
;(postCallbackInfo as jest.Mock).mockResolvedValueOnce({
kind: "success",
})
const { getByLabelText } = render(<Form />)

fireEvent.press(getByLabelText("Submit"))
await waitFor(() => {
expect(navigateSpy).toHaveBeenCalledWith(CallbackStackScreens.Success)
fireEvent.press(getByLabelText("Submit"))
await waitFor(() => {
expect(navigateSpy).toHaveBeenCalledWith(CallbackStackScreens.Success)
})
})
})

describe("on a failed call back requested", () => {
it("displays an error to the user and logs it", async () => {
const loggMetadataSpy = jest.spyOn(Logger, "addMetadata")
const loggErrorSpy = jest.spyOn(Logger, "error")
const errorMessage = "errorMessage"
const errorNature = "Unknown"
const errorResponse = {
kind: "failure",
error: errorNature,
message: errorMessage,
}
;(postCallbackInfo as jest.Mock).mockResolvedValueOnce(errorResponse)
const { getByText, getByLabelText } = render(<Form />)

fireEvent.press(getByLabelText("Submit"))
await waitFor(() => {
expect(getByText("Something went wrong")).toBeDefined()
expect(loggMetadataSpy).toHaveBeenCalledWith("requestCallbackError", {
errorMessage,
})
expect(loggErrorSpy).toHaveBeenCalledWith(
`FailureToRequestCallback.${errorNature}.${errorMessage}`,
)
})
})
})

describe("on a error for a requested call back", () => {
it("shows an alert to the user and logs the error", async () => {
const alertSpy = jest.spyOn(Alert, "alert")
const loggErrorSpy = jest.spyOn(Logger, "error")
const errorMessage = "errorMessage"
;(postCallbackInfo as jest.Mock).mockRejectedValueOnce(
new Error(errorMessage),
)
const { getByLabelText } = render(<Form />)

fireEvent.press(getByLabelText("Submit"))
await waitFor(() => {
expect(alertSpy).toHaveBeenCalledWith(
"Something went wrong",
errorMessage,
)
expect(loggErrorSpy).toHaveBeenCalledWith(
`FailureToRequestCallback.exception.${errorMessage}`,
)
})
})
})
})
11 changes: 11 additions & 0 deletions src/Callback/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { useStatusBarEffect, CallbackStackScreens } from "../navigation"
import { useConfigurationContext } from "../ConfigurationContext"
import { Button, GlobalText } from "../components"
import * as API from "./callbackAPI"
import Logger from "../logger"

import { Spacing, Layout, Forms, Colors, Outlines, Typography } from "../styles"

const defaultErrorMessage = " "
Expand Down Expand Up @@ -66,10 +68,19 @@ const CallbackForm: FunctionComponent = () => {
if (response.kind === "success") {
navigation.navigate(CallbackStackScreens.Success)
} else {
Logger.addMetadata("requestCallbackError", {
errorMessage: response.message,
})
Logger.error(
`FailureToRequestCallback.${response.error}.${
response.message || "failure_handled_response"
}`,
)
setErrorMessage(showError(response.error))
}
setIsLoading(false)
} catch (e) {
Logger.error(`FailureToRequestCallback.exception.${e.message}`)
Alert.alert(t("common.something_went_wrong"), e.message)
setIsLoading(false)
}
Expand Down