File size: 2,292 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
/**
 * @jest-environment jsdom
 */
import { renderHook } from '@testing-library/react';
import {
	ACTIVATE_PLUGIN,
	DEACTIVATE_PLUGIN,
	DISABLE_AUTOUPDATE_PLUGIN,
	ENABLE_AUTOUPDATE_PLUGIN,
	REMOVE_PLUGIN,
	UPDATE_PLUGIN,
	INSTALL_PLUGIN,
} from 'calypso/lib/plugins/constants';
import {
	PLUGIN_INSTALLATION_ERROR,
	PLUGIN_INSTALLATION_IN_PROGRESS,
} from 'calypso/state/plugins/installed/status/constants';
import AllMessagesByAction from '../messages/by-action';
import useStatusMessageText from '../use-status-message-text';

jest.mock( '../messages/by-action', () => ( {
	__esModule: true,
	default: {
		INSTALL_PLUGIN: {
			inProgress: jest.fn(),
		},
	},
} ) );

const ALL_ACTIONS = [
	[ INSTALL_PLUGIN ],
	[ ACTIVATE_PLUGIN ],
	[ UPDATE_PLUGIN ],
	[ DEACTIVATE_PLUGIN ],
	[ REMOVE_PLUGIN ],
	[ ENABLE_AUTOUPDATE_PLUGIN ],
	[ DISABLE_AUTOUPDATE_PLUGIN ],
];

describe( 'useStatusMessageText', () => {
	it.each( ALL_ACTIONS )(
		'returns an action-agnostic failure message if `status` is an error and `hasOneStatus` is false',
		( action ) => {
			const {
				result: { current: oneSiteMessage },
			} = renderHook( () =>
				useStatusMessageText( {
					action,
					status: PLUGIN_INSTALLATION_ERROR,
					hasOneStatus: false,
					siteCount: 1,
				} )
			);

			expect( oneSiteMessage ).toBe( 'Failed on 1 site' );

			const {
				result: { current: manySitesMessage },
			} = renderHook( () =>
				useStatusMessageText( {
					action,
					status: PLUGIN_INSTALLATION_ERROR,
					hasOneStatus: false,
					siteCount: 4,
				} )
			);

			expect( manySitesMessage ).toBe( 'Failed on 4 sites' );
		}
	);

	it( 'returns the appropriate status message based on the given action, status, and site details', () => {
		AllMessagesByAction[ INSTALL_PLUGIN ][ PLUGIN_INSTALLATION_IN_PROGRESS ].mockReturnValue(
			() => 'Message'
		);

		const {
			result: { current: message },
		} = renderHook( () =>
			useStatusMessageText( {
				action: INSTALL_PLUGIN,
				status: PLUGIN_INSTALLATION_IN_PROGRESS,
				hasSelectedSite: true,
				hasOneStatus: true,
				siteCount: 2,
			} )
		);

		expect( message ).toBe( 'Message' );
		expect(
			AllMessagesByAction[ INSTALL_PLUGIN ][ PLUGIN_INSTALLATION_IN_PROGRESS ]
		).toHaveBeenCalledWith( { hasSelectedSite: true, siteCount: 2 } );
	} );
} );