Skip to content

Commit

Permalink
feat(Bonus Pagamenti Digitali): [#176457237] Remove disabled account …
Browse files Browse the repository at this point in the history
…from BPay found list (#2720)

* [#176457237] remove disabled account from BPay list

* [#176457237] fix import

* [#176457237] add tests
  • Loading branch information
fabriziofff authored Jan 14, 2021
1 parent 548ba06 commit d147418
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BPay } from "../../../../../../../../definitions/pagopa/BPay";

export const bPayAttMock: BPay = {
bankName: "Bank Name",
instituteCode: "123",
numberObfuscated: "+3934*****123",
paymentInstruments: [],
serviceState: "ATT",
uidHash: "uidHash"
};

export const bPayDisMock: BPay = {
bankName: "Bank Name",
instituteCode: "123",
numberObfuscated: "+3934*****123",
paymentInstruments: [],
serviceState: "DIS",
uidHash: "uidHash"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { applicationChangeState } from "../../../../../../../store/actions/application";
import { appReducer } from "../../../../../../../store/reducers";
import { GlobalState } from "../../../../../../../store/reducers/types";
import { NetworkError } from "../../../../../../../utils/errors";
import {
remoteError,
remoteLoading,
remoteReady,
remoteUndefined
} from "../../../../../../bonus/bpd/model/RemoteValue";
import { searchUserBPay } from "../../actions";
import { bPayAttMock, bPayDisMock } from "../__mock__/bpay.mock";
import { onboardingBPayFoundAccountsSelector } from "../foundBpay";

jest.mock("@react-native-community/async-storage", () => ({
AsyncStorage: jest.fn()
}));

jest.mock("react-native-share", () => ({
open: jest.fn()
}));

describe("test onboardingBPayFoundAccountsSelector", () => {
const globalState: GlobalState = appReducer(
undefined,
applicationChangeState("active")
);
it("should return RemoteUndefined with the default state", () => {
expect(onboardingBPayFoundAccountsSelector(globalState)).toBe(
remoteUndefined
);
});
it("should return RemoteLoading after dispatch searchUserBPay.request", () => {
const loadingState: GlobalState = appReducer(
globalState,
searchUserBPay.request(undefined)
);

expect(onboardingBPayFoundAccountsSelector(loadingState)).toBe(
remoteLoading
);
});
it("should return RemoteError after dispatch searchUserBPay.failure", () => {
const error: NetworkError = { kind: "timeout" };

const loadingState: GlobalState = appReducer(
globalState,
searchUserBPay.failure(error)
);

expect(onboardingBPayFoundAccountsSelector(loadingState)).toStrictEqual(
remoteError(error)
);
});
it("should return remoteReady with a BPay serviceState===ATT", () => {
const loadingState: GlobalState = appReducer(
globalState,
searchUserBPay.success([bPayAttMock])
);

expect(onboardingBPayFoundAccountsSelector(loadingState)).toStrictEqual(
remoteReady([bPayAttMock])
);
});
it("should return remoteReady with an empty array if BPay serviceState===DIS", () => {
const loadingState: GlobalState = appReducer(
globalState,
searchUserBPay.success([bPayDisMock])
);

expect(onboardingBPayFoundAccountsSelector(loadingState)).toStrictEqual(
remoteReady([])
);
});
it("should return remoteReady with only BPay with serviceState!==DIS", () => {
const loadingState: GlobalState = appReducer(
globalState,
searchUserBPay.success([bPayDisMock, bPayAttMock])
);

expect(onboardingBPayFoundAccountsSelector(loadingState)).toStrictEqual(
remoteReady([bPayAttMock])
);
});
it("should return a BPay with a generic serviceState", () => {
const unknownServiceState = { ...bPayAttMock, serviceState: "UNKNOWN" };

const loadingState: GlobalState = appReducer(
globalState,
searchUserBPay.success([unknownServiceState])
);

expect(onboardingBPayFoundAccountsSelector(loadingState)).toStrictEqual(
remoteReady([unknownServiceState])
);
});
it("should return a BPay with an undefined serviceState", () => {
const undefinedServiceState = { ...bPayAttMock, serviceState: undefined };

const loadingState: GlobalState = appReducer(
globalState,
searchUserBPay.success([undefinedServiceState])
);

expect(onboardingBPayFoundAccountsSelector(loadingState)).toStrictEqual(
remoteReady([undefinedServiceState])
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { getType } from "typesafe-actions";
import { BPay } from "../../../../../../../definitions/pagopa/BPay";
import { Action } from "../../../../../../store/actions/types";
import { GlobalState } from "../../../../../../store/reducers/types";
import { NetworkError } from "../../../../../../utils/errors";
import {
isError,
isReady,
remoteError,
remoteLoading,
remoteReady,
remoteUndefined,
RemoteValue
} from "../../../../../bonus/bpd/model/RemoteValue";
import { searchUserBPay } from "../actions";
import { NetworkError } from "../../../../../../utils/errors";

export type RemoteBPay = RemoteValue<ReadonlyArray<BPay>, NetworkError>;

Expand All @@ -30,10 +31,27 @@ const foundBpayReducer = (
return state;
};

/**
* Return {@link RemoteBPay}, a list of BPay accounts to be viewed by the user.
* Remove from the list the disabled accounts
* @param state
*/
export const onboardingBPayFoundAccountsSelector = (
state: GlobalState
): RemoteBPay => state.wallet.onboarding.bPay.foundBPay;
): RemoteBPay =>
isReady(state.wallet.onboarding.bPay.foundBPay)
? remoteReady(
state.wallet.onboarding.bPay.foundBPay.value.filter(
// Remove from the found accounts the disabled (serviceState === "DIS")
bPay => bPay.serviceState !== "DIS"
)
)
: state.wallet.onboarding.bPay.foundBPay;

/**
* The search BPay account APi have an error
* @param state
*/
export const onboardingBpayFoundAccountsIsError = (
state: GlobalState
): boolean => isError(state.wallet.onboarding.bPay.foundBPay);
Expand Down

0 comments on commit d147418

Please sign in to comment.