realtime.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import sinon from 'sinon'
  2. import { expect } from 'chai'
  3. import { EditorFacade } from '../../../../../frontend/js/features/source-editor/extensions/realtime'
  4. import { EditorView } from '@codemirror/view'
  5. import { EditorState } from '@codemirror/state'
  6. describe('CodeMirror EditorFacade', function () {
  7. let state: EditorState, view: EditorView
  8. beforeEach(function () {
  9. state = EditorState.create()
  10. view = new EditorView({ state })
  11. })
  12. it('should allow us to manipulate the CodeMirror document', function () {
  13. const editor = new EditorFacade(view)
  14. const text = 'basic test, nothing more'
  15. editor.cmInsert(0, text)
  16. expect(editor.getValue()).to.equal(text)
  17. editor.cmDelete(0, 'b')
  18. expect(editor.getValue()).to.equal(text.slice(1))
  19. })
  20. it('should allow us to attach change listeners', function () {
  21. const editor = new EditorFacade(view)
  22. const listenerA = sinon.stub()
  23. const listenerB = sinon.stub()
  24. editor.on('change', listenerA)
  25. editor.on('change', listenerB)
  26. expect(listenerA).to.not.have.been.called
  27. expect(listenerB).to.not.have.been.called
  28. const magicNumber = Math.random()
  29. editor.emit('change', magicNumber)
  30. expect(listenerA).to.have.been.calledWith(magicNumber)
  31. expect(listenerB).to.have.been.calledWith(magicNumber)
  32. })
  33. it('should attach to ShareJs document', function () {
  34. const editor = new EditorFacade(view)
  35. const text = 'something nice'
  36. const shareDoc = {
  37. on: sinon.stub(),
  38. getText: sinon.stub().returns(text),
  39. removeListener: sinon.stub(),
  40. detach_cm6: undefined,
  41. }
  42. editor.cmInsert(0, text)
  43. // @ts-ignore
  44. editor.attachShareJs(shareDoc)
  45. expect(shareDoc.on.callCount).to.equal(2)
  46. expect(shareDoc.on).to.have.been.calledWith('insert')
  47. expect(shareDoc.on).to.have.been.calledWith('delete')
  48. expect(shareDoc.detach_cm6).to.be.a('function')
  49. })
  50. })