Skip to content

Commit 81fcaf2

Browse files
committed
Adapt watchNft tests to new messenger call invocations
- 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
1 parent 3d9b76f commit 81fcaf2

File tree

1 file changed

+102
-39
lines changed

1 file changed

+102
-39
lines changed

packages/assets-controllers/src/NftController.test.ts

Lines changed: 102 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -657,16 +657,25 @@ describe('NftController', () => {
657657

658658
const callActionSpy = jest
659659
.spyOn(messenger, 'call')
660+
// 1. `AccountsController:getAccount`
660661
.mockReturnValueOnce(OWNER_ACCOUNT)
661-
.mockResolvedValue({});
662+
// 2. `AssetsContractController:getERC721OwnerOf`
663+
.mockResolvedValueOnce(OWNER_ADDRESS)
664+
// 3. `AssetsContractController:getERC721TokenURI`
665+
.mockResolvedValueOnce('https://testtokenuri.com')
666+
// 4. `ApprovalController:addRequest`
667+
.mockResolvedValueOnce({})
668+
// 5. `AccountsController:getAccount`
669+
.mockReturnValueOnce(OWNER_ACCOUNT)
670+
// 6. `AssetsContractController:getERC721AssetName`
671+
.mockResolvedValueOnce('testERC721Name')
672+
// 7. `AssetsContractController:getERC721AssetSymbol`
673+
.mockResolvedValueOnce('testERC721Symbol');
662674

663675
await nftController.watchNft(ERC721_NFT, ERC721, 'https://test-dapp.com');
664-
// First call is `AccountsController:getAccount`
665-
// Fifth call is `ApprovalController:addRequest`
666-
// Sixth call is `AccountsController:getAccount`
667-
expect(callActionSpy).toHaveBeenCalledTimes(6);
676+
expect(callActionSpy).toHaveBeenCalledTimes(7);
668677
expect(callActionSpy).toHaveBeenNthCalledWith(
669-
5,
678+
4,
670679
'ApprovalController:addRequest',
671680
{
672681
id: requestId,
@@ -727,16 +736,25 @@ describe('NftController', () => {
727736

728737
const callActionSpy = jest
729738
.spyOn(messenger, 'call')
739+
// 1. `AccountsController:getAccount`
740+
.mockReturnValueOnce(OWNER_ACCOUNT)
741+
// 2. `AssetsContractController:getERC721OwnerOf`
742+
.mockResolvedValueOnce(OWNER_ADDRESS)
743+
// 3. `AssetsContractController:getERC721TokenURI`
744+
.mockResolvedValueOnce('https://testtokenuri.com')
745+
// 4. `ApprovalController:addRequest`
746+
.mockResolvedValueOnce({})
747+
// 5. `AccountsController:getAccount`
730748
.mockReturnValueOnce(OWNER_ACCOUNT)
731-
.mockResolvedValue({});
749+
// 6. `AssetsContractController:getERC721AssetName`
750+
.mockResolvedValueOnce('testERC721Name')
751+
// 7. `AssetsContractController:getERC721AssetSymbol`
752+
.mockResolvedValueOnce('testERC721Symbol');
732753

733754
await nftController.watchNft(ERC721_NFT, ERC721, 'https://test-dapp.com');
734-
// First call is `AccountsController:getAccount`
735-
// Fifth call is `ApprovalController:addRequest`
736-
// Sixth call is `AccountsController:getAccount`
737-
expect(callActionSpy).toHaveBeenCalledTimes(6);
755+
expect(callActionSpy).toHaveBeenCalledTimes(7);
738756
expect(callActionSpy).toHaveBeenNthCalledWith(
739-
5,
757+
4,
740758
'ApprovalController:addRequest',
741759
{
742760
id: requestId,
@@ -797,16 +815,25 @@ describe('NftController', () => {
797815

798816
const callActionSpy = jest
799817
.spyOn(messenger, 'call')
818+
// 1. `AccountsController:getAccount`
800819
.mockReturnValueOnce(OWNER_ACCOUNT)
801-
.mockResolvedValue({});
820+
// 2. `AssetsContractController:getERC721OwnerOf`
821+
.mockResolvedValueOnce(OWNER_ADDRESS)
822+
// 3. `AssetsContractController:getERC721TokenURI`
823+
.mockResolvedValueOnce('https://testtokenuri.com')
824+
// 4. `ApprovalController:addRequest`
825+
.mockResolvedValueOnce({})
826+
// 5. `AccountsController:getAccount`
827+
.mockReturnValueOnce(OWNER_ACCOUNT)
828+
// 6. `AssetsContractController:getERC721AssetName`
829+
.mockResolvedValueOnce('testERC721Name')
830+
// 7. `AssetsContractController:getERC721AssetSymbol`
831+
.mockResolvedValueOnce('testERC721Symbol');
802832

803833
await nftController.watchNft(ERC721_NFT, ERC721, 'https://test-dapp.com');
804-
// First call is `AccountsController:getAccount`
805-
// Fifth call is `ApprovalController:addRequest`
806-
// Sixth call is `AccountsController:getAccount`
807-
expect(callActionSpy).toHaveBeenCalledTimes(6);
834+
expect(callActionSpy).toHaveBeenCalledTimes(7);
808835
expect(callActionSpy).toHaveBeenNthCalledWith(
809-
5,
836+
4,
810837
'ApprovalController:addRequest',
811838
{
812839
id: requestId,
@@ -868,16 +895,25 @@ describe('NftController', () => {
868895

869896
const callActionSpy = jest
870897
.spyOn(messenger, 'call')
898+
// 1. `AccountsController:getAccount`
899+
.mockReturnValueOnce(OWNER_ACCOUNT)
900+
// 2. `AssetsContractController:getERC721OwnerOf`
901+
.mockResolvedValueOnce(OWNER_ADDRESS)
902+
// 3. `AssetsContractController:getERC721TokenURI`
903+
.mockResolvedValueOnce('https://testtokenuri.com')
904+
// 4. `ApprovalController:addRequest`
905+
.mockResolvedValueOnce({})
906+
// 5. `AccountsController:getAccount`
871907
.mockReturnValueOnce(OWNER_ACCOUNT)
872-
.mockResolvedValue({});
908+
// 6. `AssetsContractController:getERC721AssetName`
909+
.mockResolvedValueOnce('testERC721Name')
910+
// 7. `AssetsContractController:getERC721AssetSymbol`
911+
.mockResolvedValueOnce('testERC721Symbol');
873912

874913
await nftController.watchNft(ERC721_NFT, ERC721, 'https://test-dapp.com');
875-
// First call is `AccountsController:getAccount`
876-
// Fifth call is `ApprovalController:addRequest`
877-
// Sixth call is `AccountsController:getAccount`
878-
expect(callActionSpy).toHaveBeenCalledTimes(6);
914+
expect(callActionSpy).toHaveBeenCalledTimes(7);
879915
expect(callActionSpy).toHaveBeenNthCalledWith(
880-
5,
916+
4,
881917
'ApprovalController:addRequest',
882918
{
883919
id: requestId,
@@ -888,9 +924,9 @@ describe('NftController', () => {
888924
interactingAddress: OWNER_ADDRESS,
889925
asset: {
890926
...ERC721_NFT,
891-
description: null,
892-
image: null,
893-
name: null,
927+
description: 'testERC721Description',
928+
image: 'testERC721Image',
929+
name: 'testERC721Name',
894930
standard: ERC721,
895931
},
896932
},
@@ -942,20 +978,33 @@ describe('NftController', () => {
942978

943979
const callActionSpy = jest
944980
.spyOn(messenger, 'call')
981+
// 1. `AccountsController:getAccount`
945982
.mockReturnValueOnce(OWNER_ACCOUNT)
946-
.mockResolvedValue({});
983+
// 2. `AssetsContractController:getERC721OwnerOf`
984+
.mockRejectedValueOnce(new Error('Not an ERC721 contract'))
985+
// 3. `AssetsContractController:getERC1155BalanceOf`
986+
.mockResolvedValueOnce(new BN(1))
987+
// 4. `AssetsContractController:getERC721TokenURI`
988+
.mockRejectedValueOnce(new Error('Not an ERC721 contract'))
989+
// 5. `AssetsContractController:getERC1155TokenURI`
990+
.mockResolvedValueOnce('https://testtokenuri.com')
991+
// 6. `ApprovalController:addRequest`
992+
.mockResolvedValueOnce({})
993+
// 7. `AccountsController:getAccount`
994+
.mockReturnValueOnce(OWNER_ACCOUNT)
995+
// 8. `AssetsContractController:getERC1155AssetName`
996+
.mockResolvedValueOnce('testERC1155Name')
997+
// 9. `AssetsContractController:getERC1155AssetSymbol`
998+
.mockResolvedValueOnce('testERC1155Symbol');
947999

9481000
await nftController.watchNft(
9491001
ERC1155_NFT,
9501002
ERC1155,
9511003
'https://etherscan.io',
9521004
);
953-
// First call is `AccountsController:getAccount`
954-
// Fifth call is `ApprovalController:addRequest`
955-
// Sixth call is `AccountsController:getAccount`
956-
expect(callActionSpy).toHaveBeenCalledTimes(6);
1005+
expect(callActionSpy).toHaveBeenCalledTimes(9);
9571006
expect(callActionSpy).toHaveBeenNthCalledWith(
958-
5,
1007+
6,
9591008
'ApprovalController:addRequest',
9601009
{
9611010
id: requestId,
@@ -1014,20 +1063,34 @@ describe('NftController', () => {
10141063

10151064
const callActionSpy = jest
10161065
.spyOn(messenger, 'call')
1066+
// 1. `AccountsController:getAccount`
1067+
.mockReturnValueOnce(OWNER_ACCOUNT)
1068+
// 2. `AssetsContractController:getERC721OwnerOf`
1069+
.mockRejectedValueOnce(new Error('Not an ERC721 contract'))
1070+
// 3. `AssetsContractController:getERC1155BalanceOf`
1071+
.mockResolvedValueOnce(new BN(1))
1072+
// 4. `AssetsContractController:getERC721TokenURI`
1073+
.mockRejectedValueOnce(new Error('Not an ERC721 contract'))
1074+
// 5. `AssetsContractController:getERC1155TokenURI`
1075+
.mockResolvedValueOnce('https://testtokenuri.com')
1076+
// 6. `ApprovalController:addRequest`
1077+
.mockResolvedValueOnce({})
1078+
// 7. `AccountsController:getAccount`
10171079
.mockReturnValueOnce(OWNER_ACCOUNT)
1018-
.mockResolvedValue({});
1080+
// 8. `AssetsContractController:getERC1155AssetName`
1081+
.mockResolvedValueOnce('testERC1155Name')
1082+
// 9. `AssetsContractController:getERC1155AssetSymbol`
1083+
.mockResolvedValueOnce('testERC1155Symbol');
10191084

10201085
await nftController.watchNft(
10211086
ERC1155_NFT,
10221087
ERC1155,
10231088
'https://etherscan.io',
10241089
);
1025-
// First call is `AccountsController:getAccount`
1026-
// Fifth call is `ApprovalController:addRequest`
1027-
// Sixth call is `AccountsController:getAccount`
1028-
expect(callActionSpy).toHaveBeenCalledTimes(6);
1090+
1091+
expect(callActionSpy).toHaveBeenCalledTimes(9);
10291092
expect(callActionSpy).toHaveBeenNthCalledWith(
1030-
5,
1093+
6,
10311094
'ApprovalController:addRequest',
10321095
{
10331096
id: requestId,

0 commit comments

Comments
 (0)