bobbyrne01
bobbyrne01

Reputation: 6745

Provide a fake CKEDITOR while unit testing

I have 2 projects ..

.
├── dependency-project
│   └── ckeditor
│       └── ckeditor.js
└── my-project
    ├── app
    │   └── script.js
    ├── karma.conf.js
    └── test
        └── script.test.js

dependency-project contains ckeditor.js, and both applications are served from the same web server.

So my-project can access ckeditor.js via http://localhost:8080/ckeditor/ckeditor.js based on how I have configured the web server.

This setup works fine when using the apps via a browser. But for unit testing, I'm getting an error when my tests try to use CKEDITOR.

Note: I'm not referencing ckeditor.js in my karma.conf.js, I'd like to define a fake if possible.

This is the error, is there a way i can stub/mock CKEDITOR here? ..

      PhantomJS 2.1.1 (Mac OS X 0.0.0)
        ReferenceError: Can't find variable: CKEDITOR

script.js ..

document.getElementById('myElement').addEventListener('click', _editingTextStart);

function _editingTextStart() {
  CKEDITOR.disableAutoInline = true;
}

script.test.js ..

'use strict';
describe('script', function() {

  var sandbox;
  beforeEach(function() {
    sandbox = sinon.sandbox.create();
  });
  afterEach(function() {
    sandbox.restore();
  });

  it('should setup ckeditor', function() {
    var event = new Event('click');
    document.getElementById('myElement').dispatchEvent(event);
  });
});

Upvotes: 1

Views: 1616

Answers (1)

bobbyrne01
bobbyrne01

Reputation: 6745

Decided to mock the object itself using:

window['CKEDITOR'] = { disableAutoInline: false };

in the test setup.

Upvotes: 1

Related Questions