File size: 4,040 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
/**
 * @jest-environment jsdom
 */
import config from '@automattic/calypso-config';
import { render, screen } from '@testing-library/react';
import PluginDetailsHeader from 'calypso/my-sites/plugins/plugin-details-header';

jest.mock( 'react-redux', () => ( {
	...jest.requireActual( 'react-redux' ),
	useSelector: jest.fn(),
} ) );

jest.mock( '../../plugin-management-v2/hooks/use-plugin-version-info', () => {
	return jest.fn().mockImplementation( () => {
		return {
			currentVersionsRange: {
				min: '1.0.0',
				max: null,
			},
			updatedVersions: [ '1.0.0' ],
			hasUpdate: true,
		};
	} );
} );

const mockUseMarketplaceReviewsQuery = jest.fn();
const mockUseMarketplaceReviewsStatsQuery = jest.fn().mockReturnValue( { data: [] } );

jest.mock( 'calypso/data/marketplace/use-marketplace-reviews', () => ( {
	useMarketplaceReviewsQuery: () => mockUseMarketplaceReviewsQuery(),
	useMarketplaceReviewsStatsQuery: () => mockUseMarketplaceReviewsStatsQuery(),
} ) );

jest.mock( '@automattic/calypso-config', () => {
	const fn = ( key ) => {
		if ( 'magnificent_non_en_locales' === key ) {
			return [
				'es',
				'pt-br',
				'de',
				'fr',
				'he',
				'ja',
				'it',
				'nl',
				'ru',
				'tr',
				'id',
				'zh-cn',
				'zh-tw',
				'ko',
				'ar',
				'sv',
			];
		}
	};
	fn.isEnabled = jest.fn();
	return fn;
} );

const plugin = {
	name: 'Yoast SEO Premium',
	slug: 'wordpress-seo-premium',
	author_name: 'Team Yoast',
	author_profile: 'https://profiles.wordpress.org/yoast/',
	num_ratings: 0,
	last_updated: '2021-01-01',
};

describe( 'PluginDetailsHeader', () => {
	const mockedProps = {
		isJetpackCloud: false,
		isPlaceholder: false,
		plugin,
	};

	beforeEach( () => {
		mockUseMarketplaceReviewsQuery.mockReturnValue( {
			data: [
				{
					id: 0,
					meta: {
						wpcom_marketplace_rating: 1,
					},
				},
			],
		} );
	} );

	test.each( [ { configEnabled: true }, { configEnabled: false } ] )(
		'should render the correct author url (configEnabled: $configEnabled)',
		( { configEnabled } ) => {
			config.isEnabled.mockImplementation( () => configEnabled );
			render( <PluginDetailsHeader { ...mockedProps } /> );
			const want = /\/plugins\/.*\?s=developer:.*/;
			const have = screen.getByText( plugin.author_name ).getAttribute( 'href' );
			expect( have ).toMatch( want );
		}
	);

	test( 'should render ratings if they are available', () => {
		const props = {
			...mockedProps,
			plugin: {
				...mockedProps.plugin,
				num_ratings: 1,
				rating: 23,
			},
		};

		const { container } = render( <PluginDetailsHeader { ...props } /> );
		const ratingElement = container.querySelector( '.plugin-details-header__info-value' );
		expect( ratingElement ).toHaveTextContent( '1.2/5' );
	} );

	test( 'should render "add a review" if marketplace product with no ratings', () => {
		mockUseMarketplaceReviewsQuery.mockReturnValue( { data: [] } );
		const props = {
			...mockedProps,
			isMarketplaceProduct: true,
			plugin: { ...mockedProps.plugin, num_ratings: 0, rating: 0 },
		};
		render( <PluginDetailsHeader { ...props } /> );
		const button = screen.getByText( 'Add a review' );
		expect( button.tagName.toLowerCase() ).toBe( 'button' );
	} );

	test( 'should render review count if marketplace product', () => {
		const props = {
			...mockedProps,
			isMarketplaceProduct: true,
			plugin: { ...mockedProps.plugin, num_ratings: 0, rating: 0 },
		};
		render( <PluginDetailsHeader { ...props } /> );
		const button = screen.getByText( '1 review' );
		expect( button.tagName.toLowerCase() ).toBe( 'button' );
	} );

	test( 'should render "add a review" if dotOrg plugin with no ratings', () => {
		const props = {
			...mockedProps,
			plugin: { ...mockedProps.plugin, num_ratings: 0, rating: 0 },
		};
		render( <PluginDetailsHeader { ...props } /> );
		const link = screen.getByText( 'Add a review' );
		expect( link.tagName.toLowerCase() ).toBe( 'a' );
		expect( link ).toHaveAttribute(
			'href',
			`https://wordpress.org/support/plugin/${ props.plugin.slug }/reviews`
		);
	} );
} );