1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

[MMI] adds institutional duck to mmi folder (#18578)

* wip

* updates the duck

* adds test

* update duck

* run lint

* review changes

* clean up

* clean up

---------

Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com>
This commit is contained in:
António Regadas 2023-04-21 09:59:25 +01:00 committed by GitHub
parent 290353da9b
commit 16d8fc2908
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 177 additions and 0 deletions

View File

@ -0,0 +1 @@
export { default as institutionalFeature } from './institutional';

View File

@ -0,0 +1,106 @@
import { createSelector } from 'reselect';
import { createSlice } from '@reduxjs/toolkit';
import { captureException } from '@sentry/browser';
import { mmiActionsFactory } from '../../store/institutional/institution-background';
const name = 'institutionalFeatures';
const initialState = {
historicalReports: {},
complianceProjectId: '',
complianceClientId: '',
reportsInProgress: {},
};
const slice = createSlice({
name,
initialState,
reducers: {
setHistoricalReports(state, action) {
state.historicalReports[action.payload.address] = [
...action.payload.reports,
];
},
},
});
const { actions, reducer } = slice;
export default reducer;
export const getComplianceProjectId = (state) =>
state.metamask[name].complianceProjectId;
export const getComplianceClientId = (state) =>
state.metamask[name].complianceClientId;
export const getComplianceTenantSubdomain = (state) =>
state.metamask[name].complianceTenantSubdomain;
export const getComplianceHistoricalReports = (state) =>
state.metamask[name].historicalReports;
export const getComplianceReportsInProgress = (state) =>
state.metamask[name].reportsInProgress;
export const getInstitutionalConnectRequests = (state) =>
state.metamask[name].connectRequests;
export const complianceActivated = (state) =>
Boolean(state.metamask[name].complianceProjectId);
export const getComplianceHistoricalReportsByAddress = (address) =>
createSelector(getComplianceHistoricalReports, (reports) =>
reports ? reports[address] : [],
);
export const getComplianceReportsInProgressByAddress = (address) =>
createSelector(getComplianceReportsInProgress, (reports) =>
reports ? reports[address.toLowerCase()] : undefined,
);
export const fetchHistoricalReports = (address, testProjectId = undefined) => {
return async (dispatch, getState) => {
const state = getState();
const mmiActions = mmiActionsFactory();
let projectId;
// testProjectId is provided to make a test request, which checks if projectId is correct
if (!testProjectId) {
projectId = getComplianceProjectId(state);
if (!projectId) {
return;
}
}
try {
const result = await dispatch(
mmiActions.getComplianceHistoricalReportsByAddress(address, projectId),
);
dispatch(
mmiActions.syncReportsInProgress({
address,
historicalReports: result.items ? result.items : [],
}),
);
dispatch(
actions.setHistoricalReports({
address,
reports: result.items
? result.items.filter((report) => report.status !== 'inProgress')
: [],
}),
);
} catch (error) {
console.error(error);
captureException(error);
}
};
};
export function generateComplianceReport(address) {
return (dispatch, _getState) => {
const mmiActions = mmiActionsFactory();
dispatch(mmiActions.generateComplianceReport(address));
};
}
const { setHistoricalReports } = actions;
export { setHistoricalReports };

View File

@ -0,0 +1,70 @@
import InstitutionalReducer, {
fetchHistoricalReports,
getComplianceClientId,
getComplianceProjectId,
getComplianceTenantSubdomain,
getComplianceHistoricalReports,
getComplianceReportsInProgress,
getInstitutionalConnectRequests,
complianceActivated,
getComplianceReportsInProgressByAddress,
generateComplianceReport,
} from './institutional';
const mockSyncReportsInProgress = jest.fn();
const mockGenerateComplianceReport = jest.fn();
jest.mock('../../store/institutional/institution-background', () => ({
mmiActionsFactory: () => ({
generateComplianceReport: mockGenerateComplianceReport,
getComplianceHistoricalReportsByAddress: jest.fn(),
syncReportsInProgress: mockSyncReportsInProgress,
}),
}));
describe('Institutional Duck', () => {
const initState = {
historicalReports: {},
complianceProjectId: '',
complianceClientId: '',
reportsInProgress: {},
};
describe('InstitutionalReducer', () => {
it('should initialize state', () => {
expect(InstitutionalReducer(undefined, {})).toStrictEqual(initState);
});
it('should correctly return all getters values', async () => {
const state = {
metamask: {
institutionalFeatures: {
complianceProjectId: 'complianceProjectId',
complianceClientId: 'complianceClientId',
complianceTenantSubdomain: 'subdomain',
reportsInProgress: { id: [{ reportId: 'id' }] },
connectRequests: [{ id: 'id' }],
historicalReports: { id: [{ reportId: 'id' }] },
},
},
};
expect(getComplianceProjectId(state)).toBe('complianceProjectId');
expect(getComplianceClientId(state)).toBe('complianceClientId');
expect(getComplianceTenantSubdomain(state)).toBe('subdomain');
expect(getComplianceHistoricalReports(state).id[0].reportId).toBe('id');
expect(getComplianceReportsInProgress(state).id).toHaveLength(1);
expect(getInstitutionalConnectRequests(state)).toHaveLength(1);
expect(complianceActivated(state)).toBe(true);
expect(getComplianceReportsInProgressByAddress('id')(state)).toHaveLength(
1,
);
await fetchHistoricalReports('0xAddress', 'projectId')(
jest.fn().mockReturnValue({ items: [{ status: 'test' }] }),
() => state,
);
expect(mockSyncReportsInProgress).toHaveBeenCalled();
await generateComplianceReport('0xAddress')(jest.fn(), () => state);
expect(mockGenerateComplianceReport).toHaveBeenCalled();
});
});
});