-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformers.spec.ts
104 lines (80 loc) · 3.97 KB
/
transformers.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { lensToYieldDone, lensToYieldValue, randomAsyncGenerator } from '../../jest.helpers';
import { AsyncCollection } from '../containers/AsyncCollection';
import { Collection } from '../containers/Collection';
import {
from,
iterableToAsyncIterable,
toAsyncCollection,
toAsyncIterableValue,
toIterableValue,
toSameContainer,
} from './transformers';
describe('core/helpers/transformers', () => {
test('toIterableValue function returns iterable for primitive value', () => {
const iterable = toIterableValue(1);
expect(iterable).toHaveProperty([Symbol.iterator]);
expect(iterable.next().value).toBe(1);
expect(iterable.next().done).toBeTruthy();
});
test('toIterableValue function returns iterable for iterable value', () => {
const iterable = toIterableValue(['1', '2']);
expect(iterable).toHaveProperty([Symbol.iterator]);
expect(iterable.next().value).toHaveProperty([Symbol.iterator]);
expect(iterable.next().done).toBeTruthy();
});
test('toAsyncIterableValue function returns async iterable for primitive value', () => {
const iterable = toAsyncIterableValue(1);
expect(iterable).toHaveProperty([Symbol.asyncIterator]);
expect(iterable.next().then(lensToYieldValue)).resolves.toBe(1);
expect(iterable.next().then(lensToYieldDone)).resolves.toBeTruthy();
});
test('iterableToAsyncIterable function returns async iterator of primitives for iterable of primitives', () => {
const iterable = iterableToAsyncIterable([1, 2]);
expect(iterable).toHaveProperty([Symbol.asyncIterator]);
expect(iterable.next().then(lensToYieldValue)).resolves.toBe(1);
expect(iterable.next().then(lensToYieldValue)).resolves.toBe(2);
expect(iterable.next().then(lensToYieldDone)).resolves.toBeTruthy();
});
test('iterableToAsyncIterable function returns async iterator of primitives for iterable of promises', () => {
const iterable = iterableToAsyncIterable([Promise.resolve(1), Promise.resolve(2)]);
expect(iterable).toHaveProperty([Symbol.asyncIterator]);
expect(iterable.next().then(lensToYieldValue)).resolves.toBe(1);
expect(iterable.next().then(lensToYieldValue)).resolves.toBe(2);
expect(iterable.next().then(lensToYieldDone)).resolves.toBeTruthy();
});
test('toAsyncCollection returns AsyncCollection instance', () => {
const collection = toAsyncCollection([1, 2]);
expect(collection).toHaveProperty([Symbol.asyncIterator]);
expect(collection).toBeInstanceOf(AsyncCollection);
});
test('toAsyncCollection returns AsyncCollection instance when used with "switch" method', () => {
const collection = new Collection([1, 2]).switch(toAsyncCollection);
expect(collection).toHaveProperty([Symbol.asyncIterator]);
expect(collection).toBeInstanceOf(AsyncCollection);
});
test('toSameContainer returns Collection instance', () => {
const collection = new Collection([1, 2]).switch(toSameContainer);
expect(collection).toHaveProperty([Symbol.iterator]);
expect(collection).toBeInstanceOf(Collection);
});
test('toSameContainer returns AsyncCollection instance', () => {
const collection = new AsyncCollection([1, 2]).switch(toSameContainer);
expect(collection).toHaveProperty([Symbol.asyncIterator]);
expect(collection).toBeInstanceOf(AsyncCollection);
});
test('from function returns Collection instance', () => {
const collection = from([1, 2]);
expect(collection).toHaveProperty([Symbol.iterator]);
expect(collection).toBeInstanceOf(Collection);
});
test('from function returns Collection instance for non iterable values', () => {
const collection = from({ name: 'Hank', lastname: 'Hill' });
expect(collection).toHaveProperty([Symbol.iterator]);
expect(collection).toBeInstanceOf(Collection);
});
test('from function returns AsyncCollection instance', () => {
const collection = from(randomAsyncGenerator());
expect(collection).toHaveProperty([Symbol.asyncIterator]);
expect(collection).toBeInstanceOf(AsyncCollection);
});
});