2023-02-07 19:06:37 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { fireEvent } from '@testing-library/react';
|
|
|
|
import configureMockStore from 'redux-mock-store';
|
|
|
|
import thunk from 'redux-thunk';
|
2023-06-09 22:48:48 +02:00
|
|
|
import { toHex } from '@metamask/controller-utils';
|
2023-02-07 19:06:37 +01:00
|
|
|
import mockState from '../../../../test/data/mock-state.json';
|
|
|
|
import { renderWithProvider } from '../../../../test/lib/render-helpers';
|
2023-02-16 20:23:29 +01:00
|
|
|
import { updateNftDropDownState } from '../../../store/actions';
|
|
|
|
import NftsItems from '.';
|
2023-02-07 19:06:37 +01:00
|
|
|
|
|
|
|
const mockHistoryPush = jest.fn();
|
|
|
|
|
|
|
|
jest.mock('react-router-dom', () => ({
|
|
|
|
...jest.requireActual('react-router-dom'),
|
|
|
|
useLocation: jest.fn(() => ({ search: '' })),
|
|
|
|
useHistory: () => ({
|
|
|
|
push: mockHistoryPush,
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('../../../store/actions.ts', () => ({
|
|
|
|
...jest.requireActual('../../../store/actions.ts'),
|
2023-02-16 20:23:29 +01:00
|
|
|
updateNftDropDownState: jest.fn().mockReturnValue(jest.fn()),
|
2023-02-07 19:06:37 +01:00
|
|
|
}));
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
describe('NFTs Item Component', () => {
|
|
|
|
const nfts =
|
2023-06-09 22:48:48 +02:00
|
|
|
mockState.metamask.allNfts[mockState.metamask.selectedAddress][toHex(5)];
|
2023-02-07 19:06:37 +01:00
|
|
|
const props = {
|
|
|
|
collections: {
|
|
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': {
|
2023-02-16 20:23:29 +01:00
|
|
|
nfts,
|
2023-02-07 19:06:37 +01:00
|
|
|
collectionImage: '',
|
2023-02-16 20:23:29 +01:00
|
|
|
collectionName: 'NFT Collection',
|
2023-02-07 19:06:37 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
previouslyOwnedCollection: {
|
2023-02-16 20:23:29 +01:00
|
|
|
nfts: [],
|
2023-02-07 19:06:37 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockStore = configureMockStore([thunk])(mockState);
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
it('should expand NFT collection showing individual NFTs', async () => {
|
2023-03-09 17:36:12 +01:00
|
|
|
const { queryByTestId, queryAllByTestId } = renderWithProvider(
|
2023-02-16 20:23:29 +01:00
|
|
|
<NftsItems {...props} />,
|
2023-02-07 19:06:37 +01:00
|
|
|
mockStore,
|
|
|
|
);
|
|
|
|
|
|
|
|
const collectionExpanderButton = queryByTestId(
|
|
|
|
'collection-expander-button',
|
|
|
|
);
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
expect(queryAllByTestId('nft-wrapper')).toHaveLength(0);
|
2023-02-07 19:06:37 +01:00
|
|
|
|
|
|
|
fireEvent.click(collectionExpanderButton);
|
|
|
|
|
2023-03-09 17:36:12 +01:00
|
|
|
const expectedParams = {
|
2023-02-07 19:06:37 +01:00
|
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': {
|
|
|
|
'0x5': {
|
|
|
|
'0x495f947276749Ce646f68AC8c248420045cb7b5e': false,
|
2023-03-09 17:36:12 +01:00
|
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': true,
|
2023-02-07 19:06:37 +01:00
|
|
|
},
|
|
|
|
},
|
2023-03-09 17:36:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
expect(updateNftDropDownState).toHaveBeenCalledWith(expectedParams);
|
2023-02-07 19:06:37 +01:00
|
|
|
|
2023-03-09 17:36:12 +01:00
|
|
|
// Force rerender component with state/store update
|
|
|
|
mockState.metamask.nftsDropdownState = expectedParams;
|
|
|
|
renderWithProvider(<NftsItems {...props} />, mockStore);
|
2023-02-07 19:06:37 +01:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
expect(queryAllByTestId('nft-wrapper')).toHaveLength(8);
|
2023-02-07 19:06:37 +01:00
|
|
|
});
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
it('should NFT click image', () => {
|
2023-02-07 19:06:37 +01:00
|
|
|
const { queryAllByTestId } = renderWithProvider(
|
2023-02-16 20:23:29 +01:00
|
|
|
<NftsItems {...props} />,
|
2023-02-07 19:06:37 +01:00
|
|
|
mockStore,
|
|
|
|
);
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
const nftImages = queryAllByTestId('nft-image');
|
2023-08-01 20:21:51 +02:00
|
|
|
const nftDefaultImages = queryAllByTestId('nft-default-image');
|
|
|
|
if (nftImages.length) {
|
|
|
|
fireEvent.click(nftImages[0]);
|
|
|
|
} else {
|
|
|
|
fireEvent.click(nftDefaultImages[0]);
|
|
|
|
}
|
2023-02-07 19:06:37 +01:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
const firstNft = nfts[0];
|
|
|
|
const nftRoute = `/asset/${firstNft.address}/${firstNft.tokenId}`;
|
2023-02-07 19:06:37 +01:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
expect(mockHistoryPush).toHaveBeenCalledWith(nftRoute);
|
2023-02-07 19:06:37 +01:00
|
|
|
});
|
|
|
|
});
|