|
1 | 1 | import { jest } from '@jest/globals'; |
2 | 2 | import { act, renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import type { GoogleVerifyBootstrap } from '../google-verify-types'; |
3 | 4 |
|
4 | 5 | type Request = { path: string; method?: string }; |
5 | 6 |
|
6 | 7 | const mockApiFetch = jest.fn< ( options: Request ) => Promise< unknown > >(); |
7 | 8 | const saveCode = jest.fn< ( code: string ) => Promise< void > >(); |
| 9 | +const popup = |
| 10 | + jest.fn< ( url: string, callback: ( result: { keyring_id?: number } ) => void ) => void >(); |
8 | 11 |
|
9 | 12 | jest.unstable_mockModule( '@wordpress/api-fetch', () => ( { default: mockApiFetch } ) ); |
10 | 13 | jest.unstable_mockModule( '@automattic/request-external-access', () => ( { |
11 | | - default: ( _url: string, callback: ( result: { keyring_id: number } ) => void ) => |
12 | | - callback( { keyring_id: 7 } ), |
| 14 | + default: popup, |
13 | 15 | } ) ); |
| 16 | +let bootstrap: GoogleVerifyBootstrap; |
14 | 17 | jest.unstable_mockModule( '@automattic/jetpack-script-data', () => ( { |
15 | | - getScriptData: () => ( { |
16 | | - seo: { google_verify: { connect_url: 'https://example.com/connect', is_connected: true } }, |
17 | | - } ), |
| 18 | + getScriptData: () => ( { seo: { google_verify: bootstrap } } ), |
18 | 19 | } ) ); |
19 | 20 | jest.unstable_mockModule( '@wordpress/notices', () => ( { store: 'core/notices' } ) ); |
20 | 21 | const createErrorNotice = jest.fn(); |
@@ -68,6 +69,8 @@ async function mountHook() { |
68 | 69 | describe( 'useGoogleVerify', () => { |
69 | 70 | beforeEach( () => { |
70 | 71 | jest.clearAllMocks(); |
| 72 | + bootstrap = { connect_url: 'https://example.com/connect', is_connected: true }; |
| 73 | + popup.mockImplementation( ( _url, callback ) => callback( { keyring_id: 7 } ) ); |
71 | 74 | saveCode.mockResolvedValue( undefined ); |
72 | 75 | } ); |
73 | 76 |
|
@@ -119,4 +122,67 @@ describe( 'useGoogleVerify', () => { |
119 | 122 | expect( mockApiFetch ).not.toHaveBeenCalledWith( VERIFY_POST ); |
120 | 123 | expect( createErrorNotice ).toHaveBeenCalledWith( 'Nope.', expect.anything() ); |
121 | 124 | } ); |
| 125 | + it( 'uses a native host transport without a Jetpack user connection', async () => { |
| 126 | + bootstrap = { |
| 127 | + connect_url: 'https://example.com/native-connect', |
| 128 | + is_connected: false, |
| 129 | + can_auto_verify: true, |
| 130 | + api_path: '/wpcom/v2/seo/google-verify', |
| 131 | + }; |
| 132 | + respond(); |
| 133 | + const { result } = await mountHook(); |
| 134 | + expect( result.current.canAutoVerify ).toBe( true ); |
| 135 | + act( () => result.current.autoVerify() ); |
| 136 | + await waitFor( () => expect( result.current.isVerifying ).toBe( false ) ); |
| 137 | + expect( |
| 138 | + mockApiFetch.mock.calls.map( ( [ request ] ) => [ request.path, request.method ?? 'GET' ] ) |
| 139 | + ).toEqual( [ |
| 140 | + [ '/wpcom/v2/seo/google-verify', 'GET' ], |
| 141 | + [ '/wpcom/v2/seo/google-verify/7', 'GET' ], |
| 142 | + [ '/wpcom/v2/seo/google-verify', 'POST' ], |
| 143 | + [ '/wpcom/v2/seo/google-verify/7', 'GET' ], |
| 144 | + ] ); |
| 145 | + expect( saveCode ).toHaveBeenCalledWith( 'tok' ); |
| 146 | + expect( popup ).toHaveBeenCalledWith( bootstrap.connect_url, expect.any( Function ) ); |
| 147 | + } ); |
| 148 | + |
| 149 | + it.each( [ |
| 150 | + [ 'disconnected user', { is_connected: false } ], |
| 151 | + [ 'missing popup URL', { connect_url: '' } ], |
| 152 | + [ 'host disables automatic verification', { can_auto_verify: false } ], |
| 153 | + ] )( 'keeps manual entry for %s', async ( _label, overrides ) => { |
| 154 | + bootstrap = { ...bootstrap, ...overrides }; |
| 155 | + const { result } = await mountHook(); |
| 156 | + expect( result.current.canAutoVerify ).toBe( false ); |
| 157 | + expect( result.current.state ).toBe( 'unavailable' ); |
| 158 | + act( () => result.current.autoVerify() ); |
| 159 | + expect( mockApiFetch ).not.toHaveBeenCalled(); |
| 160 | + } ); |
| 161 | + |
| 162 | + it.each( [ 'forbidden', 'rest_forbidden', 'rest_no_route' ] )( |
| 163 | + 'keeps manual entry after %s', |
| 164 | + async code => { |
| 165 | + mockApiFetch.mockRejectedValue( { code } ); |
| 166 | + const { result } = await mountHook(); |
| 167 | + expect( result.current.state ).toBe( 'unavailable' ); |
| 168 | + act( () => result.current.autoVerify() ); |
| 169 | + expect( mockApiFetch ).toHaveBeenCalledTimes( 1 ); |
| 170 | + expect( saveCode ).not.toHaveBeenCalled(); |
| 171 | + expect( popup ).not.toHaveBeenCalled(); |
| 172 | + } |
| 173 | + ); |
| 174 | + |
| 175 | + it( 'allows another attempt after the connection popup is canceled', async () => { |
| 176 | + popup.mockImplementation( ( _url, callback ) => callback( {} ) ); |
| 177 | + respond(); |
| 178 | + const { result } = await mountHook(); |
| 179 | + act( () => result.current.autoVerify() ); |
| 180 | + expect( result.current.isVerifying ).toBe( false ); |
| 181 | + expect( result.current.isConfirming ).toBe( false ); |
| 182 | + expect( saveCode ).not.toHaveBeenCalled(); |
| 183 | + popup.mockImplementation( ( _url, callback ) => callback( { keyring_id: 7 } ) ); |
| 184 | + act( () => result.current.autoVerify() ); |
| 185 | + await waitFor( () => expect( result.current.isVerifying ).toBe( false ) ); |
| 186 | + expect( saveCode ).toHaveBeenCalledWith( 'tok' ); |
| 187 | + } ); |
122 | 188 | } ); |
0 commit comments