/
iterators.test.ts
77 lines (71 loc) · 2.37 KB
/
iterators.test.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
describe('iterators', () => {
it('works for arrays', () => {
const mixedArray = [1, {}, []];
expect(mixedArray).toEqual(mixedArray);
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect([1]).not.toEqual([2]);
expect([1, 2, 3]).not.toEqual([1, 2]);
expect([1, 2, 3]).not.toEqual([1, 2, 3, 4]);
});
it('works for custom iterables', () => {
const iterable = {
0: 'a',
1: 'b',
2: 'c',
length: 3,
[Symbol.iterator]: Array.prototype[Symbol.iterator],
};
const expectedIterable = {
0: 'a',
1: 'b',
2: 'c',
length: 3,
[Symbol.iterator]: Array.prototype[Symbol.iterator],
};
expect(iterable).toEqual(expectedIterable);
expect(iterable).not.toEqual(['a', 'b']);
expect(iterable).not.toEqual(['a', 'b', 'c']);
expect(iterable).not.toEqual(['a', 'b', 'c', 'd']);
});
it('works for Sets', () => {
const numbers = [1, 2, 3, 4];
const setOfNumbers = new Set(numbers);
expect(setOfNumbers).not.toEqual(new Set());
expect(setOfNumbers).not.toBe(numbers);
expect(setOfNumbers).not.toEqual([1, 2]);
expect(setOfNumbers).not.toEqual([1, 2, 3]);
expect(setOfNumbers).toEqual(new Set(numbers));
const nestedSets = new Set([new Set([1, 2])]);
expect(nestedSets).not.toEqual(new Set([new Set([1, 4])]));
expect(nestedSets).toEqual(new Set([new Set([1, 2])]));
});
it('works for Maps', () => {
const keyValuePairs: ReadonlyArray<[string, string]> = [
['key1', 'value1'],
['key2', 'value2'],
];
const smallerKeyValuePairs: ReadonlyArray<[string, string]> = [
['key1', 'value1'],
];
const biggerKeyValuePairs: ReadonlyArray<[string, string]> = [
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3'],
];
const map = new Map(keyValuePairs);
expect(map).not.toEqual(smallerKeyValuePairs);
expect(map).not.toEqual(new Map(smallerKeyValuePairs));
expect(map).not.toEqual(biggerKeyValuePairs);
expect(map).not.toEqual(new Map(biggerKeyValuePairs));
expect(map).not.toEqual(keyValuePairs);
expect(map).not.toBe(keyValuePairs);
expect(map).toEqual(new Map(keyValuePairs));
});
});