Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 7, 2019
1 parent f77bf36 commit cd1c500
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 502 deletions.
34 changes: 23 additions & 11 deletions packages/core/src/reporters/BoostReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import Reporter from '../Reporter';
import Routine from '../Routine';
import Task from '../Task';

export interface LineParts {
prefix: string;
Expand Down Expand Up @@ -34,7 +35,7 @@ export default class BoostReporter extends Reporter {
routine.on('fail', handler);
};

getLineParts(routine: Routine<any, any>): LineParts {
getRoutineLineParts(routine: Routine<any, any>): LineParts {
const { depth, startTime, stopTime } = routine.metadata;

// Prefix
Expand Down Expand Up @@ -74,12 +75,29 @@ export default class BoostReporter extends Reporter {
};
}

getStepProgress(routine: Routine<any, any>): string {
return `[${routine.metadata.index + 1}/${routine.parent!.routines.length}]`;
getStepProgress(task: Task<any>): string {
if (!task.parent) {
return '';
}

const collection = task instanceof Routine ? task.parent.routines : task.parent.tasks;

return `[${task.metadata.index + 1}/${collection.length}]`;
}

getTaskLine(task: Task<any>): string {
let line = this.strip(task.statusText || task.title).trim();

if (this.getOutputLevel() >= Reporter.OUTPUT_NORMAL) {
line += ' ';
line += this.getStepProgress(task);
}

return line;
}

renderLines(routine: Routine<any, any>): string {
const { prefix, suffix, title } = this.getLineParts(routine);
const { prefix, suffix, title } = this.getRoutineLineParts(routine);
const { columns } = this.size();
const prefixLength = this.strip(prefix).length;
const suffixLength = this.strip(suffix).length;
Expand All @@ -99,14 +117,8 @@ export default class BoostReporter extends Reporter {
// Active task lines
routine.tasks.forEach(task => {
if (task.isRunning()) {
let line = this.strip(task.statusText || task.title).trim();

if (this.getOutputLevel() >= Reporter.OUTPUT_NORMAL) {
line += ` [${task.metadata.index}/${task.parent!.tasks.length}]`;
}

output += this.truncate(
this.indent(prefixLength) + this.style(line, 'pending'),
this.indent(prefixLength) + this.style(this.getTaskLine(task), 'pending'),
columns - Reporter.BUFFER,
);
output += '\n';
Expand Down
114 changes: 112 additions & 2 deletions packages/core/tests/Console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,118 @@ describe('Console', () => {
});
});

describe.skip('renderFinalOutput()', () => {
// TODO
describe('renderFinalOutput()', () => {
it('flushes and stops loop', () => {
const queueSpy = jest.spyOn(cli, 'flushOutputQueue');
const streamSpy = jest.spyOn(cli, 'flushBufferedStreams');
const loopSpy = jest.spyOn(cli, 'stopRenderLoop');

cli.renderFinalOutput(null);

expect(queueSpy).toHaveBeenCalled();
expect(streamSpy).toHaveBeenCalled();
expect(loopSpy).toHaveBeenCalled();
});

it('marks all output as final and renders', () => {
const foo = new Output(cli, () => 'foo');
const bar = new Output(cli, () => 'bar');
const baz = new Output(cli, () => 'baz');

cli.outputQueue.push(foo, bar, baz);
cli.renderFinalOutput(null);

expect(foo.isFinal()).toBe(true);
expect(bar.isFinal()).toBe(true);
expect(baz.isFinal()).toBe(true);

expect(foo.isComplete()).toBe(true);
expect(bar.isComplete()).toBe(true);
expect(baz.isComplete()).toBe(true);

expect(out).toHaveBeenCalledWith('foo\n');
expect(out).toHaveBeenCalledWith('bar\n');
expect(out).toHaveBeenCalledWith('baz\n');
});

it('displays logs on success', () => {
cli.log('foo');
cli.log('bar');
cli.log('baz');

cli.renderFinalOutput(null);

expect(out).toHaveBeenCalledWith('\nfoo\nbar\nbaz\n');
});

it('doesnt display logs on failure', () => {
cli.log('foo');
cli.log('bar');
cli.log('baz');

cli.renderFinalOutput(new Error());

expect(out).not.toHaveBeenCalled();
});

it('displays error logs on failure', () => {
cli.logError('foo');
cli.logError('bar');
cli.logError('baz');

cli.renderFinalOutput(new Error());

expect(err).toHaveBeenCalledWith('\nfoo\nbar\nbaz\n');
});

it('doesnt display error logs on success', () => {
cli.logError('foo');
cli.logError('bar');
cli.logError('baz');

cli.renderFinalOutput(null);

expect(err).not.toHaveBeenCalled();
});

it('displays footer on success', () => {
cli.log('foo');
cli.log('bar');
cli.log('baz');

cli.renderFinalOutput(null);

expect(out).toHaveBeenCalledWith('\nfoo\nbar\nbaz\n');
});

it('doesnt display footer on failure', () => {
cli.log('foo');
cli.log('bar');
cli.log('baz');

cli.renderFinalOutput(new Error());

expect(out).not.toHaveBeenCalled();
});

it('emits `error` event on failure', () => {
const spy = jest.fn();
const error = new Error('Stop');

cli.on('error', spy);
cli.renderFinalOutput(error);

expect(spy).toHaveBeenCalledWith(error);
});

it('doesnt emit `error` event on success', () => {
const spy = jest.fn();

cli.on('error', spy);
cli.renderFinalOutput(null);

expect(spy).not.toHaveBeenCalled();
});
});

describe('resetCursor()', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ export function createTestRoutine(
}

export function createTestConsole(): Console {
const cli = new Console(createTestTool());
const cli = new Console(createTestTool(), {
stderr: jest.fn(),
stdout: jest.fn(),
});

cli.err = jest.fn();
cli.out = jest.fn();

Expand Down
Loading

0 comments on commit cd1c500

Please sign in to comment.