File size: 4,757 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
/** @jest-environment jsdom */

import { PLAN_FREE } from '@automattic/calypso-products';
import { screen } from '@testing-library/react';
import productsList from 'calypso/state/products-list/reducer';
import { reducer as ui } from 'calypso/state/ui/reducer';
import { renderWithProvider } from 'calypso/test-helpers/testing-library';
import PluginsBrowserList from '../';
import { PluginsBrowserListVariant } from '../types';

jest.mock( 'calypso/lib/route/path', () => ( {
	getMessagePathForJITM: jest.fn( () => '/plugins/' ),
} ) );

jest.mock( 'calypso/my-sites/plugins/use-preinstalled-premium-plugin', () =>
	jest.fn( () => ( { usePreinstalledPremiumPlugin: jest.fn() } ) )
);

jest.mock( 'calypso/state/plugins/last-visited/selectors', () => ( {
	isLastVisitedPlugin: () => {},
} ) );

const render = ( el, options ) =>
	renderWithProvider( el, { ...options, reducers: { ui, productsList } } );

const plugins = [
	{ name: 'woocommerce', slug: 'woocommerce' },
	{ name: 'jetpack', slug: 'jetpack' },
	{ name: 'hello-dolly', slug: 'hello-dolly' },
];

const props = {
	plugins,
	variant: PluginsBrowserListVariant.Fixed,
	listName: 'arbitrary-plugin-list-name',
	title: 'arbitrary-plugin-list-title',
	subtitle: 'arbitrary-plugin-list-subtitle',
	site: {
		plan: PLAN_FREE,
	},
	size: 6,
};

const queryPlaceholders = () => {
	const listItems = screen.queryAllByRole( 'listitem' );
	return listItems.filter( ( item ) => item.classList.contains( 'is-placeholder' ) );
};

describe( 'PluginsBrowserList basic tests', () => {
	test( 'should render the section header with title', () => {
		render( <PluginsBrowserList { ...props } /> );
		const header = screen.getByText( props.title );
		expect( header ).toBeVisible();
		expect( header ).toHaveClass( 'plugins-results-header__title' );
	} );

	test( 'should render the section header with subtitle', () => {
		render( <PluginsBrowserList { ...props } /> );
		const subtitle = screen.getByText( props.subtitle );
		expect( subtitle ).toBeVisible();
		expect( subtitle ).toHaveClass( 'plugins-results-header__subtitle' );
	} );

	test( 'should render a given number of list items when the size prop is set', () => {
		render( <PluginsBrowserList { ...props } size={ 2 } /> );
		expect( screen.getAllByRole( 'listitem' ) ).toHaveLength( 2 );
	} );
} );

describe( 'InfiniteScroll variant', () => {
	const infiniteScrollProps = {
		...props,
		variant: PluginsBrowserListVariant.InfiniteScroll,
	};

	test( 'should show placeholders if there are no plugins', () => {
		render( <PluginsBrowserList { ...infiniteScrollProps } plugins={ [] } /> );
		expect( queryPlaceholders() ).toHaveLength( 6 );
	} );

	test( 'should append placeholders if there are plugins and `showPlaceholders` is set', () => {
		render( <PluginsBrowserList { ...infiniteScrollProps } showPlaceholders /> );
		const listItems = screen.getAllByRole( 'listitem' );
		expect( listItems ).toHaveLength( 9 );
		expect( queryPlaceholders() ).toHaveLength( 6 );
	} );

	test( 'should not show placeholders if there are plugins and the `showPlaceholders` is not set', () => {
		render( <PluginsBrowserList { ...infiniteScrollProps } /> );
		expect( queryPlaceholders() ).toHaveLength( 0 );
	} );
} );

describe( 'Paginated variant', () => {
	const paginatedProps = {
		...props,
		variant: PluginsBrowserListVariant.Paginated,
	};

	test( 'should show placeholders if there are no plugins', () => {
		render( <PluginsBrowserList { ...paginatedProps } plugins={ [] } /> );
		expect( queryPlaceholders() ).toHaveLength( 6 );
	} );

	test( 'should show placeholders if there are plugins and `showPlaceholders` is set', () => {
		render( <PluginsBrowserList { ...paginatedProps } showPlaceholders /> );
		expect( queryPlaceholders() ).toHaveLength( 6 );
	} );

	test( 'should not show placeholders if there are plugins and the `showPlaceholders` is not set', () => {
		render( <PluginsBrowserList { ...paginatedProps } /> );
		expect( queryPlaceholders() ).toHaveLength( 0 );
	} );
} );

describe( 'Fixed variant', () => {
	const fixedProps = {
		...props,
		variant: PluginsBrowserListVariant.Fixed,
	};

	test( 'should show placeholders if there are no plugins', () => {
		render( <PluginsBrowserList { ...fixedProps } plugins={ [] } /> );
		expect( queryPlaceholders() ).toHaveLength( 6 );
	} );

	describe( 'should not show placeholders regardless of the `showPlaceholders` prop', () => {
		test( 'with `showPlaceholdersProp`', () => {
			render( <PluginsBrowserList { ...fixedProps } showPlaceholders /> );
			expect( queryPlaceholders() ).toHaveLength( 0 );
		} );
		test( 'without `showPlaceholdersProp`', () => {
			render( <PluginsBrowserList { ...fixedProps } /> );
			expect( queryPlaceholders() ).toHaveLength( 0 );
		} );
	} );
} );