ForgeTotal.js 5
cs

Total.js 5

Otestujte komponentu Flow samostatně pomocí Flow.test()

Total.js 5 · total5@0.0.19 flowflowstreamcomponentstests Spuštěno s Total.js 5

Flow.test(name, source, [config], [init]) sestaví tok s jednou komponentou (source je její kód jako v receptu FlowStream a config nahradí její konfiguraci) a fiktivní komponentou, která sbírá její výstupy. init(test) se zavolá, když je tok připraven, s objektem test:

  • test.send(inputid, data) (také test.input) pošle zprávu na vstup komponenty a vrátí ji.
  • test.message = function($, data): volá se s každou zprávou, kterou komponenta pošle na kterýkoli ze svých výstupů; $ je zpráva a data její data.
  • test.reconfigure(config) změní konfiguraci; test.configure = function(config) se zavolá po ní. test.variables(object) nastaví proměnné toku.
  • test.assert(ok, message), test.ok(message) a test.fail(message) vypíšou [OK] nebo [NO] s datem a selhání vyhodí výjimku. Funkce jsou obyčejné vlastnosti: nahraďte test.ok a test.fail, abyste výsledek poslali jinam, jak to dělá příklad.
  • Testovací tok má vždy id test: najednou pouze jeden Flow.test().

Je určen pro testy komponenty napsané pro Flow, spouštěné přes node index.js v malé vlastní aplikaci.

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