ForgeTotal.js 5
it

Total.js 5

Testa un componente Flow da solo con Flow.test()

Total.js 5 · total5@0.0.19 flowflowstreamcomponentstests Eseguita con Total.js 5

Flow.test(name, source, [config], [init]) costruisce un flusso con un componente (il source è il suo codice, come nella ricetta di FlowStream, e config sostituisce la sua configurazione) e un componente fittizio che raccoglie le sue uscite. init(test) viene chiamato quando il flusso è pronto, con l'oggetto test:

  • test.send(inputid, data) (anche test.input) invia un messaggio a un ingresso del componente e lo restituisce.
  • test.message = function($, data): chiamato con ogni messaggio che il componente invia a una qualsiasi delle sue uscite; $ è il messaggio e data i suoi dati.
  • test.reconfigure(config) cambia la configurazione; test.configure = function(config) viene chiamato dopo. test.variables(object) imposta le variabili del flusso.
  • test.assert(ok, message), test.ok(message) e test.fail(message) stampano [OK] o [NO] con la data, e un fallimento genera un'eccezione. Le funzioni sono semplici proprietà: sostituisci test.ok e test.fail per inviare il risultato altrove, come fa l'esempio.
  • Il flusso di test ha sempre l'id test: un solo Flow.test() alla volta.

È pensato per i test di un componente scritto per un Flow, eseguiti con node index.js in una piccola applicazione a parte.

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