forked from hunterc/expect-immutable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (97 loc) · 2.6 KB
/
index.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
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
105
106
107
108
109
import expect from 'expect';
import Immutable from 'immutable';
/**
* Check if the object is an immutable iterable.
*
* @param {Object} object
* @throws in case collection is not an instance of Immutable.Iterable
*/
function ensureIterable(object) {
expect.assert(
Immutable.Iterable.isIterable(object),
'Expected %s to be an Immutable Iterable',
object
);
}
/**
* Check if the objects have the same constructor.
*
* @param {Immutable.Iterable} actual
* @param {Immutable.Iterable} iterable
* @throws in case constructors are different
*/
function ensureSameType(actual, iterable) {
expect.assert(
actual.constructor.name === iterable.constructor.name,
`Expected ${actual} and ${iterable} to be of the same type`
);
}
/**
* Methods to extend the `expect` class.
*
* @type {Object.<string, function>}
*/
const api = {
/**
* Compares two objects using `Immutable.is` method.
*
* @param {Immutable.Iterable} iterable
* @throws in case collections are not equal
*/
toEqualImmutable(iterable) {
ensureIterable(this.actual);
ensureIterable(iterable);
ensureSameType(this.actual, iterable);
expect.assert(
Immutable.is(this.actual, iterable),
`Expected ${this.actual} to equal ${iterable}`
);
},
/**
* Compares two objects using `Immutable.is` method.
*
* @param {Immutable.Iterable} iterable
* @throws in case collections are equal
*/
toNotEqualImmutable(iterable) {
ensureIterable(this.actual);
ensureIterable(iterable);
expect.assert(
!Immutable.is(this.actual, iterable),
`Expected ${this.actual} not to equal ${iterable}`
);
},
/**
* Recursively compares if `expected` object is a superset of the comparable
* using `.isSuperset` method.
*
* @param {Immutable.Iterable} iterable
* @throws if .isSuperset returns false
*/
toBeSupersetImmutable(iterable) {
ensureIterable(this.actual);
ensureIterable(iterable);
ensureSameType(this.actual, iterable);
expect.assert(
this.actual.isSuperset(iterable),
`Expected ${this.actual} to contain ${iterable}`
);
},
/**
* Recursively compares if `expected` object is a subset of the comparable
* using `.isSubset` method.
*
* @param {Immutable.Iterable} iterable
* @throws if .isSubset returns false
*/
toBeSubsetImmutable(iterable) {
ensureIterable(this.actual);
ensureIterable(iterable);
ensureSameType(this.actual, iterable);
expect.assert(
this.actual.isSubset(iterable),
`Expected ${this.actual} to be contained by ${iterable}`
);
}
};
export default api;