Skip to content

Commit

Permalink
Rewrite Buttons.spec.js with BDD.
Browse files Browse the repository at this point in the history
 - add equalsStyle on matcher
 - rename equalIgnoreCase to equalsIgnoreCase
  • Loading branch information
hackerwins committed Jan 24, 2016
1 parent 03ba366 commit 4ec77f6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 86 deletions.
36 changes: 24 additions & 12 deletions test/chaidom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ define([
return function (chai) {
chai.dom = chai.dom || {};

chai.dom.equalIgnoreCase = function (str1, str2) {
chai.dom.equalsIgnoreCase = function (str1, str2) {
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();

Expand All @@ -21,25 +21,37 @@ define([
return str1 === str2;
};

chai.Assertion.addChainableMethod('equalIgnoreCase', function (expected) {
chai.dom.equalsStyle = function ($node, expected, style) {
var $tester = $('<div />').css(style, expected);
return $node.css(style) === $tester.css(style);
};

chai.Assertion.addChainableMethod('equalsIgnoreCase', function (expected) {
var actual = this._obj;

return this.assert(
chai.dom.equalIgnoreCase(actual, expected),
'expected ' + this._obj + ' to equal ' + expected + ' ignoring case',
'expected ' + this._obj + ' not to equal ' + expected + ' ignoring case'
);
chai.dom.equalsIgnoreCase(actual, expected),
'expected ' + this._obj + ' to equal ' + expected + ' ignoring case',
'expected ' + this._obj + ' not to equal ' + expected + ' ignoring case'
);
});

var assert = chai.assert;
chai.Assertion.addChainableMethod('equalsStyle', function (expected, style) {
var $node = this._obj;

assert.equalIgnoreCase = function (val, exp, msg) {
new chai.Assertion(val, msg).to.be.equalIgnoreCase(exp);
};
return this.assert(
chai.dom.equalsStyle($node, expected, style),
'expected ' + this._obj + ' to equal ' + expected + ' style',
'expected ' + this._obj + ' not to equal ' + expected + ' style'
);
});

assert.notEqualIgnoreCase = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.equalIgnoreCase(exp);
chai.assert.equalsIgnoreCase = function (val, exp, msg) {
new chai.Assertion(val, msg).to.be.equalsIgnoreCase(exp);
};

chai.assert.notequalsIgnoreCase = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.equalsIgnoreCase(exp);
};
};
});
4 changes: 2 additions & 2 deletions test/unit/base/Context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ define([
});

it('should get or set contents with code', function () {
expect(context.code()).to.equalIgnoreCase('<p>hello</p>');
expect(context.code()).to.equalsIgnoreCase('<p>hello</p>');
context.code('<p>hello2</p>');
expect(context.code()).to.equalIgnoreCase('<p>hello2</p>');
expect(context.code()).to.equalsIgnoreCase('<p>hello2</p>');
});

it('should enable or disable editor', function () {
Expand Down
34 changes: 17 additions & 17 deletions test/unit/base/core/dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,32 +242,32 @@ define([
var $u = $para.find('u');
dom.splitTree($para[0], {node: $u[0], offset: 0 });

expect($para.html()).to.equalIgnoreCase('<b>b</b><u><br></u>');
expect($para.next().html()).to.equalIgnoreCase('<u>u</u><s>strike</s><i>i</i>');
expect($para.html()).to.equalsIgnoreCase('<b>b</b><u><br></u>');
expect($para.next().html()).to.equalsIgnoreCase('<u>u</u><s>strike</s><i>i</i>');
});

it('should be split by u tag with offset 1', function () {
var $u = $para.find('u');
dom.splitTree($para[0], {node: $u[0], offset: 1 });

expect($para.html()).to.equalIgnoreCase('<b>b</b><u>u</u>');
expect($para.next().html()).to.equalIgnoreCase('<u><br></u><s>strike</s><i>i</i>');
expect($para.html()).to.equalsIgnoreCase('<b>b</b><u>u</u>');
expect($para.next().html()).to.equalsIgnoreCase('<u><br></u><s>strike</s><i>i</i>');
});

it('should be split by b tag with offset 0 (left edge case)', function () {
var $b = $para.find('b');
dom.splitTree($para[0], {node: $b[0], offset: 0 });

expect($para.html()).to.equalIgnoreCase('<b><br></b>');
expect($para.next().html()).to.equalIgnoreCase('<b>b</b><u>u</u><s>strike</s><i>i</i>');
expect($para.html()).to.equalsIgnoreCase('<b><br></b>');
expect($para.next().html()).to.equalsIgnoreCase('<b>b</b><u>u</u><s>strike</s><i>i</i>');
});

it('should be split by i tag with offset 1 (right edge case)', function () {
var $i = $para.find('i');
dom.splitTree($para[0], {node: $i[0], offset: 1 });

expect($para.html()).to.equalIgnoreCase('<b>b</b><u>u</u><s>strike</s><i>i</i>');
expect($para.next().html()).to.equalIgnoreCase('<i><br></i>');
expect($para.html()).to.equalsIgnoreCase('<b>b</b><u>u</u><s>strike</s><i>i</i>');
expect($para.next().html()).to.equalsIgnoreCase('<i><br></i>');
});
});

Expand All @@ -276,46 +276,46 @@ define([
var $s = $para.find('s');
dom.splitTree($para[0], {node: $s[0].firstChild, offset: 3 });

expect($para.html()).to.equalIgnoreCase('<b>b</b><u>u</u><s>str</s>');
expect($para.next().html()).to.equalIgnoreCase('<s>ike</s><i>i</i>');
expect($para.html()).to.equalsIgnoreCase('<b>b</b><u>u</u><s>str</s>');
expect($para.next().html()).to.equalsIgnoreCase('<s>ike</s><i>i</i>');
});

it('should be split by s tag with offset 0 (left edge case)', function () {
var $s = $para.find('s');
dom.splitTree($para[0], {node: $s[0].firstChild, offset: 0 });

expect($para.html()).to.equalIgnoreCase('<b>b</b><u>u</u><s><br></s>');
expect($para.next().html()).to.equalIgnoreCase('<s>strike</s><i>i</i>');
expect($para.html()).to.equalsIgnoreCase('<b>b</b><u>u</u><s><br></s>');
expect($para.next().html()).to.equalsIgnoreCase('<s>strike</s><i>i</i>');
});

it('should be split by s tag with offset 6 (right edge case)', function () {
var $s = $para.find('s');
dom.splitTree($para[0], {node: $s[0].firstChild, offset: 6 });

expect($para.html()).to.equalIgnoreCase('<b>b</b><u>u</u><s>strike</s>');
expect($para.next().html()).to.equalIgnoreCase('<s><br></s><i>i</i>');
expect($para.html()).to.equalsIgnoreCase('<b>b</b><u>u</u><s>strike</s>');
expect($para.next().html()).to.equalsIgnoreCase('<s><br></s><i>i</i>');
});

it('should be split by s tag with offset 3 (2 depth case)', function () {
var $s = $para.find('s');
dom.splitTree($s[0], {node: $s[0].firstChild, offset: 3 });

expect($para.html()).to.equalIgnoreCase('<b>b</b><u>u</u><s>str</s><s>ike</s><i>i</i>');
expect($para.html()).to.equalsIgnoreCase('<b>b</b><u>u</u><s>str</s><s>ike</s><i>i</i>');
});

it('should be split by s tag with offset 3 (1 depth and textNode case)', function () {
var $s = $para.find('s');
dom.splitTree($s[0].firstChild, {node: $s[0].firstChild, offset: 3 });

expect($para.html()).to.equalIgnoreCase('<b>b</b><u>u</u><s>strike</s><i>i</i>');
expect($para.html()).to.equalsIgnoreCase('<b>b</b><u>u</u><s>strike</s><i>i</i>');
});

it('should be split by span tag with offset 2 (1 depth and element case)', function () {
var $cont = $('<div class="note-editable"><p><span><b>b</b><u>u</u><s>s</s><i>i</i></span></p></div>'); //busi
var $span = $cont.find('span');
dom.splitTree($span[0], {node: $span[0], offset: 2 });

expect($cont.html()).to.equalIgnoreCase('<p><span><b>b</b><u>u</u></span><span><s>s</s><i>i</i></span></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><span><b>b</b><u>u</u></span><span><s>s</s><i>i</i></span></p>');
});
});
});
Expand Down
28 changes: 14 additions & 14 deletions test/unit/base/core/range.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ define([
var rng = range.create($b[0].firstChild, 2, $b[0].firstChild, 2);
rng.insertNode($p2[0]);

expect($cont.html()).to.equalIgnoreCase('<p><b>bo</b></p><p>p</p><p><b>ld</b></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>bo</b></p><p>p</p><p><b>ld</b></p>');
});

it('should not split paragraph when inserting an inline element', function () {
Expand All @@ -236,7 +236,7 @@ define([

var rng = range.create($p[0].firstChild, 2, $p[0].firstChild, 2);
rng.insertNode($u[0]);
expect($cont.html()).to.equalIgnoreCase('<p>te<u>u</u>xt</p>');
expect($cont.html()).to.equalsIgnoreCase('<p>te<u>u</u>xt</p>');
});

it('should not split paragraph when inserting an inline element case 2', function () {
Expand All @@ -246,7 +246,7 @@ define([

var rng = range.create($b[0].firstChild, 2, $b[0].firstChild, 2);
rng.insertNode($u[0]);
expect($cont.html()).to.equalIgnoreCase('<p><b>bo</b><u>u</u><b>ld</b></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>bo</b><u>u</u><b>ld</b></p>');
});
});

Expand All @@ -259,7 +259,7 @@ define([
var rng = range.create($p[0].firstChild, 2);
rng.pasteHTML(markup);

expect($cont.html()).to.equalIgnoreCase('<p>te<span>span</span><i>italic</i>xt</p>');
expect($cont.html()).to.equalsIgnoreCase('<p>te<span>span</span><i>italic</i>xt</p>');
});

it('should split an inline element when pasting inline elements into it', function () {
Expand All @@ -270,7 +270,7 @@ define([
var rng = range.create($b[0].firstChild, 2);
rng.pasteHTML(markup);

expect($cont.html()).to.equalIgnoreCase('<p><b>bo</b><span>span</span><i>italic</i><b>ld</b></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>bo</b><span>span</span><i>italic</i><b>ld</b></p>');
});

it('should split inline node when pasting an inline node and a block node into it', function () {
Expand All @@ -281,7 +281,7 @@ define([
var rng = range.create($b[0].firstChild, 2);
rng.pasteHTML(markup);

expect($cont.html()).to.equalIgnoreCase('<p><b>bo</b><span>span</span></p><p><i>italic</i></p><p><b>ld</b></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>bo</b><span>span</span></p><p><i>italic</i></p><p><b>ld</b></p>');
});
});

Expand All @@ -296,14 +296,14 @@ define([
var rng = range.create($b[0].firstChild, 1, $b[0].firstChild, 3);
rng.deleteContents();

expect($cont.html()).to.equalIgnoreCase('<p><b>bd</b><u>u</u></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>bd</b><u>u</u></p>');
});

it('should remove text for entire text', function () {
var rng = range.create($b[0].firstChild, 0, $b[0].firstChild, 4);
rng.deleteContents();

expect($cont.html()).to.equalIgnoreCase('<p><b></b><u>u</u></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b></b><u>u</u></p>');
});
});

Expand All @@ -314,7 +314,7 @@ define([
var rng = range.create($cont[0], 0);
rng.wrapBodyInlineWithPara();

expect($cont.html()).to.equalIgnoreCase('<p><br></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><br></p>');
});

it('should wrap text with paragraph for text', function () {
Expand All @@ -323,7 +323,7 @@ define([
var rng = range.create($cont[0].firstChild, 2);
rng.wrapBodyInlineWithPara();

expect($cont.html()).to.equalIgnoreCase('<p>text</p>');
expect($cont.html()).to.equalsIgnoreCase('<p>text</p>');
});

it('should wrap an inline node with paragraph when selecting text in the inline node', function () {
Expand All @@ -333,7 +333,7 @@ define([
var rng = range.create($b[0].firstChild, 2);
rng.wrapBodyInlineWithPara();

expect($cont.html()).to.equalIgnoreCase('<p><b>bold</b></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>bold</b></p>');
});

it('should wrap inline nodes with paragraph when selecting text in the inline nodes', function () {
Expand All @@ -342,7 +342,7 @@ define([
var rng = range.create($cont[0], 0);
rng.wrapBodyInlineWithPara();

expect($cont.html()).to.equalIgnoreCase('<p><b>b</b><i>i</i></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>b</b><i>i</i></p>');
});

it('should wrap inline nodes with paragraph when selection some of text in the inline nodes #1', function () {
Expand All @@ -351,7 +351,7 @@ define([
var rng = range.create($cont[0], 1);
rng.wrapBodyInlineWithPara();

expect($cont.html()).to.equalIgnoreCase('<p><b>b</b><i>i</i></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>b</b><i>i</i></p>');
});

it('should wrap inline nodes with paragraph when selection some of text in the inline nodes #2', function () {
Expand All @@ -360,7 +360,7 @@ define([
var rng = range.create($cont[0], 2);
rng.wrapBodyInlineWithPara();

expect($cont.html()).to.equalIgnoreCase('<p><b>b</b><i>i</i></p>');
expect($cont.html()).to.equalsIgnoreCase('<p><b>b</b><i>i</i></p>');
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/base/module/Editor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ define([
}

var expectContents = function (context, markup) {
expect(context.layoutInfo.editable.html()).to.equalIgnoreCase(markup);
expect(context.layoutInfo.editable.html()).to.equalsIgnoreCase(markup);
};

var expectToHaveBeenCalled = function (context, customEvent, handler) {
Expand Down
68 changes: 28 additions & 40 deletions test/unit/bs3/module/Buttons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define([
}

describe('Buttons', function () {
var context, $toolbar, $editable, $dummy;
var context, $toolbar, $editable;

beforeEach(function () {
var $note = $('<div><p>hello</p></div>').appendTo('body');
Expand All @@ -33,62 +33,50 @@ define([

$toolbar = context.layoutInfo.toolbar;
$editable = context.layoutInfo.editable;

$dummy = $('<div />');
});

describe('bold button', function () {
it('should execute bold command when it is clicked', function () {
range.createFromNode($editable.find('p')[0]).normalize().select();

describe('bold click', function () {
it('should be clicked bold button ', function () {

// select any node
var p = $editable.find('p')[0];
range.createFromNode(p).normalize().select();

// bold click
$toolbar.find('.note-btn-bold').click();

// check element count
var count = $(context.invoke('code')).find('b').length;
expect(count).to.be.equal(1);

expect($editable.html()).to.equalsIgnoreCase('<p><b>hello</b></p>');
});
});

describe('color button click', function () {
it('should be clicked color button ', function () {

// select any node
var p = $editable.find('p')[0];
range.createFromNode(p).normalize().select();
describe('recent color button', function () {
it('should execute color command when it is clicked', function () {
range.createFromNode($editable.find('p')[0]).normalize().select();

// bold click
$toolbar.find('.note-current-color-button').click();

// check first current color
$dummy.css('background-color', '#FFFF00');
expect($(p).find('> :first').css('background-color')).to.be.equal($dummy.css('background-color'));
var $span = $editable.find('span');
expect($span).to.be.equalsStyle('#FFFF00', 'background-color');
});
});

// click fore color btn
var $colorButton = $toolbar.find('[data-event=foreColor] .note-color-btn:nth-child(2)').eq(0);
var selectColor = $colorButton.data('value');
describe('fore color button', function () {
it('should execute fore color command when it is clicked', function () {
range.createFromNode($editable.find('p')[0]).normalize().select();

$colorButton.click();
expect($(p).find('font[color=' + selectColor + ']').length).to.be.equal(1);
var $button = $toolbar.find('[data-event=foreColor]').eq(10);
$button.click();

// click back color btn
var $backColorButton = $toolbar.find('[data-event=backColor] .note-color-btn:nth-child(2)').eq(0);
var selectBackColor = $backColorButton.data('value');
var $font = $editable.find('font');
expect($font).to.be.equalsStyle($button.data('value'), 'color');
});
});

$backColorButton.click();
describe('back color button', function () {
it('should execute back color command when it is clicked', function () {
range.createFromNode($editable.find('p')[0]).normalize().select();

// convert hex to rgb
$dummy.css('background-color', selectBackColor);
var $button = $toolbar.find('[data-event=backColor]').eq(10);
$button.click();

expect($(p).find('> :first').css('background-color')).to.be.equal($dummy.css('background-color'));
var $span = $editable.find('span');
expect($span).to.be.equalsStyle($button.data('value'), 'background-color');
});
});

});

});

0 comments on commit 4ec77f6

Please sign in to comment.