@sanity/sdk 0.0.0-chore-react-18-compat.1 → 0.0.0-chore-react-18-compat.3

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 (134) hide show
  1. package/dist/index.d.ts +441 -322
  2. package/dist/index.js +1685 -1481
  3. package/dist/index.js.map +1 -1
  4. package/package.json +13 -15
  5. package/src/_exports/index.ts +32 -30
  6. package/src/auth/authStore.test.ts +149 -104
  7. package/src/auth/authStore.ts +51 -100
  8. package/src/auth/handleAuthCallback.test.ts +67 -34
  9. package/src/auth/handleAuthCallback.ts +8 -7
  10. package/src/auth/logout.test.ts +61 -29
  11. package/src/auth/logout.ts +26 -28
  12. package/src/auth/refreshStampedToken.test.ts +197 -91
  13. package/src/auth/refreshStampedToken.ts +170 -59
  14. package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +5 -5
  15. package/src/auth/subscribeToStateAndFetchCurrentUser.ts +45 -47
  16. package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -5
  17. package/src/auth/subscribeToStorageEventsAndSetToken.ts +22 -24
  18. package/src/client/clientStore.test.ts +131 -67
  19. package/src/client/clientStore.ts +117 -116
  20. package/src/comlink/controller/actions/destroyController.test.ts +38 -13
  21. package/src/comlink/controller/actions/destroyController.ts +11 -15
  22. package/src/comlink/controller/actions/getOrCreateChannel.test.ts +56 -27
  23. package/src/comlink/controller/actions/getOrCreateChannel.ts +37 -35
  24. package/src/comlink/controller/actions/getOrCreateController.test.ts +27 -16
  25. package/src/comlink/controller/actions/getOrCreateController.ts +23 -22
  26. package/src/comlink/controller/actions/releaseChannel.test.ts +37 -13
  27. package/src/comlink/controller/actions/releaseChannel.ts +22 -21
  28. package/src/comlink/controller/comlinkControllerStore.test.ts +65 -36
  29. package/src/comlink/controller/comlinkControllerStore.ts +44 -5
  30. package/src/comlink/node/actions/getOrCreateNode.test.ts +31 -15
  31. package/src/comlink/node/actions/getOrCreateNode.ts +30 -29
  32. package/src/comlink/node/actions/releaseNode.test.ts +75 -55
  33. package/src/comlink/node/actions/releaseNode.ts +19 -21
  34. package/src/comlink/node/comlinkNodeStore.test.ts +6 -11
  35. package/src/comlink/node/comlinkNodeStore.ts +22 -5
  36. package/src/config/authConfig.ts +79 -0
  37. package/src/config/sanityConfig.ts +48 -0
  38. package/src/datasets/datasets.test.ts +2 -2
  39. package/src/datasets/datasets.ts +18 -5
  40. package/src/document/actions.test.ts +22 -10
  41. package/src/document/actions.ts +44 -56
  42. package/src/document/applyDocumentActions.test.ts +96 -36
  43. package/src/document/applyDocumentActions.ts +140 -99
  44. package/src/document/documentStore.test.ts +103 -155
  45. package/src/document/documentStore.ts +247 -238
  46. package/src/document/listen.ts +56 -55
  47. package/src/document/patchOperations.ts +0 -43
  48. package/src/document/permissions.test.ts +25 -12
  49. package/src/document/permissions.ts +11 -4
  50. package/src/document/processActions.test.ts +41 -8
  51. package/src/document/reducers.test.ts +87 -16
  52. package/src/document/reducers.ts +2 -2
  53. package/src/document/sharedListener.test.ts +34 -16
  54. package/src/document/sharedListener.ts +33 -11
  55. package/src/preview/getPreviewState.test.ts +40 -39
  56. package/src/preview/getPreviewState.ts +68 -56
  57. package/src/preview/previewConstants.ts +43 -0
  58. package/src/preview/previewQuery.test.ts +1 -1
  59. package/src/preview/previewQuery.ts +4 -5
  60. package/src/preview/previewStore.test.ts +13 -58
  61. package/src/preview/previewStore.ts +7 -21
  62. package/src/preview/resolvePreview.test.ts +33 -104
  63. package/src/preview/resolvePreview.ts +11 -21
  64. package/src/preview/subscribeToStateAndFetchBatches.test.ts +96 -97
  65. package/src/preview/subscribeToStateAndFetchBatches.ts +85 -81
  66. package/src/preview/util.ts +1 -0
  67. package/src/project/project.test.ts +3 -3
  68. package/src/project/project.ts +28 -5
  69. package/src/projection/getProjectionState.test.ts +188 -72
  70. package/src/projection/getProjectionState.ts +92 -62
  71. package/src/projection/projectionQuery.test.ts +114 -12
  72. package/src/projection/projectionQuery.ts +75 -32
  73. package/src/projection/projectionStore.test.ts +13 -51
  74. package/src/projection/projectionStore.ts +6 -43
  75. package/src/projection/resolveProjection.test.ts +32 -127
  76. package/src/projection/resolveProjection.ts +16 -28
  77. package/src/projection/subscribeToStateAndFetchBatches.test.ts +203 -116
  78. package/src/projection/subscribeToStateAndFetchBatches.ts +140 -85
  79. package/src/projection/types.ts +50 -0
  80. package/src/projection/util.ts +3 -1
  81. package/src/projects/projects.test.ts +13 -4
  82. package/src/projects/projects.ts +6 -1
  83. package/src/query/queryStore.test.ts +10 -47
  84. package/src/query/queryStore.ts +151 -133
  85. package/src/query/queryStoreConstants.ts +2 -0
  86. package/src/store/createActionBinder.test.ts +153 -0
  87. package/src/store/createActionBinder.ts +176 -0
  88. package/src/store/createSanityInstance.test.ts +84 -0
  89. package/src/store/createSanityInstance.ts +124 -0
  90. package/src/store/createStateSourceAction.test.ts +196 -0
  91. package/src/store/createStateSourceAction.ts +260 -0
  92. package/src/store/createStoreInstance.test.ts +81 -0
  93. package/src/store/createStoreInstance.ts +80 -0
  94. package/src/store/createStoreState.test.ts +85 -0
  95. package/src/store/createStoreState.ts +92 -0
  96. package/src/store/defineStore.test.ts +18 -0
  97. package/src/store/defineStore.ts +81 -0
  98. package/src/users/reducers.test.ts +318 -0
  99. package/src/users/reducers.ts +88 -0
  100. package/src/users/types.ts +46 -4
  101. package/src/users/usersConstants.ts +4 -0
  102. package/src/users/usersStore.test.ts +350 -223
  103. package/src/users/usersStore.ts +285 -149
  104. package/src/utils/createFetcherStore.test.ts +6 -7
  105. package/src/utils/createFetcherStore.ts +150 -153
  106. package/src/utils/createGroqSearchFilter.test.ts +75 -0
  107. package/src/utils/createGroqSearchFilter.ts +85 -0
  108. package/src/{common/util.test.ts → utils/hashString.test.ts} +1 -1
  109. package/dist/index.cjs +0 -4888
  110. package/dist/index.cjs.map +0 -1
  111. package/dist/index.d.cts +0 -2121
  112. package/src/auth/fetchLoginUrls.test.ts +0 -163
  113. package/src/auth/fetchLoginUrls.ts +0 -74
  114. package/src/common/createLiveEventSubscriber.test.ts +0 -121
  115. package/src/common/createLiveEventSubscriber.ts +0 -55
  116. package/src/common/types.ts +0 -4
  117. package/src/instance/identity.test.ts +0 -46
  118. package/src/instance/identity.ts +0 -29
  119. package/src/instance/sanityInstance.test.ts +0 -77
  120. package/src/instance/sanityInstance.ts +0 -57
  121. package/src/instance/types.ts +0 -37
  122. package/src/preview/getPreviewProjection.ts +0 -45
  123. package/src/resources/README.md +0 -370
  124. package/src/resources/createAction.test.ts +0 -101
  125. package/src/resources/createAction.ts +0 -44
  126. package/src/resources/createResource.test.ts +0 -112
  127. package/src/resources/createResource.ts +0 -102
  128. package/src/resources/createStateSourceAction.test.ts +0 -114
  129. package/src/resources/createStateSourceAction.ts +0 -83
  130. package/src/resources/createStore.test.ts +0 -67
  131. package/src/resources/createStore.ts +0 -46
  132. package/src/store/createStore.test.ts +0 -108
  133. package/src/store/createStore.ts +0 -106
  134. /package/src/{common/util.ts → utils/hashString.ts} +0 -0
@@ -1,4 +1,4 @@
1
- import {createAction} from '../resources/createAction'
1
+ import {bindActionGlobally} from '../store/createActionBinder'
2
2
  import {DEFAULT_API_VERSION, REQUEST_TAG_PREFIX} from './authConstants'
3
3
  import {AuthStateType} from './authStateType'
4
4
  import {authStore} from './authStore'
@@ -6,40 +6,38 @@ import {authStore} from './authStore'
6
6
  /**
7
7
  * @public
8
8
  */
9
- export const logout = createAction(authStore, ({state}) => {
9
+ export const logout = bindActionGlobally(authStore, async ({state}) => {
10
10
  const {clientFactory, apiHost, providedToken, storageArea, storageKey} = state.get().options
11
11
 
12
- return async function () {
13
- // If a token is statically provided, logout does nothing
14
- if (providedToken) return
12
+ // If a token is statically provided, logout does nothing
13
+ if (providedToken) return
15
14
 
16
- const {authState} = state.get()
15
+ const {authState} = state.get()
17
16
 
18
- // If we already have an inflight request, no-op
19
- if (authState.type === AuthStateType.LOGGED_OUT && authState.isDestroyingSession) return
20
- const token = authState.type === AuthStateType.LOGGED_IN && authState.token
17
+ // If we already have an inflight request, no-op
18
+ if (authState.type === AuthStateType.LOGGED_OUT && authState.isDestroyingSession) return
19
+ const token = authState.type === AuthStateType.LOGGED_IN && authState.token
21
20
 
22
- try {
23
- if (token) {
24
- state.set('loggingOut', {
25
- authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: true},
26
- })
27
-
28
- const client = clientFactory({
29
- token,
30
- requestTagPrefix: REQUEST_TAG_PREFIX,
31
- apiVersion: DEFAULT_API_VERSION,
32
- ...(apiHost && {apiHost}),
33
- useProjectHostname: false,
34
- })
21
+ try {
22
+ if (token) {
23
+ state.set('loggingOut', {
24
+ authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: true},
25
+ })
35
26
 
36
- await client.request<void>({uri: '/auth/logout', method: 'POST'})
37
- }
38
- } finally {
39
- state.set('logoutSuccess', {
40
- authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: false},
27
+ const client = clientFactory({
28
+ token,
29
+ requestTagPrefix: REQUEST_TAG_PREFIX,
30
+ apiVersion: DEFAULT_API_VERSION,
31
+ ...(apiHost && {apiHost}),
32
+ useProjectHostname: false,
41
33
  })
42
- storageArea?.removeItem(storageKey)
34
+
35
+ await client.request<void>({uri: '/auth/logout', method: 'POST'})
43
36
  }
37
+ } finally {
38
+ state.set('logoutSuccess', {
39
+ authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: false},
40
+ })
41
+ storageArea?.removeItem(storageKey)
44
42
  }
45
43
  })
@@ -1,74 +1,191 @@
1
1
  import {of, Subscription, throwError} from 'rxjs'
2
2
  import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
3
3
 
4
- import {createSanityInstance} from '../instance/sanityInstance'
5
- import {createResourceState} from '../resources/createResource'
4
+ import {createSanityInstance} from '../store/createSanityInstance'
5
+ import {createStoreState} from '../store/createStoreState'
6
6
  import {AuthStateType} from './authStateType'
7
7
  import {type AuthState, authStore} from './authStore'
8
8
  import {refreshStampedToken} from './refreshStampedToken'
9
9
 
10
+ type LockGrantedCallback = (lock: Lock | null) => Promise<boolean>
11
+
12
+ interface Lock {
13
+ name: string
14
+ mode: 'exclusive' | 'shared'
15
+ }
16
+
17
+ interface LockOptions {
18
+ mode: 'exclusive' | 'shared'
19
+ }
20
+
10
21
  describe('refreshStampedToken', () => {
22
+ let mockStorage: Storage
23
+ let originalNavigator: typeof navigator
24
+ let subscriptions: Subscription[]
25
+
11
26
  beforeEach(() => {
12
- // Clear mocks between tests
27
+ subscriptions = []
13
28
  vi.clearAllMocks()
14
- // Use fake timers so we can simulate interval triggers instantly
15
- vi.useFakeTimers()
29
+ originalNavigator = global.navigator
30
+ mockStorage = {
31
+ getItem: vi.fn(),
32
+ setItem: vi.fn(),
33
+ removeItem: vi.fn(),
34
+ clear: vi.fn(),
35
+ key: vi.fn(),
36
+ length: 0,
37
+ }
38
+ const mockLocks = {
39
+ request: vi.fn(
40
+ async (
41
+ _name: string,
42
+ _options: LockOptions | LockGrantedCallback,
43
+ callback?: LockGrantedCallback,
44
+ ) => {
45
+ const actualCallback = typeof _options === 'function' ? _options : callback
46
+ if (!actualCallback) return false
47
+ const mockLock: Lock = {name: 'mock-lock', mode: 'exclusive'}
48
+ try {
49
+ await new Promise((resolve) => setTimeout(resolve, 0))
50
+ await actualCallback(mockLock)
51
+ return true
52
+ } catch {
53
+ return false
54
+ }
55
+ },
56
+ ),
57
+ query: vi.fn(async () => ({held: [], pending: []})),
58
+ }
59
+ Object.defineProperty(global, 'navigator', {
60
+ value: {locks: mockLocks},
61
+ writable: true,
62
+ })
16
63
  })
17
64
 
18
- afterEach(() => {
19
- // Restore real timers
20
- vi.useRealTimers()
65
+ afterEach(async () => {
66
+ subscriptions.forEach((sub) => sub.unsubscribe())
67
+ Object.defineProperty(global, 'navigator', {
68
+ value: originalNavigator,
69
+ writable: true,
70
+ })
71
+ await new Promise((resolve) => setImmediate(resolve))
21
72
  })
22
73
 
23
- it('refreshes the token on interval (success scenario)', () => {
24
- // Mock client so that .request(...) yields a new token
25
- const mockClient = {
26
- observable: {
27
- request: vi.fn(() => of({token: 'sk-refreshed-token-st123'})),
28
- },
29
- }
30
- const mockClientFactory = vi.fn().mockReturnValue(mockClient)
74
+ describe('dashboard context', () => {
75
+ it('refreshes the token immediately without using locks', async () => {
76
+ const mockClient = {
77
+ observable: {
78
+ request: vi.fn(() => of({token: 'sk-refreshed-token-st123'})),
79
+ },
80
+ }
81
+ const mockClientFactory = vi.fn().mockReturnValue(mockClient)
82
+ const instance = createSanityInstance({
83
+ projectId: 'p',
84
+ dataset: 'd',
85
+ auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
86
+ })
87
+ const initialState = authStore.getInitialState(instance)
88
+ initialState.authState = {
89
+ type: AuthStateType.LOGGED_IN,
90
+ token: 'sk-initial-token-st123',
91
+ currentUser: null,
92
+ }
93
+ initialState.dashboardContext = {mode: 'test'}
94
+ const state = createStoreState(initialState)
95
+ const subscription = refreshStampedToken({state, instance})
96
+ subscriptions.push(subscription)
31
97
 
32
- // Create an instance (similar to how you do in subscribeToStateAndFetchCurrentUser.test.ts)
33
- const instance = createSanityInstance({
34
- projectId: 'p',
35
- dataset: 'd',
36
- auth: {clientFactory: mockClientFactory},
98
+ // Wait briefly for state to settle
99
+ await new Promise((resolve) => setTimeout(resolve, 10))
100
+
101
+ // Check final state and essential negative assertions
102
+ const finalAuthStateDash = state.get().authState
103
+ expect(finalAuthStateDash.type).toBe(AuthStateType.LOGGED_IN)
104
+ if (finalAuthStateDash.type === AuthStateType.LOGGED_IN) {
105
+ expect(finalAuthStateDash.token).toBe('sk-refreshed-token-st123')
106
+ }
107
+ expect(navigator.locks.request).not.toHaveBeenCalled()
37
108
  })
109
+ })
38
110
 
39
- // Create the initial state: logged in with some initial token
40
- const initialState = authStore.getInitialState(instance)
41
- // Force it to be logged in from the start
42
- initialState.authState = {
43
- type: AuthStateType.LOGGED_IN,
44
- token: 'sk-initial-token-st123',
45
- currentUser: null,
46
- }
111
+ describe('non-dashboard context', () => {
112
+ it('uses Web Locks API to coordinate token refresh', async () => {
113
+ const mockClient = {
114
+ observable: {
115
+ request: vi.fn(() => of({token: 'sk-refreshed-token-st123'})),
116
+ },
117
+ }
118
+ const mockClientFactory = vi.fn().mockReturnValue(mockClient)
119
+ const instance = createSanityInstance({
120
+ projectId: 'p',
121
+ dataset: 'd',
122
+ auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
123
+ })
124
+ const initialState = authStore.getInitialState(instance)
125
+ initialState.authState = {
126
+ type: AuthStateType.LOGGED_IN,
127
+ token: 'sk-initial-token-st123',
128
+ currentUser: null,
129
+ }
130
+ const state = createStoreState(initialState)
131
+ const subscription = refreshStampedToken({state, instance})
132
+ subscriptions.push(subscription)
47
133
 
48
- // Create the resource state
49
- const state = createResourceState(initialState)
134
+ // Wait briefly for state to settle
135
+ await new Promise((resolve) => setTimeout(resolve, 10))
50
136
 
51
- // Call refreshStampedToken ONCE
52
- // Because TParams is [], createInternalAction will call the inner function for us
53
- const subscription: Subscription = refreshStampedToken({state, instance})
137
+ // Check final state and that lock was attempted
138
+ expect(navigator.locks.request).toHaveBeenCalled()
139
+ const finalAuthStateNonDash = state.get().authState
140
+ expect(finalAuthStateNonDash.type).toBe(AuthStateType.LOGGED_IN)
141
+ if (finalAuthStateNonDash.type === AuthStateType.LOGGED_IN) {
142
+ expect(finalAuthStateNonDash.token).toBe('sk-refreshed-token-st123')
143
+ }
144
+ })
54
145
 
55
- // Advance time by 10 minutes so the interval fires
56
- vi.advanceTimersByTime(10 * 60 * 1000)
146
+ it('skips refresh if lock cannot be acquired', async () => {
147
+ const mockLocks = {
148
+ request: vi.fn(async () => {
149
+ await new Promise((resolve) => setTimeout(resolve, 0))
150
+ return false
151
+ }),
152
+ query: vi.fn(async () => ({held: [], pending: []})),
153
+ }
154
+ Object.defineProperty(global, 'navigator', {value: {locks: mockLocks}, writable: true})
155
+ const mockClient = {
156
+ observable: {request: vi.fn(() => of({token: 'sk-refreshed-token-st123'}))},
157
+ }
158
+ const mockClientFactory = vi.fn().mockReturnValue(mockClient)
159
+ const instance = createSanityInstance({
160
+ projectId: 'p',
161
+ dataset: 'd',
162
+ auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
163
+ })
164
+ const initialState = authStore.getInitialState(instance)
165
+ initialState.authState = {
166
+ type: AuthStateType.LOGGED_IN,
167
+ token: 'sk-initial-token-st123',
168
+ currentUser: null,
169
+ }
170
+ const state = createStoreState(initialState)
171
+ const subscription = refreshStampedToken({state, instance})
172
+ subscriptions.push(subscription)
57
173
 
58
- // The client request should have been called once
59
- expect(mockClient.observable.request).toHaveBeenCalledTimes(1)
60
- // The token in state should now be 'refreshed-token'
61
- expect(state.get().authState).toEqual({
62
- type: AuthStateType.LOGGED_IN,
63
- token: 'sk-refreshed-token-st123',
64
- currentUser: null,
65
- })
174
+ // Wait briefly
175
+ await new Promise((resolve) => setTimeout(resolve, 10))
66
176
 
67
- // Cleanup
68
- subscription.unsubscribe()
177
+ // Check lock was attempted, client request was NOT, and state is unchanged
178
+ expect(navigator.locks.request).toHaveBeenCalledTimes(1)
179
+ expect(mockClient.observable.request).not.toHaveBeenCalled()
180
+ expect(state.get().authState).toEqual({
181
+ type: AuthStateType.LOGGED_IN,
182
+ token: 'sk-initial-token-st123',
183
+ currentUser: null,
184
+ })
185
+ })
69
186
  })
70
187
 
71
- it('sets an error state when token refresh fails', () => {
188
+ it('sets an error state when token refresh fails', async () => {
72
189
  const error = new Error('Refresh failed')
73
190
  const mockClient = {
74
191
  observable: {
@@ -79,99 +196,88 @@ describe('refreshStampedToken', () => {
79
196
  const instance = createSanityInstance({
80
197
  projectId: 'p',
81
198
  dataset: 'd',
82
- auth: {clientFactory: mockClientFactory},
199
+ auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
83
200
  })
84
-
85
- // Start logged-in
86
201
  const initialState = authStore.getInitialState(instance)
87
202
  initialState.authState = {
88
203
  type: AuthStateType.LOGGED_IN,
89
204
  token: 'sk-initial-token-st123',
90
205
  currentUser: null,
91
206
  }
92
- const state = createResourceState(initialState)
93
-
94
- const subscription: Subscription = refreshStampedToken({state, instance})
207
+ initialState.dashboardContext = {mode: 'test'}
208
+ const state = createStoreState(initialState)
209
+ const subscription = refreshStampedToken({state, instance})
210
+ subscriptions.push(subscription)
95
211
 
96
- // Move time forward to trigger the interval
97
- vi.advanceTimersByTime(10 * 60 * 1000)
98
-
99
- // Should have set an error state
100
- expect(state.get().authState).toEqual({
101
- type: AuthStateType.ERROR,
102
- error,
103
- })
212
+ // Wait briefly for state to settle
213
+ await new Promise((resolve) => setTimeout(resolve, 10))
104
214
 
105
- subscription.unsubscribe()
215
+ // Check final state IS error
216
+ const finalAuthStateError = state.get().authState
217
+ expect(finalAuthStateError.type).toBe(AuthStateType.ERROR)
218
+ if (finalAuthStateError.type === AuthStateType.ERROR) {
219
+ expect(finalAuthStateError.error).toBe(error)
220
+ } else {
221
+ expect.fail('Expected authState type to be ERROR')
222
+ }
106
223
  })
107
224
 
108
- it('does nothing if user is not logged in', () => {
225
+ it('does nothing if user is not logged in', async () => {
109
226
  const mockClientFactory = vi.fn()
110
227
  const instance = createSanityInstance({
111
228
  projectId: 'p',
112
229
  dataset: 'd',
113
230
  auth: {clientFactory: mockClientFactory},
114
231
  })
115
-
116
- // Start logged out
117
232
  const initialState = authStore.getInitialState(instance)
118
233
  initialState.authState = {
119
234
  type: AuthStateType.LOGGED_OUT,
120
235
  isDestroyingSession: false,
121
236
  } as AuthState
122
- const state = createResourceState(initialState)
123
-
124
- const subscription: Subscription = refreshStampedToken({state, instance})
237
+ const state = createStoreState(initialState)
238
+ const subscription = refreshStampedToken({state, instance})
239
+ subscriptions.push(subscription)
125
240
 
126
- // Move time forward
127
- vi.advanceTimersByTime(10 * 60 * 1000)
241
+ // Wait briefly
242
+ await new Promise((resolve) => setTimeout(resolve, 10))
128
243
 
129
- // No calls, no changes
244
+ // Check nothing happened
130
245
  expect(mockClientFactory).not.toHaveBeenCalled()
246
+ expect(navigator.locks.request).not.toHaveBeenCalled()
131
247
  expect(state.get().authState).toEqual({
132
248
  type: AuthStateType.LOGGED_OUT,
133
249
  isDestroyingSession: false,
134
250
  })
135
-
136
- subscription.unsubscribe()
137
251
  })
138
252
 
139
- it('does nothing if token is not stamped', () => {
140
- const mockClient = {
141
- observable: {
142
- request: vi.fn(() => of({token: 'sk-nonstamped-token2'})),
143
- },
144
- }
253
+ it('does nothing if token is not stamped', async () => {
254
+ const mockClient = {observable: {request: vi.fn(() => of({token: 'sk-nonstamped-token2'}))}}
145
255
  const mockClientFactory = vi.fn().mockReturnValue(mockClient)
146
256
  const instance = createSanityInstance({
147
257
  projectId: 'p',
148
258
  dataset: 'd',
149
259
  auth: {clientFactory: mockClientFactory},
150
260
  })
151
-
152
- // Start logged in with a non-stamped token
153
261
  const initialState = authStore.getInitialState(instance)
154
262
  initialState.authState = {
155
263
  type: AuthStateType.LOGGED_IN,
156
- token: 'sk-nonstamped-token2', // Note: doesn't include '-st'
264
+ token: 'sk-nonstamped-token2',
157
265
  currentUser: null,
158
266
  }
159
- const state = createResourceState(initialState)
160
-
161
- const subscription: Subscription = refreshStampedToken({state, instance})
267
+ const state = createStoreState(initialState)
268
+ const subscription = refreshStampedToken({state, instance})
269
+ subscriptions.push(subscription)
162
270
 
163
- // Move time forward
164
- vi.advanceTimersByTime(10 * 60 * 1000)
271
+ // Wait briefly
272
+ await new Promise((resolve) => setTimeout(resolve, 10))
165
273
 
166
- // No refresh should have happened
274
+ // Check nothing happened
167
275
  expect(mockClient.observable.request).not.toHaveBeenCalled()
168
- // State should remain unchanged
276
+ expect(navigator.locks.request).not.toHaveBeenCalled()
169
277
  expect(state.get().authState).toEqual({
170
278
  type: AuthStateType.LOGGED_IN,
171
279
  token: 'sk-nonstamped-token2',
172
280
  currentUser: null,
173
281
  })
174
-
175
- subscription.unsubscribe()
176
282
  })
177
283
  })