Skip to content

add some basic testing for useAsync #15

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

Merged
merged 7 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
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
22 changes: 18 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,29 @@
"singleQuote": true,
"trailingComma": "es5"
},
"jest": {
"globals": {
"ts-jest": {
"tsConfig": "tsconfig.test.json"
}
},
"setupFilesAfterEnv": [
"@testing-library/jest-dom/extend-expect"
]
},
"devDependencies": {
"@testing-library/jest-dom": "^4.1.2",
"@testing-library/react": "^9.3.0",
"@testing-library/react-hooks": "^3.1.0",
"@types/jest": "^24.0.12",
"@types/react": "^16.8.16",
"@types/react-dom": "^16.8.4",
"@types/react": "^16.9.9",
"@types/react-dom": "^16.9.2",
"husky": "^2.2.0",
"prettier": "^1.17.0",
"pretty-quick": "^1.10.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-test-renderer": "^16.10.2",
"tsdx": "^0.7.2",
"tslib": "^1.9.3",
"typescript": "^3.4.5"
Expand Down
6 changes: 0 additions & 6 deletions test/test.tsx

This file was deleted.

75 changes: 75 additions & 0 deletions test/useAsync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useAsync } from '../src';
import { renderHook } from '@testing-library/react-hooks';

interface StarwarsHero {
name: string;
}

export const generateMockResponseData = (amount: number = 5): StarwarsHero[] =>
[...Array(amount).keys()].map(n => ({
id: n + 1,
name: `Starwars Hero ${n + 1}`,
}));

describe('useAync', () => {
const fakeResults = generateMockResponseData();

it('should have a useAsync hook', () => {
expect(useAsync).toBeDefined();
});

it('should resolve a successful request', async () => {
const onSuccess = jest.fn();
const onError = jest.fn();

const { result, waitForNextUpdate } = renderHook(() =>
useAsync(
async () => {
return Promise.resolve(fakeResults);
},
[],
{
onSuccess: () => onSuccess(),
onError: () => onError(),
}
)
);

expect(result.current.loading).toBe(true);

await waitForNextUpdate();

expect(result.current.result).toEqual(fakeResults);
expect(result.current.loading).toBe(false);
expect(result.current.error).toBeUndefined();
expect(onSuccess).toHaveBeenCalled();
expect(onError).not.toHaveBeenCalled();
});

it('should set error detail for unsuccessful request', async () => {
const onSuccess = jest.fn();
const onError = jest.fn();

const { result, waitForNextUpdate } = renderHook(() =>
useAsync(
async () => {
throw new Error('something went wrong');
},
[],
{
onSuccess: () => onSuccess(),
onError: () => onError(),
}
)
);

await waitForNextUpdate();

expect(result.current.error).toBeDefined();
expect(result.current.error!.message).toBe('something went wrong');
expect(result.current.loading).toBe(false);
expect(result.current.result).toBeUndefined();
expect(onSuccess).not.toHaveBeenCalled();
expect(onError).toHaveBeenCalled();
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"include": ["src", "types"],
"exclude": ["test"],
"compilerOptions": {
"target": "es5",
"module": "esnext",
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"downlevelIteration": true
},
"include": ["src/**/*.ts", "test/**/*.ts"]
}
Loading