filters.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. describe('Filters', function() {
  2. let $filter;
  3. beforeEach(function() {
  4. // Load 3ema.filters module
  5. module('3ema.filters');
  6. // Inject the $filter function
  7. inject(function(_$filter_) {
  8. $filter = _$filter_;
  9. });
  10. });
  11. describe('markify', function() {
  12. this.testPatterns = (cases) => {
  13. const filter = $filter('markify');
  14. for (let testcase of cases) {
  15. const input = testcase[0];
  16. const expected = testcase[1];
  17. expect(filter(input)).toEqual(expected);
  18. };
  19. };
  20. it('detects bold text', () => {
  21. this.testPatterns([
  22. ['*bold text (not italic)*',
  23. '<span class="text-bold">bold text (not italic)</span>'],
  24. ]);
  25. });
  26. it('detects italic text', () => {
  27. this.testPatterns([
  28. ['This text is not italic.',
  29. 'This text is not italic.'],
  30. ['_This text is italic._',
  31. '<span class="text-italic">This text is italic.</span>'],
  32. ['This text is _partially_ italic',
  33. 'This text is <span class="text-italic">partially</span> italic'],
  34. ['This text has _two_ _italic_ bits',
  35. 'This text has <span class="text-italic">two</span> <span class="text-italic">italic</span> bits'],
  36. ]);
  37. });
  38. it('detects strikethrough text', () => {
  39. this.testPatterns([
  40. ['so ~strikethrough~', 'so <span class="text-strike">strikethrough</span>'],
  41. ]);
  42. });
  43. it('detects mixed markup', () => {
  44. this.testPatterns([
  45. ['*bold text with _italic_ *',
  46. '<span class="text-bold">bold text with <span class="text-italic">italic</span> </span>'],
  47. ['*part bold,* _part italic_',
  48. '<span class="text-bold">part bold,</span> <span class="text-italic">part italic</span>'],
  49. ['_italic text with *bold* _',
  50. '<span class="text-italic">italic text with <span class="text-bold">bold</span> </span>'],
  51. ]);
  52. });
  53. it('is only applied on word boundaries', () => {
  54. this.testPatterns([
  55. ['so not_really_italic',
  56. 'so not_really_italic'],
  57. ['invalid*bold*stuff',
  58. 'invalid*bold*stuff'],
  59. ['no~strike~through',
  60. 'no~strike~through'],
  61. ['*bold_but_no~strike~through*',
  62. '<span class="text-bold">bold_but_no~strike~through</span>'],
  63. ]);
  64. });
  65. it('does not break URLs', () => {
  66. this.testPatterns([
  67. ['https://en.wikipedia.org/wiki/Java_class_file *nice*',
  68. 'https://en.wikipedia.org/wiki/Java_class_file <span class="text-bold">nice</span>'],
  69. ['<a href="https://threema.ch/>_Threema_</a>',
  70. '<a href="https://threema.ch/><span class="text-italic">Threema</span></a>'],
  71. ]);
  72. });
  73. it('ignores invalid markup', () => {
  74. this.testPatterns([
  75. ['*invalid markup (do not parse)_', '*invalid markup (do not parse)_'],
  76. ['random *asterisk', 'random *asterisk'],
  77. ]);
  78. });
  79. });
  80. });