globalSelectionLink.spec.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {RouterContextFixture} from 'sentry-fixture/routerContextFixture';
  2. import {RouterFixture} from 'sentry-fixture/routerFixture';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import GlobalSelectionLink from 'sentry/components/globalSelectionLink';
  5. const path = 'http://some.url/';
  6. describe('GlobalSelectionLink', function () {
  7. const getContext = (query?: {environment: string; project: string[]}) =>
  8. RouterContextFixture([
  9. {
  10. router: RouterFixture({
  11. location: {query},
  12. }),
  13. },
  14. ]);
  15. it('has global selection values in query', async function () {
  16. const query = {
  17. project: ['foo', 'bar'],
  18. environment: 'staging',
  19. };
  20. const context = getContext(query);
  21. render(<GlobalSelectionLink to={path}>Go somewhere!</GlobalSelectionLink>, {context});
  22. expect(screen.getByText('Go somewhere!')).toHaveAttribute(
  23. 'href',
  24. 'http://some.url/?environment=staging&project=foo&project=bar'
  25. );
  26. await userEvent.click(screen.getByText('Go somewhere!'));
  27. expect(context.context.router.push).toHaveBeenCalledWith({pathname: path, query});
  28. });
  29. it('does not have global selection values in query', function () {
  30. render(<GlobalSelectionLink to={path}>Go somewhere!</GlobalSelectionLink>, {
  31. context: getContext(),
  32. });
  33. expect(screen.getByText('Go somewhere!')).toHaveAttribute('href', path);
  34. });
  35. it('combines query parameters with custom query', async function () {
  36. const query = {
  37. project: ['foo', 'bar'],
  38. environment: 'staging',
  39. };
  40. const context = getContext(query);
  41. const customQuery = {query: 'something'};
  42. render(
  43. <GlobalSelectionLink to={{pathname: path, query: customQuery}}>
  44. Go somewhere!
  45. </GlobalSelectionLink>,
  46. {context}
  47. );
  48. await userEvent.click(screen.getByText('Go somewhere!'));
  49. expect(context.context.router.push).toHaveBeenCalledWith({
  50. pathname: path,
  51. query: {project: ['foo', 'bar'], environment: 'staging', query: 'something'},
  52. });
  53. });
  54. it('combines query parameters with no query', async function () {
  55. const query = {
  56. project: ['foo', 'bar'],
  57. environment: 'staging',
  58. };
  59. const context = getContext(query);
  60. render(
  61. <GlobalSelectionLink to={{pathname: path}}>Go somewhere!</GlobalSelectionLink>,
  62. {context}
  63. );
  64. await userEvent.click(screen.getByText('Go somewhere!'));
  65. expect(context.context.router.push).toHaveBeenCalledWith({pathname: path, query});
  66. });
  67. });