fractal-core
Version:
A minimalist and well crafted app, content or component is our conviction
84 lines • 3.5 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = require("ava");
const core_1 = require("../core");
const testUtils_1 = require("../core/testUtils");
// Event Bus tests
exports.ChildComp = {
state: { result: '', count: 0 },
inputs: (s, F) => ({
inc: () => __awaiter(this, void 0, void 0, function* () {
yield F.toAct('Inc');
let res = yield F.emit('myEvent', s.count);
yield F.set('result', res);
}),
changed: (value) => __awaiter(this, void 0, void 0, function* () { }),
}),
actions: {
Inc: () => s => {
s.count++;
},
},
interfaces: {},
};
exports.ReceptorComp = {
state: {},
inputs: (s, F) => ({
onInit: () => __awaiter(this, void 0, void 0, function* () {
F.on('myEvent', F.in('myEvent', core_1._, '*'), true);
}),
myEvent: (count) => __awaiter(this, void 0, void 0, function* () {
return count * 3 + 1;
}),
}),
actions: {},
interfaces: {},
};
ava_1.default('Event bus with pullable and normal subscribers', (t) => __awaiter(this, void 0, void 0, function* () {
const app = yield testUtils_1.createApp({
state: {
result: '',
_nest: {
Child: core_1.clone(exports.ChildComp),
R1: core_1.clone(exports.ReceptorComp),
R2: core_1.clone(exports.ReceptorComp),
R3: core_1.clone(exports.ReceptorComp),
},
},
inputs: (s, F) => ({
onInit: () => __awaiter(this, void 0, void 0, function* () {
F.on('myEvent', F.in('myEvent', core_1._, '*'));
}),
myEvent: (count) => __awaiter(this, void 0, void 0, function* () {
yield F.set('result', count);
}),
}),
});
yield app.moduleAPI.toComp('Root$Child', 'inc');
t.is(app.rootCtx.components.Root.state.result, 1, 'Should propagate events to not pullable susbscribers');
t.deepEqual(app.rootCtx.components.Root$Child.state.result, [4, 4, 4], 'Should pull results from subscribers before sending the event');
}));
ava_1.default('Event bus from Module API', (t) => __awaiter(this, void 0, void 0, function* () {
const app = yield testUtils_1.createApp({
state: { result: 0 },
inputs: (s, F) => ({
onInit: () => __awaiter(this, void 0, void 0, function* () {
F.on('myEvent', F.in('myEvent', core_1._, '*'));
}),
myEvent: (num) => __awaiter(this, void 0, void 0, function* () {
yield F.set('result', num + 1);
}),
}),
});
yield app.moduleAPI.emit('myEvent', 2);
t.is(app.rootCtx.components.Root.state.result, 3, 'Should send the message');
}));
//# sourceMappingURL=eventBus.spec.js.map