utils.spec.tsx 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {UserFixture} from 'sentry-fixture/user';
  4. import {act, renderHook} from 'sentry-test/reactTestingLibrary';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import {useHasStreamlinedUI} from 'sentry/views/issueDetails/utils';
  9. jest.mock('sentry/utils/useLocation');
  10. jest.mock('sentry/utils/useOrganization');
  11. describe('useHasStreamlinedUI', () => {
  12. it("respects the 'streamline' query param", () => {
  13. jest.mocked(useOrganization).mockReturnValue(OrganizationFixture());
  14. const location = LocationFixture({query: {streamline: '1'}});
  15. jest.mocked(useLocation).mockReturnValue(location);
  16. const {result: queryParamEnabled} = renderHook(useHasStreamlinedUI);
  17. expect(queryParamEnabled.current).toBe(true);
  18. location.query.streamline = '0';
  19. jest.mocked(useLocation).mockReturnValue(location);
  20. const {result: queryParamDisabled} = renderHook(useHasStreamlinedUI);
  21. expect(queryParamDisabled.current).toBe(false);
  22. });
  23. it('respects the user preferences', () => {
  24. ConfigStore.init();
  25. const user = UserFixture();
  26. user.options.prefersIssueDetailsStreamlinedUI = true;
  27. act(() => ConfigStore.set('user', user));
  28. jest.mocked(useLocation).mockReturnValue(LocationFixture());
  29. const {result: userPrefersStreamline} = renderHook(useHasStreamlinedUI);
  30. expect(userPrefersStreamline.current).toBe(true);
  31. user.options.prefersIssueDetailsStreamlinedUI = false;
  32. act(() => ConfigStore.set('user', user));
  33. const {result: userPrefersLegacy} = renderHook(useHasStreamlinedUI);
  34. expect(userPrefersLegacy.current).toBe(false);
  35. });
  36. it('values query param above user preferences and organization flags', () => {
  37. const enforceOrg = OrganizationFixture({
  38. features: ['issue-details-streamline-enforce'],
  39. });
  40. jest.mocked(useOrganization).mockReturnValue(enforceOrg);
  41. ConfigStore.init();
  42. const user = UserFixture();
  43. user.options.prefersIssueDetailsStreamlinedUI = false;
  44. act(() => ConfigStore.set('user', user));
  45. const location = LocationFixture({query: {streamline: '1'}});
  46. jest.mocked(useLocation).mockReturnValue(location);
  47. const {result: prefersLegacyButQueryParamEnabled} = renderHook(useHasStreamlinedUI);
  48. expect(prefersLegacyButQueryParamEnabled.current).toBe(true);
  49. user.options.prefersIssueDetailsStreamlinedUI = true;
  50. act(() => ConfigStore.set('user', user));
  51. location.query.streamline = '0';
  52. const {result: prefersStreamlineButQueryParamDisabled} =
  53. renderHook(useHasStreamlinedUI);
  54. expect(prefersStreamlineButQueryParamDisabled.current).toBe(false);
  55. });
  56. it('ignores preferences if enforce flag is set', () => {
  57. const enforceOrg = OrganizationFixture({
  58. features: ['issue-details-streamline-enforce'],
  59. });
  60. jest.mocked(useOrganization).mockReturnValue(enforceOrg);
  61. ConfigStore.init();
  62. const user = UserFixture();
  63. user.options.prefersIssueDetailsStreamlinedUI = false;
  64. act(() => ConfigStore.set('user', user));
  65. jest.mocked(useLocation).mockReturnValue(LocationFixture());
  66. const {result} = renderHook(useHasStreamlinedUI);
  67. expect(result.current).toBe(true);
  68. });
  69. });