utils.spec.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {
  2. addSpace,
  3. getLastTermIndex,
  4. getQueryTerms,
  5. removeSpace,
  6. } from 'sentry/components/smartSearchBar/utils';
  7. describe('addSpace()', function () {
  8. it('should add a space when there is no trailing space', function () {
  9. expect(addSpace('one')).toEqual('one ');
  10. });
  11. it('should not add another space when there is already one', function () {
  12. expect(addSpace('one ')).toEqual('one ');
  13. });
  14. it('should leave the empty string alone', function () {
  15. expect(addSpace('')).toEqual('');
  16. });
  17. });
  18. describe('removeSpace()', function () {
  19. it('should remove a trailing space', function () {
  20. expect(removeSpace('one ')).toEqual('one');
  21. });
  22. it('should not remove the last character if it is not a space', function () {
  23. expect(removeSpace('one')).toEqual('one');
  24. });
  25. it('should leave the empty string alone', function () {
  26. expect(removeSpace('')).toEqual('');
  27. });
  28. });
  29. describe('getQueryTerms()', function () {
  30. it('should extract query terms from a query string', function () {
  31. let query = 'tagname: ';
  32. expect(getQueryTerms(query, query.length)).toEqual(['tagname:']);
  33. query = 'tagname:derp browser:';
  34. expect(getQueryTerms(query, query.length)).toEqual(['tagname:derp', 'browser:']);
  35. query = ' browser:"Chrome 33.0" ';
  36. expect(getQueryTerms(query, query.length)).toEqual(['browser:"Chrome 33.0"']);
  37. });
  38. });
  39. describe('getLastTermIndex()', function () {
  40. it('should provide the index of the last query term, given cursor index', function () {
  41. let query = 'tagname:';
  42. expect(getLastTermIndex(query, 0)).toEqual(8);
  43. query = 'tagname:foo'; // 'f' (index 9)
  44. expect(getLastTermIndex(query, 9)).toEqual(11);
  45. query = 'tagname:foo anothertag:bar'; // 'f' (index 9)
  46. expect(getLastTermIndex(query, 9)).toEqual(11);
  47. });
  48. });