Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
###############################################################################
2
#
3
# CoCalc: Collaborative web-based calculation
4
# Copyright (C) 2017, Sagemath Inc.
5
# AGPLv3
6
#
7
###############################################################################
8
9
###
10
Test type checking functionality
11
12
NOTE: You can't use `mocha -w` to work on this file, because it doesn't reset the warnings
13
internally between runs.
14
15
NOTE2: Some object key names are slightly different from others due to working around
16
https://github.com/facebook/react/issues/6293
17
###
18
19
{types} = require('../opts')
20
immutable = require('immutable')
21
sinon = require('sinon')
22
should = require('should')
23
require('should-sinon')
24
25
describe 'throws with non-objects', ->
26
it 'fails if first argument is non-object', ->
27
should.throws(()=>types(1, {a:types.number}))
28
29
it 'fails if second argument is non-object', ->
30
should.throws(()=>types({a:2}, 3))
31
32
describe 'test a basic type check -- ', ->
33
warn = undefined
34
beforeEach ->
35
warn = sinon.stub(global.console, "error")
36
37
afterEach ->
38
warn.restore()
39
40
it 'succeeds', ->
41
types({a:5}, {a:types.number})
42
warn.should.have.callCount(0)
43
44
it 'fails', ->
45
types({a:5}, {a:types.string})
46
warn.should.have.callCount(1)
47
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Invalid checking a `a` of type `number` supplied to `check.types`, expected `string`.')
48
49
describe 'checking immutable Map', ->
50
warn = undefined
51
beforeEach ->
52
warn = sinon.stub(global.console, "error")
53
54
afterEach ->
55
warn.restore()
56
57
it 'succeeds', ->
58
types({a : immutable.Map({a:4})}, {a:types.immutable.Map})
59
warn.should.have.callCount(0)
60
61
it 'fails', ->
62
types({a: 4}, {a:types.immutable.Map})
63
warn.should.have.callCount(1)
64
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: NOT EVEN IMMUTABLE, wanted immutable.Map [object Object], a')
65
66
it 'works with isRequired', ->
67
types({c : 4}, {a:types.immutable.Map.isRequired})
68
warn.should.have.callCount(1)
69
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Required prop `a` was not specified in `check.types`')
70
71
it 'checks against other immutable types', ->
72
types({b : immutable.List([1, 2])}, {b : types.immutable.Map})
73
warn.should.have.callCount(1)
74
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Component `check.types` expected b to be an immutable.Map but was supplied List [ 1, 2 ]')
75
76
describe 'checking immutable List', ->
77
warn = undefined
78
beforeEach ->
79
warn = sinon.stub(global.console, "error")
80
81
afterEach ->
82
warn.restore()
83
84
it 'succeeds', ->
85
types({a : immutable.List([1, 2, 3, 4])}, {a:types.immutable.List})
86
warn.should.have.callCount(0)
87
88
it 'fails', ->
89
types({a : 4}, {a : types.immutable.List})
90
warn.should.have.callCount(1)
91
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: NOT EVEN IMMUTABLE, wanted immutable.List [object Object], a')
92
93
it 'works with isRequired', ->
94
types({c : 4}, {b : types.immutable.List.isRequired})
95
warn.should.have.callCount(1)
96
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Required prop `b` was not specified in `check.types`')
97
98
it 'checks against other immutable types', ->
99
types({b : immutable.Map(a:4)}, {b : types.immutable.List})
100
warn.should.have.callCount(1)
101
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Component `check.types` expected b to be an immutable.List but was supplied Map { "a": 4 }')
102
103
describe 'checking immutable Set', ->
104
warn = undefined
105
beforeEach ->
106
warn = sinon.stub(global.console, "error")
107
108
afterEach ->
109
warn.restore()
110
111
it 'succeeds', ->
112
types({a : immutable.Set([1, 2, 3, 4])}, {a:types.immutable.Set})
113
warn.should.have.callCount(0)
114
115
it 'fails', ->
116
types({a: b : 4}, {a:types.immutable.Set})
117
warn.should.have.callCount(1)
118
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: NOT EVEN IMMUTABLE, wanted immutable.Set [object Object], a')
119
120
it 'works with isRequired', ->
121
types({a : 4}, {c:types.immutable.Set.isRequired})
122
warn.should.have.callCount(1)
123
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Required prop `c` was not specified in `check.types`')
124
125
it 'checks against other immutable types', ->
126
types({b : immutable.Map(a:4)}, {b : types.immutable.Set})
127
warn.should.have.callCount(1)
128
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Component `check.types` expected b to be an immutable.Set but was supplied Map { "a": 4 }')
129
130
describe 'checking immutable Stack', ->
131
warn = undefined
132
beforeEach ->
133
warn = sinon.stub(global.console, "error")
134
135
afterEach ->
136
warn.restore()
137
138
it 'succeeds', ->
139
types({a : immutable.Stack([1, 2, 4])}, {a:types.immutable.Stack})
140
warn.should.have.callCount(0)
141
142
it 'fails', ->
143
types({a : 2}, {a:types.immutable.Stack})
144
warn.should.have.callCount(1)
145
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: NOT EVEN IMMUTABLE, wanted immutable.Stack [object Object], a')
146
147
it 'works with isRequired', ->
148
types({c : 4}, {d:types.immutable.Stack.isRequired})
149
warn.should.have.callCount(1)
150
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Required prop `d` was not specified in `check.types`')
151
152
it 'checks against other immutable types', ->
153
types({b : immutable.Map(a:4)}, {b : types.immutable.Stack})
154
warn.should.have.callCount(1)
155
warn.getCall(0).args[0].should.match('Warning: Failed checking a type: Component `check.types` expected b to be an immutable.Stack but was supplied Map { "a": 4 }')
156