Context.spec.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Context.spec.js
  3. * (c) 2015~ Summernote Team
  4. * summernote may be freely distributed under the MIT license./
  5. */
  6. import chai from 'chai';
  7. import spies from 'chai-spies';
  8. /* eslint-disable import/first */
  9. import $ from 'jquery'; window.jQuery = $;
  10. import 'bootstrap';
  11. import chaidom from '../../chaidom';
  12. import env from '../../../src/js/base/core/env';
  13. import Context from '../../../src/js/base/Context';
  14. /* eslint-enable import/first */
  15. var expect = chai.expect;
  16. chai.use(spies);
  17. chai.use(chaidom);
  18. describe('Context lifecycle', () => {
  19. it('should be initialized without calling callback', () => {
  20. var options = $.extend({}, $.summernote.options);
  21. options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]);
  22. var spy = chai.spy();
  23. var $note = $('<div><p>hello</p></div>');
  24. $note.on('summernote.change', spy);
  25. var context = new Context($note, options);
  26. expect(spy).to.have.not.been.called();
  27. // [workaround]
  28. // - IE8-11 can't create range in headless mode
  29. if (!env.isMSIE) {
  30. context.invoke('insertText', 'hello');
  31. expect(spy).to.have.been.called();
  32. }
  33. });
  34. it('should preserve user events handler after destroy', () => {
  35. var options = $.extend({}, $.summernote.options);
  36. options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]);
  37. var spy = chai.spy();
  38. var $note = $('<div><p>hello</p></div>');
  39. $note.on('click', spy);
  40. var context = new Context($note, options);
  41. context.destroy();
  42. $note.trigger('click');
  43. expect(spy).to.have.been.called();
  44. });
  45. });
  46. describe('Context', () => {
  47. var context;
  48. beforeEach(() => {
  49. var options = $.extend({}, $.summernote.options);
  50. options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]);
  51. context = new Context($('<div><p>hello</p></div>'), options);
  52. });
  53. it('should get or set contents with code', () => {
  54. expect(context.code()).to.equalsIgnoreCase('<p>hello</p>');
  55. context.code('<p>hello2</p>');
  56. expect(context.code()).to.equalsIgnoreCase('<p>hello2</p>');
  57. });
  58. it('should enable or disable editor', () => {
  59. expect(context.isDisabled()).to.be.false;
  60. context.disable();
  61. expect(context.isDisabled()).to.be.true;
  62. context.enable();
  63. expect(context.isDisabled()).to.be.false;
  64. });
  65. it('should preserve disabled status after reset', () => {
  66. expect(context.isDisabled()).to.be.false;
  67. context.disable();
  68. expect(context.isDisabled()).to.be.true;
  69. context.reset();
  70. expect(context.isDisabled()).to.be.true;
  71. });
  72. });