Skip to content

Commit

Permalink
test: progress
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKreil committed Feb 21, 2024
1 parent c91b5b7 commit 70fc2f4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
75 changes: 75 additions & 0 deletions src/lib/progress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { Progress } from './progress';
import { jest } from '@jest/globals';

describe('Progress', () => {
let originalStdoutWrite: typeof process.stdout.write;

beforeAll(() => {
// Capture the original process.stdout.write to restore later
originalStdoutWrite = process.stdout.write;
// Mock process.stdout.write to prevent actual console output during tests
// @ts-expect-error too lazy
process.stdout.write = jest.fn();
});

afterAll(() => {
// Restore the original process.stdout.write after all tests
process.stdout.write = originalStdoutWrite;
});

beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();
});

it('should add a progress label and redraw', () => {
const progress = new Progress();
progress.add('Test Label', 1);

expect(process.stdout.write).toHaveBeenCalledTimes(2);
// Check if the output includes the label text
const writeCalls = (process.stdout.write as jest.Mock).mock.calls;
const output = writeCalls.map(call => call[0]).join('');
expect(output).toContain('Test Label');
});

it('should handle ANSI disabled', () => {
const progress = new Progress();
progress.disableAnsi();
const label = progress.add('Test Label No ANSI', 1);

// Verify direct write was used instead of ANSI codes
expect(process.stdout.write).toHaveBeenCalledTimes(0);

label.start();

expect(process.stdout.write).toHaveBeenCalledTimes(1);
const writeCalls = (process.stdout.write as jest.Mock).mock.calls;
const output = writeCalls.map(call => call[0]).join('');
expect(output).toContain('Test Label No ANSI');
expect(output).not.toContain('\x1b['); // ANSI escape should not be present
});

it('should update a label and redraw', () => {
const progress = new Progress();
const label = progress.add('Initial Label', 1);
label.updateLabel('Updated Label');

expect(process.stdout.write).toHaveBeenCalledTimes(3);
const writeCalls = (process.stdout.write as jest.Mock).mock.calls;
const output = writeCalls.map(call => call[0]).join('');
expect(output).toContain('Updated Label');
});

it('should mark a label as finished and redraw', () => {
const progress = new Progress();
progress.add('Finishing Label', 1);
progress.finish();

expect(process.stdout.write).toHaveBeenCalledTimes(3); // Initial add and finish
const writeCalls = (process.stdout.write as jest.Mock).mock.calls;
const output = writeCalls.map(call => call[0]).join('');
expect(output).toContain('Finished');
});
});
2 changes: 1 addition & 1 deletion src/lib/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class ProgressLabel {
/**
* Manages a collection of ProgressLabel instances and handles the overall progress display.
*/
class Progress {
export class Progress {
private readonly labelList: ProgressLabel[] = []; // List of all progress labels.

private header?: string; // Optional header text for the progress display.
Expand Down

0 comments on commit 70fc2f4

Please sign in to comment.