diff --git a/.gitignore b/.gitignore index 53dd45cd..46837709 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules npm-debug.log* src/index.d.ts *.test.ts +*.test.ssr.ts diff --git a/index.d.ts b/index.d.ts index 12bbe272..b9728f20 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,28 +5,28 @@ import { AxiosStatic, AxiosInstance, AxiosResponse -} from "axios"; -import LRUCache from "lru-cache"; +} from 'axios' +import LRUCache from 'lru-cache' interface ResponseValues { - data: T; - loading: boolean; - error?: AxiosError; - response?: AxiosResponse; + data: T + loading: boolean + error?: AxiosError + response?: AxiosResponse } interface Options { - manual?: boolean; - useCache?: boolean; + manual?: boolean + useCache?: boolean } interface RefetchOptions { - useCache?: boolean; + useCache?: boolean } interface ConfigureOptions { - axios?: AxiosInstance | AxiosStatic | any; - cache?: LRUCache; + axios?: AxiosInstance | AxiosStatic | any + cache?: LRUCache } export default function useAxios( @@ -35,7 +35,13 @@ export default function useAxios( ): [ ResponseValues, (config?: AxiosRequestConfig, options?: RefetchOptions) => void -]; +] -export function configure(options: ConfigureOptions): void; -export function resetConfigure(): void; +export function loadCache(data: any[]): void +export function serializeCache(): any[] + +export function configure(options: ConfigureOptions): void +export function resetConfigure(): void + +// private +export const __ssrPromises: Promise[] diff --git a/jest.config.js b/jest.config.js index efe81a37..90d5beed 100644 --- a/jest.config.js +++ b/jest.config.js @@ -9,6 +9,19 @@ const projects = [ coverageDirectory: 'coverage', testMatch: ['**/?(*.)+(spec|test).ts?(x)'], preset: 'ts-jest/presets/js-with-ts' + }, + { + clearMocks: true, + coverageDirectory: 'coverage', + testMatch: ['**/?(*.)+(spec|test).ssr.js?(x)'], + testEnvironment: 'node' + }, + { + clearMocks: true, + coverageDirectory: 'coverage', + testMatch: ['**/?(*.)+(spec|test).ssr.ts?(x)'], + preset: 'ts-jest/presets/js-with-ts', + testEnvironment: 'node' } ] diff --git a/package.json b/package.json index f7d52aba..5bbf3ca8 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "prepare": "npm run clean && npm run build", "prepublishOnly": "npm run build", "release": "standard-version", - "pretest": "cp ./src/index.test.js ./src/index.test.ts && cp ./index.d.ts ./src", + "pretest": "cp ./src/index.test.js ./src/index.test.ts && cp ./src/index.test.ssr.js ./src/index.test.ssr.ts && cp ./index.d.ts ./src", "test": "jest --no-cache" }, "dependencies": { diff --git a/src/index.js b/src/index.js index 6dd35cc7..897d28d4 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ const actions = { REQUEST_END: 'REQUEST_END' } -const ssrPromises = [] +export const __ssrPromises = [] let cache let axiosInstance @@ -34,9 +34,9 @@ export function loadCache(data) { } export async function serializeCache() { - const ssrPromisesCopy = [...ssrPromises] + const ssrPromisesCopy = [...__ssrPromises] - ssrPromises.length = 0 + __ssrPromises.length = 0 await Promise.all(ssrPromisesCopy) @@ -130,8 +130,8 @@ export default function useAxios(config, options) { createInitialState(options) ) - if (typeof window === 'undefined') { - ssrPromises.push(axiosInstance({ ...config, adapter: cacheAdapter })) + if (typeof window === 'undefined' && !options.manual) { + __ssrPromises.push(axiosInstance({ ...config, adapter: cacheAdapter })) } React.useEffect(() => { diff --git a/src/index.test.ssr.js b/src/index.test.ssr.js new file mode 100644 index 00000000..7f0ff4c0 --- /dev/null +++ b/src/index.test.ssr.js @@ -0,0 +1,11 @@ +import { renderHook } from '@testing-library/react-hooks' + +import useAxios, { __ssrPromises } from './' + +jest.mock('axios') + +it('should not populate promises on server when manual', () => { + renderHook(() => useAxios('', { manual: true })) + + expect(__ssrPromises.length).toBe(0) +})