Ghost/apps/admin-x-framework/test/unit/utils/api/fetchApi.test.tsx
Jono M 5e057dee11
Added tests to AdminX framework package (#19022)
refs https://github.com/TryGhost/Product/issues/4159

---

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
<!--
copilot:summary
-->
### <samp>🤖[[deprecated]](https://githubnext.com/copilot-for-prs-sunset)
Generated by Copilot at 9e68f4d</samp>

This pull request refactors several components in the `admin-x-settings`
app to use common hooks from the `@tryghost/admin-x-framework` package,
which reduces code duplication and improves consistency. It also updates
the `package.json` file and adds unit tests for the `admin-x-framework`
package, which improves the formatting, testing, and dependency
management. Additionally, it makes some minor changes to the `hooks.ts`,
`FrameworkProvider.tsx`, and `.eslintrc.cjs` files in the
`admin-x-framework` package, which enhance the public API and the
linting configuration.
2023-11-20 11:00:51 +00:00

59 lines
1.9 KiB
TypeScript

import {renderHook} from '@testing-library/react';
import React, {ReactNode} from 'react';
import FrameworkProvider from '../../../../src/providers/FrameworkProvider';
import {useFetchApi} from '../../../../src/utils/api/fetchApi';
import {withMockFetch} from '../../../utils/mockFetch';
const wrapper: React.FC<{ children: ReactNode }> = ({children}) => (
<FrameworkProvider
basePath=''
externalNavigate={() => {}}
ghostVersion='5.x'
sentryDSN=''
unsplashConfig={{
Authorization: '',
'Accept-Version': '',
'Content-Type': '',
'App-Pragma': '',
'X-Unsplash-Cache': true
}}
onDelete={() => {}}
onInvalidate={() => {}}
onUpdate={() => {}}
>
{children}
</FrameworkProvider>
);
describe('useFetchApi', function () {
it('makes an API request', async function () {
await withMockFetch({
json: {test: 1}
}, async (mock) => {
const {result} = renderHook(() => useFetchApi(), {wrapper});
const data = await result.current<{test: number}>('http://localhost:3000/ghost/api/admin/test/', {
method: 'POST',
body: 'test',
retry: false
});
expect(data).toEqual({test: 1});
expect(mock.calls.length).toBe(1);
expect(mock.calls[0]).toEqual(['http://localhost:3000/ghost/api/admin/test/', {
body: 'test',
credentials: 'include',
headers: {
'app-pragma': 'no-cache',
'x-ghost-version': '5.x',
'content-type': 'application/json'
},
method: 'POST',
mode: 'cors',
signal: expect.any(AbortSignal)
}]);
});
});
});