From 7e7170c54a27f7afd7a221fbbc8ae1408bb06808 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Wed, 18 Sep 2024 19:51:23 +0300 Subject: [PATCH] Test parameters for each hook --- js/test/demo-hooks.test.tsx | 122 ++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 68 deletions(-) diff --git a/js/test/demo-hooks.test.tsx b/js/test/demo-hooks.test.tsx index ca7282f4..8a66bd46 100644 --- a/js/test/demo-hooks.test.tsx +++ b/js/test/demo-hooks.test.tsx @@ -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'); @@ -41,6 +38,33 @@ const Providers = ({ children }: { children: ReactNode }) => ( const renderHook = (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)); @@ -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`, + ); });