5e057dee11
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.
98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
import {deleteFromQueryCache, insertToQueryCache, updateQueryCache} from '../../../../src/utils/api/updateQueries';
|
|
|
|
describe('cache update functions', function () {
|
|
describe('insertToQueryCache', function () {
|
|
it('appends records from the new data', function () {
|
|
const newData = {
|
|
posts: [{id: '2'}]
|
|
};
|
|
|
|
const currentData = {
|
|
posts: [{id: '1'}]
|
|
};
|
|
|
|
const result = insertToQueryCache('posts')(newData, currentData);
|
|
|
|
expect(result).toEqual({
|
|
posts: [{id: '1'}, {id: '2'}]
|
|
});
|
|
});
|
|
|
|
it('appends to the last page for paginated queries', function () {
|
|
const newData = {
|
|
posts: [{id: '3'}]
|
|
};
|
|
|
|
const currentData = {
|
|
pages: [{posts: [{id: '1'}]}, {posts: [{id: '2'}]}]
|
|
};
|
|
|
|
const result = insertToQueryCache('posts')(newData, currentData);
|
|
|
|
expect(result).toEqual({
|
|
pages: [{posts: [{id: '1'}]}, {posts: [{id: '2'}, {id: '3'}]}]
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('updateQueryCache', function () {
|
|
it('updates based on the ID', function () {
|
|
const newData = {
|
|
posts: [{id: '2', title: 'New Title'}]
|
|
};
|
|
|
|
const currentData = {
|
|
posts: [{id: '1'}, {id: '2', title: 'Old Title'}]
|
|
};
|
|
|
|
const result = updateQueryCache('posts')(newData, currentData);
|
|
|
|
expect(result).toEqual({
|
|
posts: [{id: '1'}, {id: '2', title: 'New Title'}]
|
|
});
|
|
});
|
|
|
|
it('updates nested records in paginated queries', function () {
|
|
const newData = {
|
|
posts: [{id: '2', title: 'New Title'}]
|
|
};
|
|
|
|
const currentData = {
|
|
pages: [{posts: [{id: '1'}]}, {posts: [{id: '2', title: 'Old Title'}]}]
|
|
};
|
|
|
|
const result = updateQueryCache('posts')(newData, currentData);
|
|
|
|
expect(result).toEqual({
|
|
pages: [{posts: [{id: '1'}]}, {posts: [{id: '2', title: 'New Title'}]}]
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('deleteFromQueryCache', function () {
|
|
it('deletes based on the ID', function () {
|
|
const currentData = {
|
|
posts: [{id: '1'}, {id: '2'}]
|
|
};
|
|
|
|
const result = deleteFromQueryCache('posts')(null, currentData, '2');
|
|
|
|
expect(result).toEqual({
|
|
posts: [{id: '1'}]
|
|
});
|
|
});
|
|
|
|
it('deletes nested records in paginated queries', function () {
|
|
const currentData = {
|
|
pages: [{posts: [{id: '1'}]}, {posts: [{id: '2'}]}]
|
|
};
|
|
|
|
const result = deleteFromQueryCache('posts')(null, currentData, '2');
|
|
|
|
expect(result).toEqual({
|
|
pages: [{posts: [{id: '1'}]}, {posts: []}]
|
|
});
|
|
});
|
|
});
|
|
});
|