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

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import preferencesReducer from 'calypso/state/preferences/reducer';
import { renderWithProvider } from 'calypso/test-helpers/testing-library';
import { Banner } from '../index';

jest.mock( 'calypso/state/analytics/actions', () => ( {
	recordTracksEvent: jest.fn( () => ( {
		type: 'ANALYTICS_EVENT_RECORD',
	} ) ),
} ) );

function renderWithRedux( ui ) {
	return renderWithProvider( ui, {
		reducers: {
			preferences: preferencesReducer,
		},
		initialState: { preferences: { remoteValues: {} } },
	} );
}

const props = {
	callToAction: null,
	title: 'banner title',
};

describe( 'Banner basic tests', () => {
	afterEach( () => {
		jest.clearAllMocks();
	} );

	test( 'should render Card if dismissPreferenceName is null', () => {
		const { container } = render( <Banner { ...props } dismissPreferenceName={ null } /> );
		expect( container.firstChild ).toHaveClass( 'card' );
		expect( container.firstChild ).not.toHaveClass( 'is-dismissible' );
	} );

	test( 'should render DismissibleCard if dismissPreferenceName is defined', () => {
		renderWithRedux( <Banner { ...props } dismissPreferenceName="banner-test" /> );
		expect( screen.getByLabelText( 'Dismiss' ) ).toBeVisible();
	} );

	test( 'should render a <Button /> when callToAction is specified', () => {
		render( <Banner { ...props } callToAction="Buy something!" /> );
		expect( screen.getByRole( 'button' ) ).toHaveTextContent( 'Buy something!' );
	} );

	test( 'should not render a <Button /> when callToAction is not specified', () => {
		render( <Banner { ...props } /> );
		expect( screen.queryByRole( 'button' ) ).not.toBeInTheDocument();
	} );

	test( 'should have .is-jetpack class and JetpackLogo if jetpack prop is defined', () => {
		const { plan, ...propsWithoutPlan } = props;
		const { container } = render( <Banner { ...propsWithoutPlan } jetpack /> );
		expect( container.firstChild ).toHaveClass( 'is-jetpack' );
		expect( container.getElementsByClassName( 'jetpack-logo' ) ).toHaveLength( 1 );
	} );

	test( 'should render have .is-horizontal class if horizontal prop is defined', () => {
		const { container } = render( <Banner { ...props } horizontal /> );
		expect( container.firstChild ).toHaveClass( 'is-horizontal' );
	} );

	test( 'should render a <PlanPrice /> when price is specified', () => {
		const { container } = render( <Banner { ...props } price={ 100 } /> );
		expect( container.getElementsByClassName( 'plan-price' ) ).toHaveLength( 1 );
		const [ price ] = container.getElementsByClassName( 'plan-price__integer' );
		expect( price ).toHaveTextContent( '100' );
	} );

	test( 'should render two <PlanPrice /> components when there are two prices', () => {
		const { container } = render( <Banner { ...props } price={ [ 100, 80 ] } /> );
		expect( container.getElementsByClassName( 'plan-price' ) ).toHaveLength( 2 );

		const [ price1, price2 ] = container.getElementsByClassName( 'plan-price__integer' );
		expect( price1 ).toHaveTextContent( 100 );
		expect( price2 ).toHaveTextContent( 80 );
	} );

	test( 'should render no <PlanPrice /> components when there are no prices', () => {
		render( <Banner { ...props } /> );
		expect( screen.queryByTestId( 'plan-price' ) ).not.toBeInTheDocument();
	} );

	test( 'should render a .banner__description when description is specified', () => {
		render( <Banner { ...props } description="test" /> );
		expect( screen.getByText( 'test' ) ).toHaveClass( 'banner__description' );
	} );

	test( 'should not render a .banner__description when description is not specified', () => {
		const { container } = render( <Banner { ...props } /> );
		expect( container.getElementsByClassName( 'banner__description' ) ).toHaveLength( 0 );
	} );

	test( 'should render a .banner__list when list is specified', () => {
		render( <Banner { ...props } list={ [ 'test1', 'test2' ] } /> );
		expect( screen.queryByRole( 'list' ) ).toBeVisible();
		expect( screen.queryAllByRole( 'listitem' ) ).toHaveLength( 2 );
		expect( screen.queryAllByRole( 'listitem' )[ 0 ] ).toHaveTextContent( 'test1' );
		expect( screen.queryAllByRole( 'listitem' )[ 1 ] ).toHaveTextContent( 'test2' );
	} );

	test( 'should not render a .banner__list when description is not specified', () => {
		render( <Banner { ...props } /> );
		expect( screen.queryByRole( 'list' ) ).not.toBeInTheDocument();
	} );

	test( 'should record Tracks event when event is specified', () => {
		renderWithProvider( <Banner { ...props } event="test" /> );
		expect( recordTracksEvent ).toHaveBeenCalledWith(
			'calypso_banner_cta_impression',
			expect.objectContaining( {
				cta_name: 'test',
			} )
		);
	} );

	test( 'should not record Tracks event when event is not specified', () => {
		renderWithProvider( <Banner { ...props } /> );
		expect( recordTracksEvent ).not.toHaveBeenCalled();
	} );

	test( 'should render Card with href if href prop is passed', () => {
		const { container } = render( <Banner { ...props } href="/" /> );
		expect( container.firstChild ).toHaveClass( 'card' );
		expect( container.firstChild ).toHaveAttribute( 'href', '/' );
	} );

	test( 'should render Card with no href if href prop is passed but disableHref is true', () => {
		const { container } = render( <Banner { ...props } href="/" disableHref /> );
		expect( container.firstChild ).toHaveClass( 'card' );
		expect( container.firstChild ).not.toHaveAttribute( 'href' );
	} );

	test( 'should render Card with href if href prop is passed but disableHref is true and forceHref is true', () => {
		const { container } = render( <Banner { ...props } href="/" disableHref forceHref /> );
		expect( container.firstChild ).toHaveClass( 'card' );
		expect( container.firstChild ).toHaveAttribute( 'href', '/' );
	} );

	test( 'should render Card with no href and CTA button with href if href prop is passed and callToAction is also passed', async () => {
		const handleClick = jest.fn();
		const { container } = render(
			<Banner { ...props } href="/" callToAction="Go WordPress!" onClick={ handleClick } />
		);
		const btnText = screen.getByText( 'Go WordPress!' );
		await userEvent.click( btnText );

		expect( handleClick ).toHaveBeenCalledTimes( 1 );
		expect( container.firstChild ).toHaveClass( 'card' );
		expect( container.firstChild ).not.toHaveAttribute( 'href' );
		expect( btnText ).toBeVisible();
		expect( btnText ).toHaveAttribute( 'href', '/' );
	} );

	test( 'should render Card with href and CTA button with no href if href prop is passed and callToAction is also passed and forceHref is true', async () => {
		const handleClick = jest.fn();
		const { container } = render(
			<Banner
				{ ...props }
				href="/"
				callToAction="Go WordPress!"
				forceHref
				onClick={ handleClick }
			/>
		);
		const btn = screen.getByRole( 'button' );
		await userEvent.click( container.firstChild );

		expect( handleClick ).toHaveBeenCalledTimes( 1 );
		expect( container.firstChild ).toHaveClass( 'card' );
		expect( container.firstChild ).toHaveAttribute( 'href', '/' );
		expect( btn ).toBeVisible();
		expect( btn ).not.toHaveAttribute( 'href' );
		expect( btn ).toHaveTextContent( 'Go WordPress!' );
	} );
} );