@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.
- package/dist/index.d.ts +441 -322
- package/dist/index.js +1685 -1481
- package/dist/index.js.map +1 -1
- package/package.json +13 -15
- package/src/_exports/index.ts +32 -30
- package/src/auth/authStore.test.ts +149 -104
- package/src/auth/authStore.ts +51 -100
- package/src/auth/handleAuthCallback.test.ts +67 -34
- package/src/auth/handleAuthCallback.ts +8 -7
- package/src/auth/logout.test.ts +61 -29
- package/src/auth/logout.ts +26 -28
- package/src/auth/refreshStampedToken.test.ts +197 -91
- package/src/auth/refreshStampedToken.ts +170 -59
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +5 -5
- package/src/auth/subscribeToStateAndFetchCurrentUser.ts +45 -47
- package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -5
- package/src/auth/subscribeToStorageEventsAndSetToken.ts +22 -24
- package/src/client/clientStore.test.ts +131 -67
- package/src/client/clientStore.ts +117 -116
- package/src/comlink/controller/actions/destroyController.test.ts +38 -13
- package/src/comlink/controller/actions/destroyController.ts +11 -15
- package/src/comlink/controller/actions/getOrCreateChannel.test.ts +56 -27
- package/src/comlink/controller/actions/getOrCreateChannel.ts +37 -35
- package/src/comlink/controller/actions/getOrCreateController.test.ts +27 -16
- package/src/comlink/controller/actions/getOrCreateController.ts +23 -22
- package/src/comlink/controller/actions/releaseChannel.test.ts +37 -13
- package/src/comlink/controller/actions/releaseChannel.ts +22 -21
- package/src/comlink/controller/comlinkControllerStore.test.ts +65 -36
- package/src/comlink/controller/comlinkControllerStore.ts +44 -5
- package/src/comlink/node/actions/getOrCreateNode.test.ts +31 -15
- package/src/comlink/node/actions/getOrCreateNode.ts +30 -29
- package/src/comlink/node/actions/releaseNode.test.ts +75 -55
- package/src/comlink/node/actions/releaseNode.ts +19 -21
- package/src/comlink/node/comlinkNodeStore.test.ts +6 -11
- package/src/comlink/node/comlinkNodeStore.ts +22 -5
- package/src/config/authConfig.ts +79 -0
- package/src/config/sanityConfig.ts +48 -0
- package/src/datasets/datasets.test.ts +2 -2
- package/src/datasets/datasets.ts +18 -5
- package/src/document/actions.test.ts +22 -10
- package/src/document/actions.ts +44 -56
- package/src/document/applyDocumentActions.test.ts +96 -36
- package/src/document/applyDocumentActions.ts +140 -99
- package/src/document/documentStore.test.ts +103 -155
- package/src/document/documentStore.ts +247 -238
- package/src/document/listen.ts +56 -55
- package/src/document/patchOperations.ts +0 -43
- package/src/document/permissions.test.ts +25 -12
- package/src/document/permissions.ts +11 -4
- package/src/document/processActions.test.ts +41 -8
- package/src/document/reducers.test.ts +87 -16
- package/src/document/reducers.ts +2 -2
- package/src/document/sharedListener.test.ts +34 -16
- package/src/document/sharedListener.ts +33 -11
- package/src/preview/getPreviewState.test.ts +40 -39
- package/src/preview/getPreviewState.ts +68 -56
- package/src/preview/previewConstants.ts +43 -0
- package/src/preview/previewQuery.test.ts +1 -1
- package/src/preview/previewQuery.ts +4 -5
- package/src/preview/previewStore.test.ts +13 -58
- package/src/preview/previewStore.ts +7 -21
- package/src/preview/resolvePreview.test.ts +33 -104
- package/src/preview/resolvePreview.ts +11 -21
- package/src/preview/subscribeToStateAndFetchBatches.test.ts +96 -97
- package/src/preview/subscribeToStateAndFetchBatches.ts +85 -81
- package/src/preview/util.ts +1 -0
- package/src/project/project.test.ts +3 -3
- package/src/project/project.ts +28 -5
- package/src/projection/getProjectionState.test.ts +188 -72
- package/src/projection/getProjectionState.ts +92 -62
- package/src/projection/projectionQuery.test.ts +114 -12
- package/src/projection/projectionQuery.ts +75 -32
- package/src/projection/projectionStore.test.ts +13 -51
- package/src/projection/projectionStore.ts +6 -43
- package/src/projection/resolveProjection.test.ts +32 -127
- package/src/projection/resolveProjection.ts +16 -28
- package/src/projection/subscribeToStateAndFetchBatches.test.ts +203 -116
- package/src/projection/subscribeToStateAndFetchBatches.ts +140 -85
- package/src/projection/types.ts +50 -0
- package/src/projection/util.ts +3 -1
- package/src/projects/projects.test.ts +13 -4
- package/src/projects/projects.ts +6 -1
- package/src/query/queryStore.test.ts +10 -47
- package/src/query/queryStore.ts +151 -133
- package/src/query/queryStoreConstants.ts +2 -0
- package/src/store/createActionBinder.test.ts +153 -0
- package/src/store/createActionBinder.ts +176 -0
- package/src/store/createSanityInstance.test.ts +84 -0
- package/src/store/createSanityInstance.ts +124 -0
- package/src/store/createStateSourceAction.test.ts +196 -0
- package/src/store/createStateSourceAction.ts +260 -0
- package/src/store/createStoreInstance.test.ts +81 -0
- package/src/store/createStoreInstance.ts +80 -0
- package/src/store/createStoreState.test.ts +85 -0
- package/src/store/createStoreState.ts +92 -0
- package/src/store/defineStore.test.ts +18 -0
- package/src/store/defineStore.ts +81 -0
- package/src/users/reducers.test.ts +318 -0
- package/src/users/reducers.ts +88 -0
- package/src/users/types.ts +46 -4
- package/src/users/usersConstants.ts +4 -0
- package/src/users/usersStore.test.ts +350 -223
- package/src/users/usersStore.ts +285 -149
- package/src/utils/createFetcherStore.test.ts +6 -7
- package/src/utils/createFetcherStore.ts +150 -153
- package/src/utils/createGroqSearchFilter.test.ts +75 -0
- package/src/utils/createGroqSearchFilter.ts +85 -0
- package/src/{common/util.test.ts → utils/hashString.test.ts} +1 -1
- package/dist/index.cjs +0 -4888
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -2121
- package/src/auth/fetchLoginUrls.test.ts +0 -163
- package/src/auth/fetchLoginUrls.ts +0 -74
- package/src/common/createLiveEventSubscriber.test.ts +0 -121
- package/src/common/createLiveEventSubscriber.ts +0 -55
- package/src/common/types.ts +0 -4
- package/src/instance/identity.test.ts +0 -46
- package/src/instance/identity.ts +0 -29
- package/src/instance/sanityInstance.test.ts +0 -77
- package/src/instance/sanityInstance.ts +0 -57
- package/src/instance/types.ts +0 -37
- package/src/preview/getPreviewProjection.ts +0 -45
- package/src/resources/README.md +0 -370
- package/src/resources/createAction.test.ts +0 -101
- package/src/resources/createAction.ts +0 -44
- package/src/resources/createResource.test.ts +0 -112
- package/src/resources/createResource.ts +0 -102
- package/src/resources/createStateSourceAction.test.ts +0 -114
- package/src/resources/createStateSourceAction.ts +0 -83
- package/src/resources/createStore.test.ts +0 -67
- package/src/resources/createStore.ts +0 -46
- package/src/store/createStore.test.ts +0 -108
- package/src/store/createStore.ts +0 -106
- /package/src/{common/util.ts → utils/hashString.ts} +0 -0
package/src/auth/logout.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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 =
|
|
9
|
+
export const logout = bindActionGlobally(authStore, async ({state}) => {
|
|
10
10
|
const {clientFactory, apiHost, providedToken, storageArea, storageKey} = state.get().options
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (providedToken) return
|
|
12
|
+
// If a token is statically provided, logout does nothing
|
|
13
|
+
if (providedToken) return
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
const {authState} = state.get()
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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 '../
|
|
5
|
-
import {
|
|
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
|
-
|
|
27
|
+
subscriptions = []
|
|
13
28
|
vi.clearAllMocks()
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
20
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
49
|
-
|
|
134
|
+
// Wait briefly for state to settle
|
|
135
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
50
136
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
56
|
-
|
|
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
|
-
|
|
59
|
-
|
|
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
|
-
|
|
68
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
const subscription
|
|
207
|
+
initialState.dashboardContext = {mode: 'test'}
|
|
208
|
+
const state = createStoreState(initialState)
|
|
209
|
+
const subscription = refreshStampedToken({state, instance})
|
|
210
|
+
subscriptions.push(subscription)
|
|
95
211
|
|
|
96
|
-
//
|
|
97
|
-
|
|
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
|
-
|
|
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 =
|
|
123
|
-
|
|
124
|
-
|
|
237
|
+
const state = createStoreState(initialState)
|
|
238
|
+
const subscription = refreshStampedToken({state, instance})
|
|
239
|
+
subscriptions.push(subscription)
|
|
125
240
|
|
|
126
|
-
//
|
|
127
|
-
|
|
241
|
+
// Wait briefly
|
|
242
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
128
243
|
|
|
129
|
-
//
|
|
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',
|
|
264
|
+
token: 'sk-nonstamped-token2',
|
|
157
265
|
currentUser: null,
|
|
158
266
|
}
|
|
159
|
-
const state =
|
|
160
|
-
|
|
161
|
-
|
|
267
|
+
const state = createStoreState(initialState)
|
|
268
|
+
const subscription = refreshStampedToken({state, instance})
|
|
269
|
+
subscriptions.push(subscription)
|
|
162
270
|
|
|
163
|
-
//
|
|
164
|
-
|
|
271
|
+
// Wait briefly
|
|
272
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
165
273
|
|
|
166
|
-
//
|
|
274
|
+
// Check nothing happened
|
|
167
275
|
expect(mockClient.observable.request).not.toHaveBeenCalled()
|
|
168
|
-
|
|
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
|
})
|