選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HomePageSelect-test.tsx 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { screen } from '@testing-library/react';
  21. import * as React from 'react';
  22. import { setHomePage } from '../../../api/users';
  23. import { mockLoggedInUser } from '../../../helpers/testMocks';
  24. import { renderComponent } from '../../../helpers/testReactTestingUtils';
  25. import { DEFAULT_HOMEPAGE, HomePageSelect } from '../HomePageSelect';
  26. jest.mock('../../../api/users', () => ({
  27. setHomePage: jest.fn().mockResolvedValue(null)
  28. }));
  29. it('renders and behaves correctly', async () => {
  30. const updateCurrentUserHomepage = jest.fn();
  31. renderHomePageSelect({ updateCurrentUserHomepage });
  32. const button = screen.getByRole('button');
  33. expect(button).toBeInTheDocument();
  34. button.click();
  35. await new Promise(setImmediate);
  36. expect(setHomePage).toHaveBeenCalledWith({ type: 'MY_PROJECTS' });
  37. expect(updateCurrentUserHomepage).toHaveBeenCalled();
  38. expect(button).toHaveFocus();
  39. });
  40. it('renders correctly if user is on the homepage', async () => {
  41. renderHomePageSelect({ currentUser: mockLoggedInUser({ homepage: { type: 'MY_PROJECTS' } }) });
  42. const button = screen.getByRole('button');
  43. expect(button).toBeInTheDocument();
  44. button.click();
  45. await new Promise(setImmediate);
  46. expect(setHomePage).toHaveBeenCalledWith(DEFAULT_HOMEPAGE);
  47. expect(button).toHaveFocus();
  48. });
  49. function renderHomePageSelect(props: Partial<HomePageSelect['props']> = {}) {
  50. return renderComponent(
  51. <HomePageSelect
  52. currentPage={{ type: 'MY_PROJECTS' }}
  53. currentUser={mockLoggedInUser()}
  54. updateCurrentUserHomepage={jest.fn()}
  55. {...props}
  56. />
  57. );
  58. }