useCrossPlatformProject.spec.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/performance/mobile/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. placeholders: [],
  38. projects,
  39. });
  40. }
  41. describe('useCrossPlatformProject', () => {
  42. let mockProject: Project;
  43. beforeEach(() => {
  44. jest.clearAllMocks();
  45. mockProject = ProjectFixture({platform: 'flutter'});
  46. jest.mocked(useLocation).mockReturnValue(LocationFixture());
  47. mockProjects([mockProject]);
  48. });
  49. it('returns null for project if >1 project is selected', () => {
  50. mockPageFilters([1, 2, 3]);
  51. const {result} = renderHook(useCrossPlatformProject);
  52. const {project, isProjectCrossPlatform} = result.current;
  53. expect(project).toBeNull();
  54. expect(isProjectCrossPlatform).toBe(false);
  55. });
  56. it('returns the corresponding project data if a single project is selected', () => {
  57. mockPageFilters([parseInt(mockProject.id, 10)]);
  58. const {result} = renderHook(useCrossPlatformProject);
  59. const {project, isProjectCrossPlatform, selectedPlatform} = result.current;
  60. expect(project).toBe(mockProject);
  61. expect(isProjectCrossPlatform).toBe(true);
  62. // The default selection result is "Android"
  63. expect(selectedPlatform).toBe('Android');
  64. });
  65. it('returns false for isProjectCrossPlatform if project is not cross platform', () => {
  66. const testProject = ProjectFixture({platform: 'python'});
  67. mockProjects([testProject]);
  68. mockPageFilters([parseInt(testProject.id, 10)]);
  69. const {result} = renderHook(useCrossPlatformProject);
  70. const {project, isProjectCrossPlatform} = result.current;
  71. expect(project).toBe(testProject);
  72. expect(isProjectCrossPlatform).toBe(false);
  73. });
  74. });