Merge branch 'release/0.0.3'
This commit is contained in:
@@ -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");
|
||||
};
|
||||
|
||||
92
src/components/Contexts/SMSContext.test.jsx
Normal file
92
src/components/Contexts/SMSContext.test.jsx
Normal file
@@ -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 (
|
||||
<>
|
||||
<div data-testid="error">{message}</div>
|
||||
<div data-testid="busy">{busy.toString()}</div>
|
||||
<button
|
||||
data-testid="sendSMSNull"
|
||||
onClick={() => send(null)}
|
||||
/>
|
||||
<button
|
||||
data-testid="sendSMSNoICCID"
|
||||
onClick={() => send({messageText: "test"})}
|
||||
/>
|
||||
<button
|
||||
data-testid="sendSMSNoMessage"
|
||||
onClick={() => send({ICCID: "test"})}
|
||||
/>
|
||||
<button
|
||||
data-testid="sendSMS"
|
||||
onClick={() => send({messageText: "test", ICCID: "test"})}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
render(
|
||||
<StatusProvider>
|
||||
<SMSProvider>
|
||||
<TestComp/>
|
||||
</SMSProvider>
|
||||
</StatusProvider>
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
it("initial state", () => {
|
||||
checkBaseResults("", "false");
|
||||
});
|
||||
|
||||
it("should return an error if no data is passed", async () => {
|
||||
fireEvent.click(screen.getByTestId("sendSMSNull"));
|
||||
await waitFor(() => checkBaseResults("No SMS data provided", "false"));
|
||||
});
|
||||
|
||||
it("should return an error if no ICCID is passed", async () => {
|
||||
fireEvent.click(screen.getByTestId("sendSMSNoICCID"));
|
||||
await waitFor(() => checkBaseResults("ICCID is required", "false"));
|
||||
});
|
||||
|
||||
it("should return an error if no message is passed", async () => {
|
||||
fireEvent.click(screen.getByTestId("sendSMSNoMessage"));
|
||||
await waitFor(() => checkBaseResults("message is required", "false"));
|
||||
});
|
||||
|
||||
it("should work as expected", async () => {
|
||||
fireEvent.click(screen.getByTestId("sendSMS"));
|
||||
await waitFor(() => checkBaseResults("", "true"));
|
||||
});
|
||||
});
|
||||
});
|
||||
28
src/components/Contexts/__mocks__/SMSContext.jsx
Normal file
28
src/components/Contexts/__mocks__/SMSContext.jsx
Normal file
@@ -0,0 +1,28 @@
|
||||
import React from 'react';
|
||||
|
||||
const SMSContext = React.createContext();
|
||||
|
||||
let busy = false;
|
||||
const smsResult = {
|
||||
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 const SMSProvider = ({ children }) => {
|
||||
return <div data-testid="mocked-smsprovider">{children}</div>;
|
||||
}
|
||||
|
||||
export const useSMSContext = () => {
|
||||
return {
|
||||
busy,
|
||||
sendSMS: jest.fn(() => smsResult),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user