-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(sanity): add
patch
test suite to patch
Actions API operation
- Loading branch information
Showing
2 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
...re/store/_legacy/document/document-pair/serverOperations/__snapshots__/patch.test.ts.snap
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,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 [], | ||
} | ||
`; |
207 changes: 207 additions & 0 deletions
207
packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/patch.test.ts
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,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() | ||
}) | ||
}) | ||
}) |