Skip to content

Commit

Permalink
Adapt watchNft tests to new messenger call invocations
Browse files Browse the repository at this point in the history
- Explicitly mock all messenger call invocation results
- Fix results for `'should handle ERC721 type and add pending request to ApprovalController with the OpenSea API enabled and IPFS gateway disabled'` test based on fully mocked return values
  • Loading branch information
MajorLift committed Jul 16, 2024
1 parent 3d9b76f commit 81fcaf2
Showing 1 changed file with 102 additions and 39 deletions.
141 changes: 102 additions & 39 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,25 @@ describe('NftController', () => {

const callActionSpy = jest
.spyOn(messenger, 'call')
// 1. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
.mockResolvedValue({});
// 2. `AssetsContractController:getERC721OwnerOf`
.mockResolvedValueOnce(OWNER_ADDRESS)
// 3. `AssetsContractController:getERC721TokenURI`
.mockResolvedValueOnce('https://testtokenuri.com')
// 4. `ApprovalController:addRequest`
.mockResolvedValueOnce({})
// 5. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
// 6. `AssetsContractController:getERC721AssetName`
.mockResolvedValueOnce('testERC721Name')
// 7. `AssetsContractController:getERC721AssetSymbol`
.mockResolvedValueOnce('testERC721Symbol');

await nftController.watchNft(ERC721_NFT, ERC721, 'https://test-dapp.com');
// First call is `AccountsController:getAccount`
// Fifth call is `ApprovalController:addRequest`
// Sixth call is `AccountsController:getAccount`
expect(callActionSpy).toHaveBeenCalledTimes(6);
expect(callActionSpy).toHaveBeenCalledTimes(7);
expect(callActionSpy).toHaveBeenNthCalledWith(
5,
4,
'ApprovalController:addRequest',
{
id: requestId,
Expand Down Expand Up @@ -727,16 +736,25 @@ describe('NftController', () => {

const callActionSpy = jest
.spyOn(messenger, 'call')
// 1. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
// 2. `AssetsContractController:getERC721OwnerOf`
.mockResolvedValueOnce(OWNER_ADDRESS)
// 3. `AssetsContractController:getERC721TokenURI`
.mockResolvedValueOnce('https://testtokenuri.com')
// 4. `ApprovalController:addRequest`
.mockResolvedValueOnce({})
// 5. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
.mockResolvedValue({});
// 6. `AssetsContractController:getERC721AssetName`
.mockResolvedValueOnce('testERC721Name')
// 7. `AssetsContractController:getERC721AssetSymbol`
.mockResolvedValueOnce('testERC721Symbol');

await nftController.watchNft(ERC721_NFT, ERC721, 'https://test-dapp.com');
// First call is `AccountsController:getAccount`
// Fifth call is `ApprovalController:addRequest`
// Sixth call is `AccountsController:getAccount`
expect(callActionSpy).toHaveBeenCalledTimes(6);
expect(callActionSpy).toHaveBeenCalledTimes(7);
expect(callActionSpy).toHaveBeenNthCalledWith(
5,
4,
'ApprovalController:addRequest',
{
id: requestId,
Expand Down Expand Up @@ -797,16 +815,25 @@ describe('NftController', () => {

const callActionSpy = jest
.spyOn(messenger, 'call')
// 1. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
.mockResolvedValue({});
// 2. `AssetsContractController:getERC721OwnerOf`
.mockResolvedValueOnce(OWNER_ADDRESS)
// 3. `AssetsContractController:getERC721TokenURI`
.mockResolvedValueOnce('https://testtokenuri.com')
// 4. `ApprovalController:addRequest`
.mockResolvedValueOnce({})
// 5. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
// 6. `AssetsContractController:getERC721AssetName`
.mockResolvedValueOnce('testERC721Name')
// 7. `AssetsContractController:getERC721AssetSymbol`
.mockResolvedValueOnce('testERC721Symbol');

await nftController.watchNft(ERC721_NFT, ERC721, 'https://test-dapp.com');
// First call is `AccountsController:getAccount`
// Fifth call is `ApprovalController:addRequest`
// Sixth call is `AccountsController:getAccount`
expect(callActionSpy).toHaveBeenCalledTimes(6);
expect(callActionSpy).toHaveBeenCalledTimes(7);
expect(callActionSpy).toHaveBeenNthCalledWith(
5,
4,
'ApprovalController:addRequest',
{
id: requestId,
Expand Down Expand Up @@ -868,16 +895,25 @@ describe('NftController', () => {

const callActionSpy = jest
.spyOn(messenger, 'call')
// 1. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
// 2. `AssetsContractController:getERC721OwnerOf`
.mockResolvedValueOnce(OWNER_ADDRESS)
// 3. `AssetsContractController:getERC721TokenURI`
.mockResolvedValueOnce('https://testtokenuri.com')
// 4. `ApprovalController:addRequest`
.mockResolvedValueOnce({})
// 5. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
.mockResolvedValue({});
// 6. `AssetsContractController:getERC721AssetName`
.mockResolvedValueOnce('testERC721Name')
// 7. `AssetsContractController:getERC721AssetSymbol`
.mockResolvedValueOnce('testERC721Symbol');

await nftController.watchNft(ERC721_NFT, ERC721, 'https://test-dapp.com');
// First call is `AccountsController:getAccount`
// Fifth call is `ApprovalController:addRequest`
// Sixth call is `AccountsController:getAccount`
expect(callActionSpy).toHaveBeenCalledTimes(6);
expect(callActionSpy).toHaveBeenCalledTimes(7);
expect(callActionSpy).toHaveBeenNthCalledWith(
5,
4,
'ApprovalController:addRequest',
{
id: requestId,
Expand All @@ -888,9 +924,9 @@ describe('NftController', () => {
interactingAddress: OWNER_ADDRESS,
asset: {
...ERC721_NFT,
description: null,
image: null,
name: null,
description: 'testERC721Description',
image: 'testERC721Image',
name: 'testERC721Name',
standard: ERC721,
},
},
Expand Down Expand Up @@ -942,20 +978,33 @@ describe('NftController', () => {

const callActionSpy = jest
.spyOn(messenger, 'call')
// 1. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
.mockResolvedValue({});
// 2. `AssetsContractController:getERC721OwnerOf`
.mockRejectedValueOnce(new Error('Not an ERC721 contract'))
// 3. `AssetsContractController:getERC1155BalanceOf`
.mockResolvedValueOnce(new BN(1))
// 4. `AssetsContractController:getERC721TokenURI`
.mockRejectedValueOnce(new Error('Not an ERC721 contract'))
// 5. `AssetsContractController:getERC1155TokenURI`
.mockResolvedValueOnce('https://testtokenuri.com')
// 6. `ApprovalController:addRequest`
.mockResolvedValueOnce({})
// 7. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
// 8. `AssetsContractController:getERC1155AssetName`
.mockResolvedValueOnce('testERC1155Name')
// 9. `AssetsContractController:getERC1155AssetSymbol`
.mockResolvedValueOnce('testERC1155Symbol');

await nftController.watchNft(
ERC1155_NFT,
ERC1155,
'https://etherscan.io',
);
// First call is `AccountsController:getAccount`
// Fifth call is `ApprovalController:addRequest`
// Sixth call is `AccountsController:getAccount`
expect(callActionSpy).toHaveBeenCalledTimes(6);
expect(callActionSpy).toHaveBeenCalledTimes(9);
expect(callActionSpy).toHaveBeenNthCalledWith(
5,
6,
'ApprovalController:addRequest',
{
id: requestId,
Expand Down Expand Up @@ -1014,20 +1063,34 @@ describe('NftController', () => {

const callActionSpy = jest
.spyOn(messenger, 'call')
// 1. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
// 2. `AssetsContractController:getERC721OwnerOf`
.mockRejectedValueOnce(new Error('Not an ERC721 contract'))
// 3. `AssetsContractController:getERC1155BalanceOf`
.mockResolvedValueOnce(new BN(1))
// 4. `AssetsContractController:getERC721TokenURI`
.mockRejectedValueOnce(new Error('Not an ERC721 contract'))
// 5. `AssetsContractController:getERC1155TokenURI`
.mockResolvedValueOnce('https://testtokenuri.com')
// 6. `ApprovalController:addRequest`
.mockResolvedValueOnce({})
// 7. `AccountsController:getAccount`
.mockReturnValueOnce(OWNER_ACCOUNT)
.mockResolvedValue({});
// 8. `AssetsContractController:getERC1155AssetName`
.mockResolvedValueOnce('testERC1155Name')
// 9. `AssetsContractController:getERC1155AssetSymbol`
.mockResolvedValueOnce('testERC1155Symbol');

await nftController.watchNft(
ERC1155_NFT,
ERC1155,
'https://etherscan.io',
);
// First call is `AccountsController:getAccount`
// Fifth call is `ApprovalController:addRequest`
// Sixth call is `AccountsController:getAccount`
expect(callActionSpy).toHaveBeenCalledTimes(6);

expect(callActionSpy).toHaveBeenCalledTimes(9);
expect(callActionSpy).toHaveBeenNthCalledWith(
5,
6,
'ApprovalController:addRequest',
{
id: requestId,
Expand Down

0 comments on commit 81fcaf2

Please sign in to comment.