Skip to content

Commit

Permalink
test(sanity): add patch test suite to patch Actions API operation
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Sep 12, 2024
1 parent d928123 commit d7854e8
Show file tree
Hide file tree
Showing 2 changed files with 304 additions and 0 deletions.
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 [],
}
`;
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()
})
})
})

0 comments on commit d7854e8

Please sign in to comment.