chore: accounts-table reload after partyId change (#1764)

* chore: accounts-table reload after partyId change

* chore: accounts-table reload after partyId change

Co-authored-by: maciek <maciek@vegaprotocol.io>
This commit is contained in:
macqbat 2022-10-18 12:40:26 +02:00 committed by GitHub
parent 3b41e9f2f8
commit d8544ddd00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -0,0 +1,24 @@
import { act, render } from '@testing-library/react';
import { AccountManager } from './accounts-manager';
const mockReload = jest.fn();
jest.mock('@vegaprotocol/react-helpers', () => ({
...jest.requireActual('@vegaprotocol/react-helpers'),
useDataProvider: jest.fn(() => ({
data: [],
reload: mockReload,
})),
}));
describe('AccountManager', () => {
it('change partyId should reload data provider', async () => {
const { rerender } = render(
<AccountManager partyId="partyOne" onClickAsset={jest.fn} />
);
expect(mockReload).not.toHaveBeenCalled();
await act(() => {
rerender(<AccountManager partyId="partyTwo" onClickAsset={jest.fn} />);
});
expect(mockReload).toHaveBeenCalledWith(true);
});
});

View File

@ -21,6 +21,7 @@ export const AccountManager = ({
onClickDeposit,
partyId,
}: AccountManagerProps) => {
const partyIdRef = useRef<string>(partyId);
const gridRef = useRef<AgGridReact | null>(null);
const dataRef = useRef<AccountFields[] | null>(null);
const variables = useMemo(() => ({ partyId }), [partyId]);
@ -32,11 +33,19 @@ export const AccountManager = ({
},
[gridRef]
);
const { data, loading, error } = useDataProvider<AccountFields[], never>({
const { data, loading, error, reload } = useDataProvider<
AccountFields[],
never
>({
dataProvider: aggregatedAccountsDataProvider,
update,
variables,
});
if (partyId !== partyIdRef.current) {
reload(true);
partyIdRef.current = partyId;
}
if (!dataRef.current && data) {
dataRef.current = data;
}