index.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import type {UseQueryResult} from '@tanstack/react-query';
  2. import {BroadcastFixture} from 'sentry-fixture/broadcast';
  3. import {LocationFixture} from 'sentry-fixture/locationFixture';
  4. import {OrganizationFixture} from 'sentry-fixture/organization';
  5. import {ServiceIncidentFixture} from 'sentry-fixture/serviceIncident';
  6. import {UserFixture} from 'sentry-fixture/user';
  7. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  8. import {OnboardingContextProvider} from 'sentry/components/onboarding/onboardingContext';
  9. import SidebarContainer from 'sentry/components/sidebar';
  10. import ConfigStore from 'sentry/stores/configStore';
  11. import type {Organization} from 'sentry/types/organization';
  12. import type {StatuspageIncident} from 'sentry/types/system';
  13. import localStorage from 'sentry/utils/localStorage';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import * as incidentsHook from 'sentry/utils/useServiceIncidents';
  16. jest.mock('sentry/utils/useServiceIncidents');
  17. jest.mock('sentry/utils/useLocation');
  18. const mockUseLocation = jest.mocked(useLocation);
  19. const ALL_AVAILABLE_FEATURES = [
  20. 'insights-entry-points',
  21. 'discover',
  22. 'discover-basic',
  23. 'discover-query',
  24. 'dashboards-basic',
  25. 'dashboards-edit',
  26. 'custom-metrics',
  27. 'user-feedback-ui',
  28. 'session-replay-ui',
  29. 'performance-view',
  30. 'performance-trace-explorer',
  31. 'starfish-mobile-ui-module',
  32. 'profiling',
  33. ];
  34. describe('Sidebar', function () {
  35. const organization = OrganizationFixture();
  36. const broadcast = BroadcastFixture();
  37. const user = UserFixture();
  38. const apiMocks = {
  39. broadcasts: jest.fn(),
  40. broadcastsMarkAsSeen: jest.fn(),
  41. sdkUpdates: jest.fn(),
  42. };
  43. const getElement = () => (
  44. <OnboardingContextProvider>
  45. <SidebarContainer />
  46. </OnboardingContextProvider>
  47. );
  48. const renderSidebar = ({organization: org}: {organization: Organization | null}) =>
  49. render(getElement(), {organization: org});
  50. const renderSidebarWithFeatures = (features: string[] = []) => {
  51. return renderSidebar({
  52. organization: {
  53. ...organization,
  54. features: [...organization.features, ...features],
  55. },
  56. });
  57. };
  58. beforeEach(function () {
  59. mockUseLocation.mockReturnValue(LocationFixture());
  60. jest.spyOn(incidentsHook, 'useServiceIncidents').mockImplementation(
  61. () =>
  62. ({
  63. data: [ServiceIncidentFixture()],
  64. }) as UseQueryResult<StatuspageIncident[]>
  65. );
  66. apiMocks.broadcasts = MockApiClient.addMockResponse({
  67. url: `/organizations/${organization.slug}/broadcasts/`,
  68. body: [broadcast],
  69. });
  70. apiMocks.broadcastsMarkAsSeen = MockApiClient.addMockResponse({
  71. url: '/broadcasts/',
  72. method: 'PUT',
  73. });
  74. apiMocks.sdkUpdates = MockApiClient.addMockResponse({
  75. url: `/organizations/${organization.slug}/sdk-updates/`,
  76. body: [],
  77. });
  78. });
  79. afterEach(function () {
  80. mockUseLocation.mockReset();
  81. });
  82. it('renders', async function () {
  83. renderSidebar({organization});
  84. expect(await screen.findByTestId('sidebar-dropdown')).toBeInTheDocument();
  85. });
  86. it('renders without org', async function () {
  87. renderSidebar({organization: null});
  88. // no org displays user details
  89. expect(await screen.findByText(user.name)).toBeInTheDocument();
  90. expect(screen.getByText(user.email)).toBeInTheDocument();
  91. await userEvent.click(screen.getByTestId('sidebar-dropdown'));
  92. });
  93. it('does not render collapse with navigation-sidebar-v2 flag', async function () {
  94. renderSidebar({
  95. organization: {...organization, features: ['navigation-sidebar-v2']},
  96. });
  97. // await for the page to be rendered
  98. expect(await screen.findByText('Issues')).toBeInTheDocument();
  99. // Check that the user name is no longer visible
  100. expect(screen.queryByText(user.name)).not.toBeInTheDocument();
  101. // Check that the organization name is no longer visible
  102. expect(screen.queryByText(organization.name)).not.toBeInTheDocument();
  103. expect(screen.queryByTestId('sidebar-collapse')).not.toBeInTheDocument();
  104. });
  105. it('has can logout', async function () {
  106. const mock = MockApiClient.addMockResponse({
  107. url: '/auth/',
  108. method: 'DELETE',
  109. status: 204,
  110. });
  111. jest.spyOn(window.location, 'assign').mockImplementation(() => {});
  112. renderSidebar({
  113. organization: OrganizationFixture({access: ['member:read']}),
  114. });
  115. await userEvent.click(await screen.findByTestId('sidebar-dropdown'));
  116. await userEvent.click(screen.getByTestId('sidebar-signout'));
  117. await waitFor(() => expect(mock).toHaveBeenCalled());
  118. expect(window.location.assign).toHaveBeenCalledWith('/auth/login/');
  119. });
  120. it('can toggle help menu', async function () {
  121. renderSidebar({organization});
  122. await userEvent.click(await screen.findByText('Help'));
  123. expect(screen.getByText('Visit Help Center')).toBeInTheDocument();
  124. });
  125. describe('SidebarDropdown', function () {
  126. it('can open Sidebar org/name dropdown menu', async function () {
  127. renderSidebar({organization});
  128. await userEvent.click(await screen.findByTestId('sidebar-dropdown'));
  129. const orgSettingsLink = screen.getByText('Organization settings');
  130. expect(orgSettingsLink).toBeInTheDocument();
  131. });
  132. it('has link to Members settings with `member:write`', async function () {
  133. renderSidebar({
  134. organization: OrganizationFixture({access: ['member:read']}),
  135. });
  136. await userEvent.click(await screen.findByTestId('sidebar-dropdown'));
  137. expect(screen.getByText('Members')).toBeInTheDocument();
  138. });
  139. it('can open "Switch Organization" sub-menu', async function () {
  140. act(() => void ConfigStore.set('features', new Set(['organizations:create'])));
  141. renderSidebar({organization});
  142. await userEvent.click(await screen.findByTestId('sidebar-dropdown'));
  143. jest.useFakeTimers();
  144. await userEvent.hover(screen.getByText('Switch organization'), {delay: null});
  145. act(() => jest.advanceTimersByTime(500));
  146. jest.useRealTimers();
  147. const createOrg = screen.getByText('Create a new organization');
  148. expect(createOrg).toBeInTheDocument();
  149. });
  150. });
  151. describe('SidebarPanel', function () {
  152. it('hides when path changes', async function () {
  153. const {rerender} = renderSidebar({organization});
  154. await userEvent.click(await screen.findByText("What's new"));
  155. expect(await screen.findByRole('dialog')).toBeInTheDocument();
  156. expect(screen.getByText("What's new in Sentry")).toBeInTheDocument();
  157. mockUseLocation.mockReturnValue({...LocationFixture(), pathname: '/other/path'});
  158. rerender(getElement());
  159. expect(screen.queryByText("What's new in Sentry")).not.toBeInTheDocument();
  160. });
  161. it('can have onboarding feature', async function () {
  162. renderSidebar({
  163. organization: {...organization, features: ['onboarding']},
  164. });
  165. const quickStart = await screen.findByText('Quick Start');
  166. expect(quickStart).toBeInTheDocument();
  167. await userEvent.click(quickStart);
  168. expect(await screen.findByRole('dialog')).toBeInTheDocument();
  169. expect(screen.getByText('Capture your first error')).toBeInTheDocument();
  170. await userEvent.click(quickStart);
  171. expect(screen.queryByText('Capture your first error')).not.toBeInTheDocument();
  172. await tick();
  173. });
  174. it('displays empty panel when there are no Broadcasts', async function () {
  175. MockApiClient.addMockResponse({
  176. url: `/organizations/${organization.slug}/broadcasts/`,
  177. body: [],
  178. });
  179. renderSidebar({organization});
  180. await userEvent.click(await screen.findByText("What's new"));
  181. expect(await screen.findByRole('dialog')).toBeInTheDocument();
  182. expect(screen.getByText("What's new in Sentry")).toBeInTheDocument();
  183. expect(
  184. screen.getByText('No recent updates from the Sentry team.')
  185. ).toBeInTheDocument();
  186. // Close the sidebar
  187. await userEvent.click(screen.getByText("What's new"));
  188. expect(screen.queryByText("What's new in Sentry")).not.toBeInTheDocument();
  189. await tick();
  190. });
  191. it('can display Broadcasts panel and mark as seen', async function () {
  192. jest.useFakeTimers();
  193. renderSidebar({organization});
  194. expect(apiMocks.broadcasts).toHaveBeenCalled();
  195. await userEvent.click(await screen.findByText("What's new"), {delay: null});
  196. expect(await screen.findByRole('dialog')).toBeInTheDocument();
  197. expect(screen.getByText("What's new in Sentry")).toBeInTheDocument();
  198. const broadcastTitle = screen.getByText(broadcast.title);
  199. expect(broadcastTitle).toBeInTheDocument();
  200. // Should mark as seen after a delay
  201. act(() => jest.advanceTimersByTime(2000));
  202. expect(apiMocks.broadcastsMarkAsSeen).toHaveBeenCalledWith(
  203. '/broadcasts/',
  204. expect.objectContaining({
  205. data: {hasSeen: '1'},
  206. query: {id: ['8']},
  207. })
  208. );
  209. jest.useRealTimers();
  210. // Close the sidebar
  211. await userEvent.click(screen.getByText("What's new"));
  212. expect(screen.queryByText("What's new in Sentry")).not.toBeInTheDocument();
  213. await tick();
  214. });
  215. it('can unmount Sidebar (and Broadcasts) and kills Broadcast timers', async function () {
  216. jest.useFakeTimers();
  217. const {unmount} = renderSidebar({organization});
  218. // This will start timer to mark as seen
  219. await userEvent.click(await screen.findByTestId('sidebar-broadcasts'), {
  220. delay: null,
  221. });
  222. expect(await screen.findByText("What's new in Sentry")).toBeInTheDocument();
  223. act(() => jest.advanceTimersByTime(500));
  224. // Unmounting will cancel timers
  225. unmount();
  226. // This advances timers enough so that mark as seen should be called if
  227. // it wasn't unmounted
  228. act(() => jest.advanceTimersByTime(600));
  229. expect(apiMocks.broadcastsMarkAsSeen).not.toHaveBeenCalled();
  230. jest.useRealTimers();
  231. });
  232. it('can show Incidents in Sidebar Panel', async function () {
  233. renderSidebar({organization});
  234. await userEvent.click(await screen.findByText('Service status'));
  235. await screen.findByText('Recent service updates');
  236. });
  237. });
  238. it('can toggle collapsed state', async function () {
  239. renderSidebar({organization});
  240. expect(await screen.findByText(user.name)).toBeInTheDocument();
  241. expect(screen.getByText(organization.name)).toBeInTheDocument();
  242. await userEvent.click(screen.getByTestId('sidebar-collapse'));
  243. // Check that the organization name is no longer visible
  244. expect(screen.queryByText(organization.name)).not.toBeInTheDocument();
  245. // Un-collapse he sidebar and make sure the org name is visible again
  246. await userEvent.click(screen.getByTestId('sidebar-collapse'));
  247. expect(await screen.findByText(organization.name)).toBeInTheDocument();
  248. });
  249. describe('sidebar links', () => {
  250. beforeEach(function () {
  251. ConfigStore.init();
  252. ConfigStore.set('features', new Set([]));
  253. ConfigStore.set('user', user);
  254. mockUseLocation.mockReturnValue({...LocationFixture()});
  255. });
  256. it('renders navigation', async function () {
  257. renderSidebar({organization});
  258. await waitFor(function () {
  259. expect(apiMocks.broadcasts).toHaveBeenCalled();
  260. });
  261. expect(
  262. screen.getByRole('navigation', {name: 'Primary Navigation'})
  263. ).toBeInTheDocument();
  264. });
  265. it('in self-hosted-errors-only mode, only shows links to basic features', async function () {
  266. ConfigStore.set('isSelfHostedErrorsOnly', true);
  267. renderSidebarWithFeatures(ALL_AVAILABLE_FEATURES);
  268. await waitFor(function () {
  269. expect(apiMocks.broadcasts).toHaveBeenCalled();
  270. });
  271. const links = screen.getAllByRole('link');
  272. expect(links).toHaveLength(12);
  273. [
  274. 'Issues',
  275. 'Projects',
  276. 'Alerts',
  277. 'Discover',
  278. 'Dashboards',
  279. 'Releases',
  280. 'Stats',
  281. 'Settings',
  282. 'Help',
  283. /What's new/,
  284. 'Service status',
  285. ].forEach((title, index) => {
  286. expect(links[index]).toHaveAccessibleName(title);
  287. });
  288. });
  289. it('in regular mode, also shows links to Performance and Crons', async function () {
  290. localStorage.setItem('sidebar-accordion-insights:expanded', 'true');
  291. renderSidebarWithFeatures([...ALL_AVAILABLE_FEATURES]);
  292. await waitFor(function () {
  293. expect(apiMocks.broadcasts).toHaveBeenCalled();
  294. });
  295. const links = screen.getAllByRole('link');
  296. expect(links).toHaveLength(31);
  297. [
  298. 'Issues',
  299. 'Projects',
  300. /Explore/,
  301. /Traces/,
  302. /Metrics/,
  303. 'Profiles',
  304. 'Replays',
  305. 'Discover',
  306. /Insights/,
  307. 'Requests',
  308. 'Queries',
  309. 'Assets',
  310. 'App Starts',
  311. 'Screen Loads',
  312. 'Web Vitals',
  313. /Caches/,
  314. /Queues/,
  315. /Mobile UI/,
  316. /LLM Monitoring/,
  317. 'Performance',
  318. 'User Feedback',
  319. 'Crons',
  320. 'Alerts',
  321. 'Dashboards',
  322. 'Releases',
  323. 'Stats',
  324. 'Settings',
  325. 'Help',
  326. /What's new/,
  327. 'Service status',
  328. ].forEach((title, index) => {
  329. expect(links[index]).toHaveAccessibleName(title);
  330. });
  331. });
  332. it('mobile screens module hides all other mobile modules', async function () {
  333. localStorage.setItem('sidebar-accordion-insights:expanded', 'true');
  334. renderSidebarWithFeatures([
  335. 'insights-entry-points',
  336. 'starfish-mobile-ui-module',
  337. 'insights-mobile-screens-module',
  338. ]);
  339. await waitFor(function () {
  340. expect(apiMocks.broadcasts).toHaveBeenCalled();
  341. });
  342. ['App Starts', 'Screen Loads', /Mobile UI/].forEach(title => {
  343. expect(screen.queryByText(title)).not.toBeInTheDocument();
  344. });
  345. expect(screen.getByText(/Mobile Screens/)).toBeInTheDocument();
  346. });
  347. it('should not render floating accordion when expanded', async () => {
  348. renderSidebarWithFeatures(ALL_AVAILABLE_FEATURES);
  349. await userEvent.click(screen.getByTestId('sidebar-accordion-insights-item'));
  350. expect(screen.queryByTestId('floating-accordion')).not.toBeInTheDocument();
  351. });
  352. it('should render floating accordion when collapsed', async () => {
  353. renderSidebarWithFeatures(ALL_AVAILABLE_FEATURES);
  354. await userEvent.click(screen.getByTestId('sidebar-collapse'));
  355. await userEvent.click(screen.getByTestId('sidebar-accordion-insights-item'));
  356. expect(await screen.findByTestId('floating-accordion')).toBeInTheDocument();
  357. });
  358. });
  359. });