# Otestujte komponent Flow samostatne cez Flow.test()

> Načítajte jeden komponent do vlastného toku, pošlite mu správu z kódu a prečítajte, čo posiela na svoje výstupy, bez zostavovania celého toku.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: sk
Content-SHA256: b06416826058c8bbebf8fc08fc6fdb96f54d0b432a4ad433026e9c8e83ccc34c
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`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.

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

```js controllers/results.js
exports.install = function() {
	ROUTE('GET /results/', results);
};

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