From d7854e8f4f932e82f987c9aa6841c126c8c80477 Mon Sep 17 00:00:00 2001 From: Ash Date: Thu, 12 Sep 2024 15:46:55 +0100 Subject: [PATCH] test(sanity): add `patch` test suite to `patch` Actions API operation --- .../__snapshots__/patch.test.ts.snap | 97 ++++++++ .../serverOperations/patch.test.ts | 207 ++++++++++++++++++ 2 files changed, 304 insertions(+) create mode 100644 packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/__snapshots__/patch.test.ts.snap create mode 100644 packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/patch.test.ts diff --git a/packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/__snapshots__/patch.test.ts.snap b/packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/__snapshots__/patch.test.ts.snap new file mode 100644 index 000000000000..ade38ea2553d --- /dev/null +++ b/packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/__snapshots__/patch.test.ts.snap @@ -0,0 +1,97 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`patch execute calls createOrReplace with _revision_lock_pseudo_field_ if there is an already published document 1`] = ` +Object { + "listen": Array [], + "observable": Object { + "action": Array [ + Object { + "actions": Object { + "actionType": "sanity.action.document.publish", + "draftId": "drafts.my-id", + "ifPublishedRevisionId": "exampleRev", + "publishedId": "my-id", + }, + "options": Object { + "tag": "document.publish", + }, + }, + ], + "fetch": Array [], + "getDocuments": Array [], + "listen": Array [], + "request": Array [], + }, + "request": Array [], + "transaction": Array [], +} +`; + +exports[`patch execute removes the \`_updatedAt\` field 1`] = ` +Object { + "listen": Array [], + "observable": Object { + "action": Array [ + Object { + "actions": Object { + "actionType": "sanity.action.document.publish", + "draftId": "drafts.my-id", + "ifPublishedRevisionId": undefined, + "publishedId": "my-id", + }, + "options": Object { + "tag": "document.publish", + }, + }, + ], + "fetch": Array [], + "getDocuments": Array [], + "listen": Array [], + "request": Array [], + }, + "request": Array [], + "transaction": Array [], +} +`; + +exports[`patch execute takes in any and strengthens references where _strengthenOnPublish is true 1`] = ` +Object { + "listen": Array [], + "observable": Object { + "action": Array [ + Object { + "actions": Object { + "actionType": "sanity.action.document.publish", + "draftId": "drafts.my-id", + "ifPublishedRevisionId": undefined, + "publishedId": "my-id", + }, + "options": Object { + "tag": "document.publish", + }, + }, + ], + "fetch": Array [], + "getDocuments": Array [], + "listen": Array [], + "request": Array [], + }, + "request": Array [], + "transaction": Array [], +} +`; + +exports[`patch execute throws an error if the client has no draft snaphot 1`] = ` +Object { + "listen": Array [], + "observable": Object { + "action": Array [], + "fetch": Array [], + "getDocuments": Array [], + "listen": Array [], + "request": Array [], + }, + "request": Array [], + "transaction": Array [], +} +`; diff --git a/packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/patch.test.ts b/packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/patch.test.ts new file mode 100644 index 000000000000..3a19e9546e5d --- /dev/null +++ b/packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/patch.test.ts @@ -0,0 +1,207 @@ +import {beforeEach, describe, expect, it, jest} from '@jest/globals' +import {type SanityDocument} from 'sanity' + +import {createMockSanityClient} from '../../../../../../../test/mocks/mockSanityClient' +import {type OperationArgs} from '../operations/types' +import {isLiveEditEnabled} from '../utils/isLiveEditEnabled' +import {publish} from './publish' + +jest.mock('../utils/isLiveEditEnabled', () => ({isLiveEditEnabled: jest.fn()})) + +beforeEach(() => { + ;(isLiveEditEnabled as jest.Mock).mockClear() +}) + +describe('patch', () => { + describe('disabled', () => { + it('returns with LIVE_EDIT_ENABLED if isLiveEditEnabled', () => { + ;(isLiveEditEnabled as jest.Mock).mockImplementation( + // eslint-disable-next-line max-nested-callbacks + () => true, + ) + + expect( + publish.disabled( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + {} as any, + ), + ).toBe('LIVE_EDIT_ENABLED') + }) + + it('returns ALREADY_PUBLISHED if there is no draft and there is a published version', () => { + ;(isLiveEditEnabled as jest.Mock).mockImplementation( + // eslint-disable-next-line max-nested-callbacks + () => false, + ) + + expect( + publish.disabled({ + typeName: 'blah', + snapshots: { + draft: undefined, + published: {} as SanityDocument, + }, + } as unknown as OperationArgs), + ).toBe('ALREADY_PUBLISHED') + }) + + it("otherwise the operation isn't disabled", () => { + ;(isLiveEditEnabled as jest.Mock).mockImplementation( + // eslint-disable-next-line max-nested-callbacks + () => false, + ) + + expect( + publish.disabled({ + typeName: 'blah', + snapshots: { + draft: {} as SanityDocument, + published: {} as SanityDocument, + }, + } as unknown as OperationArgs), + ).toBe(false) + }) + }) + + describe('execute', () => { + it('removes the `_updatedAt` field', () => { + const client = createMockSanityClient() + + publish.execute({ + client, + idPair: { + draftId: 'drafts.my-id', + publishedId: 'my-id', + }, + snapshots: { + draft: { + _createdAt: '2021-09-14T22:48:02.303Z', + _rev: 'exampleRev', + _id: 'drafts.my-id', + _type: 'example', + _updatedAt: '2021-09-14T22:48:02.303Z', + newValue: 'hey', + }, + }, + } as unknown as OperationArgs) + + expect(client.$log).toMatchSnapshot() + }) + + it('calls createOrReplace with _revision_lock_pseudo_field_ if there is an already published document', () => { + const client = createMockSanityClient() + + publish.execute({ + client, + idPair: { + draftId: 'drafts.my-id', + publishedId: 'my-id', + }, + snapshots: { + draft: { + _createdAt: '2021-09-14T22:48:02.303Z', + _rev: 'exampleRev', + _id: 'drafts.my-id', + _type: 'example', + _updatedAt: '2021-09-14T22:48:02.303Z', + newValue: 'hey', + }, + published: { + _createdAt: '2021-09-14T22:48:02.303Z', + _rev: 'exampleRev', + _id: 'drafts.my-id', + _type: 'example', + _updatedAt: '2021-09-14T22:48:02.303Z', + }, + }, + } as unknown as OperationArgs) + + expect(client.$log).toMatchSnapshot() + }) + + it('takes in any and strengthens references where _strengthenOnPublish is true', () => { + const client = createMockSanityClient() + + publish.execute({ + client, + idPair: { + draftId: 'drafts.my-id', + publishedId: 'my-id', + }, + snapshots: { + draft: { + _createdAt: '2021-09-14T22:48:02.303Z', + _id: 'drafts.my-id', + _rev: 'exampleRev', + _type: 'my-type', + _updatedAt: '2021-09-14T22:48:02.303Z', + simpleRef: { + _type: 'reference', + _weak: true, + _ref: 'my-ref', + _strengthenOnPublish: true, + }, + notToBeStrengthened: { + _type: 'reference', + _weak: true, + _ref: 'my-ref', + }, + inAn: [ + { + _type: 'reference', + _weak: true, + _ref: 'my-ref-in-an-', + _strengthenOnPublish: true, + _key: 'my-key', + }, + { + _key: 'my-other-key', + _type: 'nestedObj', + myRef: { + _weak: true, + _ref: 'my-ref-in-an--nested', + _strengthenOnPublish: true, + }, + }, + { + _type: 'reference', + _weak: true, + _ref: 'my-ref-in-an--no-key', + _strengthenOnPublish: true, + }, + ], + }, + published: null, + }, + } as unknown as OperationArgs) + + expect(client.$log).toMatchSnapshot() + }) + + it('throws an error if the client has no draft snaphot', () => { + const client = createMockSanityClient() + + // eslint-disable-next-line max-nested-callbacks + expect(() => { + publish.execute({ + client, + idPair: { + draftId: 'drafts.my-id', + publishedId: 'my-id', + }, + snapshots: { + published: { + _createdAt: '2021-09-14T22:48:02.303Z', + _rev: 'exampleRev', + _id: 'drafts.my-id', + _type: 'example', + _updatedAt: '2021-09-14T22:48:02.303Z', + }, + }, + } as unknown as OperationArgs) + }).toThrow('cannot execute "publish" when draft is missing') + + expect(client.$log).toMatchSnapshot() + }) + }) +})