Skip to content

Commit

Permalink
Test parameters for each hook
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Sep 18, 2024
1 parent 12f86d2 commit 7e7170c
Showing 1 changed file with 54 additions and 68 deletions.
122 changes: 54 additions & 68 deletions js/test/demo-hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ import { HexString } from '@gear-js/api';
import * as GearHooks from '@gear-js/react-hooks';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook as renderReactHook } from '@testing-library/react';
import * as fs from 'fs';
import path from 'path';
import { ReactNode } from 'react';
import { test, expect, vi } from 'vitest';
import { test, expect, vi, MockInstance } from 'vitest';
import { SailsIdlParser } from 'sails-js-parser';
import { toLowerCaseFirst } from 'sails-js-util';

import { Sails } from '..';
import { Program } from './demo/lib';
import {
useCounterAddedEvent,
useCounterValueQuery,
useDogAvgWeightQuery,
useDogBarkedEvent,
usePrepareCounterAddTransaction,
usePrepareDogMakeSoundTransaction,
usePreparePingPongPingTransaction,
usePrepareReferencesSetNumTransaction,
usePrepareThisThatNoopTransaction,
useProgram,
useReferencesLastByteQuery,
useSendCounterAddTransaction,
useSendDogMakeSoundTransaction,
useSendPingPongPingTransaction,
useSendReferencesSetNumTransaction,
useSendThisThatNoopTransaction,
useThisThatThatQuery,
} from './demo/hooks';

const { useProgram, ...demoHooks } = await import('./demo/hooks');

const getSails = async () => {
const idlPath = path.resolve(__dirname, '../../examples/demo/client/demo.idl');
const idl = fs.readFileSync(idlPath, 'utf8');
const parser = await SailsIdlParser.new();

return new Sails(parser).parseIdl(idl);
};

const SAILS = await getSails();
const QUERY_CLIENT = new QueryClient();

const useProgramSpy = vi.spyOn(GearHooks, 'useProgram');
Expand All @@ -41,6 +38,33 @@ const Providers = ({ children }: { children: ReactNode }) => (
const renderHook = <TProps, TReturn>(hook: (initialProps: TProps) => TReturn) =>
renderReactHook(hook, { wrapper: Providers });

const testHookParameters = (
type: 'functions' | 'queries' | 'events',
getName: (value: string) => string,
spy: MockInstance,
extraArgs = {},
getFunctionName?: (value: string) => string,
) => {
const { result } = renderHook(() => useProgram({ id: '0x01' }));
const program = result.current.data;

Object.entries(SAILS.services).forEach(([serviceName, { [type]: functions }]) => {
Object.keys(functions).forEach((functionName) => {
const hookName = getName(`${serviceName}${functionName}`);
const useHook = demoHooks[hookName];

renderHook(() => useHook({ program, ...extraArgs }));

expect(spy).toHaveBeenCalledWith({
program,
serviceName: toLowerCaseFirst(serviceName),
functionName: toLowerCaseFirst(getFunctionName ? getFunctionName(functionName) : functionName),
...extraArgs,
});
});
});
};

test('useProgram', () => {
const ARGS = { id: '0x01' as HexString, query: { enabled: true } };
renderHook(() => useProgram(ARGS));
Expand All @@ -49,61 +73,23 @@ test('useProgram', () => {
});

test('useSendTransaction', () => {
const { result } = renderHook(() => useProgram({ id: '0x01' }));
const program = result.current.data;

renderHook(() => useSendCounterAddTransaction({ program }));
renderHook(() => useSendDogMakeSoundTransaction({ program }));
renderHook(() => useSendPingPongPingTransaction({ program }));
renderHook(() => useSendReferencesSetNumTransaction({ program }));
renderHook(() => useSendThisThatNoopTransaction({ program }));

expect(useSendTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'counter', functionName: 'add' });
expect(useSendTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'dog', functionName: 'makeSound' });
expect(useSendTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'pingPong', functionName: 'ping' });
expect(useSendTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'references', functionName: 'setNum' });
expect(useSendTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'thisThat', functionName: 'noop' });
testHookParameters('functions', (name) => `useSend${name}Transaction`, useSendTransactionSpy);
});

test('usePrepareTransaction', () => {
const { result } = renderHook(() => useProgram({ id: '0x01' }));
const program = result.current.data;

renderHook(() => usePrepareCounterAddTransaction({ program }));
renderHook(() => usePrepareDogMakeSoundTransaction({ program }));
renderHook(() => usePreparePingPongPingTransaction({ program }));
renderHook(() => usePrepareReferencesSetNumTransaction({ program }));
renderHook(() => usePrepareThisThatNoopTransaction({ program }));

expect(usePrepareTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'counter', functionName: 'add' });
expect(usePrepareTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'dog', functionName: 'makeSound' });
expect(usePrepareTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'pingPong', functionName: 'ping' });
expect(usePrepareTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'references', functionName: 'setNum' });
expect(usePrepareTransactionSpy).toHaveBeenCalledWith({ program, serviceName: 'thisThat', functionName: 'noop' });
testHookParameters('functions', (name) => `usePrepare${name}Transaction`, usePrepareTransactionSpy);
});

test('useQuery', () => {
const { result } = renderHook(() => useProgram({ id: '0x01' }));
const ARGS = { program: result.current.data, query: { enabled: true } };

renderHook(() => useCounterValueQuery({ ...ARGS, args: [] }));
renderHook(() => useDogAvgWeightQuery({ ...ARGS, args: [] }));
renderHook(() => useReferencesLastByteQuery({ ...ARGS, args: [] }));
renderHook(() => useThisThatThatQuery({ ...ARGS, args: [] }));

expect(useQuerySpy).toHaveBeenCalledWith({ ...ARGS, serviceName: 'counter', functionName: 'value', args: [] });
expect(useQuerySpy).toHaveBeenCalledWith({ ...ARGS, serviceName: 'dog', functionName: 'avgWeight', args: [] });
expect(useQuerySpy).toHaveBeenCalledWith({ ...ARGS, serviceName: 'references', functionName: 'lastByte', args: [] });
expect(useQuerySpy).toHaveBeenCalledWith({ ...ARGS, serviceName: 'thisThat', functionName: 'that', args: [] });
testHookParameters('queries', (name) => `use${name}Query`, useQuerySpy, { query: { enabled: true } });
});

test('useEvent', () => {
const { result } = renderHook(() => useProgram({ id: '0x01' }));
const ARGS = { program: result.current.data, query: { enabled: true }, onData: () => {} };

renderHook(() => useCounterAddedEvent(ARGS));
renderHook(() => useDogBarkedEvent(ARGS));

expect(useEventSpy).toHaveBeenCalledWith({ ...ARGS, serviceName: 'counter', functionName: 'subscribeToAddedEvent' });
expect(useEventSpy).toHaveBeenCalledWith({ ...ARGS, serviceName: 'dog', functionName: 'subscribeToBarkedEvent' });
testHookParameters(
'events',
(name) => `use${name}Event`,
useEventSpy,
{ query: { enabled: true }, onData: () => {} },
(name) => `subscribeTo${name}Event`,
);
});

0 comments on commit 7e7170c

Please sign in to comment.