Skip to content

Commit

Permalink
chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Aug 28, 2024
2 parents 050fcc7 + 8f4aa4d commit 92bd589
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 20 deletions.
143 changes: 125 additions & 18 deletions src/types/wallet-request.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,135 @@
import {WalletRequestOptionsSchema} from './wallet-request';
import {IDL} from '@dfinity/candid';
import {mockPrincipalText} from '../constants/icrc-accounts.mocks';
import {
WalletRequestOptionsSchema,
extendIcrcCallCanisterRequestParamsSchema,
type WalletCallParams
} from './wallet-request';

describe('WalletRequest', () => {
it('should validate with a specified timeoutInMilliseconds', () => {
const validData = {
timeoutInMilliseconds: 3000
};
describe('Options', () => {
it('should validate with a specified timeoutInMilliseconds', () => {
const validData = {
timeoutInMilliseconds: 3000
};

const result = WalletRequestOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
});
const result = WalletRequestOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
});

it('should validate without specifying timeoutInMilliseconds', () => {
const validData = {};

const result = WalletRequestOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
});

it('should validate without specifying timeoutInMilliseconds', () => {
const validData = {};
it('should fail validation with a non-numeric timeoutInMilliseconds', () => {
const invalidData = {
timeoutInMilliseconds: 'three thousand'
};

const result = WalletRequestOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
const result = WalletRequestOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});
});

it('should fail validation with a non-numeric timeoutInMilliseconds', () => {
const invalidData = {
timeoutInMilliseconds: 'three thousand'
};
describe('Call', () => {
interface MyTest {
hello: string;
}

const argType = IDL.Record({
hello: IDL.Text
});

const schema = extendIcrcCallCanisterRequestParamsSchema<MyTest>();

it('should validate correct parameters', () => {
const validParams: WalletCallParams<{hello: string}> = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
method: 'some_method',
arg: {hello: 'world'},
argType
};

const result = schema.safeParse(validParams);
expect(result.success).toBe(true);
});

// TODO: not sure how to solve this with zod that's why this test succeed.
it('should validate incorrect "arg" type', () => {
const invalidParams = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
method: 'some_method',
arg: 'invalid_arg',
argType
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(true);
});

it('should fail validation with missing "argType"', () => {
const invalidParams = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
method: 'some_method',
arg: {hello: 'world'}
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});

it('should fail validation with missing "arg"', () => {
const invalidParams = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
method: 'some_method',
argType
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});

it('should fail validation with missing "canisterId"', () => {
const invalidParams = {
sender: mockPrincipalText,
method: 'some_method',
arg: {hello: 'world'},
argType
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});

it('should fail validation with missing "sender"', () => {
const invalidParams = {
canisterId: mockPrincipalText,
method: 'some_method',
arg: {hello: 'world'},
argType
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});

it('should fail validation with missing "method"', () => {
const invalidParams = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
arg: {hello: 'world'},
argType
};

const result = WalletRequestOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});
});
});
7 changes: 5 additions & 2 deletions src/types/wallet-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Type} from '@dfinity/candid/lib/cjs/idl';
import {nonNullish} from '@dfinity/utils';
import {z} from 'zod';
import {IcrcCallCanisterRequestParamsSchema} from './icrc-requests';
import {RpcIdSchema} from './rpc';
Expand Down Expand Up @@ -43,9 +44,11 @@ export type WalletRequestOptionsWithTimeout = z.infer<typeof WalletRequestOption
* @returns {z.ZodObject} - A Zod schema object that includes the base fields and the custom `arg` and `argType`.
*/
/* eslint-disable @typescript-eslint/explicit-function-return-type */
const extendIcrcCallCanisterRequestParamsSchema = <T>() => {
export const extendIcrcCallCanisterRequestParamsSchema = <T>() => {
return IcrcCallCanisterRequestParamsSchema.omit({arg: true}).extend({
arg: z.custom<T>(),
arg: z.custom<T>().refine(nonNullish, {
message: 'arg is required'
}),
argType: z.instanceof(Type) as z.ZodType<Type>
});
};
Expand Down

0 comments on commit 92bd589

Please sign in to comment.