Skip to content

Commit

Permalink
fix: reset refreshTime on account change
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri committed May 28, 2024
1 parent 11cc267 commit e8b5283
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
100 changes: 100 additions & 0 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3927,5 +3927,105 @@ describe('NftController', () => {

expect(spy).toHaveBeenCalledTimes(1);
});

it('should call getNftInformation only one time per interval', async () => {
const { nftController, triggerPreferencesStateChange } =
setupController();
const { selectedAddress } = nftController.config;
const spy = jest.spyOn(nftController, 'updateNft');
const testNetworkClientId = 'sepolia';
await nftController.addNft('0xtest', '3', {
nftMetadata: { name: '', description: '', image: '', standard: '' },
networkClientId: testNetworkClientId,
});

sinon
.stub(nftController, 'getNftInformation' as keyof typeof nftController)
.returns({
name: 'name pudgy',
image: 'url pudgy',
description: 'description pudgy',
});
const testInputNfts: Nft[] = [
{
address: '0xtest',
description: null,
favorite: false,
image: null,
isCurrentlyOwned: true,
name: null,
standard: 'ERC721',
tokenId: '3',
tokenURI: 'https://api.pudgypenguins.io/lil/4',
},
];

// Make first call to updateNftMetadata should trigger state update
await nftController.updateNftMetadata({
nfts: testInputNfts,
networkClientId: testNetworkClientId,
});
expect(spy).toHaveBeenCalledTimes(1);

expect(
nftController.state.allNfts[selectedAddress][SEPOLIA.chainId][0],
).toStrictEqual({
address: '0xtest',
description: 'description pudgy',
image: 'url pudgy',
name: 'name pudgy',
tokenId: '3',
standard: 'ERC721',
favorite: false,
isCurrentlyOwned: true,
tokenURI: 'https://api.pudgypenguins.io/lil/4',
});

spy.mockClear();

// trigger calling updateNFTMetadata again on the same account should not trigger state update
const spy2 = jest.spyOn(nftController, 'updateNft');
await nftController.updateNftMetadata({
nfts: testInputNfts,
networkClientId: testNetworkClientId,
});
// No updates to state should be made
expect(spy2).toHaveBeenCalledTimes(0);

// trigger preference change and change selectedAccount
const testNewAccountAddress = 'OxDifferentAddress';
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: testNewAccountAddress,
});

spy.mockClear();
await nftController.addNft('0xtest', '4', {
nftMetadata: { name: '', description: '', image: '', standard: '' },
networkClientId: testNetworkClientId,
});

const testInputNfts2: Nft[] = [
{
address: '0xtest',
description: null,
favorite: false,
image: null,
isCurrentlyOwned: true,
name: null,
standard: 'ERC721',
tokenId: '4',
tokenURI: 'https://api.pudgypenguins.io/lil/4',
},
];

const spy3 = jest.spyOn(nftController, 'updateNft');
await nftController.updateNftMetadata({
nfts: testInputNfts2,
networkClientId: testNetworkClientId,
});
// When the account changed, and updateNftMetadata is called state update should be triggered
expect(spy3).toHaveBeenCalledTimes(1);
});
});
});
5 changes: 5 additions & 0 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,11 @@ export class NftController extends BaseControllerV1<NftConfig, NftState> {
openSeaEnabled,
isIpfsGatewayEnabled,
}) => {
const { selectedAddress: previouslySelectedAddress } = this.config;
if (selectedAddress !== previouslySelectedAddress) {
this.lastNftRefreshTime = 0;
}

this.configure({
selectedAddress,
ipfsGateway,
Expand Down

0 comments on commit e8b5283

Please sign in to comment.