helpers.spec.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { textToHtml, debouncedQuery } 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. })