utils.spec.tsx 6.7 KB

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