notificationSettingsByProjects.spec.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import {Project} from 'sentry/types';
  5. import NotificationSettingsByProjects from 'sentry/views/settings/account/notifications/notificationSettingsByProjects';
  6. const renderComponent = (projects: Project[]) => {
  7. const {organization} = initializeOrg();
  8. MockApiClient.addMockResponse({
  9. url: `/projects/`,
  10. method: 'GET',
  11. body: projects,
  12. });
  13. const notificationSettings = {
  14. alerts: {
  15. user: {me: {email: 'always', slack: 'always'}},
  16. project: Object.fromEntries(
  17. projects.map(project => [project.id, {email: 'never', slack: 'never'}])
  18. ),
  19. },
  20. };
  21. render(
  22. <NotificationSettingsByProjects
  23. notificationType="alerts"
  24. notificationSettings={notificationSettings}
  25. onChange={jest.fn()}
  26. onSubmitSuccess={jest.fn()}
  27. organizations={[organization]}
  28. />
  29. );
  30. };
  31. describe('NotificationSettingsByProjects', function () {
  32. afterEach(() => {
  33. MockApiClient.clearMockResponses();
  34. jest.clearAllMocks();
  35. });
  36. it('should render when there are no projects', function () {
  37. renderComponent([]);
  38. expect(screen.getByTestId('empty-message')).toHaveTextContent('No projects found');
  39. expect(screen.queryByPlaceholderText('Search Projects')).not.toBeInTheDocument();
  40. });
  41. it('should show search bar when there are enough projects', function () {
  42. const organization = TestStubs.Organization();
  43. const projects = [...Array(3).keys()].map(id =>
  44. TestStubs.Project({organization, id})
  45. );
  46. renderComponent(projects);
  47. expect(screen.getByPlaceholderText('Search Projects')).toBeInTheDocument();
  48. });
  49. it('should default to the subdomain org', async function () {
  50. const organization = TestStubs.Organization();
  51. const otherOrganization = TestStubs.Organization({
  52. id: '2',
  53. slug: 'other-org',
  54. name: 'other org',
  55. });
  56. ConfigStore.set('customerDomain', {
  57. ...ConfigStore.get('customerDomain')!,
  58. subdomain: otherOrganization.slug,
  59. });
  60. const projectsMock = MockApiClient.addMockResponse({
  61. url: '/projects/',
  62. query: {
  63. organizationId: otherOrganization.id,
  64. },
  65. method: 'GET',
  66. body: [],
  67. });
  68. render(
  69. <NotificationSettingsByProjects
  70. notificationType="alerts"
  71. notificationSettings={{}}
  72. onChange={jest.fn()}
  73. onSubmitSuccess={jest.fn()}
  74. organizations={[organization, otherOrganization]}
  75. />
  76. );
  77. expect(await screen.findByText(otherOrganization.name)).toBeInTheDocument();
  78. expect(projectsMock).toHaveBeenCalledTimes(1);
  79. });
  80. });