@ventlio/tanstack-query 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/queries/useDeleteRequest.js +2 -1
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/package.json +2 -1
- package/src/__tests__/queries/usePostRequest.spec.tsx +84 -0
- package/src/queries/useDeleteRequest.ts +2 -1
- package/src/__tests__/queries/usePostRequest.spec.ts +0 -77
package/dist/index.mjs
CHANGED
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -6,7 +6,7 @@ import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
|
6
6
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
7
7
|
import 'axios';
|
|
8
8
|
import { makeRequest } from '../request/make-request.js';
|
|
9
|
-
import '../request/request.enum.js';
|
|
9
|
+
import { HttpMethod } from '../request/request.enum.js';
|
|
10
10
|
import { usePauseFutureRequests } from '../stores/usePauseFutureRequests.js';
|
|
11
11
|
|
|
12
12
|
const useDeleteRequest = (deleteOptions) => {
|
|
@@ -27,6 +27,7 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
27
27
|
path: requestUrl,
|
|
28
28
|
headers: { ...globalHeaders, ...headers },
|
|
29
29
|
baseURL: baseUrl ?? API_URL,
|
|
30
|
+
method: HttpMethod.DELETE,
|
|
30
31
|
timeout: TIMEOUT,
|
|
31
32
|
};
|
|
32
33
|
let shouldContinue = true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ventlio/tanstack-query",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"contributors": [
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
49
49
|
"@rollup/plugin-typescript": "^11.0.0",
|
|
50
50
|
"@tanstack/react-query": "^4.26.1",
|
|
51
|
+
"@testing-library/react": "^13.1",
|
|
51
52
|
"@testing-library/react-hooks": "^8.0.1",
|
|
52
53
|
"@types/axios": "^0.14.0",
|
|
53
54
|
"@types/jest": "^29.5.1",
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
2
|
+
import { renderHook } from '@testing-library/react';
|
|
3
|
+
import axios from 'axios';
|
|
4
|
+
import MockAdapter from 'axios-mock-adapter';
|
|
5
|
+
import type { ReactNode } from 'react';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
import { useGetRequest } from '../../queries';
|
|
8
|
+
|
|
9
|
+
const mockAxios = new MockAdapter(axios);
|
|
10
|
+
|
|
11
|
+
describe('usePostRequest', () => {
|
|
12
|
+
const path = '/test';
|
|
13
|
+
const postData = { name: 'John Doe' };
|
|
14
|
+
const response = { id: 123, name: 'John Doe' };
|
|
15
|
+
|
|
16
|
+
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
17
|
+
<QueryClientProvider client={new QueryClient()}>{children}</QueryClientProvider>
|
|
18
|
+
);
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
mockAxios.reset();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should return post function and mutation object', () => {
|
|
24
|
+
const { result } = renderHook(() => useGetRequest<{ id: number; name: string }>({ path, load: false }), {
|
|
25
|
+
wrapper,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
expect(result.current.isLoading).toBe(false);
|
|
29
|
+
expect(result.current.isSuccess).toBe(false);
|
|
30
|
+
expect(result.current.isError).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// it('should make post request and return response data', async () => {
|
|
34
|
+
// mockAxios.onPost(path).reply(200, response);
|
|
35
|
+
|
|
36
|
+
// const { result, waitForNextUpdate } = renderHook(() => usePostRequest<{ id: number; name: string }>({ path }));
|
|
37
|
+
|
|
38
|
+
// const responsePromise = result.current.post(postData);
|
|
39
|
+
|
|
40
|
+
// expect(result.current.isLoading).toBe(true);
|
|
41
|
+
|
|
42
|
+
// await waitForNextUpdate();
|
|
43
|
+
|
|
44
|
+
// expect(result.current.isLoading).toBe(false);
|
|
45
|
+
// expect(result.current.isSuccess).toBe(true);
|
|
46
|
+
// expect(result.current.data).toEqual(response);
|
|
47
|
+
// expect(await responsePromise).toEqual(response);
|
|
48
|
+
// });
|
|
49
|
+
|
|
50
|
+
// it('should make post request and return error', async () => {
|
|
51
|
+
// const errorMessage = 'Request failed with status code 500';
|
|
52
|
+
// mockAxios.onPost(path).reply(500, { message: errorMessage });
|
|
53
|
+
|
|
54
|
+
// const { result, waitForNextUpdate } = renderHook(() => usePostRequest<{ id: number; name: string }>({ path }));
|
|
55
|
+
|
|
56
|
+
// const responsePromise = result.current.post(postData);
|
|
57
|
+
|
|
58
|
+
// expect(result.current.isLoading).toBe(true);
|
|
59
|
+
|
|
60
|
+
// await waitForNextUpdate();
|
|
61
|
+
|
|
62
|
+
// expect(result.current.isLoading).toBe(false);
|
|
63
|
+
// expect(result.current.isError).toBe(true);
|
|
64
|
+
// expect(result.current.error?.message).toBe(errorMessage);
|
|
65
|
+
|
|
66
|
+
// await expect(responsePromise).rejects.toEqual({
|
|
67
|
+
// message: errorMessage,
|
|
68
|
+
// });
|
|
69
|
+
// });
|
|
70
|
+
|
|
71
|
+
// it('should make post request with FormData', async () => {
|
|
72
|
+
// const formData = new FormData();
|
|
73
|
+
// formData.append('name', 'John Doe');
|
|
74
|
+
// mockAxios.onPost(path).reply(200, response);
|
|
75
|
+
|
|
76
|
+
// const { result } = renderHook(() => usePostRequest<{ id: number; name: string }>({ path, isFormData: true }));
|
|
77
|
+
|
|
78
|
+
// const responsePromise = result.current.post(formData);
|
|
79
|
+
|
|
80
|
+
// expect(result.current.isLoading).toBe(true);
|
|
81
|
+
|
|
82
|
+
// await expect(responsePromise).resolves.toEqual(response);
|
|
83
|
+
// });
|
|
84
|
+
});
|
|
@@ -4,7 +4,7 @@ import type { RawAxiosRequestHeaders } from 'axios';
|
|
|
4
4
|
import { useEffect, useState } from 'react';
|
|
5
5
|
import { useEnvironmentVariables, useQueryConfig, useQueryHeaders } from '../config';
|
|
6
6
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
7
|
-
import { makeRequest } from '../request';
|
|
7
|
+
import { HttpMethod, makeRequest } from '../request';
|
|
8
8
|
import { usePauseFutureRequests } from '../stores';
|
|
9
9
|
import type { DefaultRequestOptions } from './queries.interface';
|
|
10
10
|
|
|
@@ -33,6 +33,7 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
33
33
|
path: requestUrl,
|
|
34
34
|
headers: { ...globalHeaders, ...headers },
|
|
35
35
|
baseURL: baseUrl ?? API_URL,
|
|
36
|
+
method: HttpMethod.DELETE,
|
|
36
37
|
timeout: TIMEOUT,
|
|
37
38
|
};
|
|
38
39
|
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
-
import axios from 'axios';
|
|
3
|
-
import MockAdapter from 'axios-mock-adapter';
|
|
4
|
-
import { usePostRequest } from '../../queries';
|
|
5
|
-
|
|
6
|
-
const mockAxios = new MockAdapter(axios);
|
|
7
|
-
|
|
8
|
-
describe('usePostRequest', () => {
|
|
9
|
-
const path = '/test';
|
|
10
|
-
const postData = { name: 'John Doe' };
|
|
11
|
-
const response = { id: 123, name: 'John Doe' };
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
mockAxios.reset();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('should return post function and mutation object', () => {
|
|
18
|
-
const { result } = renderHook(() => usePostRequest<{ id: number; name: string }>({ path }));
|
|
19
|
-
|
|
20
|
-
expect(result.current.post).toBeInstanceOf(Function);
|
|
21
|
-
expect(result.current.isLoading).toBe(false);
|
|
22
|
-
expect(result.current.isSuccess).toBe(false);
|
|
23
|
-
expect(result.current.isError).toBe(false);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should make post request and return response data', async () => {
|
|
27
|
-
mockAxios.onPost(path).reply(200, response);
|
|
28
|
-
|
|
29
|
-
const { result, waitForNextUpdate } = renderHook(() => usePostRequest<{ id: number; name: string }>({ path }));
|
|
30
|
-
|
|
31
|
-
const responsePromise = result.current.post(postData);
|
|
32
|
-
|
|
33
|
-
expect(result.current.isLoading).toBe(true);
|
|
34
|
-
|
|
35
|
-
await waitForNextUpdate();
|
|
36
|
-
|
|
37
|
-
expect(result.current.isLoading).toBe(false);
|
|
38
|
-
expect(result.current.isSuccess).toBe(true);
|
|
39
|
-
expect(result.current.data).toEqual(response);
|
|
40
|
-
expect(await responsePromise).toEqual(response);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should make post request and return error', async () => {
|
|
44
|
-
const errorMessage = 'Request failed with status code 500';
|
|
45
|
-
mockAxios.onPost(path).reply(500, { message: errorMessage });
|
|
46
|
-
|
|
47
|
-
const { result, waitForNextUpdate } = renderHook(() => usePostRequest<{ id: number; name: string }>({ path }));
|
|
48
|
-
|
|
49
|
-
const responsePromise = result.current.post(postData);
|
|
50
|
-
|
|
51
|
-
expect(result.current.isLoading).toBe(true);
|
|
52
|
-
|
|
53
|
-
await waitForNextUpdate();
|
|
54
|
-
|
|
55
|
-
expect(result.current.isLoading).toBe(false);
|
|
56
|
-
expect(result.current.isError).toBe(true);
|
|
57
|
-
expect(result.current.error?.message).toBe(errorMessage);
|
|
58
|
-
|
|
59
|
-
await expect(responsePromise).rejects.toEqual({
|
|
60
|
-
message: errorMessage,
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should make post request with FormData', async () => {
|
|
65
|
-
const formData = new FormData();
|
|
66
|
-
formData.append('name', 'John Doe');
|
|
67
|
-
mockAxios.onPost(path).reply(200, response);
|
|
68
|
-
|
|
69
|
-
const { result } = renderHook(() => usePostRequest<{ id: number; name: string }>({ path, isFormData: true }));
|
|
70
|
-
|
|
71
|
-
const responsePromise = result.current.post(formData);
|
|
72
|
-
|
|
73
|
-
expect(result.current.isLoading).toBe(true);
|
|
74
|
-
|
|
75
|
-
await expect(responsePromise).resolves.toEqual(response);
|
|
76
|
-
});
|
|
77
|
-
});
|