index.spec.tsx 14 KB

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