globalSelectionLink.spec.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {router} from 'fixtures/js-stubs/router';
  2. import {routerContext} from 'fixtures/js-stubs/routerContext';
  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. routerContext([
  9. {
  10. router: router({
  11. location: {query},
  12. }),
  13. },
  14. ]);
  15. it('has global selection values in query', function () {
  16. const query = {
  17. project: ['foo', 'bar'],
  18. environment: 'staging',
  19. };
  20. const context = getContext(query);
  21. const {container} = render(
  22. <GlobalSelectionLink to={path}>Go somewhere!</GlobalSelectionLink>,
  23. {context}
  24. );
  25. expect(container).toSnapshot();
  26. expect(screen.getByText('Go somewhere!')).toHaveAttribute(
  27. 'href',
  28. 'http://some.url/?environment=staging&project=foo&project=bar'
  29. );
  30. userEvent.click(screen.getByText('Go somewhere!'));
  31. expect(context.context.router.push).toHaveBeenCalledWith({pathname: path, query});
  32. });
  33. it('does not have global selection values in query', function () {
  34. const {container} = render(
  35. <GlobalSelectionLink to={path}>Go somewhere!</GlobalSelectionLink>
  36. );
  37. expect(screen.getByText('Go somewhere!')).toHaveAttribute('href', path);
  38. expect(container).toSnapshot();
  39. });
  40. it('combines query parameters with custom query', function () {
  41. const query = {
  42. project: ['foo', 'bar'],
  43. environment: 'staging',
  44. };
  45. const context = getContext(query);
  46. const customQuery = {query: 'something'};
  47. render(
  48. <GlobalSelectionLink to={{pathname: path, query: customQuery}}>
  49. Go somewhere!
  50. </GlobalSelectionLink>,
  51. {context}
  52. );
  53. userEvent.click(screen.getByText('Go somewhere!'));
  54. expect(context.context.router.push).toHaveBeenCalledWith({
  55. pathname: path,
  56. query: {project: ['foo', 'bar'], environment: 'staging', query: 'something'},
  57. });
  58. });
  59. it('combines query parameters with no query', function () {
  60. const query = {
  61. project: ['foo', 'bar'],
  62. environment: 'staging',
  63. };
  64. const context = getContext(query);
  65. render(
  66. <GlobalSelectionLink to={{pathname: path}}>Go somewhere!</GlobalSelectionLink>,
  67. {context}
  68. );
  69. userEvent.click(screen.getByText('Go somewhere!'));
  70. expect(context.context.router.push).toHaveBeenCalledWith({pathname: path, query});
  71. });
  72. });