UNPKG

redioactive

Version:

Reactive streams for chaining overlapping promises.

80 lines (79 loc) 3.1 kB
"use strict"; 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('Finding a value in a stream', () => { test('Finds a value', async () => { await expect((0, redio_1.default)([1, 2, 3]) .find((x) => x >= 2) .toArray()).resolves.toEqual([2]); }); test('Works with an empty stream', async () => { await expect((0, redio_1.default)([]) .find((x) => x >= 2) .toArray()).resolves.toEqual([]); }); test('Documentation example', async () => { const docs = [ { name: 'one', value: 1 }, { name: 'two', value: 2 }, { name: 'three', value: 3 } ]; await expect((0, redio_1.default)(docs) .find((x) => x.value >= 2) .toArray()).resolves.toEqual([{ name: 'two', value: 2 }]); }); test('No matches', async () => { await expect((0, redio_1.default)([1, 2, 3]) .find(() => false) .toArray()).resolves.toEqual([]); }); test('Matches the first', async () => { await expect((0, redio_1.default)([1, 2, 3]) .find(() => true) .toArray()).resolves.toEqual([1]); }); test('No matches - toPromise', async () => { await expect((0, redio_1.default)([1, 2, 3]) .find(() => false) .each(() => { /* void */ }) .toPromise()).resolves.toEqual(redio_1.nil); }); test('Matches the first - toPromise', async () => { await expect((0, redio_1.default)([1, 2, 3]) .find(() => true) .each(() => { /* void */ }) .toPromise()).resolves.toEqual(1); }); test('Finds a value with a promise', async () => { await expect((0, redio_1.default)([1, 2, 3]) .find((x) => Promise.resolve(x >= 2)) .toArray()).resolves.toEqual([2]); }); });