Skip to content

Commit

Permalink
refactor: Isolate vitest helpers from other imports to avoid cjs prob…
Browse files Browse the repository at this point in the history
…lems.
  • Loading branch information
brunomenezes committed Nov 15, 2024
1 parent 595ef57 commit e97e193
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion tests/handlers/ApplicationCreated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
RollupVersion,
} from '../../src/model';
import { generateIDFrom } from '../../src/utils';
import { mockModelImplementation } from '../stubs/mocks';
import { block, ctx, logs } from '../stubs/params';
import { mockModelImplementation } from '../stubs/utils';

vi.mock('../../src/model/', async (importOriginal) => {
const actualMods = await importOriginal;
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/InputAdded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
NFT,
Token,
} from '../../src/model';
import { mockModelImplementation } from '../stubs/mocks';
import {
block,
ctx,
Expand All @@ -25,7 +26,6 @@ import {
logErc721Transfer,
logs,
} from '../stubs/params';
import { mockModelImplementation } from '../stubs/utils';

vi.mock('../../src/abi/ERC20');

Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/OwnershipTransferred.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { sepolia } from 'viem/chains';
import OwnerShipTransferred from '../../src/handlers/OwnershipTransferred';
import { Application, Chain, RollupVersion } from '../../src/model';
import { generateIDFrom } from '../../src/utils';
import { mockModelImplementation } from '../stubs/mocks';
import { block, ctx, logs } from '../stubs/params';
import { mockModelImplementation } from '../stubs/utils';

vi.mock('../../src/model/', async (importOriginal) => {
const actualMods = await importOriginal;
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/v2/ApplicationCreated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
RollupVersion,
} from '../../../src/model';
import { generateIDFrom } from '../../../src/utils';
import { mockModelImplementation } from '../../stubs/mocks';
import { block, ctx, logApplicationCreatedV2, logs } from '../../stubs/params';
import { mockModelImplementation } from '../../stubs/utils';

vi.mock('../../../src/model/', async (importOriginal) => {
const actualMods = await importOriginal;
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/v2/InputAdded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
NFT,
Token,
} from '../../../src/model';
import { mockModelImplementation } from '../../stubs/mocks';
import {
block,
ctx,
Expand All @@ -27,7 +28,6 @@ import {
logInputAddedV2,
logs,
} from '../../stubs/params';
import { mockModelImplementation } from '../../stubs/utils';

vi.mock('../../../src/abi/ERC20');

Expand Down
8 changes: 8 additions & 0 deletions tests/stubs/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { MockedObject, vi } from 'vitest';

export const mockModelImplementation = <T>(object: T) => {
const Mock = vi.mocked(object) as MockedObject<typeof object>;
// @ts-ignore
Mock.mockImplementation((args) => ({ ...args } as object));
return Mock;
};
24 changes: 16 additions & 8 deletions tests/stubs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@ import {
Hex,
parseAbiParameters,
} from 'viem';
import { MockedObject, vi } from 'vitest';
import { evmAdvanceAbi } from '../../src/decoders/evmAdvance';

export const mockModelImplementation = <T>(object: T) => {
const Mock = vi.mocked(object) as MockedObject<typeof object>;
// @ts-ignore
Mock.mockImplementation((args) => ({ ...args } as object));
return Mock;
};

type ERC721PortalInput = {
token: Address;
sender: Address;
Expand Down Expand Up @@ -50,6 +42,12 @@ type ERC20PortalInput = {
execLayerData: Hex;
};

type EtherPortalInput = {
sender: Address;
value: bigint;
execLayerData: Hex;
};

type EvmAdvanceInput = {
chainId: bigint;
appContract: Address;
Expand All @@ -65,6 +63,16 @@ const baseExecLayerAbiParameters = parseAbiParameters(
'bytes baseLayer, bytes execLayer',
);

export const encodeEtherPortalInput = ({
sender,
value,
execLayerData,
}: EtherPortalInput) =>
encodePacked(
['address', 'uint256', 'bytes'],
[sender, value, execLayerData],
);

export const encodeErc20PortalInput = ({
token,
sender,
Expand Down

0 comments on commit e97e193

Please sign in to comment.