@rxx/worker
Version:
React MVI micro framework.
239 lines (238 loc) • 8.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var intent_1 = require("../intent/intent");
var store_1 = require("../store/store");
var operators_1 = require("rxjs/operators");
var factory_1 = require("../factory");
var worker_1 = require("../worker");
var power_assert_1 = tslib_1.__importDefault(require("power-assert"));
var assert_1 = tslib_1.__importDefault(require("assert"));
var Intent = (function () {
function Intent() {
}
Intent.prototype.test = function () {
return this.intent.for('test').pipe(operators_1.share());
};
Intent = tslib_1.__decorate([
intent_1.intent
], Intent);
return Intent;
}());
var TestStore = (function () {
function TestStore() {
}
TestStore.prototype.initialize = function () {
return {
view: {
base: this.intent.test().pipe(operators_1.scan(function (acc, next) { return acc + 1; }, 1), operators_1.share(), operators_1.startWith(1)),
test: this.intent.test().pipe(operators_1.scan(function (acc, next) { return acc + 1; }, 1), operators_1.share(), operators_1.startWith(1)),
test2: this.subject.pipe(operators_1.map(function (_a) {
var type = _a.type, payload = _a.payload, states = _a.states;
switch (type) {
case 'subject-test':
return payload + states.test2;
default:
return payload;
}
}), operators_1.startWith(1)),
},
};
};
TestStore = tslib_1.__decorate([
store_1.store
], TestStore);
return TestStore;
}());
function testStore(observable, initialState) {
return {
view: observable.pipe(operators_1.scan(function (acc, args) {
switch (args.type) {
case 'test':
return acc + 1;
default:
return acc;
}
}, initialState), operators_1.startWith(initialState)),
};
}
var TestStore2 = (function () {
function TestStore2() {
}
TestStore2.prototype.initialize = function () {
return {
view: {
test3: this.intent.test().pipe(operators_1.scan(function (acc, next) { return next.data; }, 1), operators_1.share(), operators_1.startWith(0)),
},
};
};
TestStore2 = tslib_1.__decorate([
store_1.store
], TestStore2);
return TestStore2;
}());
var TestStore3 = (function () {
function TestStore3() {
}
TestStore3.prototype.initialize = function () {
return {
view: {
test: this.intent.test().pipe(operators_1.scan(function (acc, next) { return acc + 1; }, this.foo), operators_1.share(), operators_1.startWith(this.foo)),
},
};
};
TestStore3 = tslib_1.__decorate([
store_1.store
], TestStore3);
return TestStore3;
}());
var Intent4 = (function () {
function Intent4() {
}
Intent4.prototype.test = function () {
return this.intent.for('on-click').pipe(operators_1.share());
};
Intent4 = tslib_1.__decorate([
intent_1.intent
], Intent4);
return Intent4;
}());
var TestStore4 = (function () {
function TestStore4() {
}
TestStore4.prototype.initialize = function () {
return {
view: {
test3: this.intent.test().pipe(operators_1.map(function (_a) {
var state = _a.state;
return state.view.base + 1;
}), operators_1.startWith(0)),
},
};
};
TestStore4 = tslib_1.__decorate([
store_1.store
], TestStore4);
return TestStore4;
}());
var WAIT_TIME_MS = 200;
describe('factory.tsx', function () {
var dispatched = [];
beforeAll(function () {
global['RECEIVE_WORKER_MESSAGE'] = function (_a) {
var type = _a.type, payload = _a.payload;
dispatched.push({ type: type, payload: payload });
};
});
afterEach(function () {
dispatched.length = 0;
});
describe('makeView/makeApp', function () {
it('should pass intent and store to children.', function (done) {
var subject = factory_1.run({}, {}, {
store: TestStore,
intent: Intent,
});
subject.next({ type: 'INITIALIZE', payload: {} });
subject.next({
type: 'DISPATCH',
payload: { type: 'test', payload: 1 },
});
setTimeout(function () {
power_assert_1.default.strictEqual(dispatched.length, 3);
assert_1.default.deepStrictEqual(dispatched[0], {
type: worker_1.WorkerPostEventType.INITIALIZED,
payload: {
base: 1,
test: 1,
test2: 1,
},
});
assert_1.default.deepStrictEqual(dispatched[1], {
type: worker_1.WorkerPostEventType.UPDATE,
payload: {
base: 2,
test: 2,
test2: 1,
},
});
assert_1.default.deepStrictEqual(dispatched[2], {
type: worker_1.WorkerPostEventType.DISPATCHED,
payload: { type: 'test', payload: 1 },
});
done();
}, WAIT_TIME_MS);
});
it('should pass intent and store group to children.', function (done) {
var subject = factory_1.run({}, {}, {
store: [TestStore, TestStore2],
intent: Intent,
});
setTimeout(function () {
subject.next({ type: 'INITIALIZE', payload: {} });
subject.next({
type: 'DISPATCH',
payload: { type: 'test', payload: 1 },
});
setTimeout(function () {
power_assert_1.default.strictEqual(dispatched.length, 3);
assert_1.default.deepStrictEqual(dispatched[0], {
type: worker_1.WorkerPostEventType.INITIALIZED,
payload: {
base: 1,
test: 1,
test2: 1,
test3: 0,
},
});
assert_1.default.deepStrictEqual(dispatched[1], {
type: worker_1.WorkerPostEventType.UPDATE,
payload: {
base: 2,
test: 2,
test2: 1,
test3: 1,
},
});
assert_1.default.deepStrictEqual(dispatched[2], {
type: worker_1.WorkerPostEventType.DISPATCHED,
payload: { type: 'test', payload: 1 },
});
done();
}, WAIT_TIME_MS);
});
});
it('should pass services', function (done) {
var subject = factory_1.run({}, {}, {
store: TestStore3,
intent: Intent,
service: { foo: 10 },
});
subject.next({ type: 'INITIALIZE', payload: {} });
subject.next({
type: 'DISPATCH',
payload: { type: 'test', payload: 1 },
});
setTimeout(function () {
power_assert_1.default.strictEqual(dispatched.length, 3);
assert_1.default.deepStrictEqual(dispatched[0], {
type: worker_1.WorkerPostEventType.INITIALIZED,
payload: {
test: 10,
},
});
assert_1.default.deepStrictEqual(dispatched[1], {
type: worker_1.WorkerPostEventType.UPDATE,
payload: {
test: 11,
},
});
assert_1.default.deepStrictEqual(dispatched[2], {
type: worker_1.WorkerPostEventType.DISPATCHED,
payload: { type: 'test', payload: 1 },
});
done();
}, WAIT_TIME_MS);
});
});
});