Skip to content

Commit

Permalink
Remove cached assets (#4046)
Browse files Browse the repository at this point in the history
* Remove cached assets not on the asset list anymore

* Simplify code

* Add test

* Improve test
  • Loading branch information
FrederikBolding authored Jul 2, 2021
1 parent d5cc2c7 commit ff35271
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/services/Store/store/asset.slice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ describe('AccountSlice', () => {
expect(actual).toEqual(expected);
});

it('addFromAPI(): merges current assets with new assets', () => {
const a1 = fAssets[10];
const a2 = fAssets[11];
const a3 = {
...fAssets[2],
isCustom: true
};
const a4 = fAssets[0];
const state = [a1, a2, a3, a4];
const modifiedEntities = [
{ ...a1, contractAddress: '0xchanged' },
{ ...a2, contractAddress: '0xchanged1' }
];
const newAssets = modifiedEntities.reduce((acc, cur) => ({ ...acc, [cur.uuid]: cur }), {});
const actual = reducer(state, addAssetsFromAPI(newAssets));
const expected = [a3, a4, ...modifiedEntities];
expect(actual).toEqual(expected);
});

it('reset(): can reset', () => {
const entity = { uuid: 'random', contractAddress: '0x0' } as ExtendedAsset;
const state = [entity];
Expand Down
6 changes: 3 additions & 3 deletions src/services/Store/store/asset.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { all, call, put, takeLatest } from 'redux-saga/effects';
import { EXCLUDED_ASSETS } from '@config';
import { MyCryptoApiService } from '@services';
import { ExtendedAsset, LSKeys, Network, NetworkId, TUuid } from '@types';
import { arrayToObj } from '@utils';
import { filter, findIndex, map, mergeRight, pipe, propEq, toPairs } from '@vendor';

import { initialLegacyState } from './legacy.initialState';
Expand Down Expand Up @@ -41,9 +42,8 @@ const slice = createSlice({
});
},
addFromAPI(state, action: PayloadAction<Record<string, ExtendedAsset>>) {
const currentAssets = state.reduce(
(acc, a: ExtendedAsset) => ({ ...acc, [a.uuid]: a }),
{} as Record<string, ExtendedAsset>
const currentAssets = arrayToObj('uuid')(
state.filter((a) => a.isCustom || a.type === 'base')
);
const mergeAssets = pipe(
(assets: Record<TUuid, ExtendedAsset>) => mergeRight(currentAssets, assets),
Expand Down

0 comments on commit ff35271

Please sign in to comment.