Skip to content

Commit

Permalink
feat: add 'clearOnSuccess' option to renderer
Browse files Browse the repository at this point in the history
- mimics the "clear" option in ui.run, but applies to all tasks
  • Loading branch information
acburdine committed Sep 17, 2018
1 parent 4621bcc commit 7cfbfb7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/ui/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const chalk = require('chalk');

const defaultOptions = {
refreshRate: 100,
separator: chalk.cyan('>')
separator: chalk.cyan('>'),
clearOnSuccess: false
};

/**
Expand Down Expand Up @@ -52,10 +53,21 @@ class Renderer {
*/
subscribeToEvents() {
this.tasks.forEach((task) => {
task.subscribe((event) => {
if (event.type === 'STATE' && (task.isCompleted() || task.isSkipped() || task.hasFailed())) {
const spinnerMethod = task.isCompleted() ? 'succeed' : (task.isSkipped() ? 'info' : 'fail');
this.spinner[spinnerMethod](task.isSkipped() ? `${task.title} ${chalk.gray('[skipped]')}` : task.title);
task.subscribe(({type}) => {
if (type !== 'STATE') {
return;
}

if (task.isCompleted()) {
this.spinner[this.options.clearOnSuccess ? 'stop' : 'succeed'](task.title);
}

if (task.isSkipped()) {
this.spinner.info(`${task.title} ${chalk.gray('[skipped]')}`);
}

if (task.hasFailed()) {
this.spinner.fail(task.title);
}
});
});
Expand Down
29 changes: 29 additions & 0 deletions test/unit/ui/renderer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,35 @@ describe('Unit: UI > Renderer', function () {
expect(spinner.fail.called).to.be.false;
});

it('stop spinner called when task completes and clearOnSuccess is true', function () {
const subStub = sinon.stub();
const renderer = new Renderer({}, [{
subscribe: subStub,
isCompleted: () => true,
isSkipped: () => false,
hasFailed: () => false,
isEnabled
}], {clearOnSuccess: true});

const spinner = {
succeed: sinon.stub(),
stop: sinon.stub(),
info: sinon.stub(),
fail: sinon.stub()
};
renderer.spinner = spinner;
renderer.subscribeToEvents();

expect(subStub.calledOnce).to.be.true;
// update values and execute callback
subStub.firstCall.args[0]({type: 'STATE'});

expect(spinner.stop.calledOnce).to.be.true;
expect(spinner.succeed.called).to.be.false;
expect(spinner.info.called).to.be.false;
expect(spinner.fail.called).to.be.false;
});

it('info spinner called when task skips', function () {
const subStub = sinon.stub();
const renderer = new Renderer({}, [{
Expand Down

0 comments on commit 7cfbfb7

Please sign in to comment.