notificationSettingsByProjects.spec.tsx 2.7 KB

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