Skip to content

Commit

Permalink
feat: [IOCOM-793] Rename the sagas in the features/messages/saga fo…
Browse files Browse the repository at this point in the history
…lder (#5403)

## Short description
This PR renames all the sagas in the `features/messages/saga` folder.

## List of changes proposed in this pull request
- renamed the sagas using the `handle` prefix
- created `watchMessagesSaga` to listen to all messages actions and
updated startup saga
- updated tests

## How to test
Tests should be solved correctly

---------

Co-authored-by: Andrea <[email protected]>
  • Loading branch information
adelloste and Vangaorth authored Jan 17, 2024
1 parent a320053 commit b33bafb
Show file tree
Hide file tree
Showing 25 changed files with 665 additions and 805 deletions.
13 changes: 7 additions & 6 deletions ts/api/__mocks__/backend.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* Mocked version of the BackendClient.
*/

const mockedGetSession = jest.fn();

export const BackendClient = () => ({
getSession: mockedGetSession
});
export const BackendClient = {
getMessage: jest.fn(),
getMessages: jest.fn(),
getSession: jest.fn(),
getThirdPartyMessagePrecondition: jest.fn(),
upsertMessageStatusAttributes: jest.fn()
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,28 @@ import {
paymentValidInvalidAfterDueDate,
successLoadMessageDetails
} from "../../__mocks__/message";
import { testTryLoadMessageDetails } from "../watchLoadMessageDetails";
import { withRefreshApiCall } from "../../../fastLogin/saga/utils";

const tryLoadMessageDetails = testTryLoadMessageDetails!;
import { handleLoadMessageDetails } from "../handleLoadMessageDetails";
import { BackendClient } from "../../../../api/__mocks__/backend";

const id = paymentValidInvalidAfterDueDate.id as UIMessageId;

describe("tryReloadAllMessages", () => {
describe("handleLoadMessageDetails", () => {
const getMessagesPayload = { id };

describe("when the response is successful", () => {
it(`should put ${getType(
action.success
)} with the parsed messages and pagination data`, () => {
const getMessage = jest.fn();
testSaga(tryLoadMessageDetails(getMessage), action.request({ id }))
testSaga(
handleLoadMessageDetails,
BackendClient.getMessage,
action.request({ id })
)
.next()
.call(
withRefreshApiCall,
getMessage(getMessagesPayload),
BackendClient.getMessages(getMessagesPayload),
action.request({ id })
)
// .call(getMessage, getMessagesPayload)
Expand All @@ -41,12 +43,15 @@ describe("tryReloadAllMessages", () => {

describe("when the response is an Error", () => {
it(`should put ${getType(action.failure)} with the error message`, () => {
const getMessage = jest.fn();
testSaga(tryLoadMessageDetails(getMessage), action.request({ id }))
testSaga(
handleLoadMessageDetails,
BackendClient.getMessage,
action.request({ id })
)
.next()
.call(
withRefreshApiCall,
getMessage(getMessagesPayload),
BackendClient.getMessages(getMessagesPayload),
action.request({ id })
)
.next(E.right({ status: 500, value: { title: "Backend error" } }))
Expand All @@ -58,12 +63,13 @@ describe("tryReloadAllMessages", () => {

describe("when the handler throws", () => {
it(`should catch it and put ${getType(action.failure)}`, () => {
const getMessage = jest.fn().mockImplementation(() => {
throw new Error("I made a boo-boo, sir!");
});

testSaga(tryLoadMessageDetails(getMessage), action.request({ id }))
testSaga(
handleLoadMessageDetails,
BackendClient.getMessage,
action.request({ id })
)
.next()
.throw(new Error("I made a boo-boo, sir!"))
.put(
action.failure({
id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ import * as E from "fp-ts/lib/Either";
import { testSaga } from "redux-saga-test-plan";
import { getType } from "typesafe-actions";

import {
loadNextPageMessages as action,
loadNextPageMessages
} from "../../store/actions";
import { loadNextPageMessages as action } from "../../store/actions";
import {
apiPayload,
defaultRequestPayload,
successLoadNextPageMessagesPayload
} from "../../__mocks__/messages";
import { testTryLoadNextPageMessages } from "../watchLoadNextPageMessages";
import { withRefreshApiCall } from "../../../fastLogin/saga/utils";
import { handleLoadNextPageMessages } from "../handleLoadNextPageMessages";
import { BackendClient } from "../../../../api/__mocks__/backend";

const tryLoadNextPageMessages = testTryLoadNextPageMessages!;

describe("tryLoadNextPageMessages", () => {
describe("handleLoadNextPageMessages", () => {
const getMessagesPayload = {
enrich_result_data: true,
page_size: defaultRequestPayload.pageSize,
Expand All @@ -28,16 +24,16 @@ describe("tryLoadNextPageMessages", () => {
it(`should put ${getType(
action.success
)} with the parsed messages and pagination data`, () => {
const getMessages = jest.fn();
testSaga(
tryLoadNextPageMessages(getMessages),
handleLoadNextPageMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.call(
withRefreshApiCall,
getMessages(getMessagesPayload),
loadNextPageMessages.request(defaultRequestPayload)
BackendClient.getMessages(getMessagesPayload),
action.request(defaultRequestPayload)
)
.next(E.right({ status: 200, value: apiPayload }))
.put(action.success(successLoadNextPageMessagesPayload))
Expand All @@ -48,16 +44,16 @@ describe("tryLoadNextPageMessages", () => {

describe("when the response is an Error", () => {
it(`should put ${getType(action.failure)} with the error message`, () => {
const getMessages = jest.fn();
testSaga(
tryLoadNextPageMessages(getMessages),
handleLoadNextPageMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.call(
withRefreshApiCall,
getMessages(getMessagesPayload),
loadNextPageMessages.request(defaultRequestPayload)
BackendClient.getMessages(getMessagesPayload),
action.request(defaultRequestPayload)
)
.next(E.right({ status: 500, value: { title: "Backend error" } }))
.put(
Expand All @@ -73,14 +69,13 @@ describe("tryLoadNextPageMessages", () => {

describe("when the handler throws", () => {
it(`should catch it and put ${getType(action.failure)}`, () => {
const getMessages = () => {
throw new Error("I made a boo-boo, sir!");
};
testSaga(
tryLoadNextPageMessages(getMessages),
handleLoadNextPageMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.throw(new Error("I made a boo-boo, sir!"))
.put(
action.failure({
error: new Error("I made a boo-boo, sir!"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
defaultRequestPayload,
successLoadPreviousPageMessagesPayload
} from "../../__mocks__/messages";
import { testTryLoadPreviousPageMessages } from "../watchLoadPreviousPageMessages";
import { withRefreshApiCall } from "../../../fastLogin/saga/utils";
import { handleLoadPreviousPageMessages } from "../handleLoadPreviousPageMessages";
import { BackendClient } from "../../../../api/__mocks__/backend";

const tryLoadPreviousPageMessages = testTryLoadPreviousPageMessages!;

describe("tryLoadPreviousPageMessages", () => {
describe("handleLoadPreviousPageMessages", () => {
const getMessagesPayload = {
enrich_result_data: true,
page_size: 8,
Expand All @@ -28,15 +27,15 @@ describe("tryLoadPreviousPageMessages", () => {
it(`should put ${getType(
action.success
)} with the parsed messages and pagination data`, () => {
const getMessages = jest.fn();
testSaga(
tryLoadPreviousPageMessages(getMessages),
handleLoadPreviousPageMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.call(
withRefreshApiCall,
getMessages(getMessagesPayload),
BackendClient.getMessages(getMessagesPayload),
loadPreviousPageMessages.request(defaultRequestPayload)
)
.next(E.right({ status: 200, value: apiPayload }))
Expand All @@ -48,15 +47,15 @@ describe("tryLoadPreviousPageMessages", () => {

describe("when the response is an Error", () => {
it(`should put ${getType(action.failure)} with the error message`, () => {
const getMessages = jest.fn();
testSaga(
tryLoadPreviousPageMessages(getMessages),
handleLoadPreviousPageMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.call(
withRefreshApiCall,
getMessages(getMessagesPayload),
BackendClient.getMessages(getMessagesPayload),
loadPreviousPageMessages.request(defaultRequestPayload)
)
.next(E.right({ status: 500, value: { title: "Backend error" } }))
Expand All @@ -73,14 +72,13 @@ describe("tryLoadPreviousPageMessages", () => {

describe("when the handler throws", () => {
it(`should catch it and put ${getType(action.failure)}`, () => {
const getMessages = () => {
throw new Error("I made a boo-boo, sir!");
};
testSaga(
tryLoadPreviousPageMessages(getMessages),
handleLoadPreviousPageMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.throw(new Error("I made a boo-boo, sir!"))
.put(
action.failure({
error: new Error("I made a boo-boo, sir!"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { testSaga } from "redux-saga-test-plan";
import { getType } from "typesafe-actions";
import { getMessagePrecondition } from "../../store/actions";
import { UIMessageId } from "../../types";
import { testWorkerMessagePrecondition } from "../watchMessagePrecondition";
import { testMessagePreconditionWorker } from "../handleMessagePrecondition";
import { ThirdPartyMessagePrecondition } from "../../../../../definitions/backend/ThirdPartyMessagePrecondition";
import { TagEnum as TagEnumPN } from "../../../../../definitions/backend/MessageCategoryPN";
import { withRefreshApiCall } from "../../../fastLogin/saga/utils";
import { BackendClient } from "../../../../api/__mocks__/backend";

const workerMessagePrecondition = testWorkerMessagePrecondition!;
const messagePreconditionWorker = testMessagePreconditionWorker!;

const action = {
id: "MSG001" as UIMessageId,
Expand All @@ -19,21 +20,19 @@ const mockResponseSuccess: ThirdPartyMessagePrecondition = {
markdown: "-"
};

describe("workerMessagePrecondition", () => {
describe("messagePreconditionWorker", () => {
it(`should put ${getType(
getMessagePrecondition.success
)} when the response is successful`, () => {
const getThirdPartyMessagePrecondition = jest.fn();

testSaga(
workerMessagePrecondition,
getThirdPartyMessagePrecondition,
messagePreconditionWorker,
BackendClient.getThirdPartyMessagePrecondition,
getMessagePrecondition.request(action)
)
.next()
.call(
withRefreshApiCall,
getThirdPartyMessagePrecondition(action),
BackendClient.getThirdPartyMessagePrecondition(action),
getMessagePrecondition.request(action)
)
.next(E.right({ status: 200, value: mockResponseSuccess }))
Expand All @@ -45,17 +44,15 @@ describe("workerMessagePrecondition", () => {
it(`should put ${getType(
getMessagePrecondition.failure
)} when the response is an error`, () => {
const getThirdPartyMessagePrecondition = jest.fn();

testSaga(
workerMessagePrecondition,
getThirdPartyMessagePrecondition,
messagePreconditionWorker,
BackendClient.getThirdPartyMessagePrecondition,
getMessagePrecondition.request(action)
)
.next()
.call(
withRefreshApiCall,
getThirdPartyMessagePrecondition(action),
BackendClient.getThirdPartyMessagePrecondition(action),
getMessagePrecondition.request(action)
)
.next(E.right({ status: 500, value: `response status ${500}` }))
Expand All @@ -67,11 +64,9 @@ describe("workerMessagePrecondition", () => {
it(`should put ${getType(
getMessagePrecondition.failure
)} when the handler throws an exception`, () => {
const getThirdPartyMessagePrecondition = jest.fn();

testSaga(
workerMessagePrecondition,
getThirdPartyMessagePrecondition,
messagePreconditionWorker,
BackendClient.getThirdPartyMessagePrecondition,
getMessagePrecondition.request(action)
)
.next()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import {
defaultRequestPayload,
successReloadMessagesPayload
} from "../../__mocks__/messages";
import { testTryLoadPreviousPageMessages } from "../watchReloadAllMessages";
import { withRefreshApiCall } from "../../../fastLogin/saga/utils";
import { handleReloadAllMessages } from "../handleReloadAllMessages";
import { BackendClient } from "../../../../api/__mocks__/backend";

const tryReloadAllMessages = testTryLoadPreviousPageMessages!;

describe("tryReloadAllMessages", () => {
describe("handleReloadAllMessages", () => {
const getMessagesPayload = {
enrich_result_data: true,
page_size: defaultRequestPayload.pageSize,
Expand All @@ -25,15 +24,15 @@ describe("tryReloadAllMessages", () => {
it(`should put ${getType(
action.success
)} with the parsed messages and pagination data`, () => {
const getMessages = jest.fn();
testSaga(
tryReloadAllMessages(getMessages),
handleReloadAllMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.call(
withRefreshApiCall,
getMessages(getMessagesPayload),
BackendClient.getMessages(getMessagesPayload),
action.request(defaultRequestPayload)
)
.next(E.right({ status: 200, value: apiPayload }))
Expand All @@ -45,15 +44,15 @@ describe("tryReloadAllMessages", () => {

describe("when the response is an Error", () => {
it(`should put ${getType(action.failure)} with the error message`, () => {
const getMessages = jest.fn();
testSaga(
tryReloadAllMessages(getMessages),
handleReloadAllMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.call(
withRefreshApiCall,
getMessages(getMessagesPayload),
BackendClient.getMessages(getMessagesPayload),
action.request(defaultRequestPayload)
)
.next(
Expand All @@ -70,14 +69,13 @@ describe("tryReloadAllMessages", () => {

describe("when the handler throws", () => {
it(`should catch it and put ${getType(action.failure)}`, () => {
const getMessages = () => {
throw new Error(defaultRequestError.error.message);
};
testSaga(
tryReloadAllMessages(getMessages),
handleReloadAllMessages,
BackendClient.getMessages,
action.request(defaultRequestPayload)
)
.next()
.throw(new Error(defaultRequestError.error.message))
.put(
action.failure({
error: new Error(defaultRequestError.error.message),
Expand Down
Loading

0 comments on commit b33bafb

Please sign in to comment.