@daml/react: Use jest.fn mock functions in test (#4650)

Also mock the constructor of the `Ledger` class and separete out the
test for initializing the `DamlLedger` component.

CHANGELOG_BEGIN
CHANGELOG_END
This commit is contained in:
Martin Huschenbett 2020-02-21 15:31:13 +01:00 committed by GitHub
parent 0fd7bcaadc
commit 4a8c5cf24e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,31 +1,60 @@
// Copyright (c) 2020 The DAML Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { ComponentType } from 'react';
import { renderHook } from '@testing-library/react-hooks';
import DamlLedger from './DamlLedger'
import { useQuery } from './hooks';
import { renderHook, RenderHookResult } from '@testing-library/react-hooks';
import DamlLedger, { useQuery} from './index';
import { Template } from '@daml/types';
import { useParty } from './hooks';
let mockResolveQuery: (contracts: string[]) => void = () => {
throw Error('using mockResolveQuery before init');
}
const mockConstructor = jest.fn();
const mockQuery = jest.fn();
const mockFunctions = [mockConstructor, mockQuery];
jest.mock('@daml/ledger', () => class {
query(): Promise<string[]> {
return new Promise((resolve) => mockResolveQuery = resolve);
constructor(...args: unknown[]) {
mockConstructor(...args);
}
query(...args: unknown[]): Promise<string> {
return mockQuery(...args);
}
});
const TOKEN = 'test_token';
const PARTY = 'test_party';
function renderDamlHook<P, R>(callback: (props: P) => R): RenderHookResult<P, R> {
const wrapper: ComponentType = ({children}) => React.createElement(DamlLedger, {token: TOKEN, party: PARTY}, children);
return renderHook(callback, {wrapper});
}
const Foo = undefined as unknown as Template<object>;
beforeEach(() => {
mockFunctions.forEach(mock => mock.mockClear());
});
test('DamlLedger', () => {
renderDamlHook(() => { return; });
expect(mockConstructor).toHaveBeenCalledTimes(1);
expect(mockConstructor).toHaveBeenLastCalledWith({token: TOKEN, httpBaseUrl: undefined, wsBaseUrl: undefined});
});
test('useParty', () => {
const {result} = renderDamlHook(() => useParty());
expect(result.current).toBe(PARTY);
});
test('useQuery', async () => {
const wrapper: ComponentType = ({children}) => React.createElement(DamlLedger, {token: 'token', party: 'party'}, children);
const {result, waitForNextUpdate} = renderHook(() => useQuery(Foo), {wrapper});
const resolvent = ['foo'];
mockQuery.mockReturnValueOnce(Promise.resolve(resolvent));
const {result, waitForNextUpdate} = renderDamlHook(() => useQuery(Foo));
expect(mockQuery).toHaveBeenCalledTimes(1);
expect(mockQuery).toHaveBeenLastCalledWith(Foo, undefined);
mockQuery.mockClear();
expect(result.current.contracts).toEqual([]);
expect(result.current.loading).toBe(true);
const resolvent = ['foo'];
mockResolveQuery(resolvent);
await waitForNextUpdate();
expect(mockQuery).not.toHaveBeenCalled();
expect(result.current.contracts).toBe(resolvent);
expect(result.current.loading).toBe(false);
});