@sanity/sdk-react 0.0.0-alpha.4 → 0.0.0-alpha.5
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/README.md +79 -26
- package/dist/components.d.ts +1 -0
- package/dist/components.js +47 -24
- package/dist/components.js.map +1 -1
- package/dist/hooks.d.ts +111 -16
- package/dist/hooks.js +4 -7
- package/dist/hooks.js.map +1 -1
- package/package.json +5 -4
- package/src/_exports/hooks.ts +6 -2
- package/src/components/auth/AuthBoundary.test.tsx +4 -10
- package/src/components/auth/Login.test.tsx +3 -10
- package/src/components/auth/Login.tsx +27 -11
- package/src/components/auth/LoginCallback.test.tsx +2 -9
- package/src/components/auth/LoginCallback.tsx +7 -4
- package/src/components/auth/LoginError.test.tsx +2 -10
- package/src/components/auth/LoginError.tsx +8 -12
- package/src/components/auth/LoginFooter.test.tsx +2 -9
- package/src/components/auth/LoginFooter.tsx +20 -13
- package/src/components/auth/LoginLayout.test.tsx +2 -9
- package/src/components/auth/LoginLayout.tsx +9 -8
- package/src/components/auth/authTestHelpers.tsx +18 -0
- package/src/hooks/documentCollection/useDocuments.ts +53 -6
- package/src/hooks/preview/usePreview.test.tsx +16 -7
- package/src/hooks/preview/usePreview.tsx +52 -7
- package/src/components/Login/LoginLinks.test.tsx +0 -91
- package/src/components/Login/LoginLinks.tsx +0 -58
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import {AuthStateType, createSanityInstance} from '@sanity/sdk'
|
|
2
|
-
import {SanityProvider} from '@sanity/sdk-react/context'
|
|
3
|
-
import {useAuthState, useLoginUrls} from '@sanity/sdk-react/hooks'
|
|
4
|
-
import {render, screen} from '@testing-library/react'
|
|
5
|
-
import React from 'react'
|
|
6
|
-
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
7
|
-
|
|
8
|
-
import {LoginLinks} from './LoginLinks'
|
|
9
|
-
|
|
10
|
-
// Mock the hooks and SDK functions
|
|
11
|
-
vi.mock('../../hooks/auth/useLoginUrls', () => ({
|
|
12
|
-
useLoginUrls: vi.fn(() => [
|
|
13
|
-
{
|
|
14
|
-
name: 'google',
|
|
15
|
-
title: 'Google',
|
|
16
|
-
url: 'https://google.com/auth',
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
name: 'github',
|
|
20
|
-
title: 'GitHub',
|
|
21
|
-
url: 'https://github.com/auth',
|
|
22
|
-
},
|
|
23
|
-
]),
|
|
24
|
-
}))
|
|
25
|
-
vi.mock('@sanity/sdk', async () => {
|
|
26
|
-
const actual = await vi.importActual('@sanity/sdk')
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
...actual,
|
|
30
|
-
tradeTokenForSession: vi.fn(),
|
|
31
|
-
getSidUrlHash: vi.fn().mockReturnValue(null),
|
|
32
|
-
getSidUrlSearch: vi.fn(),
|
|
33
|
-
}
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
vi.mock('../../hooks/auth/useAuthState', () => ({
|
|
37
|
-
useAuthState: vi.fn(() => 'logged-out'),
|
|
38
|
-
}))
|
|
39
|
-
|
|
40
|
-
vi.mock('../../hooks/auth/useHandleCallback', () => ({
|
|
41
|
-
useHandleCallback: vi.fn(),
|
|
42
|
-
}))
|
|
43
|
-
|
|
44
|
-
describe('LoginLinks', () => {
|
|
45
|
-
beforeEach(() => {
|
|
46
|
-
vi.clearAllMocks()
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
const sanityInstance = createSanityInstance({projectId: 'test-project-id', dataset: 'production'})
|
|
50
|
-
const renderWithWrappers = (ui: React.ReactElement) => {
|
|
51
|
-
return render(<SanityProvider sanityInstance={sanityInstance}>{ui}</SanityProvider>)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
it('renders auth provider links correctly when not authenticated', () => {
|
|
55
|
-
vi.mocked(useAuthState).mockReturnValue({
|
|
56
|
-
type: AuthStateType.LOGGED_OUT,
|
|
57
|
-
isDestroyingSession: false,
|
|
58
|
-
})
|
|
59
|
-
renderWithWrappers(<LoginLinks />)
|
|
60
|
-
|
|
61
|
-
expect(screen.getByText('Choose login provider')).toBeInTheDocument()
|
|
62
|
-
|
|
63
|
-
const authProviders = useLoginUrls()
|
|
64
|
-
authProviders.forEach((provider) => {
|
|
65
|
-
const button = screen.getByRole('link', {name: provider.title})
|
|
66
|
-
expect(button).toBeInTheDocument()
|
|
67
|
-
expect(button).toHaveAttribute('href', provider.url)
|
|
68
|
-
})
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
it('shows loading state while logging in', () => {
|
|
72
|
-
vi.mocked(useAuthState).mockReturnValue({
|
|
73
|
-
type: AuthStateType.LOGGING_IN,
|
|
74
|
-
isExchangingToken: false,
|
|
75
|
-
})
|
|
76
|
-
renderWithWrappers(<LoginLinks />)
|
|
77
|
-
|
|
78
|
-
expect(screen.getByText('Logging in...')).toBeInTheDocument()
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
it('shows success message when logged in', () => {
|
|
82
|
-
vi.mocked(useAuthState).mockReturnValue({
|
|
83
|
-
type: AuthStateType.LOGGED_IN,
|
|
84
|
-
token: 'test-token',
|
|
85
|
-
currentUser: null,
|
|
86
|
-
})
|
|
87
|
-
renderWithWrappers(<LoginLinks />)
|
|
88
|
-
|
|
89
|
-
expect(screen.getByText('You are logged in')).toBeInTheDocument()
|
|
90
|
-
})
|
|
91
|
-
})
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import {type ReactElement} from 'react'
|
|
2
|
-
|
|
3
|
-
import {useAuthState} from '../../hooks/auth/useAuthState'
|
|
4
|
-
import {useHandleCallback} from '../../hooks/auth/useHandleCallback'
|
|
5
|
-
import {useLoginUrls} from '../../hooks/auth/useLoginUrls'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Component that handles Sanity authentication flow and renders login provider options
|
|
9
|
-
*
|
|
10
|
-
* @public
|
|
11
|
-
*
|
|
12
|
-
* @returns Rendered component
|
|
13
|
-
*
|
|
14
|
-
* @remarks
|
|
15
|
-
* The component handles three states:
|
|
16
|
-
* 1. Loading state during token exchange
|
|
17
|
-
* 2. Success state after successful authentication
|
|
18
|
-
* 3. Provider selection UI when not authenticated
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```tsx
|
|
22
|
-
* const config = { projectId: 'your-project-id', dataset: 'production' }
|
|
23
|
-
* return <LoginLinks sanityInstance={config} />
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export const LoginLinks = (): ReactElement => {
|
|
27
|
-
const loginUrls = useLoginUrls()
|
|
28
|
-
const authState = useAuthState()
|
|
29
|
-
useHandleCallback()
|
|
30
|
-
|
|
31
|
-
if (authState.type === 'logging-in') {
|
|
32
|
-
return <div className="sc-login-links__logging-in">Logging in...</div>
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Show success state after authentication
|
|
36
|
-
if (authState.type === 'logged-in') {
|
|
37
|
-
return <div className="sc-login-links__logged-in">You are logged in</div>
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Render provider selection UI
|
|
42
|
-
*/
|
|
43
|
-
return (
|
|
44
|
-
<div className="sc-login-links">
|
|
45
|
-
<h2 className="sc-login-links__title">Choose login provider</h2>
|
|
46
|
-
|
|
47
|
-
<ul className="sc-login-links__list">
|
|
48
|
-
{loginUrls.map((provider, index) => (
|
|
49
|
-
<li key={`${provider.url}_${index}`} className="sc-login-links__item">
|
|
50
|
-
<a href={provider.url} className="sc-login-links__link">
|
|
51
|
-
{provider.title}
|
|
52
|
-
</a>
|
|
53
|
-
</li>
|
|
54
|
-
))}
|
|
55
|
-
</ul>
|
|
56
|
-
</div>
|
|
57
|
-
)
|
|
58
|
-
}
|