File size: 3,623 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
/** @jest-environment jsdom */
// @ts-nocheck - TODO: Fix TypeScript issues

import { isEnabled } from '@automattic/calypso-config';
import { PLAN_FREE } from '@automattic/calypso-products';
import { useCurrentPlan } from '@automattic/data-stores/src/plans';
import { screen, within, act } from '@testing-library/react';
import React from 'react';
import PlansWrapper from 'calypso/my-sites/plans/main';
import purchasesReducer from 'calypso/state/purchases/reducer';
import uiReducer from 'calypso/state/ui/reducer';
import { renderWithProvider } from 'calypso/test-helpers/testing-library';

jest.mock( '@automattic/calypso-config' );
jest.mock( 'calypso/components/data/document-head', () => () => null );
jest.mock( 'calypso/components/data/query-active-promotions', () => jest.fn() );
jest.mock( 'calypso/components/data/query-sites', () => jest.fn() );
jest.mock( 'calypso/components/data/query-site-purchases', () => jest.fn() );
jest.mock( 'calypso/components/data/query-products-list', () => jest.fn() );
jest.mock( '@automattic/data-stores/src/plans/hooks/use-current-plan', () => jest.fn() );

const mockPlan = {
	planSlug: PLAN_FREE,
	is_free: true,
};

const mockSite = {
	ID: 1,
	plan: mockPlan,
};

const initialState = {
	sites: {
		items: {
			[ mockSite.ID ]: {
				ID: mockSite.ID,
				slug: 'test-site',
				plan: mockPlan,
			},
		},
	},
	ui: {
		selectedSiteId: mockSite.ID,
	},
	currentUser: {
		capabilities: {
			[ mockSite.ID ]: {
				manage_options: true,
			},
		},
	},
};

describe( '/plans/:siteSlug', () => {
	let originalScrollTo;

	beforeAll( () => {
		originalScrollTo = window.scrollTo;
		window.scrollTo = () => null;
		window.IntersectionObserver = jest.fn( () => ( {
			observe: jest.fn(),
			unobserve: jest.fn(),
			disconnect: jest.fn(),
		} ) );
	} );

	afterAll( () => {
		window.scrollTo = originalScrollTo;
	} );

	const renderPage = ( { initialPath } = {} ) => {
		renderWithProvider( <PlansWrapper context={ { path: '/plans' } } />, {
			initialPath,
			initialState,
			reducers: {
				ui: uiReducer,
				purchases: purchasesReducer,
			},
		} );
	};

	beforeEach( () => {
		jest.clearAllMocks();

		const configMock = ( values ) => ( key ) => values[ key ];
		( isEnabled as jest.Mock ).mockImplementation( configMock( { 'untangling/plans': true } ) );

		( useCurrentPlan as jest.Mock ).mockReturnValue( mockPlan );
	} );

	describe( "'Manage add-ons' button", () => {
		beforeEach( renderPage );

		it( 'should be visible', () => {
			expect( screen.getByRole( 'button', { name: 'Manage add-ons' } ) ).toBeInTheDocument();
		} );

		it( 'should hide the modal initially', () => {
			expect( screen.queryByRole( 'dialog' ) ).not.toBeInTheDocument();
		} );

		describe( 'when the button is clicked', () => {
			beforeEach( () => {
				const button = screen.getByRole( 'button', { name: 'Manage add-ons' } );
				act( () => {
					button.click();
				} );
			} );

			it( 'should show the modal', () => {
				const dialog = screen.getByRole( 'dialog' );
				expect( dialog ).toBeInTheDocument();
				expect( within( dialog ).getByRole( 'heading' ) ).toHaveTextContent(
					'Boost your plan with add-ons'
				);
			} );
		} );
	} );

	describe( "when the '?add-ons-modal=true' query param is present", () => {
		beforeEach( () => {
			renderPage( { initialPath: '?add-ons-modal=true' } );
		} );

		it( 'should show the add-ons modal initially', () => {
			const dialog = screen.getByRole( 'dialog' );
			expect( dialog ).toBeInTheDocument();
			expect( within( dialog ).getByRole( 'heading' ) ).toHaveTextContent(
				'Boost your plan with add-ons'
			);
		} );
	} );
} );