diff --git a/src/components/Contexts/SMSContext.jsx b/src/components/Contexts/SMSContext.jsx index c40de21..e2fb4bd 100644 --- a/src/components/Contexts/SMSContext.jsx +++ b/src/components/Contexts/SMSContext.jsx @@ -19,6 +19,10 @@ export class SMS { * @param {SMS} data */ const validateSend = (data) => { + if (data == null) { + throw new Error("No SMS data provided"); + } + if (!data.messageText) throw new Error("message is required"); if (!data.ICCID) throw new Error("ICCID is required"); }; diff --git a/src/components/Contexts/SMSContext.test.jsx b/src/components/Contexts/SMSContext.test.jsx new file mode 100644 index 0000000..92a2130 --- /dev/null +++ b/src/components/Contexts/SMSContext.test.jsx @@ -0,0 +1,92 @@ +jest.mock("../../services/smsAPI"); + +import { + render, + cleanup, + screen, + fireEvent, + waitFor, +} from "@testing-library/react"; +import { SMSProvider, useSMSContext } from "./SMSContext"; +import { StatusProvider, useStatusContext } from "./StatusContext"; + +const checkBaseResults = (error, busy) => { + expect(screen.getByTestId("error").innerHTML).toEqual(error); + expect(screen.getByTestId("busy").innerHTML).toEqual(busy); +} + +describe("SMSContext", () => { + describe("sendSMS", () => { + beforeEach(async () => { + const TestComp = () => { + const {busy, sendSMS} = useSMSContext(); + const {message, setMessage} = useStatusContext(); + const send = async (data) => { + try { + await + sendSMS(data); + } catch (e) { + setMessage(e.message); + } + } + + return ( + <> +
{message}
+
{busy.toString()}
+ + + + +`; diff --git a/src/components/SMS/Send/__snapshots__/index.test.jsx.snap b/src/components/SMS/Send/__snapshots__/index.test.jsx.snap new file mode 100644 index 0000000..d03c0ae --- /dev/null +++ b/src/components/SMS/Send/__snapshots__/index.test.jsx.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`SMS Send Component Render 1`] = `undefined`; diff --git a/src/components/SMS/Send/index.test.jsx b/src/components/SMS/Send/index.test.jsx new file mode 100644 index 0000000..29ce3dd --- /dev/null +++ b/src/components/SMS/Send/index.test.jsx @@ -0,0 +1,44 @@ +import {TEST_AUTH_OBJECT_FISKER, TEST_TOKEN_FISKER} from "../../../utils/testing"; + +jest.mock("../../Contexts/SMSContext"); +jest.mock("../../Contexts/UserContext"); + + +import SMSSend from "./index"; +import {BrowserRouter} from "react-router-dom"; +import {UserProvider, setToken} from "../../Contexts/UserContext"; +import {StatusProvider} from "../../Contexts/StatusContext"; +import addSnapshotSerializer from "../../../utils/snapshot"; +import {render, waitFor} from "@testing-library/react"; + + +const renderSendSMS = async () => { + const {container} = render( + + + + + + + + ); + await waitFor(() => { + /* render */ + }); + + return container; +} + + +describe("SMS Send Component", () => { + beforeAll(() => { + setToken(TEST_AUTH_OBJECT_FISKER); + addSnapshotSerializer(expect); + }); + + it("Render", async () => { + // setToken(TEST_AUTH_OBJECT_FISKER); + const {container} = await renderSendSMS(); + expect(container).toMatchSnapshot(); + }); +}); \ No newline at end of file diff --git a/src/services/__mocks__/smsAPI.js b/src/services/__mocks__/smsAPI.js new file mode 100644 index 0000000..6c9a21a --- /dev/null +++ b/src/services/__mocks__/smsAPI.js @@ -0,0 +1,19 @@ +const smsAPI ={ + send: async(data, token) => { + return { + smsMsgID:438222039, + status:"Pending", + messageText:"Test message", + senderLogin:"tfbenterpriseapiuser", + sentTo:"18707906682", + sentFrom: "Server", + msgType: "MT", + dateSent: "2021-08-17T15:00:00.000Z", + dateModified: "2021-08-17T15:00:00.000Z", + ICCID: "8988300000000000000", + } + } +} + + +export default smsAPI; \ No newline at end of file diff --git a/src/services/smsAPI.js b/src/services/smsAPI.js index d82355d..a01c196 100644 --- a/src/services/smsAPI.js +++ b/src/services/smsAPI.js @@ -6,10 +6,10 @@ const API_ENDPOINT = process.env.REACT_APP_OTA_SERVICE_URL; const smsAPI = { /** - * Sends a SMS to an ICCID - * @param {*} data - * @param {*} token - * @returns + * Sends an SMS to an ICCID + * @param {*} data + * @param {*} token + * @returns */ send: async (data, token) => fetch(`${API_ENDPOINT}/sms`, {