utils.spec.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {
  2. descopeFeatureName,
  3. escapeDoubleQuotes,
  4. explodeSlug,
  5. extractMultilineFields,
  6. } from 'sentry/utils';
  7. describe('utils.extractMultilineFields', function () {
  8. it('should work for basic, simple values', function () {
  9. expect(extractMultilineFields('one\ntwo\nthree')).toEqual(['one', 'two', 'three']);
  10. });
  11. it('should return an empty array if only whitespace', function () {
  12. expect(extractMultilineFields(' \n \n\n\n \n')).toEqual([]);
  13. });
  14. it('should trim values and ignore empty lines', function () {
  15. expect(
  16. extractMultilineFields(
  17. `one
  18. two
  19. three
  20. four
  21. five`
  22. )
  23. ).toEqual(['one', 'two', 'three', 'four', 'five']);
  24. });
  25. });
  26. describe('utils.explodeSlug', function () {
  27. it('replaces slug special chars with whitespace', function () {
  28. expect(explodeSlug('test--slug__replace-')).toEqual('test slug replace');
  29. });
  30. });
  31. describe('utils.descopeFeatureName', function () {
  32. it('descopes the feature name', () => {
  33. [
  34. ['organizations:feature', 'feature'],
  35. ['projects:feature', 'feature'],
  36. ['unknown-scope:feature', 'unknown-scope:feature'],
  37. ['', ''],
  38. ].forEach(([input, expected]) => expect(descopeFeatureName(input)).toEqual(expected));
  39. });
  40. });
  41. describe('utils.escapeDoubleQuotes', function () {
  42. // test cases from https://gist.github.com/getify/3667624
  43. it('should escape any unescaped double quotes', function () {
  44. const cases = [
  45. ['a"b', 'a\\"b'], //
  46. ['a\\"b', 'a\\"b'], //
  47. ['a\\\\"b', 'a\\\\\\"b'],
  48. ['a"b"c', 'a\\"b\\"c'],
  49. ['a""b', 'a\\"\\"b'],
  50. ['""', '\\"\\"'],
  51. ];
  52. for (const testCase of cases) {
  53. const [input, expected] = testCase;
  54. expect(escapeDoubleQuotes(input)).toBe(expected);
  55. }
  56. // should return the same input as the output
  57. const cases2 = ['ab', 'a\\"b', 'a\\\\\\"b'];
  58. for (const test of cases2) {
  59. expect(escapeDoubleQuotes(test)).toBe(test);
  60. }
  61. // don't unnecessarily escape
  62. const actual = escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes('a"b')));
  63. expect(actual).toBe('a\\"b');
  64. });
  65. });