globalSelectionLink.spec.tsx 2.4 KB

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