ForgeTotal.js 5
sk

Total.js 5

Otestujte komponent Flow samostatne cez Flow.test()

Total.js 5 · total5@0.0.19 flowflowstreamcomponentstests Spustené s Total.js 5

Flow.test(name, source, [config], [init]) zostaví tok s jedným komponentom (source je jeho kód ako v recepte FlowStream a config nahradí jeho konfiguráciu) a fiktívnym komponentom, ktorý zbiera jeho výstupy. init(test) sa zavolá, keď je tok pripravený, s objektom test:

  • test.send(inputid, data) (aj test.input) pošle správu na vstup komponentu a vráti ju.
  • test.message = function($, data): volá sa s každou správou, ktorú komponent pošle na ktorýkoľvek zo svojich výstupov; $ je správa a data jej dáta.
  • test.reconfigure(config) zmení konfiguráciu; test.configure = function(config) sa zavolá po nej. test.variables(object) nastaví premenné toku.
  • test.assert(ok, message), test.ok(message) a test.fail(message) vypíšu [OK] alebo [NO] s dátumom a zlyhanie vyhodí výnimku. Funkcie sú obyčajné vlastnosti: nahraďte test.ok a test.fail, aby ste výsledok poslali inam, ako to robí príklad.
  • Testovací tok má vždy id test: naraz iba jeden Flow.test().

Je určený na testy komponentu napísaného pre Flow, spúšťané cez node index.js v malej vlastnej aplikácii.

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