useCrossPlatformProject.spec.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {renderHook} from 'sentry-test/reactTestingLibrary';
  4. import type {Project} from 'sentry/types/project';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import useProjects from 'sentry/utils/useProjects';
  8. import useCrossPlatformProject from 'sentry/views/insights/mobile/common/queries/useCrossPlatformProject';
  9. jest.mock('sentry/utils/usePageFilters');
  10. jest.mock('sentry/utils/useLocation');
  11. jest.mock('sentry/utils/useProjects');
  12. function mockPageFilters(projects: number[]) {
  13. jest.mocked(usePageFilters).mockReturnValue({
  14. isReady: true,
  15. desyncedFilters: new Set(),
  16. pinnedFilters: new Set(),
  17. shouldPersist: true,
  18. selection: {
  19. datetime: {
  20. period: '10d',
  21. start: null,
  22. end: null,
  23. utc: false,
  24. },
  25. environments: [],
  26. projects,
  27. },
  28. });
  29. }
  30. function mockProjects(projects: Project[]) {
  31. jest.mocked(useProjects).mockReturnValue({
  32. fetchError: null,
  33. fetching: false,
  34. hasMore: false,
  35. initiallyLoaded: false,
  36. onSearch: jest.fn(),
  37. reloadProjects: jest.fn(),
  38. placeholders: [],
  39. projects,
  40. });
  41. }
  42. describe('useCrossPlatformProject', () => {
  43. let mockProject: Project;
  44. beforeEach(() => {
  45. jest.clearAllMocks();
  46. mockProject = ProjectFixture({platform: 'flutter'});
  47. jest.mocked(useLocation).mockReturnValue(LocationFixture());
  48. mockProjects([mockProject]);
  49. });
  50. it('returns null for project if >1 project is selected', () => {
  51. mockPageFilters([1, 2, 3]);
  52. const {result} = renderHook(useCrossPlatformProject);
  53. const {project, isProjectCrossPlatform} = result.current;
  54. expect(project).toBeNull();
  55. expect(isProjectCrossPlatform).toBe(false);
  56. });
  57. it('returns the corresponding project data if a single project is selected', () => {
  58. mockPageFilters([parseInt(mockProject.id, 10)]);
  59. const {result} = renderHook(useCrossPlatformProject);
  60. const {project, isProjectCrossPlatform, selectedPlatform} = result.current;
  61. expect(project).toBe(mockProject);
  62. expect(isProjectCrossPlatform).toBe(true);
  63. // The default selection result is "Android"
  64. expect(selectedPlatform).toBe('Android');
  65. });
  66. it('returns false for isProjectCrossPlatform if project is not cross platform', () => {
  67. const testProject = ProjectFixture({platform: 'python'});
  68. mockProjects([testProject]);
  69. mockPageFilters([parseInt(testProject.id, 10)]);
  70. const {result} = renderHook(useCrossPlatformProject);
  71. const {project, isProjectCrossPlatform} = result.current;
  72. expect(project).toBe(testProject);
  73. expect(isProjectCrossPlatform).toBe(false);
  74. });
  75. });