Skip to content

Commit

Permalink
test(nft-controller): expose mock constructors for allowedActions
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Jul 16, 2024
1 parent 236844a commit b758d93
Showing 1 changed file with 64 additions and 38 deletions.
102 changes: 64 additions & 38 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { Network } from '@ethersproject/providers';
import type { AccountsControllerSelectedAccountChangeEvent } from '@metamask/accounts-controller';
import type {
AccountsControllerGetAccountAction,
AccountsControllerGetSelectedAccountAction,
AccountsControllerSelectedAccountChangeEvent,
} from '@metamask/accounts-controller';
import type { ApprovalControllerMessenger } from '@metamask/approval-controller';
import { ApprovalController } from '@metamask/approval-controller';
import { ControllerMessenger } from '@metamask/base-controller';
Expand Down Expand Up @@ -146,7 +150,13 @@ jest.mock('uuid', () => {
* @param args.mockNetworkClientConfigurationsByNetworkClientId - Used to construct
* mock versions of network clients and ultimately mock the
* `NetworkController:getNetworkClientById` action.
* @param args.getAccount - Used to construct mock versions of the
* `AccountsController:getAccount` action.
* @param args.getSelectedAccount - Used to construct mock versions of the
* `AccountsController:getSelectedAccount` action.
* @param args.defaultSelectedAccount - The default selected account to use in
* @param args.addApprovalRequest - Used to construct mock versions of the

Check failure on line 158 in packages/assets-controllers/src/NftController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

@param "args.addApprovalRequest" does not exist on args
* `ApprovalController:addRequest` action.
* @returns A collection of test controllers and mocks.
*/
function setupController({
Expand All @@ -157,6 +167,8 @@ function setupController({
getERC721OwnerOf,
getERC1155BalanceOf,
getERC1155TokenURI,
getAccount,
getSelectedAccount,
mockNetworkClientConfigurationsByNetworkClientId = {},
defaultSelectedAccount = OWNER_ACCOUNT,
}: {
Expand Down Expand Up @@ -185,6 +197,14 @@ function setupController({
ReturnType<AssetsContractControllerGetERC1155TokenURIAction['handler']>,
Parameters<AssetsContractControllerGetERC1155TokenURIAction['handler']>
>;
getAccount?: jest.Mock<
ReturnType<AccountsControllerGetAccountAction['handler']>,
Parameters<AccountsControllerGetAccountAction['handler']> | [null]
>;
getSelectedAccount?: jest.Mock<
ReturnType<AccountsControllerGetSelectedAccountAction['handler']>,
Parameters<AccountsControllerGetSelectedAccountAction['handler']>
>;
mockNetworkClientConfigurationsByNetworkClientId?: Record<
NetworkClientId,
NetworkClientConfiguration
Expand All @@ -209,19 +229,15 @@ function setupController({
getNetworkClientById,
);

const mockGetAccount = jest
.fn()
.mockReturnValue(defaultSelectedAccount ?? OWNER_ACCOUNT);

const mockGetAccount =
getAccount ?? jest.fn().mockReturnValue(defaultSelectedAccount);
messenger.registerActionHandler(
'AccountsController:getAccount',
mockGetAccount,
);

const mockGetSelectedAccount = jest
.fn()
.mockReturnValue(defaultSelectedAccount ?? OWNER_ACCOUNT);

const mockGetSelectedAccount =
getSelectedAccount ?? jest.fn().mockReturnValue(defaultSelectedAccount);
messenger.registerActionHandler(
'AccountsController:getSelectedAccount',
mockGetSelectedAccount,
Expand Down Expand Up @@ -637,10 +653,13 @@ describe('NftController', () => {
triggerPreferencesStateChange,
triggerSelectedAccountChange,
} = setupController({
getAccount: jest.fn().mockReturnValue(OWNER_ACCOUNT),
getERC721OwnerOf: jest.fn().mockResolvedValue(OWNER_ADDRESS),
getERC721TokenURI: jest
.fn()
.mockImplementation(() => 'https://testtokenuri.com'),
getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS),
.mockResolvedValue('https://testtokenuri.com'),
getERC721AssetName: jest.fn().mockResolvedValue('testERC721Name'),
getERC721AssetSymbol: jest.fn().mockResolvedValue('testERC721Symbol'),
});
triggerSelectedAccountChange(OWNER_ACCOUNT);
triggerPreferencesStateChange({
Expand Down Expand Up @@ -716,10 +735,13 @@ describe('NftController', () => {
triggerPreferencesStateChange,
triggerSelectedAccountChange,
} = setupController({
getAccount: jest.fn().mockReturnValue(OWNER_ACCOUNT),
getERC721OwnerOf: jest.fn().mockResolvedValue(OWNER_ADDRESS),
getERC721TokenURI: jest
.fn()
.mockImplementation(() => 'https://testtokenuri.com'),
getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS),
.mockResolvedValue('https://testtokenuri.com'),
getERC721AssetName: jest.fn().mockResolvedValue('testERC721Name'),
getERC721AssetSymbol: jest.fn().mockResolvedValue('testERC721Symbol'),
});
triggerSelectedAccountChange(OWNER_ACCOUNT);
triggerPreferencesStateChange({
Expand Down Expand Up @@ -795,10 +817,13 @@ describe('NftController', () => {
triggerPreferencesStateChange,
triggerSelectedAccountChange,
} = setupController({
getAccount: jest.fn().mockReturnValue(OWNER_ACCOUNT),
getERC721OwnerOf: jest.fn().mockResolvedValue(OWNER_ADDRESS),
getERC721TokenURI: jest
.fn()
.mockImplementation(() => 'ipfs://testtokenuri.com'),
getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS),
.mockResolvedValue('https://testtokenuri.com'),
getERC721AssetName: jest.fn().mockResolvedValue('testERC721Name'),
getERC721AssetSymbol: jest.fn().mockResolvedValue('testERC721Symbol'),
});
triggerSelectedAccountChange(OWNER_ACCOUNT);
triggerPreferencesStateChange({
Expand Down Expand Up @@ -874,10 +899,13 @@ describe('NftController', () => {
triggerPreferencesStateChange,
triggerSelectedAccountChange,
} = setupController({
getAccount: jest.fn().mockReturnValue(OWNER_ACCOUNT),
getERC721OwnerOf: jest.fn().mockResolvedValue(OWNER_ADDRESS),
getERC721TokenURI: jest
.fn()
.mockImplementation(() => 'ipfs://testtokenuri.com'),
getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS),
.mockResolvedValue('https://testtokenuri.com'),
getERC721AssetName: jest.fn().mockResolvedValue('testERC721Name'),
getERC721AssetSymbol: jest.fn().mockResolvedValue('testERC721Symbol'),
});

triggerSelectedAccountChange(OWNER_ACCOUNT);
Expand Down Expand Up @@ -955,13 +983,17 @@ describe('NftController', () => {
triggerPreferencesStateChange,
triggerSelectedAccountChange,
} = setupController({
getAccount: jest.fn().mockReturnValue(OWNER_ACCOUNT),
getERC721OwnerOf: jest
.fn()
.mockRejectedValue(new Error('Not an ERC721 contract')),
getERC1155BalanceOf: jest.fn().mockResolvedValue(new BN(1)),
getERC721TokenURI: jest
.fn()
.mockRejectedValue(new Error('Not an ERC721 contract')),
getERC1155TokenURI: jest
.fn()
.mockImplementation(() => 'https://testtokenuri.com'),
getERC1155BalanceOf: jest.fn().mockImplementation(() => new BN(1)),
.mockResolvedValue('https://testtokenuri.com'),
});

triggerSelectedAccountChange(OWNER_ACCOUNT);
Expand Down Expand Up @@ -1042,13 +1074,17 @@ describe('NftController', () => {

const { nftController, messenger, triggerPreferencesStateChange } =
setupController({
getAccount: jest.fn().mockReturnValue(OWNER_ACCOUNT),
getERC721OwnerOf: jest
.fn()
.mockRejectedValue(new Error('Not an ERC721 contract')),
getERC1155BalanceOf: jest.fn().mockResolvedValue(new BN(1)),
getERC721TokenURI: jest
.fn()
.mockRejectedValue(new Error('Not an ERC721 contract')),
getERC1155TokenURI: jest
.fn()
.mockImplementation(() => 'https://testtokenuri.com'),
getERC1155BalanceOf: jest.fn().mockImplementation(() => new BN(1)),
.mockResolvedValue('https://testtokenuri.com'),
});
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
Expand Down Expand Up @@ -1134,18 +1170,12 @@ describe('NftController', () => {
triggerPreferencesStateChange,
triggerSelectedAccountChange,
} = setupController({
getERC721OwnerOf: jest
.fn()
.mockImplementation(() => SECOND_OWNER_ADDRESS),
getERC721OwnerOf: jest.fn().mockResolvedValue(SECOND_OWNER_ADDRESS),
getERC721TokenURI: jest
.fn()
.mockImplementation(() => 'https://testtokenuri.com'),
getERC721AssetName: jest
.fn()
.mockImplementation(() => 'testERC721Name'),
getERC721AssetSymbol: jest
.fn()
.mockImplementation(() => 'testERC721Symbol'),
.mockResolvedValue('https://testtokenuri.com'),
getERC721AssetName: jest.fn().mockResolvedValue('testERC721Name'),
getERC721AssetSymbol: jest.fn().mockResolvedValue('testERC721Symbol'),
});

const requestId = 'approval-request-id-1';
Expand Down Expand Up @@ -1241,13 +1271,9 @@ describe('NftController', () => {
getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS),
getERC721TokenURI: jest
.fn()
.mockImplementation(() => 'https://testtokenuri.com'),
getERC721AssetName: jest
.fn()
.mockImplementation(() => 'testERC721Name'),
getERC721AssetSymbol: jest
.fn()
.mockImplementation(() => 'testERC721Symbol'),
.mockResolvedValue('https://testtokenuri.com'),
getERC721AssetName: jest.fn().mockResolvedValue('testERC721Name'),
getERC721AssetSymbol: jest.fn().mockResolvedValue('testERC721Symbol'),
});

const requestId = 'approval-request-id-1';
Expand Down

0 comments on commit b758d93

Please sign in to comment.