Skip to content

Commit

Permalink
add some renderer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PondWader committed Nov 2, 2024
1 parent feb667d commit 4747d95
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/test/renderer-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {test} from 'node:test';
import {Renderer, TextComponent} from '../renderer';
import * as assert from 'node:assert/strict';
import {interceptStdout} from './utils';
import * as constants from '../constants';

test('TextComponent', async (t) => {
const component = new TextComponent('lorem ipsum dolor');

await t.test('Instantiates with correct value', () => {
assert.equal(component.output(), 'lorem ipsum dolor');
});

await t.test('.setText behaves correctly', () => {
let onChangeCalled = false;
component.onChange = () => (onChangeCalled = true);
component.setText('sit amet');
assert.equal(component.output(), 'sit amet');
assert.ok(onChangeCalled);
});

await t.test('finish behaves correctly', () => {
let finishCallbackRuns = 0;
component.onFinish = () => finishCallbackRuns++;

component.finish();
component.finish();

assert.ok(component.finished);
assert.equal(finishCallbackRuns, 1);
});
});

function createRendererOutput(text: string) {
return constants.CLEAR_LINE + constants.HIDE_CURSOR + text + '\n';
}

test('Renderer', async (t) => {
await t.test('displays TextComponent', async () => {
const stdout = await interceptStdout(async () => {
const renderer = new Renderer();
renderer.addComponent(new TextComponent('lorem ipsum dolor'));
});
assert.equal(stdout, createRendererOutput('lorem ipsum dolor'));
});
});

0 comments on commit 4747d95

Please sign in to comment.