helpers.spec.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { textToHtml, debouncedQuery, findChangedIndex } from '../helpers.ts'
  3. describe('textToHtml', () => {
  4. it('adds links to URL-like text', () => {
  5. const input = 'Some Text\n\nhttp://example.com'
  6. const output =
  7. '<div>Some Text</div><div><br></div><div><a href="http://example.com">http://example.com</a></div>'
  8. expect(textToHtml(input)).toBe(output)
  9. })
  10. it('escapes HTML-like text to make sure it is presented as-is', () => {
  11. const input = '<p>&It;div&gt;hello world&lt;/div&gt;</p>'
  12. const output =
  13. '<div>&lt;p&gt;&amp;It;div&amp;gt;hello world&amp;lt;/div&amp;gt;&lt;/p&gt;</div>'
  14. expect(textToHtml(input)).toBe(output)
  15. })
  16. })
  17. describe('debouncedQuery', () => {
  18. it('returns values correctly', async () => {
  19. let i = 0
  20. const fn = debouncedQuery(async () => {
  21. i += 1
  22. return i
  23. }, 0)
  24. const res1 = fn()
  25. const res2 = fn()
  26. const res3 = fn()
  27. // cancels the first two calls, and returns default value in that case
  28. expect(await res1).toBe(0)
  29. expect(await res2).toBe(0)
  30. expect(await res3).toBe(1)
  31. const res4 = fn()
  32. const res5 = fn()
  33. // cancels the first call, and returns the last value in that case
  34. expect(await res4).toBe(1)
  35. expect(await res5).toBe(2)
  36. })
  37. })
  38. describe('findChangedIndex', () => {
  39. it('returns the index of the first changed item', () => {
  40. const a = [1, 2, 3, 4, 5]
  41. const b = [1, 2, 3, 5, 5]
  42. expect(findChangedIndex(a, b)).toBe(3)
  43. const c = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }]
  44. const d = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 5 }, { id: 5 }]
  45. expect(findChangedIndex(c, d)).toBe(3)
  46. const e = [[1], [2], [3], [4], [5]]
  47. const f = [[1], [2], [3], [5], [5]]
  48. expect(findChangedIndex(e, f)).toBe(3)
  49. })
  50. it('returns -1 if no item changed', () => {
  51. const a = [1, 2, 3, 4, 5]
  52. const b = [1, 2, 3, 4, 5]
  53. expect(findChangedIndex(a, b)).toBe(-1)
  54. })
  55. })