ForgeTotal.js 5
pl

Total.js 5

Przetestuj komponent Flow osobno przez Flow.test()

Total.js 5 · total5@0.0.19 flowflowstreamcomponentstests Uruchomiono z Total.js 5

Flow.test(name, source, [config], [init]) buduje przepływ z jednym komponentem (source to jego kod, jak w przepisie o FlowStream, a config zastępuje jego konfigurację) i atrapą komponentu zbierającą jego wyjścia. init(test) jest wywoływane, gdy przepływ jest gotowy, z obiektem test:

  • test.send(inputid, data) (także test.input) wysyła wiadomość na wejście komponentu i ją zwraca.
  • test.message = function($, data): wywoływane z każdą wiadomością, którą komponent wysyła na dowolne ze swoich wyjść; $ to wiadomość, a data jej dane.
  • test.reconfigure(config) zmienia konfigurację; test.configure = function(config) jest wywoływane po niej. test.variables(object) ustawia zmienne przepływu.
  • test.assert(ok, message), test.ok(message) i test.fail(message) wypisują [OK] lub [NO] z datą, a porażka zgłasza wyjątek. Funkcje to zwykłe właściwości: zastąp test.ok i test.fail, aby wysłać wynik gdzie indziej, jak robi to przykład.
  • Przepływ testowy ma zawsze id test: tylko jeden Flow.test() naraz.

Jest przeznaczony do testów komponentu napisanego dla Flow, uruchamianych przez node index.js w małej, własnej aplikacji.

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);
}