index.spec.tsx 14 KB

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