File size: 5,612 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * These tests shouldn't require the jsdom environment,
 * but we're waiting for this PR to merge:
 * https://github.com/WordPress/gutenberg/pull/20486
 *
 * @jest-environment jsdom
 */

import { createActions } from '../actions';
import { sites, siteTheme, launchStatus, siteSetupErrors } from '../reducer';
import {
	CurrentTheme,
	SiteLaunchError,
	SiteLaunchState,
	SiteLaunchStatus,
	SiteDetails,
	SiteError,
} from '../types';

describe( 'Site', () => {
	const siteDetailsResponse: SiteDetails = {
		ID: 12345,
		name: 'My test site',
		description: '',
		URL: 'http://mytestsite12345.wordpress.com',
	};

	const siteErrorResponse: SiteError = {
		error: 'unknown_blog',
		message: 'Unknown blog',
	};

	it( 'returns the correct default state', () => {
		const state = sites( undefined, { type: 'TEST_ACTION' } );
		expect( state ).toEqual( {} );
	} );

	it( 'returns site data keyed by id', () => {
		const state = sites( undefined, {
			type: 'RECEIVE_SITE',
			siteId: 12345,
			response: siteDetailsResponse,
		} );
		expect( state ).toEqual( {
			12345: siteDetailsResponse,
		} );
	} );

	it( 'clears data keyed by id, and no other data is affected', () => {
		const originalState = {
			12345: siteDetailsResponse,
			23456: siteDetailsResponse,
		};
		const updatedState = sites( originalState, {
			type: 'RECEIVE_SITE_FAILED',
			siteId: 23456,
			response: siteErrorResponse,
		} );

		expect( updatedState ).toEqual( {
			12345: siteDetailsResponse,
		} );
	} );

	describe( 'Launch Status', () => {
		type ClientCredentials = { client_id: string; client_secret: string };

		let siteId: number;
		let client_id: string;
		let client_secret: string;
		let mockedClientCredentials: ClientCredentials;
		let originalState: { [ key: number ]: SiteLaunchState };

		beforeEach( () => {
			siteId = 12345;
			client_id = 'magic_client_id';
			client_secret = 'magic_client_secret';
			mockedClientCredentials = { client_id, client_secret };
			originalState = {
				[ siteId ]: { status: SiteLaunchStatus.UNINITIALIZED, errorCode: undefined },
			};
		} );

		it( 'should default to the initial state when an unknown action is dispatched', () => {
			const state = launchStatus( undefined, { type: 'TEST_ACTION' } );
			expect( state ).toStrictEqual( {} );
		} );

		it( 'should set the status to SiteLaunchStatus.IN_PROGRESS when a LAUNCH_SITE_START action is dispatched', () => {
			const { launchSiteStart } = createActions( mockedClientCredentials );

			const action = launchSiteStart( siteId );
			const expected = {
				...originalState,
				[ siteId ]: { ...originalState[ siteId ], status: SiteLaunchStatus.IN_PROGRESS },
			};

			expect( launchStatus( originalState, action ) ).toEqual( expected );
		} );

		it( 'should set the status to SiteLaunchStatus.SUCCESS when a LAUNCH_SITE_SUCCESS action is dispatched', () => {
			const { launchSiteSuccess } = createActions( mockedClientCredentials );

			const action = launchSiteSuccess( siteId );
			const expected = {
				...originalState,
				[ siteId ]: { ...originalState[ siteId ], status: SiteLaunchStatus.SUCCESS },
			};

			expect( launchStatus( originalState, action ) ).toEqual( expected );
		} );

		it( 'should set the status to SiteLaunchStatus.FAILURE and set an errorCode when a LAUNCH_SITE_FAILURE action is dispatched', () => {
			const { launchSiteFailure } = createActions( mockedClientCredentials );
			const error = SiteLaunchError.INTERNAL;
			const action = launchSiteFailure( siteId, error );
			const expected = {
				...originalState,
				[ siteId ]: {
					...originalState[ siteId ],
					status: SiteLaunchStatus.FAILURE,
					errorCode: SiteLaunchError.INTERNAL,
				},
			};

			expect( launchStatus( originalState, action ) ).toEqual( expected );
		} );
	} );

	describe( 'Site Setup Errors', () => {
		type ClientCredentials = { client_id: string; client_secret: string };

		let siteId: number;
		let client_id: string;
		let client_secret: string;
		let mockedClientCredentials: ClientCredentials;
		let originalState: { [ key: number ]: SiteLaunchState };

		beforeEach( () => {
			siteId = 12345;
			client_id = 'magic_client_id';
			client_secret = 'magic_client_secret';
			mockedClientCredentials = { client_id, client_secret };
			originalState = {};
		} );

		it( 'should default to the initial state when an unknown action is dispatched', () => {
			const state = siteSetupErrors( undefined, { type: 'TEST_ACTION' } );
			expect( state ).toStrictEqual( {} );
		} );

		it( 'should set a site setup error when a SET_SITE_SETUP_ERROR action is dispatched', () => {
			const { setSiteSetupError } = createActions( mockedClientCredentials );

			const error = 'test_error';
			const message = 'This is a test error';

			const action = setSiteSetupError( error, message );
			const expected = {
				error,
				message,
			};

			expect( siteSetupErrors( originalState, action ) ).toEqual( expected );
		} );

		it( 'should clear a site setup error when a CLEAR_SITE_SETUP_ERROR action is dispatched', () => {
			const { clearSiteSetupError } = createActions( mockedClientCredentials );

			const action = clearSiteSetupError( siteId );
			const expected = {};

			expect( siteSetupErrors( originalState, action ) ).toEqual( expected );
		} );
	} );
} );

describe( 'Site Theme', () => {
	const siteThemeResponse: CurrentTheme = {
		id: 'tazza',
	};

	it( 'returns site data keyed by id', () => {
		const state = siteTheme( undefined, {
			type: 'RECEIVE_SITE_THEME',
			siteId: 12345,
			theme: siteThemeResponse,
		} );
		expect( state ).toEqual( {
			12345: siteThemeResponse,
		} );
	} );
} );