File size: 6,251 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
/**
 * @jest-environment jsdom
 */
// @ts-nocheck - TODO: Fix TypeScript issues
import { WPCOM_FEATURES_INSTALL_PLUGINS } from '@automattic/calypso-products';
import userEvent from '@testing-library/user-event';
import moment from 'moment';
import React from 'react';
import documentHead from 'calypso/state/document-head/reducer';
import marketplace from 'calypso/state/marketplace/reducer';
import plugins from 'calypso/state/plugins/reducer';
import productsList from 'calypso/state/products-list/reducer';
import siteConnection from 'calypso/state/site-connection/reducer';
import { reducer as ui } from 'calypso/state/ui/reducer';
import { renderWithProvider } from 'calypso/test-helpers/testing-library';
import PluginRowFormatter from '../plugin-row-formatter';
import { site, plugin, paidPlugin } from './utils/constants';

const initialReduxState = {
	siteConnection: { items: { [ site.ID ]: true } },
	sites: {
		items: { [ site.ID ]: site },
		features: { [ site.ID ]: { data: { active: [ WPCOM_FEATURES_INSTALL_PLUGINS ] } } },
	},
	currentUser: {
		capabilities: {},
	},
	plugins: {
		installed: {
			isRequesting: {},
			isRequestingAll: false,
			plugins: {
				[ `${ site.ID }` ]: [ plugin ],
			},
		},
	},
	productsList: {
		items: {},
	},
	marketplace: {
		billingInterval: {
			interval: 'yearly',
		},
	},
};

const render = ( el, partialState ) =>
	renderWithProvider( el, {
		initialState: { ...initialReduxState, ...partialState },
		reducers: { ui, plugins, documentHead, productsList, siteConnection, marketplace },
		store: undefined,
	} );

const props = {
	item: plugin,
	columnKey: 'site-name',
	selectedSite: { ...site, canUpdateFiles: true },
	isSmallScreen: false,
};

describe( '<PluginRowFormatter>', () => {
	beforeAll( () => {
		window.matchMedia = jest.fn().mockImplementation( ( query ) => {
			return {
				matches: true,
				media: query,
				onchange: null,
				addListener: jest.fn(),
				removeListener: jest.fn(),
			};
		} );
	} );

	test( 'should render correctly and show site domain', () => {
		const { container } = render( <PluginRowFormatter { ...props } /> );

		const [ value ] = container.getElementsByClassName( 'plugin-row-formatter__row-container' );
		expect( value.textContent ).toEqual( site.domain );
	} );

	test( 'should render correctly and show plugin name', () => {
		props.columnKey = 'plugin';
		const { container } = render( <PluginRowFormatter { ...props } /> );

		const [ value ] = container.getElementsByClassName( 'plugin-row-formatter__plugin-name' );
		expect( value.textContent ).toEqual( plugin.name );
	} );

	test( 'should render correctly and show site count', () => {
		props.columnKey = 'sites';
		const { container } = render( <PluginRowFormatter { ...props } /> );

		const [ value ] = container.getElementsByClassName(
			'plugin-row-formatter__sites-count-button'
		);
		expect( value.textContent && parseInt( value.textContent ) ).toEqual(
			Object.keys( plugin.sites ).length
		);
	} );

	test( 'should render correctly and show site count(singular) on small screen', () => {
		props.columnKey = 'sites';
		props.isSmallScreen = true;
		const { getAllByText } = render( <PluginRowFormatter { ...props } /> );

		const [ autoManagedSite ] = getAllByText(
			`Installed on ${ Object.keys( plugin.sites ).length } site`
		);
		expect( autoManagedSite ).toBeInTheDocument();
	} );

	test( 'should render correctly and show site count(plural) on small screen', () => {
		props.columnKey = 'sites';
		plugin.sites = {
			[ site.ID ]: { ID: site.ID, canUpdateFiles: true },
			[ site.ID + 1 ]: { ID: site.ID + 1, canUpdateFiles: true },
		};
		const { getAllByText } = render( <PluginRowFormatter { ...props } /> );

		const [ autoManagedSite ] = getAllByText(
			`Installed on ${ Object.keys( plugin.sites ).length } sites`
		);
		expect( autoManagedSite ).toBeInTheDocument();
	} );

	test( 'should render correctly and show activate and toggle checked value', async () => {
		props.columnKey = 'activate';
		const { container } = render( <PluginRowFormatter { ...props } /> );

		const [ value ] = container.getElementsByClassName( 'plugin-row-formatter__toggle' );
		expect( value.textContent ).toEqual( 'Active' );

		const [ toggleButton ] = value.getElementsByClassName( 'components-form-toggle__input' );
		expect( toggleButton ).toHaveProperty( 'checked', false );
		await userEvent.click( toggleButton );
		expect( toggleButton ).toHaveProperty( 'checked', true );
	} );

	test( 'should render correctly and show auto-update and toggle checked value', async () => {
		props.columnKey = 'autoupdate';
		const { container } = render( <PluginRowFormatter { ...props } /> );

		const [ value ] = container.getElementsByClassName( 'plugin-row-formatter__toggle' );
		expect( value.textContent ).toEqual( 'Autoupdates' );

		const [ toggleButton ] = value.getElementsByClassName( 'components-form-toggle__input' );
		expect( toggleButton ).toHaveProperty( 'checked', false );
		await userEvent.click( toggleButton );
		expect( toggleButton ).toHaveProperty( 'checked', true );
	} );

	test( 'should render correctly and show last updated', () => {
		props.columnKey = 'last-updated';
		const { container } = render( <PluginRowFormatter { ...props } /> );

		const [ value ] = container.getElementsByClassName( 'plugin-row-formatter__last-updated' );
		expect( value.textContent ).toEqual(
			`Updated ${ moment.utc( plugin.last_updated, 'YYYY-MM-DD hh:mma' ).fromNow() }`
		);
	} );

	test( 'should render correctly and show install button', () => {
		props.columnKey = 'install';
		const { getAllByText } = render( <PluginRowFormatter { ...props } />, {
			sites: {
				items: { [ site.ID ]: { ...site, options: { ...site.options, is_wpcom_atomic: true } } },
				features: initialReduxState.sites.features,
			},
		} );

		const [ autoManagedSite ] = getAllByText( 'Install' );
		expect( autoManagedSite ).toBeInTheDocument();
	} );

	test( 'should render correctly and show disabled upgrade button', () => {
		props.columnKey = 'install';
		const { getAllByText } = render( <PluginRowFormatter { ...props } item={ paidPlugin } /> );

		const [ autoManagedSite ] = getAllByText( 'Upgrade disabled' );
		expect( autoManagedSite ).toBeInTheDocument();
	} );
} );