@sanity/sdk-react 0.0.0-alpha.9 → 0.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +33 -126
  2. package/dist/index.d.ts +4742 -2
  3. package/dist/index.js +1054 -2
  4. package/dist/index.js.map +1 -1
  5. package/package.json +19 -43
  6. package/src/_exports/index.ts +66 -10
  7. package/src/components/Login/LoginLinks.test.tsx +90 -0
  8. package/src/components/Login/LoginLinks.tsx +58 -0
  9. package/src/components/SDKProvider.test.tsx +79 -0
  10. package/src/components/SDKProvider.tsx +42 -0
  11. package/src/components/SanityApp.test.tsx +104 -2
  12. package/src/components/SanityApp.tsx +54 -17
  13. package/src/components/auth/AuthBoundary.test.tsx +2 -2
  14. package/src/components/auth/AuthBoundary.tsx +13 -3
  15. package/src/components/auth/Login.test.tsx +1 -1
  16. package/src/components/auth/Login.tsx +11 -26
  17. package/src/components/auth/LoginCallback.tsx +4 -7
  18. package/src/components/auth/LoginError.tsx +12 -8
  19. package/src/components/auth/LoginFooter.tsx +13 -20
  20. package/src/components/auth/LoginLayout.tsx +8 -9
  21. package/src/components/auth/authTestHelpers.tsx +1 -8
  22. package/src/components/utils.ts +22 -0
  23. package/src/context/SanityInstanceContext.ts +4 -0
  24. package/src/context/SanityProvider.test.tsx +1 -1
  25. package/src/context/SanityProvider.tsx +10 -8
  26. package/src/hooks/_synchronous-groq-js.mjs +4 -0
  27. package/src/hooks/auth/useAuthState.tsx +0 -2
  28. package/src/hooks/auth/useCurrentUser.tsx +26 -20
  29. package/src/hooks/auth/useDashboardOrganizationId.test.tsx +42 -0
  30. package/src/hooks/auth/useDashboardOrganizationId.tsx +29 -0
  31. package/src/hooks/client/useClient.ts +8 -30
  32. package/src/hooks/comlink/useFrameConnection.test.tsx +55 -10
  33. package/src/hooks/comlink/useFrameConnection.ts +39 -43
  34. package/src/hooks/comlink/useManageFavorite.test.ts +106 -0
  35. package/src/hooks/comlink/useManageFavorite.ts +101 -0
  36. package/src/hooks/comlink/useRecordDocumentHistoryEvent.test.ts +77 -0
  37. package/src/hooks/comlink/useRecordDocumentHistoryEvent.ts +79 -0
  38. package/src/hooks/comlink/useWindowConnection.test.ts +53 -12
  39. package/src/hooks/comlink/useWindowConnection.ts +69 -29
  40. package/src/hooks/context/useSanityInstance.test.tsx +1 -1
  41. package/src/hooks/context/useSanityInstance.ts +21 -5
  42. package/src/hooks/dashboard/useNavigateToStudioDocument.ts +97 -0
  43. package/src/hooks/dashboard/useStudioWorkspacesByResourceId.test.tsx +274 -0
  44. package/src/hooks/dashboard/useStudioWorkspacesByResourceId.ts +91 -0
  45. package/src/hooks/datasets/useDatasets.ts +37 -0
  46. package/src/hooks/document/useApplyActions.test.ts +5 -4
  47. package/src/hooks/document/useApplyActions.ts +55 -5
  48. package/src/hooks/document/useDocument.test.ts +2 -2
  49. package/src/hooks/document/useDocument.ts +90 -21
  50. package/src/hooks/document/useDocumentEvent.test.ts +13 -3
  51. package/src/hooks/document/useDocumentEvent.ts +36 -4
  52. package/src/hooks/document/useDocumentSyncStatus.test.ts +1 -1
  53. package/src/hooks/document/useDocumentSyncStatus.ts +26 -2
  54. package/src/hooks/document/useEditDocument.test.ts +55 -10
  55. package/src/hooks/document/useEditDocument.ts +159 -31
  56. package/src/hooks/document/usePermissions.ts +82 -0
  57. package/src/hooks/helpers/createCallbackHook.tsx +3 -2
  58. package/src/hooks/helpers/createStateSourceHook.test.tsx +66 -0
  59. package/src/hooks/helpers/createStateSourceHook.tsx +29 -10
  60. package/src/hooks/infiniteList/useInfiniteList.test.tsx +152 -0
  61. package/src/hooks/infiniteList/useInfiniteList.ts +174 -0
  62. package/src/hooks/paginatedList/usePaginatedList.test.tsx +259 -0
  63. package/src/hooks/paginatedList/usePaginatedList.ts +290 -0
  64. package/src/hooks/preview/usePreview.test.tsx +6 -6
  65. package/src/hooks/preview/usePreview.tsx +12 -9
  66. package/src/hooks/projection/useProjection.test.tsx +218 -0
  67. package/src/hooks/projection/useProjection.ts +147 -0
  68. package/src/hooks/projects/useProject.ts +45 -0
  69. package/src/hooks/projects/useProjects.ts +41 -0
  70. package/src/hooks/query/useQuery.test.tsx +188 -0
  71. package/src/hooks/query/useQuery.ts +103 -0
  72. package/src/hooks/users/useUsers.test.ts +163 -0
  73. package/src/hooks/users/useUsers.ts +107 -0
  74. package/src/utils/getEnv.ts +21 -0
  75. package/src/version.ts +8 -0
  76. package/dist/_chunks-es/context.js +0 -8
  77. package/dist/_chunks-es/context.js.map +0 -1
  78. package/dist/_chunks-es/useLogOut.js +0 -45
  79. package/dist/_chunks-es/useLogOut.js.map +0 -1
  80. package/dist/components.d.ts +0 -111
  81. package/dist/components.js +0 -153
  82. package/dist/components.js.map +0 -1
  83. package/dist/context.d.ts +0 -45
  84. package/dist/context.js +0 -5
  85. package/dist/context.js.map +0 -1
  86. package/dist/hooks.d.ts +0 -3532
  87. package/dist/hooks.js +0 -218
  88. package/dist/hooks.js.map +0 -1
  89. package/src/_exports/components.ts +0 -2
  90. package/src/_exports/context.ts +0 -2
  91. package/src/_exports/hooks.ts +0 -32
  92. package/src/hooks/client/useClient.test.tsx +0 -130
  93. package/src/hooks/documentCollection/useDocuments.test.ts +0 -130
  94. package/src/hooks/documentCollection/useDocuments.ts +0 -135
@@ -1,4 +1,4 @@
1
- import {applyActions, createDocument} from '@sanity/sdk'
1
+ import {applyActions, createDocument, type ResourceId} from '@sanity/sdk'
2
2
  import {describe, it} from 'vitest'
3
3
 
4
4
  import {createCallbackHook} from '../helpers/createCallbackHook'
@@ -14,11 +14,12 @@ vi.mock('@sanity/sdk', async (importOriginal) => {
14
14
  describe('useApplyActions', () => {
15
15
  it('calls `createCallbackHook` with `applyActions`', async () => {
16
16
  const {useApplyActions} = await import('./useApplyActions')
17
- expect(createCallbackHook).toHaveBeenCalledWith(applyActions)
17
+ const resourceId: ResourceId = 'project1.dataset1'
18
+ expect(createCallbackHook).not.toHaveBeenCalled()
18
19
 
19
20
  expect(applyActions).not.toHaveBeenCalled()
20
- const apply = useApplyActions()
21
+ const apply = useApplyActions(resourceId)
21
22
  apply(createDocument({_type: 'author'}))
22
- expect(applyActions).toHaveBeenCalled()
23
+ expect(applyActions).toHaveBeenCalledWith(createDocument({_type: 'author'}))
23
24
  })
24
25
  })
@@ -3,22 +3,72 @@ import {
3
3
  applyActions,
4
4
  type ApplyActionsOptions,
5
5
  type DocumentAction,
6
+ type ResourceId,
6
7
  } from '@sanity/sdk'
7
8
  import {type SanityDocument} from '@sanity/types'
8
9
 
9
10
  import {createCallbackHook} from '../helpers/createCallbackHook'
10
11
 
11
- /** @beta */
12
- export function useApplyActions(): <TDocument extends SanityDocument>(
12
+ /**
13
+ *
14
+ * @beta
15
+ *
16
+ * Provides a callback for applying one or more actions to a document.
17
+ *
18
+ * @category Documents
19
+ * @param resourceId - The resource ID of the document to apply actions to. If not provided, the document will use the default resource.
20
+ * @returns A function that takes one more more {@link DocumentAction}s and returns a promise that resolves to an {@link ActionsResult}.
21
+ * @example Publish or unpublish a document
22
+ * ```
23
+ * import { publishDocument, unpublishDocument } from '@sanity/sdk'
24
+ * import { useApplyActions } from '@sanity/sdk-react'
25
+ *
26
+ * const apply = useApplyActions()
27
+ * const myDocument = { _id: 'my-document-id', _type: 'my-document-type' }
28
+ *
29
+ * return (
30
+ * <button onClick={() => apply(publishDocument(myDocument))}>Publish</button>
31
+ * <button onClick={() => apply(unpublishDocument(myDocument))}>Unpublish</button>
32
+ * )
33
+ * ```
34
+ *
35
+ * @example Create and publish a new document
36
+ * ```
37
+ * import { createDocument, publishDocument } from '@sanity/sdk'
38
+ * import { useApplyActions } from '@sanity/sdk-react'
39
+ *
40
+ * const apply = useApplyActions()
41
+ *
42
+ * const handleCreateAndPublish = () => {
43
+ * const handle = { _id: window.crypto.randomUUID(), _type: 'my-document-type' }
44
+ * apply([
45
+ * createDocument(handle),
46
+ * publishDocument(handle),
47
+ * ])
48
+ * }
49
+ *
50
+ * return (
51
+ * <button onClick={handleCreateAndPublish}>
52
+ * I’m feeling lucky
53
+ * </button>
54
+ * )
55
+ * ```
56
+ */
57
+ export function useApplyActions(
58
+ resourceId?: ResourceId,
59
+ ): <TDocument extends SanityDocument>(
13
60
  action: DocumentAction<TDocument> | DocumentAction<TDocument>[],
14
61
  options?: ApplyActionsOptions,
15
62
  ) => Promise<ActionsResult<TDocument>>
63
+
16
64
  /** @beta */
17
- export function useApplyActions(): (
65
+ export function useApplyActions(
66
+ resourceId?: ResourceId,
67
+ ): (
18
68
  action: DocumentAction | DocumentAction[],
19
69
  options?: ApplyActionsOptions,
20
70
  ) => Promise<ActionsResult> {
21
- return _useApplyActions()
71
+ return _useApplyActions(resourceId)()
22
72
  }
23
73
 
24
- const _useApplyActions = createCallbackHook(applyActions)
74
+ const _useApplyActions = (resourceId?: ResourceId) => createCallbackHook(applyActions, resourceId)
@@ -46,7 +46,7 @@ describe('useDocument hook', () => {
46
46
  subscribe,
47
47
  } as unknown as StateSource<unknown>)
48
48
 
49
- const {result} = renderHook(() => useDocument('doc1'))
49
+ const {result} = renderHook(() => useDocument({_id: 'doc1', _type: 'book'}))
50
50
 
51
51
  expect(result.current).toEqual(doc)
52
52
  expect(getCurrent).toHaveBeenCalled()
@@ -69,7 +69,7 @@ describe('useDocument hook', () => {
69
69
  // Render the hook and capture the thrown promise.
70
70
  const {result} = renderHook(() => {
71
71
  try {
72
- return useDocument('doc1')
72
+ return useDocument({_id: 'doc1', _type: 'book'})
73
73
  } catch (e) {
74
74
  return e
75
75
  }
@@ -1,38 +1,107 @@
1
1
  import {
2
2
  type DocumentHandle,
3
3
  getDocumentState,
4
+ getResourceId,
4
5
  type JsonMatch,
5
6
  type JsonMatchPath,
6
7
  resolveDocument,
7
8
  } from '@sanity/sdk'
8
9
  import {type SanityDocument} from '@sanity/types'
9
- import {useCallback, useMemo, useSyncExternalStore} from 'react'
10
10
 
11
- import {useSanityInstance} from '../context/useSanityInstance'
11
+ import {createStateSourceHook} from '../helpers/createStateSourceHook'
12
12
 
13
- /** @beta */
13
+ /**
14
+ * @beta
15
+ *
16
+ * ## useDocument(doc, path)
17
+ * Read and subscribe to nested values in a document
18
+ * @category Documents
19
+ * @param doc - The document to read state from. If you pass a `DocumentHandle` with a `resourceId` in the DocumentResourceId format (`document:projectId.dataset:documentId`)
20
+ * the document will be read from the specified Sanity project and dataset that is included in the handle. If no `resourceId` is provided, the default project and dataset from your `SanityApp` configuration will be used.
21
+ * @param path - The path to the nested value to read from
22
+ * @returns The value at the specified path
23
+ * @example
24
+ * ```tsx
25
+ * import {type DocumentHandle, useDocument} from '@sanity/sdk-react'
26
+ *
27
+ * function OrderLink({documentHandle}: {documentHandle: DocumentHandle}) {
28
+ * const title = useDocument(documentHandle, 'title')
29
+ * const id = useDocument(documentHandle, '_id')
30
+ *
31
+ * return (
32
+ * <a href=`/order/${id}`>Order {title} today!</a>
33
+ * )
34
+ * }
35
+ * ```
36
+ *
37
+ */
14
38
  export function useDocument<
15
39
  TDocument extends SanityDocument,
16
40
  TPath extends JsonMatchPath<TDocument>,
17
- >(doc: string | DocumentHandle<TDocument>, path: TPath): JsonMatch<TDocument, TPath> | undefined
18
- /** @beta */
41
+ >(doc: DocumentHandle<TDocument>, path: TPath): JsonMatch<TDocument, TPath> | undefined
42
+
43
+ /**
44
+ * @beta
45
+ * ## useDocument(doc)
46
+ * Read and subscribe to an entire document
47
+ * @param doc - The document to read state from
48
+ * @returns The document state as an object
49
+ * @example
50
+ * ```tsx
51
+ * import {type SanityDocument, type DocumentHandle, useDocument} from '@sanity/sdk-react'
52
+ *
53
+ * interface Book extends SanityDocument {
54
+ * title: string
55
+ * author: string
56
+ * summary: string
57
+ * }
58
+ *
59
+ * function DocumentView({documentHandle}: {documentHandle: DocumentHandle}) {
60
+ * const book = useDocument<Book>(documentHandle)
61
+ *
62
+ * return (
63
+ * <article>
64
+ * <h1>{book?.title}</h1>
65
+ * <address>By {book?.author}</address>
66
+ *
67
+ * <h2>Summary</h2>
68
+ * {book?.summary}
69
+ *
70
+ * <h2>Order</h2>
71
+ * <a href=`/order/${book._id}`>Order {book?.title} today!</a>
72
+ * </article>
73
+ * )
74
+ * }
75
+ * ```
76
+ *
77
+ */
19
78
  export function useDocument<TDocument extends SanityDocument>(
20
- doc: string | DocumentHandle<TDocument>,
79
+ doc: DocumentHandle<TDocument>,
21
80
  ): TDocument | null
22
- /** @beta */
23
- export function useDocument(doc: string | DocumentHandle, path?: string): unknown {
24
- const documentId = typeof doc === 'string' ? doc : doc._id
25
- const instance = useSanityInstance()
26
- const isDocumentReady = useCallback(
27
- () => getDocumentState(instance, documentId).getCurrent() !== undefined,
28
- [instance, documentId],
29
- )
30
- if (!isDocumentReady()) throw resolveDocument(instance, documentId)
31
-
32
- const {subscribe, getCurrent} = useMemo(
33
- () => getDocumentState(instance, documentId, path),
34
- [documentId, instance, path],
35
- )
36
81
 
37
- return useSyncExternalStore(subscribe, getCurrent)
82
+ /**
83
+ * @beta
84
+ * Reads and subscribes to a document’s realtime state, incorporating both local and remote changes.
85
+ * When called with a `path` argument, the hook will return the nested value’s state.
86
+ * When called without a `path` argument, the entire document’s state will be returned.
87
+ *
88
+ * @remarks
89
+ * `useDocument` is designed to be used within a realtime context in which local updates to documents
90
+ * need to be displayed before they are persisted to the remote copy. This can be useful within a collaborative
91
+ * or realtime editing interface where local changes need to be reflected immediately.
92
+ *
93
+ * However, this hook can be too resource intensive for applications where static document values simply
94
+ * need to be displayed (or when changes to documents don’t need to be reflected immediately);
95
+ * consider using `usePreview` or `useQuery` for these use cases instead. These hooks leverage the Sanity
96
+ * Live Content API to provide a more efficient way to read and subscribe to document state.
97
+ */
98
+ export function useDocument(doc: DocumentHandle, path?: string): unknown {
99
+ return _useDocument(doc, path)
38
100
  }
101
+
102
+ const _useDocument = createStateSourceHook<[doc: DocumentHandle, path?: string], unknown>({
103
+ getState: getDocumentState,
104
+ shouldSuspend: (instance, doc) => getDocumentState(instance, doc._id).getCurrent() === undefined,
105
+ suspender: resolveDocument,
106
+ getResourceId: (doc) => getResourceId(doc.resourceId),
107
+ })
@@ -1,5 +1,10 @@
1
1
  // tests/useDocumentEvent.test.ts
2
- import {createSanityInstance, type DocumentEvent, subscribeDocumentEvents} from '@sanity/sdk'
2
+ import {
3
+ createSanityInstance,
4
+ type DocumentEvent,
5
+ type DocumentHandle,
6
+ subscribeDocumentEvents,
7
+ } from '@sanity/sdk'
3
8
  import {renderHook} from '@testing-library/react'
4
9
  import {beforeEach, describe, expect, it, vi} from 'vitest'
5
10
 
@@ -16,6 +21,11 @@ vi.mock('../context/useSanityInstance', () => ({
16
21
  }))
17
22
 
18
23
  const instance = createSanityInstance({projectId: 'p', dataset: 'd'})
24
+ const docHandle: DocumentHandle = {
25
+ _id: 'doc1',
26
+ _type: 'book',
27
+ resourceId: 'document:p.d:doc1',
28
+ }
19
29
 
20
30
  describe('useDocumentEvent hook', () => {
21
31
  beforeEach(() => {
@@ -28,7 +38,7 @@ describe('useDocumentEvent hook', () => {
28
38
  const unsubscribe = vi.fn()
29
39
  vi.mocked(subscribeDocumentEvents).mockReturnValue(unsubscribe)
30
40
 
31
- renderHook(() => useDocumentEvent(handler))
41
+ renderHook(() => useDocumentEvent(handler, docHandle))
32
42
 
33
43
  expect(vi.mocked(subscribeDocumentEvents)).toHaveBeenCalledTimes(1)
34
44
  expect(vi.mocked(subscribeDocumentEvents).mock.calls[0][0]).toBe(instance)
@@ -46,7 +56,7 @@ describe('useDocumentEvent hook', () => {
46
56
  const unsubscribe = vi.fn()
47
57
  vi.mocked(subscribeDocumentEvents).mockReturnValue(unsubscribe)
48
58
 
49
- const {unmount} = renderHook(() => useDocumentEvent(handler))
59
+ const {unmount} = renderHook(() => useDocumentEvent(handler, docHandle))
50
60
  unmount()
51
61
  expect(unsubscribe).toHaveBeenCalledTimes(1)
52
62
  })
@@ -1,10 +1,42 @@
1
- import {type DocumentEvent, subscribeDocumentEvents} from '@sanity/sdk'
1
+ import {
2
+ type DocumentEvent,
3
+ type DocumentHandle,
4
+ getResourceId,
5
+ subscribeDocumentEvents,
6
+ } from '@sanity/sdk'
2
7
  import {useCallback, useEffect, useInsertionEffect, useRef} from 'react'
3
8
 
4
9
  import {useSanityInstance} from '../context/useSanityInstance'
5
10
 
6
- /** @beta */
7
- export function useDocumentEvent(handler: (documentEvent: DocumentEvent) => void): void {
11
+ /**
12
+ *
13
+ * @beta
14
+ *
15
+ * Subscribes an event handler to events in your application’s document store, such as document
16
+ * creation, deletion, and updates.
17
+ *
18
+ * @category Documents
19
+ * @param handler - The event handler to register.
20
+ * @param doc - The document to subscribe to events for. If you pass a `DocumentHandle` with a `resourceId` (in the format of `document:projectId.dataset:documentId`)
21
+ * the document will be read from the specified Sanity project and dataset that is included in the handle. If no `resourceId` is provided, the default project and dataset from your `SanityApp` configuration will be used.
22
+ * @example
23
+ * ```
24
+ * import {useDocumentEvent} from '@sanity/sdk-react'
25
+ * import {type DocumentEvent} from '@sanity/sdk'
26
+ *
27
+ * useDocumentEvent((event) => {
28
+ * if (event.type === DocumentEvent.DocumentDeletedEvent) {
29
+ * alert(`Document with ID ${event.documentId} deleted!`)
30
+ * } else {
31
+ * console.log(event)
32
+ * }
33
+ * })
34
+ * ```
35
+ */
36
+ export function useDocumentEvent(
37
+ handler: (documentEvent: DocumentEvent) => void,
38
+ doc: DocumentHandle,
39
+ ): void {
8
40
  const ref = useRef(handler)
9
41
 
10
42
  useInsertionEffect(() => {
@@ -15,7 +47,7 @@ export function useDocumentEvent(handler: (documentEvent: DocumentEvent) => void
15
47
  return ref.current(documentEvent)
16
48
  }, [])
17
49
 
18
- const instance = useSanityInstance()
50
+ const instance = useSanityInstance(getResourceId(doc.resourceId))
19
51
  useEffect(() => {
20
52
  return subscribeDocumentEvents(instance, stableHandler)
21
53
  }, [instance, stableHandler])
@@ -7,7 +7,7 @@ import {createStateSourceHook} from '../helpers/createStateSourceHook'
7
7
  vi.mock('../helpers/createStateSourceHook', () => ({createStateSourceHook: vi.fn(identity)}))
8
8
  vi.mock('@sanity/sdk', () => ({getDocumentSyncStatus: vi.fn()}))
9
9
 
10
- describe('useAuthToken', () => {
10
+ describe('useDocumentSyncStatus', () => {
11
11
  it('calls `createStateSourceHook` with `getTokenState`', async () => {
12
12
  const {useDocumentSyncStatus} = await import('./useDocumentSyncStatus')
13
13
  expect(createStateSourceHook).toHaveBeenCalledWith(getDocumentSyncStatus)
@@ -1,6 +1,30 @@
1
- import {getDocumentSyncStatus} from '@sanity/sdk'
1
+ import {type DocumentHandle, getDocumentSyncStatus} from '@sanity/sdk'
2
2
 
3
3
  import {createStateSourceHook} from '../helpers/createStateSourceHook'
4
4
 
5
+ type UseDocumentSyncStatus = {
6
+ /**
7
+ * Exposes the document’s sync status between local and remote document states.
8
+ *
9
+ * @category Documents
10
+ * @param doc - The document handle to get sync status for. If you pass a `DocumentHandle` with a `resourceId` (in the format of `document:projectId.dataset:documentId`)
11
+ * the document will be read from the specified Sanity project and dataset that is included in the handle. If no `resourceId` is provided, the default project and dataset from your `SanityApp` configuration will be used.
12
+ * @returns `true` if local changes are synced with remote, `false` if the changes are not synced, and `undefined` if the document is not found
13
+ * @example Disable a Save button when there are no changes to sync
14
+ * ```
15
+ * const myDocumentHandle = { _id: 'documentId', _type: 'documentType', resourceId: 'document:projectId:dataset:documentId' }
16
+ * const documentSynced = useDocumentSyncStatus(myDocumentHandle)
17
+ *
18
+ * return (
19
+ * <button disabled={documentSynced}>
20
+ * Save Changes
21
+ * </button>
22
+ * )
23
+ * ```
24
+ */
25
+ (doc: DocumentHandle): boolean | undefined
26
+ }
27
+
5
28
  /** @beta */
6
- export const useDocumentSyncStatus = createStateSourceHook(getDocumentSyncStatus)
29
+ export const useDocumentSyncStatus: UseDocumentSyncStatus =
30
+ createStateSourceHook(getDocumentSyncStatus)
@@ -1,6 +1,7 @@
1
1
  // tests/useEditDocument.test.ts
2
2
  import {
3
3
  createSanityInstance,
4
+ type DocumentHandle,
4
5
  editDocument,
5
6
  getDocumentState,
6
7
  resolveDocument,
@@ -35,13 +36,19 @@ vi.mock('./useApplyActions', () => ({
35
36
  // Create a fake instance to be returned by useSanityInstance.
36
37
  const instance = createSanityInstance({projectId: 'p', dataset: 'd'})
37
38
 
38
- const doc: SanityDocument = {
39
+ const doc = {
39
40
  _id: 'doc1',
40
41
  foo: 'bar',
41
42
  _type: 'book',
42
43
  _rev: 'tx0',
43
44
  _createdAt: '2025-02-06T00:11:00.000Z',
44
45
  _updatedAt: '2025-02-06T00:11:00.000Z',
46
+ } satisfies SanityDocument
47
+
48
+ const docHandle: DocumentHandle<SanityDocument> = {
49
+ _id: 'doc1',
50
+ _type: 'book',
51
+ resourceId: 'document:p.d:doc1',
45
52
  }
46
53
 
47
54
  describe('useEditDocument hook', () => {
@@ -61,10 +68,10 @@ describe('useEditDocument hook', () => {
61
68
  const apply = vi.fn().mockResolvedValue({transactionId: 'tx1'})
62
69
  vi.mocked(useApplyActions).mockReturnValue(apply)
63
70
 
64
- const {result} = renderHook(() => useEditDocument('doc1', 'foo'))
71
+ const {result} = renderHook(() => useEditDocument(docHandle, 'foo'))
65
72
  const promise = result.current('newValue')
66
- expect(editDocument).toHaveBeenCalledWith('doc1', {set: {foo: 'newValue'}})
67
- expect(apply).toHaveBeenCalledWith(editDocument('doc1', {set: {foo: 'newValue'}}))
73
+ expect(editDocument).toHaveBeenCalledWith(docHandle, {set: {foo: 'newValue'}})
74
+ expect(apply).toHaveBeenCalledWith(editDocument(docHandle, {set: {foo: 'newValue'}}))
68
75
  const actionsResult = await promise
69
76
  expect(actionsResult).toEqual({transactionId: 'tx1'})
70
77
  })
@@ -82,13 +89,51 @@ describe('useEditDocument hook', () => {
82
89
  const apply = vi.fn().mockResolvedValue({transactionId: 'tx2'})
83
90
  vi.mocked(useApplyActions).mockReturnValue(apply)
84
91
 
85
- const {result} = renderHook(() => useEditDocument('doc1'))
86
- const promise = result.current({foo: 'baz', extra: 'old', _id: 'doc1'})
87
- expect(apply).toHaveBeenCalledWith([editDocument('doc1', {set: {foo: 'baz'}})])
92
+ const {result} = renderHook(() => useEditDocument(docHandle))
93
+ const promise = result.current({...doc, foo: 'baz', extra: 'old', _id: 'doc1'})
94
+ expect(apply).toHaveBeenCalledWith([editDocument(docHandle, {set: {foo: 'baz'}})])
88
95
  const actionsResult = await promise
89
96
  expect(actionsResult).toEqual({transactionId: 'tx2'})
90
97
  })
91
98
 
99
+ it('applies a single edit action using an updater function for the given path', async () => {
100
+ const getCurrent = vi.fn().mockReturnValue(doc.foo)
101
+ const subscribe = vi.fn().mockReturnValue(vi.fn())
102
+ vi.mocked(getDocumentState).mockReturnValue({
103
+ getCurrent,
104
+ subscribe,
105
+ } as unknown as StateSource<SanityDocument>)
106
+
107
+ const apply = vi.fn().mockResolvedValue({transactionId: 'tx3'})
108
+ vi.mocked(useApplyActions).mockReturnValue(apply)
109
+
110
+ const {result} = renderHook(() => useEditDocument(docHandle, 'foo'))
111
+ const promise = result.current((prev: unknown) => `${prev}Updated`) // 'bar' becomes 'barUpdated'
112
+ expect(editDocument).toHaveBeenCalledWith(docHandle, {set: {foo: 'barUpdated'}})
113
+ expect(apply).toHaveBeenCalledWith(editDocument(docHandle, {set: {foo: 'barUpdated'}}))
114
+ const actionsResult = await promise
115
+ expect(actionsResult).toEqual({transactionId: 'tx3'})
116
+ })
117
+
118
+ it('applies edit actions using an updater function for the entire document', async () => {
119
+ const currentDoc = {...doc, foo: 'bar', extra: 'old'}
120
+ const getCurrent = vi.fn().mockReturnValue(currentDoc)
121
+ const subscribe = vi.fn().mockReturnValue(vi.fn())
122
+ vi.mocked(getDocumentState).mockReturnValue({
123
+ getCurrent,
124
+ subscribe,
125
+ } as unknown as StateSource<SanityDocument>)
126
+
127
+ const apply = vi.fn().mockResolvedValue({transactionId: 'tx4'})
128
+ vi.mocked(useApplyActions).mockReturnValue(apply)
129
+
130
+ const {result} = renderHook(() => useEditDocument(docHandle))
131
+ const promise = result.current((prevDoc) => ({...prevDoc, foo: 'baz'}))
132
+ expect(apply).toHaveBeenCalledWith([editDocument(docHandle, {set: {foo: 'baz'}})])
133
+ const actionsResult = await promise
134
+ expect(actionsResult).toEqual({transactionId: 'tx4'})
135
+ })
136
+
92
137
  it('throws an error if next value is not an object', () => {
93
138
  const getCurrent = vi.fn().mockReturnValue(doc)
94
139
  const subscribe = vi.fn().mockReturnValue(vi.fn())
@@ -100,8 +145,8 @@ describe('useEditDocument hook', () => {
100
145
  const fakeApply = vi.fn()
101
146
  vi.mocked(useApplyActions).mockReturnValue(fakeApply)
102
147
 
103
- const {result} = renderHook(() => useEditDocument('doc1'))
104
- expect(() => result.current('notAnObject' as unknown as Partial<SanityDocument>)).toThrowError(
148
+ const {result} = renderHook(() => useEditDocument(docHandle))
149
+ expect(() => result.current('notAnObject' as unknown as SanityDocument)).toThrowError(
105
150
  'No path was provided to `useEditDocument` and the value provided was not a document object.',
106
151
  )
107
152
  })
@@ -122,7 +167,7 @@ describe('useEditDocument hook', () => {
122
167
  // Render the hook and capture the thrown promise.
123
168
  const {result} = renderHook(() => {
124
169
  try {
125
- return useEditDocument('doc1')
170
+ return useEditDocument(docHandle)
126
171
  } catch (e) {
127
172
  return e
128
173
  }