ForgeTotal.js 5
bg

Total.js 5

Тествайте компонент на Flow самостоятелно с Flow.test()

Total.js 5 · total5@0.0.19 flowflowstreamcomponentstests Изпълнено с Total.js 5

Flow.test(name, source, [config], [init]) изгражда поток с един компонент (source е неговият код, както в рецептата за FlowStream, а config заменя конфигурацията му) и фиктивен компонент, който събира изходите му. init(test) се извиква, когато потокът е готов, с обекта test:

  • test.send(inputid, data) (също test.input) изпраща съобщение към вход на компонента и го връща.
  • test.message = function($, data): извиква се с всяко съобщение, което компонентът изпраща към който и да е от изходите си; $ е съобщението, а data данните му.
  • test.reconfigure(config) променя конфигурацията; test.configure = function(config) се извиква след това. test.variables(object) задава променливите на потока.
  • test.assert(ok, message), test.ok(message) и test.fail(message) отпечатват [OK] или [NO] с датата, а провал хвърля изключение. Функциите са обикновени свойства: заменете test.ok и test.fail, за да изпратите резултата другаде, както прави примерът.
  • Тестовият поток винаги има id test: само един Flow.test() наведнъж.

Предназначен е за тестовете на компонент, написан за Flow, изпълнявани с node index.js в малко собствено приложение.

definitions/component-test.js
MAIN.results = [];

const source = `<script total>
exports.id = 'tax';
exports.name = 'Tax';
exports.inputs = [{ id: 'input', name: 'Input' }];
exports.outputs = [{ id: 'output', name: 'Output' }];
exports.config = { rate: 21 };
exports.make = function(instance, config) {
	instance.message = function($) {
		$.send('output', { net: $.data, total: Math.round($.data * (100 + config.rate)) / 100 });
	};
};
</script>`;

ON('ready', function() {
	Flow.test('tax', source, { rate: 10 }, function(test) {

		test.ok = message => MAIN.results.push('ok: ' + message);
		test.fail = message => MAIN.results.push('no: ' + message);

		test.message = function($, data) {
			test.assert(data.net === 100, 'the net amount comes back');
			test.assert(data.total === 110, 'the configuration of the test (10 %) is used, got ' + data.total);
			MAIN.results.push('message: ' + JSON.stringify(data));
		};

		test.send('input', 100);
	});
});
controllers/results.js
exports.install = function() {
	ROUTE('GET /results/', results);
};

function results($) {
	$.json(MAIN.results);
}