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

Improve test for hooks/service. Spy on useEffect #14

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions __tests__/hooks/service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import { act, renderHook } from '@testing-library/react-hooks';
import { useService } from '../../hooks/service';
import { success, loading } from '../../libs/remoteData';
Expand All @@ -8,11 +9,13 @@ describe('Hook `useService`', () => {
const service = () => Promise.resolve(dataSuccess);

test('change `Loading` to `Success` status when service resolved', async () => {
const spyEffect = jest.spyOn(React, 'useEffect');
const { result, waitForNextUpdate } = renderHook(() => useService(service));
const {
current: [firstRemoteData],
} = result;

expect(spyEffect).toHaveBeenLastCalledWith(expect.anything(), [0]);
expect(firstRemoteData).toEqual(loading);

await waitForNextUpdate();
Expand All @@ -21,10 +24,12 @@ describe('Hook `useService`', () => {
current: [secondRemoteData],
} = result;

expect(spyEffect).toHaveBeenLastCalledWith(expect.anything(), [0]);
expect(secondRemoteData).toEqual(dataSuccess);
});

test('method `reload` returns the same data ', async () => {
const spyEffect = jest.spyOn(React, 'useEffect');
const deps = [1, 2];
const { result, waitForNextUpdate } = renderHook(() => useService(service, deps));

Expand All @@ -34,6 +39,7 @@ describe('Hook `useService`', () => {
current: [firstRemoteData, { reload }],
} = result;

expect(spyEffect).toHaveBeenLastCalledWith(expect.anything(), [1, 2, 0]);
expect(firstRemoteData).toEqual(dataSuccess);

act(() => {
Expand All @@ -44,6 +50,7 @@ describe('Hook `useService`', () => {
current: [secondRemoteData],
} = result;

expect(spyEffect).toHaveBeenLastCalledWith(expect.anything(), [1, 2, 1]);
expect(secondRemoteData).toEqual(loading);

await waitForNextUpdate();
Expand All @@ -56,11 +63,13 @@ describe('Hook `useService`', () => {
});

test('has `set` data method returns success remote data', async () => {
const { result, waitForNextUpdate } = renderHook(() => useService(service));
const spyEffect = jest.spyOn(React, 'useEffect');
const deps = [1, 2];
const { result, waitForNextUpdate } = renderHook(() => useService(service, deps));
const data = { custom: 'data-new' };

await waitForNextUpdate();

expect(spyEffect).toHaveBeenLastCalledWith(expect.anything(), [1, 2, 0]);
const {
current: [, { set }],
} = result;
Expand All @@ -74,5 +83,6 @@ describe('Hook `useService`', () => {
} = result;

expect(remoteData).toEqual(success(data));
expect(spyEffect).toHaveBeenLastCalledWith(expect.anything(), [1, 2, 0]);
});
});