redioactive
Version:
Reactive streams for chaining overlapping promises.
76 lines (75 loc) • 3.27 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const redio_1 = __importStar(require("../redio"));
describe('Test the each processing of values', () => {
test('Each is called the expected number of times', async () => {
const mockEach = jest.fn();
await (0, redio_1.default)([1, 2, 3, 4]).each(mockEach).toPromise();
expect(mockEach).toHaveBeenCalledTimes(4);
expect(mockEach.mock.calls).toEqual([[1], [2], [3], [4]]);
});
test('Empty stream, no calls', async () => {
const mockEach = jest.fn();
await (0, redio_1.default)([]).each(mockEach).toPromise();
expect(mockEach).not.toHaveBeenCalled();
});
test('Done is called at the end', async () => {
const mockEach = jest.fn();
const mockDone = jest.fn();
await (0, redio_1.default)([1, 2]).each(mockEach).done(mockDone).toPromise();
expect(mockEach).toHaveBeenCalledTimes(2);
expect(mockDone).toHaveBeenCalledTimes(1);
});
test('Done is called for empty streams', async () => {
const mockEach = jest.fn();
const mockDone = jest.fn();
await (0, redio_1.default)([]).each(mockEach).done(mockDone).toPromise();
expect(mockEach).not.toHaveBeenCalled();
expect(mockDone).toHaveBeenCalledTimes(1);
});
test('Each and async work', async () => {
const wait = jest.fn((t) => {
return new Promise((resolve) => setTimeout(resolve, t));
});
await (0, redio_1.default)([100, 200, 300, 400]).each(wait, { debug: false }).toPromise();
expect(wait).toHaveBeenCalledTimes(4);
expect(wait).toHaveBeenLastCalledWith(400);
});
test('End with a promise', async () => {
await expect((0, redio_1.default)([301, 401, 501, 601])
.each(() => {
/* void */
})
.toPromise()).resolves.toEqual(601);
});
test('Empty strean end with promise', async () => {
await expect((0, redio_1.default)([])
.each(() => {
/* void */
})
.toPromise()).resolves.toEqual(redio_1.nil);
});
});