Skip to content

Commit

Permalink
clean up saga logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ratik21 committed Sep 4, 2024
1 parent 296089d commit 2236028
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Container', () => {
const state = {
authentication: { user: {} },
web3: { value: {} },
accountManagement: { errors: [], isWalletSelectModalOpen: false },
accountManagement: { errors: [] },
...inputState,
} as RootState;
return Container.mapState(state);
Expand Down
12 changes: 1 addition & 11 deletions src/store/account-management/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import { Connectors } from '../../lib/web3';
export enum SagaActionTypes {
AddNewWallet = 'Wallets/addNewWallet',
AddEmailAccount = 'Wallets/addEmailAccount',
OpenWalletSelectModal = 'Wallets/openWalletSelectModal',
CloseWalletSelectModal = 'Wallets/closeWalletSelectModal',
OpenAddEmailAccountModal = 'Wallets/openAddEmailAccountModal',
CloseAddEmailAccountModal = 'Wallets/closeAddEmailAccountModal',
}

export type AccountManagementState = {
errors: string[];
isWalletSelectModalOpen: boolean;
isAddEmailAccountModalOpen: boolean;
successMessage: string;
};

export const initialState: AccountManagementState = {
errors: [],
isWalletSelectModalOpen: false,
isAddEmailAccountModalOpen: false,
successMessage: '',
};
Expand All @@ -30,8 +26,6 @@ export enum Errors {

export const addNewWallet = createAction<{ connector: Connectors }>(SagaActionTypes.AddNewWallet);
export const addEmailAccount = createAction<{ email: string; password: string }>(SagaActionTypes.AddEmailAccount);
export const openWalletSelectModal = createAction(SagaActionTypes.OpenWalletSelectModal);
export const closeWalletSelectModal = createAction(SagaActionTypes.CloseWalletSelectModal);
export const openAddEmailAccountModal = createAction(SagaActionTypes.OpenAddEmailAccountModal);
export const closeAddEmailAccountModal = createAction(SagaActionTypes.CloseAddEmailAccountModal);

Expand All @@ -42,9 +36,6 @@ const slice = createSlice({
setErrors: (state, action: PayloadAction<AccountManagementState['errors']>) => {
state.errors = action.payload;
},
setWalletSelectModalStatus: (state, action: PayloadAction<AccountManagementState['isWalletSelectModalOpen']>) => {
state.isWalletSelectModalOpen = action.payload;
},
setAddEmailAccountModalStatus: (
state,
action: PayloadAction<AccountManagementState['isAddEmailAccountModalOpen']>
Expand All @@ -57,6 +48,5 @@ const slice = createSlice({
},
});

export const { setErrors, setWalletSelectModalStatus, setAddEmailAccountModalStatus, setSuccessMessage } =
slice.actions;
export const { setErrors, setAddEmailAccountModalStatus, setSuccessMessage } = slice.actions;
export const { reducer } = slice;
43 changes: 1 addition & 42 deletions src/store/account-management/saga.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
import { expectSaga } from '../../test/saga';
import {
openWalletSelectModal,
closeWalletSelectModal,
linkNewWalletToZEROAccount,
openAddEmailAccountModal,
closeAddEmailAccountModal,
addEmailToZEROAccount,
updateCurrentUserPrimaryEmail,
} from './saga';
import { call } from 'redux-saga/effects';

import { setAddEmailAccountModalStatus, setWalletSelectModalStatus } from '.';
import { setAddEmailAccountModalStatus } from '.';
import { rootReducer } from '../reducer';
import { addEmailAccount } from '../registration/saga';
import { StoreBuilder } from '../test/store';

describe('openWalletSelectModal', () => {
it('opens the wallet select modal', async () => {
const initialState = new StoreBuilder().build();
const { storeState } = await expectSaga(openWalletSelectModal)
.withReducer(rootReducer, initialState)
.put(setWalletSelectModalStatus(true))
.run();

expect(storeState.accountManagement.isWalletSelectModalOpen).toEqual(true);
});
});

describe('closeWalletSelectModal', () => {
it('closes the wallet select modal', async () => {
const initialState = new StoreBuilder().withAccountManagement({ isWalletSelectModalOpen: true });

const { storeState } = await expectSaga(closeWalletSelectModal)
.withReducer(rootReducer, initialState.build())
.put(setWalletSelectModalStatus(false))
.run();

expect(storeState.accountManagement.isWalletSelectModalOpen).toEqual(false);
});
});

describe('addEmailAccountModal', () => {
it('opens the add email account modal', async () => {
const initialState = new StoreBuilder().build();
Expand All @@ -63,19 +35,6 @@ describe('addEmailAccountModal', () => {
});
});

// todo: this will change when we have actual wallet select implementation
describe('linkNewWalletToZEROAccount', () => {
it('closes the wallet select modal', async () => {
const connector = 'MetaMask';
const initialState = new StoreBuilder().withAccountManagement({ isWalletSelectModalOpen: true });
const { storeState } = await expectSaga(linkNewWalletToZEROAccount, { payload: { connector } })
.withReducer(rootReducer, initialState.build())
.run();

expect(storeState.accountManagement.isWalletSelectModalOpen).toEqual(false);
});
});

describe(addEmailToZEROAccount, () => {
it('close modal & sets success message when email is added successfully', async () => {
const email = '[email protected]';
Expand Down
21 changes: 2 additions & 19 deletions src/store/account-management/saga.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { call, put, select, spawn, take, takeLeading } from 'redux-saga/effects';

import {
Errors,
SagaActionTypes,
setAddEmailAccountModalStatus,
setErrors,
setSuccessMessage,
setWalletSelectModalStatus,
} from '.';
import { Errors, SagaActionTypes, setAddEmailAccountModalStatus, setErrors, setSuccessMessage } from '.';

import { addEmailAccount } from '../registration/saga';
import { currentUserSelector } from '../authentication/saga';
Expand All @@ -25,7 +18,7 @@ export function* linkNewWalletToZEROAccount(action) {
console.log('Connector: ', connector);

try {
yield call(closeWalletSelectModal);
//yield call(closeWalletSelectModal);
} catch (e) {
yield put(setErrors([Errors.UNKNOWN_ERROR]));
} finally {
Expand Down Expand Up @@ -56,14 +49,6 @@ export function* addEmailToZEROAccount(action) {
return;
}

export function* openWalletSelectModal() {
yield put(setWalletSelectModalStatus(true));
}

export function* closeWalletSelectModal() {
yield put(setWalletSelectModalStatus(false));
}

export function* openAddEmailAccountModal() {
yield put(setAddEmailAccountModalStatus(true));
}
Expand All @@ -86,8 +71,6 @@ export function* saga() {
yield takeLeading(SagaActionTypes.AddNewWallet, linkNewWalletToZEROAccount);
yield takeLeading(SagaActionTypes.AddEmailAccount, addEmailToZEROAccount);

yield takeLeading(SagaActionTypes.OpenWalletSelectModal, openWalletSelectModal);
yield takeLeading(SagaActionTypes.CloseWalletSelectModal, closeWalletSelectModal);
yield takeLeading(SagaActionTypes.OpenAddEmailAccountModal, openAddEmailAccountModal);
yield takeLeading(SagaActionTypes.CloseAddEmailAccountModal, closeAddEmailAccountModal);
}

0 comments on commit 2236028

Please sign in to comment.