From c4d8c1cd7984c4555cf8f0d5eeab08aaa61e25e2 Mon Sep 17 00:00:00 2001 From: Ana Maksimovskikh Date: Thu, 14 Mar 2024 15:46:45 +0000 Subject: [PATCH] Add method to remove all batches for given accounts to BatchesSlice --- src/utils/redux/slices/batches.test.ts | 68 +++++++++++++++++++++++++- src/utils/redux/slices/batches.ts | 7 +++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/utils/redux/slices/batches.test.ts b/src/utils/redux/slices/batches.test.ts index d0574ce365..46da328e52 100644 --- a/src/utils/redux/slices/batches.test.ts +++ b/src/utils/redux/slices/batches.test.ts @@ -10,7 +10,7 @@ import { DefaultNetworks, Network } from "../../../types/Network"; import { Operation } from "../../../types/Operation"; import { store } from "../store"; -const { add, clear, removeItem } = batchesActions; +const { add, clear, removeItem, removeByAccounts } = batchesActions; describe("batchesSlice", () => { describe.each(DefaultNetworks)("on $name", network => { @@ -175,5 +175,71 @@ describe("batchesSlice", () => { expect(store.getState().batches[network.name]).toEqual([accountOperations]); }); }); + + describe("removeByAccounts", () => { + it("does nothing if there is no batches for the accounts", () => { + const accountOperations = makeAccountOperations( + mockImplicitAccount(1), + mockImplicitAccount(1), + [mockTezOperation(0), mockTezOperation(1)] + ); + store.dispatch(add({ operations: accountOperations, network })); + + store.dispatch(removeByAccounts({ pkhs: [mockImplicitAccount(2).address.pkh] })); + + expect(store.getState().batches[network.name]).toEqual([accountOperations]); + }); + + it("removes batches for all listed accounts", () => { + const accountOperations1 = makeAccountOperations( + mockImplicitAccount(1), + mockImplicitAccount(1), + [mockTezOperation(0), mockTezOperation(1)] + ); + const accountOperations2 = makeAccountOperations( + mockImplicitAccount(2), + mockImplicitAccount(2), + [mockTezOperation(1), mockTezOperation(3)] + ); + const accountOperations3 = makeAccountOperations( + mockImplicitAccount(3), + mockImplicitAccount(3), + [mockTezOperation(0)] + ); + store.dispatch(add({ operations: accountOperations1, network })); + store.dispatch(add({ operations: accountOperations2, network })); + store.dispatch(add({ operations: accountOperations3, network })); + + store.dispatch( + removeByAccounts({ + pkhs: [mockImplicitAccount(2).address.pkh, mockImplicitAccount(3).address.pkh], + }) + ); + + expect(store.getState().batches[network.name]).toEqual([accountOperations1]); + }); + + it("removes batches from all networks", () => { + const accountOperations1 = makeAccountOperations( + mockImplicitAccount(1), + mockImplicitAccount(1), + [mockTezOperation(0), mockTezOperation(1)] + ); + const accountOperations2 = makeAccountOperations( + mockImplicitAccount(2), + mockImplicitAccount(2), + [mockTezOperation(1), mockTezOperation(3)] + ); + store.dispatch(add({ operations: accountOperations1, network })); + store.dispatch(add({ operations: accountOperations2, network })); + store.dispatch(add({ operations: accountOperations1, network: anotherNetwork })); + store.dispatch(add({ operations: accountOperations2, network: anotherNetwork })); + + store.dispatch(removeByAccounts({ pkhs: [mockImplicitAccount(2).address.pkh] })); + + expect(store.getState().batches[network.name]).toEqual([accountOperations1]); + expect(store.getState().batches[anotherNetwork.name]).toEqual([accountOperations1]); + }); + }); }); }); diff --git a/src/utils/redux/slices/batches.ts b/src/utils/redux/slices/batches.ts index 61b8f333d2..dae472bcf0 100644 --- a/src/utils/redux/slices/batches.ts +++ b/src/utils/redux/slices/batches.ts @@ -65,6 +65,13 @@ export const batchesSlice = createSlice({ batches.splice(batchIndex, 1); } }, + removeByAccounts: (state, { payload: { pkhs } }: { payload: { pkhs: RawPkh[] } }) => { + Object.keys(state).forEach(networkName => { + const batches = state[networkName as NetworkName] || []; + const newBatches = batches.filter(batch => !pkhs.includes(batch.sender.address.pkh)); + state[networkName as NetworkName] = newBatches; + }); + }, }, });