Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@daml/react: Add rerendering test for useQuery #4662

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions language-support/ts/daml-react/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('useQuery', () => {
const resolvent1 = ['foo'];
const resolvent2 = ['bar'];

// First rendering works?
mockQuery.mockReturnValueOnce(Promise.resolve(resolvent1));
const {result, waitForNextUpdate} = renderDamlHook(() => {
const [query, setQuery] = useState(query1);
Expand All @@ -83,6 +84,7 @@ describe('useQuery', () => {
await waitForNextUpdate();
expect(result.current.queryResult).toEqual({contracts: resolvent1, loading: false});

// Change to query triggers another call to JSON API?
mockQuery.mockReturnValueOnce(Promise.resolve(resolvent2));
act(() => result.current.setQuery(query2));
expect(mockQuery).toHaveBeenCalledTimes(1);
Expand All @@ -93,4 +95,26 @@ describe('useQuery', () => {
await waitForNextUpdate();
expect(result.current.queryResult).toEqual({contracts: resolvent2, loading: false});
});

test('rerendering without query change', async () => {
const query = 'query';
const resolvent = ['foo'];

// First rendering works?
mockQuery.mockReturnValueOnce(Promise.resolve(resolvent));
const {result, waitForNextUpdate} = renderDamlHook(() => {
const setState = useState('state')[1];
const queryResult = useQuery(Foo, () => ({query}), [query]);
return {queryResult, query, setState};
});
expect(mockQuery).toHaveBeenCalledTimes(1);
mockQuery.mockClear();
await waitForNextUpdate();
expect(result.current.queryResult).toEqual({contracts: resolvent, loading: false});

// Change to unrelated state does _not_ trigger another call to JSON API?
act(() => result.current.setState('new-state'));
expect(mockQuery).not.toHaveBeenCalled();
expect(result.current.queryResult).toEqual({contracts: resolvent, loading: false});
});
});