utils.spec.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import {
  2. addSpace,
  3. getLastTermIndex,
  4. getQueryTerms,
  5. getTagItemsFromKeys,
  6. removeSpace,
  7. } from 'sentry/components/smartSearchBar/utils';
  8. import {FieldValueKind} from 'sentry/views/eventsV2/table/types';
  9. describe('addSpace()', function () {
  10. it('should add a space when there is no trailing space', function () {
  11. expect(addSpace('one')).toEqual('one ');
  12. });
  13. it('should not add another space when there is already one', function () {
  14. expect(addSpace('one ')).toEqual('one ');
  15. });
  16. it('should leave the empty string alone', function () {
  17. expect(addSpace('')).toEqual('');
  18. });
  19. });
  20. describe('removeSpace()', function () {
  21. it('should remove a trailing space', function () {
  22. expect(removeSpace('one ')).toEqual('one');
  23. });
  24. it('should not remove the last character if it is not a space', function () {
  25. expect(removeSpace('one')).toEqual('one');
  26. });
  27. it('should leave the empty string alone', function () {
  28. expect(removeSpace('')).toEqual('');
  29. });
  30. });
  31. describe('getQueryTerms()', function () {
  32. it('should extract query terms from a query string', function () {
  33. let query = 'tagname: ';
  34. expect(getQueryTerms(query, query.length)).toEqual(['tagname:']);
  35. query = 'tagname:derp browser:';
  36. expect(getQueryTerms(query, query.length)).toEqual(['tagname:derp', 'browser:']);
  37. query = ' browser:"Chrome 33.0" ';
  38. expect(getQueryTerms(query, query.length)).toEqual(['browser:"Chrome 33.0"']);
  39. });
  40. });
  41. describe('getLastTermIndex()', function () {
  42. it('should provide the index of the last query term, given cursor index', function () {
  43. let query = 'tagname:';
  44. expect(getLastTermIndex(query, 0)).toEqual(8);
  45. query = 'tagname:foo'; // 'f' (index 9)
  46. expect(getLastTermIndex(query, 9)).toEqual(11);
  47. query = 'tagname:foo anothertag:bar'; // 'f' (index 9)
  48. expect(getLastTermIndex(query, 9)).toEqual(11);
  49. });
  50. });
  51. describe('getTagItemsFromKeys()', function () {
  52. it('gets items from tags', () => {
  53. const supportedTags = {
  54. browser: {
  55. kind: FieldValueKind.FIELD,
  56. key: 'browser',
  57. name: 'Browser',
  58. predefined: true,
  59. desc: '',
  60. values: [],
  61. },
  62. device: {
  63. kind: FieldValueKind.FIELD,
  64. key: 'device',
  65. name: 'Device',
  66. predefined: true,
  67. desc: '',
  68. values: [],
  69. },
  70. has: {
  71. kind: FieldValueKind.TAG,
  72. key: 'has',
  73. name: 'Has',
  74. predefined: true,
  75. desc: '',
  76. values: [],
  77. },
  78. };
  79. const tagKeys = Object.keys(supportedTags);
  80. const items = getTagItemsFromKeys(tagKeys, supportedTags);
  81. expect(items).toMatchObject([
  82. {
  83. title: 'browser',
  84. value: 'browser:',
  85. kind: FieldValueKind.FIELD,
  86. documentation: '-',
  87. },
  88. {
  89. title: 'device',
  90. value: 'device:',
  91. kind: FieldValueKind.FIELD,
  92. documentation: '-',
  93. },
  94. {
  95. title: 'has',
  96. value: 'has:',
  97. kind: FieldValueKind.TAG,
  98. documentation: '-',
  99. },
  100. ]);
  101. });
  102. it('groups tags', () => {
  103. const supportedTags = {
  104. 'device.arch': {
  105. kind: FieldValueKind.FIELD,
  106. key: 'device.arch',
  107. name: 'Device Arch',
  108. predefined: true,
  109. desc: '',
  110. values: [],
  111. },
  112. 'device.family': {
  113. kind: FieldValueKind.FIELD,
  114. key: 'device.family',
  115. name: 'Device Family',
  116. predefined: true,
  117. desc: '',
  118. values: [],
  119. },
  120. has: {
  121. kind: FieldValueKind.TAG,
  122. key: 'has',
  123. name: 'Has',
  124. predefined: true,
  125. desc: '',
  126. values: [],
  127. },
  128. };
  129. const tagKeys = Object.keys(supportedTags);
  130. const items = getTagItemsFromKeys(tagKeys, supportedTags);
  131. expect(items).toMatchObject([
  132. {
  133. title: 'device',
  134. value: null,
  135. kind: FieldValueKind.FIELD,
  136. documentation: '-',
  137. children: [
  138. {
  139. title: 'device.arch',
  140. value: 'device.arch:',
  141. kind: FieldValueKind.FIELD,
  142. documentation: '-',
  143. },
  144. {
  145. title: 'device.family',
  146. value: 'device.family:',
  147. kind: FieldValueKind.FIELD,
  148. documentation: '-',
  149. },
  150. ],
  151. },
  152. {
  153. title: 'has',
  154. value: 'has:',
  155. kind: FieldValueKind.TAG,
  156. documentation: '-',
  157. },
  158. ]);
  159. });
  160. it('groups tags with single word parent', () => {
  161. const supportedTags = {
  162. device: {
  163. kind: FieldValueKind.FIELD,
  164. key: 'device',
  165. name: 'Device',
  166. predefined: true,
  167. desc: '',
  168. values: [],
  169. },
  170. 'device.family': {
  171. kind: FieldValueKind.FIELD,
  172. key: 'device.family',
  173. name: 'Device Family',
  174. predefined: true,
  175. desc: '',
  176. values: [],
  177. },
  178. has: {
  179. kind: FieldValueKind.TAG,
  180. key: 'has',
  181. name: 'Has',
  182. predefined: true,
  183. desc: '',
  184. values: [],
  185. },
  186. };
  187. const tagKeys = Object.keys(supportedTags);
  188. const items = getTagItemsFromKeys(tagKeys, supportedTags);
  189. expect(items).toMatchObject([
  190. {
  191. title: 'device',
  192. value: 'device:',
  193. kind: FieldValueKind.FIELD,
  194. documentation: '-',
  195. children: [
  196. {
  197. title: 'device.family',
  198. value: 'device.family:',
  199. kind: FieldValueKind.FIELD,
  200. documentation: '-',
  201. },
  202. ],
  203. },
  204. {
  205. title: 'has',
  206. value: 'has:',
  207. kind: FieldValueKind.TAG,
  208. documentation: '-',
  209. },
  210. ]);
  211. });
  212. });