File size: 4,152 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
 * @jest-environment jsdom
 */
// @ts-nocheck - TODO: Fix TypeScript issues

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';
import nock from 'nock';
import React from 'react';
import { useRequestTransferWithSoftware } from '../use-transfer-with-software-start-mutation';

const SITE_ID = 123;
const API_SETTINGS = { migration_source_site_domain: 'example.com' };

const Wrapper =
	( queryClient: QueryClient ) =>
	( { children } ) => (
		<QueryClientProvider client={ queryClient }>{ children }</QueryClientProvider>
	);

const render = ( plugin_slug?: string, theme_slug?: string ) => {
	const queryClient = new QueryClient();

	const renderResult = renderHook(
		() =>
			useRequestTransferWithSoftware(
				{
					siteId: SITE_ID,
					apiSettings: API_SETTINGS,
					plugin_slug: plugin_slug,
					theme_slug: theme_slug,
				},
				{ retry: 0 }
			),
		{
			wrapper: Wrapper( queryClient ),
		}
	);

	return {
		...renderResult,
		queryClient,
	};
};

describe( 'useRequestTransferWithSoftware', () => {
	beforeAll( () => {
		nock.disableNetConnect();
	} );

	beforeEach( () => nock.cleanAll() );

	it( 'should successfully request transfer with software and return the transfer_id', async () => {
		nock( 'https://public-api.wordpress.com' )
			.post( '/wpcom/v2/sites/' + SITE_ID + '/atomic/transfer-with-software', {
				plugin_slug: 'plugin-1',
				theme_slug: 'theme-1',
				settings: API_SETTINGS,
			} )
			.query( {
				http_envelope: 1,
			} )
			.reply( 200, {
				transfer_id: 456,
				blog_id: SITE_ID,
				transfer_status: 'pending',
			} );

		const { result } = render( 'plugin-1', 'theme-1' );

		result.current.mutate();

		await waitFor(
			() => {
				expect( result.current.isSuccess ).toBe( true );
				expect( result.current.data ).toEqual( {
					transfer_id: 456,
					blog_id: SITE_ID,
					transfer_status: 'pending',
				} );
			},
			{ timeout: 3000 }
		);
	} );

	it( 'should return an error if both plugin_slug and theme_slug are not provided', async () => {
		nock( 'https://public-api.wordpress.com' )
			.post( '/wpcom/v2/sites/' + SITE_ID + '/atomic/transfer-with-software', {
				plugin_slug: undefined,
				theme_slug: undefined,
				settings: API_SETTINGS,
			} )
			.query( { http_envelope: 1 } )
			.reply( 400, { message: 'plugin_slug and theme_slug are required' } );

		const { result } = render();

		result.current.mutate();

		await waitFor(
			() => {
				expect( result.current.isError ).toBe( true );
				expect( result.current.error?.message ).toBe( 'plugin_slug and theme_slug are required' );
			},
			{ timeout: 3000 }
		);
	} );

	it( 'should successfully request the transfer with a plugin slug', async () => {
		nock( 'https://public-api.wordpress.com' )
			.post( '/wpcom/v2/sites/' + SITE_ID + '/atomic/transfer-with-software', {
				plugin_slug: 'plugin-1',
				settings: API_SETTINGS,
			} )
			.query( { http_envelope: 1 } )
			.reply( 200, {
				transfer_id: 456,
				blog_id: SITE_ID,
				transfer_status: 'pending',
			} );

		const { result } = render( 'plugin-1' );

		result.current.mutate();

		await waitFor(
			() => {
				expect( result.current.isSuccess ).toBe( true );
				expect( result.current.data ).toEqual( {
					transfer_id: 456,
					blog_id: SITE_ID,
					transfer_status: 'pending',
				} );
			},
			{ timeout: 3000 }
		);
	} );

	it( 'should successfully request the transfer with a theme slug', async () => {
		nock( 'https://public-api.wordpress.com' )
			.post( '/wpcom/v2/sites/' + SITE_ID + '/atomic/transfer-with-software', {
				theme_slug: 'theme-1',
				settings: API_SETTINGS,
			} )
			.query( { http_envelope: 1 } )
			.reply( 200, {
				transfer_id: 456,
				blog_id: SITE_ID,
				transfer_status: 'pending',
			} );

		const { result } = render( undefined, 'theme-1' );

		result.current.mutate();

		await waitFor(
			() => {
				expect( result.current.isSuccess ).toBe( true );
				expect( result.current.data ).toEqual( {
					transfer_id: 456,
					blog_id: SITE_ID,
					transfer_status: 'pending',
				} );
			},
			{ timeout: 3000 }
		);
	} );
} );