Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadOnly feature tests (#538) #542

Merged
merged 4 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions src/ui/react/src/adapter/alloy-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
AlloyEditor.Lang.mix(editor.config, config);

editor.once('contentDom', function() {
if (editor.config.readOnly) {
this._addReadOnlyLinkClickListener(editor);
}

var editable = editor.editable();

editable.addClass('ae-editable');
Expand Down Expand Up @@ -78,6 +82,7 @@

if (nativeEditor) {
var editable = nativeEditor.editable();

if (editable) {
editable.removeClass('ae-editable');

Expand All @@ -90,6 +95,19 @@
}
},

/**
* Method to set default link behavior
*
* @protected
* @method _addReadOnlyLinkClickListener
* @param {Object} editor
*/
_addReadOnlyLinkClickListener: function(editor) {
editor.editable().on('click', this._defaultReadOnlyClickFn, this, {
editor: editor
});
},

/**
* Called on `click` event when the editor is in read only mode. Navigates to link's URL or opens
* the link in a new window.
Expand All @@ -106,13 +124,10 @@

if (link) {
var href = link.$.attributes.href ? link.$.attributes.href.value : null;

var target = link.$.attributes.target ? link.$.attributes.target.value : null;

if (target && href) {
window.open(href, target);
} else if (href) {
window.location.href = href;
}
this._redirectLink(href, target);
}
}
},
Expand All @@ -137,14 +152,30 @@
*/
_onReadOnlyChangeFn: function(event) {
if (event.editor.readOnly) {
event.editor.editable().on('click', this._defaultReadOnlyClickFn, this, {
editor: event.editor
});
} else {
this._addReadOnlyLinkClickListener(event.editor);
}
else {
event.editor.editable().removeListener('click', this._defaultReadOnlyClickFn);
}
},

/**
* Redirects the browser to a given link
*
* @protected
* @method _redirectLink
* @param {string} href The href to take the browser to
* @param {string=} target Specifies where to display the link
*/
_redirectLink: function(href, target) {
if (target && href) {
window.open(href, target);
}
else if (href) {
window.location.href = href;
}
},

/**
* Renders the specified from the user toolbars.
*
Expand Down
85 changes: 85 additions & 0 deletions src/ui/react/test/alloy-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,91 @@
});
});

describe('with readOnly set to false', function() {
beforeEach(function(done) {
initEditor.call(this, done);
});

afterEach(function() {
cleanUpEditor.call(this);
});

it('should not redirect when clicking on links', function() {
var spy = sinon.spy(this.alloyEditor, '_redirectLink');

bender.tools.selection.setWithHtml(this.alloyEditor.get('nativeEditor'), '<a id="link_foo" href="foo.com">Foo</a>');

happen.click(document.getElementById('link_foo'));

assert.isFalse(spy.called);
});

it('should redirect when clicking on links and readonly has been changed to true', function() {
var nativeEditor = this.alloyEditor.get('nativeEditor');

nativeEditor.setReadOnly(true);

// Stub `_redirectLink` method to avoid page refreshes during the test
var stub = sinon.stub(this.alloyEditor, '_redirectLink');

bender.tools.selection.setWithHtml(nativeEditor, '<a id="link_foo" href="foo.com">Foo</a>');

happen.click(document.getElementById('link_foo'));

assert.isTrue(stub.calledOnce);
});
});

describe('with readonly set to true', function() {
beforeEach(function(done) {
initEditor.call(this, done, {
readOnly: true
});
});

afterEach(function() {
cleanUpEditor.call(this);
});

it('should open a new window when clicking in links with a target attribute', function() {
var stub = sinon.stub(window, 'open');

bender.tools.selection.setWithHtml(this.alloyEditor.get('nativeEditor'), '<a id="link_foo" href="foo.com" target="_blank">Foo</a>');

happen.click(document.getElementById('link_foo'));

stub.restore();

assert.isTrue(stub.calledOnce);
});

it('should update the current window url when clicking in links without a target attribute', function() {
var locationHref = window.location.href;

bender.tools.selection.setWithHtml(this.alloyEditor.get('nativeEditor'), '<a id="link_foo" href="#foo">Foo</a>');

happen.click(document.getElementById('link_foo'));

assert.strictEqual(window.location.href, locationHref + '#foo');
});

it('should not redirect when clicking on links and readonly has been set to', function() {
var nativeEditor = this.alloyEditor.get('nativeEditor');

nativeEditor.setReadOnly(false);

var stub = sinon.stub(this.alloyEditor, '_redirectLink');

bender.tools.selection.setWithHtml(nativeEditor, '<a id="link_foo" href="foo.com">Foo</a>');

var link = document.getElementById('link_foo');

happen.click(link);

assert.strictEqual(0, stub.callCount);
});
});

it('should create an instance when the passed srcNode is a DOM element', function(done) {
var el = document.createElement('div');
el.setAttribute('id', 'editable1');
Expand Down