forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests to AdminX framework package (TryGhost#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.
- Loading branch information
1 parent
b1666f5
commit 5e057de
Showing
37 changed files
with
874 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 1,6 @@ | ||
export {default as useFilterableApi} from './hooks/useFilterableApi'; | ||
export {default as useForm} from './hooks/useForm'; | ||
export type {Dirtyable, ErrorMessages, FormHook, OkProps, SaveHandler, SaveState} from './hooks/useForm'; | ||
export {default as useHandleError} from './hooks/useHandleError'; | ||
export {usePermission} from './hooks/usePermissions'; | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 1,6 @@ | ||
module.exports = { | ||
parser: '@typescript-eslint/parser', | ||
plugins: ['ghost'], | ||
extends: [ | ||
'plugin:ghost/test' | ||
'plugin:ghost/ts-test' | ||
] | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,75 @@ | ||
import {act, renderHook} from '@testing-library/react'; | ||
import * as assert from 'assert/strict'; | ||
import useForm from '../../../src/hooks/useForm'; | ||
|
||
describe('useForm', function () { | ||
describe('formState', function () { | ||
it('returns the initial form state', function () { | ||
const {result} = renderHook(() => useForm({ | ||
initialState: {a: 1}, | ||
onSave: () => {} | ||
})); | ||
|
||
assert.deepEqual(result.current.formState, {a: 1}); | ||
}); | ||
}); | ||
|
||
describe('updateForm', function () { | ||
it('updates the form state', function () { | ||
const {result} = renderHook(() => useForm({ | ||
initialState: {a: 1}, | ||
onSave: () => {} | ||
})); | ||
|
||
act(() => result.current.updateForm(state => ({...state, b: 2}))); | ||
|
||
assert.deepEqual(result.current.formState, {a: 1, b: 2}); | ||
}); | ||
|
||
it('sets the saveState to unsaved', function () { | ||
const {result} = renderHook(() => useForm({ | ||
initialState: {a: 1}, | ||
onSave: () => {} | ||
})); | ||
|
||
act(() => result.current.updateForm(state => ({...state, a: 2}))); | ||
|
||
assert.deepEqual(result.current.saveState, 'unsaved'); | ||
}); | ||
}); | ||
|
||
describe('handleSave', function () { | ||
it('does nothing when the state has not changed', async function () { | ||
let onSaveCalled = false; | ||
|
||
const {result} = renderHook(() => useForm({ | ||
initialState: {a: 1}, | ||
onSave: () => { | ||
onSaveCalled = true; | ||
} | ||
})); | ||
|
||
assert.equal(await act(() => result.current.handleSave()), true); | ||
|
||
assert.equal(result.current.saveState, ''); | ||
assert.equal(onSaveCalled, false); | ||
}); | ||
|
||
it('calls the onSave callback when the state has changed', async function () { | ||
let onSaveCalled = false; | ||
|
||
const {result} = renderHook(() => useForm({ | ||
initialState: {a: 1}, | ||
onSave: () => { | ||
onSaveCalled = true; | ||
} | ||
})); | ||
|
||
act(() => result.current.updateForm(state => ({...state, a: 2}))); | ||
assert.equal(await act(() => result.current.handleSave()), true); | ||
|
||
assert.equal(result.current.saveState, 'saved'); | ||
assert.equal(onSaveCalled, true); | ||
}); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
apps/admin-x-framework/test/unit/utils/api/fetchApi.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,58 @@ | ||
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) | ||
}]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.