forked from graphql/graphql-js
/
isCollection-test.js
71 lines (55 loc) · 2.29 KB
/
isCollection-test.js
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
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { identityFunc } from '../identityFunc';
import { isCollection } from '../isCollection';
describe('isCollection', () => {
it('should return `true` for collections', () => {
expect(isCollection([])).to.equal(true);
expect(isCollection(new Int8Array(1))).to.equal(true);
// eslint-disable-next-line no-new-wrappers
expect(isCollection(new String('ABC'))).to.equal(true);
function getArguments() {
return arguments;
}
expect(isCollection(getArguments())).to.equal(true);
const arrayLike = {};
arrayLike[0] = 'Alpha';
arrayLike[1] = 'Bravo';
arrayLike[2] = 'Charlie';
arrayLike.length = 3;
expect(isCollection(arrayLike)).to.equal(true);
const iterator = { [Symbol.iterator]: identityFunc };
expect(isCollection(iterator)).to.equal(true);
// istanbul ignore next (Never called and use just as a placeholder)
function* generatorFunc() {
/* do nothing */
}
expect(isCollection(generatorFunc())).to.equal(true);
// But generator function itself is not iteratable
expect(isCollection(generatorFunc)).to.equal(false);
});
it('should return `false` for non-collections', () => {
expect(isCollection(null)).to.equal(false);
expect(isCollection(undefined)).to.equal(false);
expect(isCollection('ABC')).to.equal(false);
expect(isCollection('0')).to.equal(false);
expect(isCollection('')).to.equal(false);
expect(isCollection(1)).to.equal(false);
expect(isCollection(0)).to.equal(false);
expect(isCollection(NaN)).to.equal(false);
// eslint-disable-next-line no-new-wrappers
expect(isCollection(new Number(123))).to.equal(false);
expect(isCollection(true)).to.equal(false);
expect(isCollection(false)).to.equal(false);
// eslint-disable-next-line no-new-wrappers
expect(isCollection(new Boolean(true))).to.equal(false);
expect(isCollection({})).to.equal(false);
expect(isCollection({ iterable: true })).to.equal(false);
const iteratorWithoutSymbol = { next: identityFunc };
expect(isCollection(iteratorWithoutSymbol)).to.equal(false);
const invalidIteratable = {
[Symbol.iterator]: { next: identityFunc },
};
expect(isCollection(invalidIteratable)).to.equal(false);
});
});