Skip to content

Commit

Permalink
Implement childExpect#exportStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Mar 6, 2017
1 parent b13ffa0 commit 4ddade1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/Unexpected.js
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,13 @@ Unexpected.prototype.child = function () {
parent.addType(type, childUnexpected);
return this;
};
childExpect.exportStyle = function (name, handler) {
parent.addStyle(name, function () { // ...
var childOutput = childExpect.createOutput(this.format);
this.append(handler.apply(childOutput, arguments) || childOutput);
});
return this;
};
return childExpect;
};

Expand Down
46 changes: 46 additions & 0 deletions test/api/exportStyle.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*global expect*/
describe('styleType', function () {
var parentExpect;
var childExpect;
beforeEach(function () {
parentExpect = expect.clone();
childExpect = parentExpect.child();
});

it('is chainable', function () {
childExpect
.exportStyle('firstStyle', function () {})
.exportStyle('secondStyle', function () {});

expect(parentExpect.output.firstStyle, 'to be a function');
expect(parentExpect.output.secondStyle, 'to be a function');
});

it('makes the style available to the parent expect', function () {
childExpect.exportStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});

expect(
parentExpect.createOutput().fancyQuotes('yadda').toString(),
'to equal',
'>>yadda<<'
);
});

it('binds the style handler to a child expect output so custom types are available inside the exported style', function () {
childExpect.addStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});

childExpect.exportStyle('emphasize', function (text) {
this.fancyQuotes(text);
});

expect(
parentExpect.createOutput().emphasize('yadda').toString(),
'to equal',
'>>yadda<<'
);
});
});

0 comments on commit 4ddade1

Please sign in to comment.