File size: 4,086 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
import { isPlansPageUntangled } from 'calypso/lib/plans/untangling-plans-experiment';
import sections from 'calypso/sections';
import { getSidebarType, SidebarType } from '../selectors';

jest.mock( 'calypso/lib/plans/untangling-plans-experiment', () => ( {
	isPlansPageUntangled: jest.fn(),
} ) );

interface Site {
	ID: number;
	adminInterface?: string;
	isPlansPageUntangled?: boolean;
}

describe( 'getSidebarType', () => {
	const site: Site = {
		ID: 123,
	};
	const siteDefault: Site = {
		ID: 123,
		adminInterface: 'calypso',
	};
	const siteClassic: Site = {
		ID: 123,
		adminInterface: 'wp-admin',
	};
	const sitePlansTangled: Site = {
		ID: 123,
		isPlansPageUntangled: false,
	};
	const sitePlansUntangled: Site = {
		ID: 123,
		isPlansPageUntangled: true,
	};

	afterEach( () => {
		jest.clearAllMocks();
	} );

	it.each( [
		[ null, '/sites', SidebarType.Global ],
		[ null, '/p2s', SidebarType.Global ],
		[ null, '/domains/manage', SidebarType.Global ],
		[ null, '/themes', SidebarType.Global ],
		[ null, '/plugins', SidebarType.Global ],
		[ null, '/plugins/scheduled-updates', SidebarType.Global ],
		[ null, '/me', SidebarType.Global ],
		[ null, '/reader', SidebarType.Global ],

		[ site, '/overview/example.com', SidebarType.GlobalCollapsed ],
		[ site, '/hosting-features/example.com', SidebarType.GlobalCollapsed ],
		[ site, '/github-deployments/example.com', SidebarType.GlobalCollapsed ],
		[ site, '/staging-site/example.com', SidebarType.GlobalCollapsed ],
		[ site, '/site-monitoring/example.com', SidebarType.GlobalCollapsed ],
		[ site, '/site-logs/example.com', SidebarType.GlobalCollapsed ],
		[ site, '/sites/performance/example.com', SidebarType.GlobalCollapsed ],
		[ site, '/sites/settings/site/example.com', SidebarType.GlobalCollapsed ],
		[ site, '/sites/settings/server/example.com', SidebarType.GlobalCollapsed ],

		[ null, '/domains/manage/all/overview/domain.com', SidebarType.GlobalCollapsed ],
		[ null, '/domains/manage/all/email/domain.com', SidebarType.GlobalCollapsed ],
		[ null, '/domains/manage/all/contact-info/edit/domain.com', SidebarType.GlobalCollapsed ],

		[ null, '/plugins/manage/sites', SidebarType.GlobalCollapsed ],
		[ null, '/plugins/scheduled-updates/create', SidebarType.GlobalCollapsed ],
		[ null, '/plugins/scheduled-updates/edit/000-daily-86400-06:00', SidebarType.GlobalCollapsed ],

		[ siteDefault, '/home/example.com', SidebarType.UnifiedSiteDefault ],
		[ siteDefault, '/domains/manage/example.com', SidebarType.UnifiedSiteDefault ],
		[ siteDefault, '/themes/example.com', SidebarType.UnifiedSiteDefault ],
		[ siteDefault, '/plugins/example.com', SidebarType.UnifiedSiteDefault ],

		[ siteClassic, '/home/example.com', SidebarType.UnifiedSiteClassic ],
		[ siteClassic, '/domains/manage/example.com', SidebarType.UnifiedSiteClassic ],
		[ siteClassic, '/themes/example.com', SidebarType.UnifiedSiteClassic ],
		[ siteClassic, '/plugins/example.com', SidebarType.UnifiedSiteClassic ],

		[ null, '/start', SidebarType.None ],
		[ null, '/setup', SidebarType.None ],

		[ sitePlansTangled, '/plans/example.com', SidebarType.UnifiedSiteDefault ],
		[ sitePlansUntangled, '/plans/example.com', SidebarType.GlobalCollapsed ],
	] )( 'should return correct sidebar type', ( selectedSite, route, expected ) => {
		const section =
			sections.findLast( ( s ) =>
				s.paths.some(
					( path ) =>
						route.startsWith( path ) &&
						[ 'me', 'reader', 'sites', 'sites-dashboard' ].includes( s.group || '' )
				)
			) || ( {} as any ); // eslint-disable-line @typescript-eslint/no-explicit-any

		const siteId = selectedSite?.ID || null;
		const state = siteId
			? {
					sites: {
						items: {
							[ siteId ]: {
								options: {
									wpcom_admin_interface: selectedSite?.adminInterface,
								},
							},
						},
					},
			  }
			: {};

		if ( selectedSite?.isPlansPageUntangled ) {
			( isPlansPageUntangled as jest.Mock ).mockReturnValue( true );
		}

		const actual = getSidebarType( {
			state,
			siteId,
			section,
			route,
		} );
		expect( actual ).toEqual( expected );
	} );
} );