showdown.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. require('source-map-support').install();
  2. require('chai').should();
  3. var expect = require('chai').expect,
  4. showdown = require('../../.build/showdown.js');
  5. describe('showdown.options', function () {
  6. 'use strict';
  7. describe('setOption() and getOption()', function () {
  8. it('should set option foo=bar', function () {
  9. showdown.setOption('foo', 'bar');
  10. showdown.getOption('foo').should.equal('bar');
  11. showdown.resetOptions();
  12. (typeof showdown.getOption('foo')).should.equal('undefined');
  13. });
  14. });
  15. describe('getDefaultOptions()', function () {
  16. it('should get default options', function () {
  17. var opts = require('./optionswp.js').getDefaultOpts(true);
  18. expect(showdown.getDefaultOptions()).to.be.eql(opts);
  19. });
  20. });
  21. });
  22. describe('showdown.extension()', function () {
  23. 'use strict';
  24. var extObjMock = {
  25. type: 'lang',
  26. filter: function () {}
  27. },
  28. extObjFunc = function () {
  29. return extObjMock;
  30. };
  31. describe('should register', function () {
  32. it('an extension object', function () {
  33. showdown.extension('foo', extObjMock);
  34. showdown.extension('foo').should.eql([extObjMock]);
  35. showdown.resetExtensions();
  36. });
  37. it('an extension function', function () {
  38. showdown.extension('foo', extObjFunc);
  39. showdown.extension('foo').should.eql([extObjMock]);
  40. showdown.resetExtensions();
  41. });
  42. it('a listener extension', function () {
  43. showdown.extension('foo', {
  44. type: 'listener',
  45. listeners: {
  46. foo: function (name, txt) {
  47. return txt;
  48. }
  49. }
  50. });
  51. showdown.resetExtensions();
  52. });
  53. });
  54. describe('should refuse to register', function () {
  55. it('a generic object', function () {
  56. var fn = function () {
  57. showdown.extension('foo', {});
  58. };
  59. expect(fn).to.throw();
  60. });
  61. it('an extension with invalid type', function () {
  62. var fn = function () {
  63. showdown.extension('foo', {
  64. type: 'foo'
  65. });
  66. };
  67. expect(fn).to.throw(/type .+? is not recognized\. Valid values: "lang\/language", "output\/html" or "listener"/);
  68. });
  69. it('an extension without regex or filter', function () {
  70. var fn = function () {
  71. showdown.extension('foo', {
  72. type: 'lang'
  73. });
  74. };
  75. expect(fn).to.throw(/extensions must define either a "regex" property or a "filter" method/);
  76. });
  77. it('a listener extension without a listeners property', function () {
  78. var fn = function () {
  79. showdown.extension('foo', {
  80. type: 'listener'
  81. });
  82. };
  83. expect(fn).to.throw(/Extensions of type "listener" must have a property called "listeners"/);
  84. });
  85. });
  86. });
  87. describe('showdown.getAllExtensions()', function () {
  88. 'use strict';
  89. var extObjMock = {
  90. type: 'lang',
  91. filter: function () {}
  92. };
  93. it('should return all extensions', function () {
  94. showdown.extension('bar', extObjMock);
  95. showdown.getAllExtensions().should.eql({bar: [extObjMock]});
  96. });
  97. });
  98. describe('showdown.setFlavor()', function () {
  99. 'use strict';
  100. it('should set flavor to github', function () {
  101. showdown.setFlavor('github');
  102. showdown.getFlavor().should.equal('github');
  103. showdown.setFlavor('vanilla');
  104. });
  105. it('should set options correctly', function () {
  106. showdown.setFlavor('github');
  107. var ghOpts = showdown.getFlavorOptions('github'),
  108. shOpts = showdown.getOptions();
  109. for (var opt in ghOpts) {
  110. if (ghOpts.hasOwnProperty(opt)) {
  111. shOpts.should.have.property(opt);
  112. shOpts[opt].should.equal(ghOpts[opt]);
  113. }
  114. }
  115. showdown.setFlavor('vanilla');
  116. });
  117. it('should switch between flavors correctly', function () {
  118. showdown.setFlavor('github');
  119. var ghOpts = showdown.getFlavorOptions('github'),
  120. shOpts = showdown.getOptions(),
  121. dfOpts = showdown.getDefaultOptions();
  122. for (var opt in dfOpts) {
  123. if (ghOpts.hasOwnProperty(opt)) {
  124. shOpts[opt].should.equal(ghOpts[opt]);
  125. } else {
  126. shOpts[opt].should.equal(dfOpts[opt]);
  127. }
  128. }
  129. showdown.setFlavor('original');
  130. var orOpts = showdown.getFlavorOptions('original');
  131. shOpts = showdown.getOptions();
  132. for (opt in dfOpts) {
  133. if (orOpts.hasOwnProperty(opt)) {
  134. shOpts[opt].should.equal(orOpts[opt]);
  135. } else {
  136. shOpts[opt].should.equal(dfOpts[opt]);
  137. }
  138. }
  139. showdown.setFlavor('vanilla');
  140. });
  141. });