globalSelectionLink.spec.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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', async 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. await 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. {context: getContext()}
  35. );
  36. expect(screen.getByText('Go somewhere!')).toHaveAttribute('href', path);
  37. expect(container).toSnapshot();
  38. });
  39. it('combines query parameters with custom query', async function () {
  40. const query = {
  41. project: ['foo', 'bar'],
  42. environment: 'staging',
  43. };
  44. const context = getContext(query);
  45. const customQuery = {query: 'something'};
  46. render(
  47. <GlobalSelectionLink to={{pathname: path, query: customQuery}}>
  48. Go somewhere!
  49. </GlobalSelectionLink>,
  50. {context}
  51. );
  52. await userEvent.click(screen.getByText('Go somewhere!'));
  53. expect(context.context.router.push).toHaveBeenCalledWith({
  54. pathname: path,
  55. query: {project: ['foo', 'bar'], environment: 'staging', query: 'something'},
  56. });
  57. });
  58. it('combines query parameters with no query', async function () {
  59. const query = {
  60. project: ['foo', 'bar'],
  61. environment: 'staging',
  62. };
  63. const context = getContext(query);
  64. render(
  65. <GlobalSelectionLink to={{pathname: path}}>Go somewhere!</GlobalSelectionLink>,
  66. {context}
  67. );
  68. await userEvent.click(screen.getByText('Go somewhere!'));
  69. expect(context.context.router.push).toHaveBeenCalledWith({pathname: path, query});
  70. });
  71. });