Skip to content

Commit

Permalink
output-writer: better group #write() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavohenke committed Oct 20, 2023
1 parent 77fbc03 commit bdbbe6a
Showing 1 changed file with 60 additions and 58 deletions.
118 changes: 60 additions & 58 deletions src/output-writer.spec.ts
Expand Up @@ -30,71 +30,73 @@ beforeEach(() => {
];
});

describe('#write group=false', () => {
it('writes instantly', () => {
const writer = createWriter({ group: false });
writer.write(commands[2], 'hello');
expect(outputStream.write).toHaveBeenCalledTimes(1);
expect(outputStream.write).toHaveBeenCalledWith('hello');
describe('#write()', () => {
describe('with group=false', () => {
it('writes instantly', () => {
const writer = createWriter({ group: false });
writer.write(commands[2], 'hello');
expect(outputStream.write).toHaveBeenCalledTimes(1);
expect(outputStream.write).toHaveBeenCalledWith('hello');
});
});
});

describe('#write group=true', () => {
it('writes for null commands', () => {
const writer = createWriter({ group: true });
writer.write(undefined, 'hello');
expect(outputStream.write).toHaveBeenCalledTimes(1);
expect(outputStream.write).toHaveBeenCalledWith('hello');
});
describe('with group=true', () => {
it('writes for null commands', () => {
const writer = createWriter({ group: true });
writer.write(undefined, 'hello');
expect(outputStream.write).toHaveBeenCalledTimes(1);
expect(outputStream.write).toHaveBeenCalledWith('hello');
});

it('does not write instantly for non-active command', () => {
const writer = createWriter({ group: true });
writer.write(commands[2], 'hello');
expect(outputStream.write).toHaveBeenCalledTimes(0);
expect(writer.buffers[2]).toEqual(['hello']);
});
it('does not write instantly for non-active command', () => {
const writer = createWriter({ group: true });
writer.write(commands[2], 'hello');
expect(outputStream.write).toHaveBeenCalledTimes(0);
expect(writer.buffers[2]).toEqual(['hello']);
});

it('write instantly for active command', () => {
const writer = createWriter({ group: true });
writer.write(commands[0], 'hello');
expect(outputStream.write).toHaveBeenCalledTimes(1);
expect(outputStream.write).toHaveBeenCalledWith('hello');
});
it('write instantly for active command', () => {
const writer = createWriter({ group: true });
writer.write(commands[0], 'hello');
expect(outputStream.write).toHaveBeenCalledTimes(1);
expect(outputStream.write).toHaveBeenCalledWith('hello');
});

it('does not wait for write from next command to flush', () => {
const writer = createWriter({ group: true });
writer.write(commands[1], 'hello');
writer.write(commands[1], 'foo bar');
expect(outputStream.write).toHaveBeenCalledTimes(0);
closeCommand(commands[0]);
expect(outputStream.write).toHaveBeenCalledTimes(2);
expect(writer.activeCommandIndex).toBe(1);
outputStream.write.mockClear();
it('does not wait for write from next command to flush', () => {
const writer = createWriter({ group: true });
writer.write(commands[1], 'hello');
writer.write(commands[1], 'foo bar');
expect(outputStream.write).toHaveBeenCalledTimes(0);
closeCommand(commands[0]);
expect(outputStream.write).toHaveBeenCalledTimes(2);
expect(writer.activeCommandIndex).toBe(1);
outputStream.write.mockClear();

writer.write(commands[1], 'blah');
expect(outputStream.write).toHaveBeenCalledTimes(1);
});
writer.write(commands[1], 'blah');
expect(outputStream.write).toHaveBeenCalledTimes(1);
});

it('does not flush for non-active command', () => {
const writer = createWriter({ group: true });
writer.write(commands[1], 'hello');
writer.write(commands[1], 'foo bar');
expect(outputStream.write).toHaveBeenCalledTimes(0);
closeCommand(commands[1]);
expect(outputStream.write).toHaveBeenCalledTimes(0);
closeCommand(commands[0]);
expect(outputStream.write).toHaveBeenCalledTimes(2);
});
it('does not flush for non-active command', () => {
const writer = createWriter({ group: true });
writer.write(commands[1], 'hello');
writer.write(commands[1], 'foo bar');
expect(outputStream.write).toHaveBeenCalledTimes(0);
closeCommand(commands[1]);
expect(outputStream.write).toHaveBeenCalledTimes(0);
closeCommand(commands[0]);
expect(outputStream.write).toHaveBeenCalledTimes(2);
});

it('flushes multiple commands at a time if necessary', () => {
const writer = createWriter({ group: true });
writer.write(commands[2], 'hello');
closeCommand(commands[1]);
closeCommand(commands[2]);
expect(outputStream.write).toHaveBeenCalledTimes(0);
closeCommand(commands[0]);
expect(outputStream.write).toHaveBeenCalledTimes(1);
expect(outputStream.write).toHaveBeenCalledWith('hello');
expect(writer.activeCommandIndex).toBe(2);
it('flushes multiple commands at a time if necessary', () => {
const writer = createWriter({ group: true });
writer.write(commands[2], 'hello');
closeCommand(commands[1]);
closeCommand(commands[2]);
expect(outputStream.write).toHaveBeenCalledTimes(0);
closeCommand(commands[0]);
expect(outputStream.write).toHaveBeenCalledTimes(1);
expect(outputStream.write).toHaveBeenCalledWith('hello');
expect(writer.activeCommandIndex).toBe(2);
});
});
});

0 comments on commit bdbbe6a

Please sign in to comment.