forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiProgressBarSpec.coffee
More file actions
176 lines (145 loc) · 6.13 KB
/
Copy pathApiProgressBarSpec.coffee
File metadata and controls
176 lines (145 loc) · 6.13 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#
# Copyright (C) 2015 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
define [
'underscore',
'react',
'react-dom',
'react-addons-test-utils',
'jsx/shared/ApiProgressBar'
'jsx/shared/stores/ProgressStore'
], (_, React, ReactDOM, TestUtils, ApiProgressBar, ProgressStore) ->
QUnit.module 'ApiProgressBarSpec',
setup: ->
@progress_id = '1'
@progress = {
id: @progress_id,
context_id: 1,
context_type: 'EpubExport',
user_id: 1,
tag: 'epub_export',
completion: 0,
workflow_state: 'queued'
}
@store_state = {}
@store_state[@progress_id] = @progress
@storeSpy = sinon.stub(ProgressStore, 'get').callsFake((=>
ProgressStore.setState(@store_state)
))
@clock = sinon.useFakeTimers()
teardown: ->
ProgressStore.get.restore()
ProgressStore.clearState()
@clock.restore()
test 'shouldComponentUpdate', ->
ApiProgressBarElement = React.createElement(ApiProgressBar)
component = TestUtils.renderIntoDocument(ApiProgressBarElement)
ok component.shouldComponentUpdate({
progress_id: @progress_id
}, {}), 'should update when progress_id prop changes'
ok component.shouldComponentUpdate({}, {
workflow_state: 'running'
}), 'should update when workflow_state changes'
ok component.shouldComponentUpdate({}, {
completion: 10
}), 'should update when completion level changes'
component.setProps(progress_id: @progress_id)
component.setState(workflow_state: 'running')
ok !component.shouldComponentUpdate({
progress_id: @progress_id
}, {
completion: component.state.completion,
workflow_state: component.state.workflow_state
}), 'should not update if state & props are the same'
test 'componentDidUpdate', ->
onCompleteSpy = sinon.spy()
ApiProgressBarElement = React.createElement(ApiProgressBar, {
onComplete: onCompleteSpy,
progress_id: @progress_id
})
component = TestUtils.renderIntoDocument(ApiProgressBarElement)
@clock.tick(component.props.delay + 5)
ok !_.isNull(component.intervalID), 'should have interval id'
@progress.workflow_state = 'running'
@clock.tick(component.props.delay + 5)
ok !_.isNull(component.intervalID), 'should have an inverval id after updating to running'
@progress.workflow_state = 'completed'
@clock.tick(component.props.delay + 5)
ok _.isNull(component.intervalID), 'should not have an inverval id after updating to completed'
ok onCompleteSpy.called, 'should call callback on update if complete'
test 'handleStoreChange', ->
ApiProgressBarElement = React.createElement(ApiProgressBar, {
progress_id: @progress_id
})
component = TestUtils.renderIntoDocument(ApiProgressBarElement)
@clock.tick(component.props.delay + 5)
_.each [ 'completion', 'workflow_state' ], (stateName) =>
equal component.state[stateName], @progress[stateName],
"component #{stateName} should equal progress #{stateName}"
@progress.workflow_state = 'running'
@progress.completion = 50
ProgressStore.setState(@store_state)
_.each [ 'completion', 'workflow_state' ], (stateName) =>
equal component.state[stateName], @progress[stateName],
"component #{stateName} should equal progress #{stateName}"
ReactDOM.unmountComponentAtNode(component.getDOMNode().parentNode)
test 'isComplete', ->
ApiProgressBarElement = React.createElement(ApiProgressBar, {
progress_id: @progress_id
})
component = TestUtils.renderIntoDocument(ApiProgressBarElement)
@clock.tick(component.props.delay + 5)
ok !component.isComplete(), 'is not complete if state is queued'
@progress.workflow_state = 'running'
@clock.tick(component.props.delay + 5)
ok !component.isComplete(), 'is not complete if state is running'
@progress.workflow_state = 'completed'
@clock.tick(component.props.delay + 5)
ok component.isComplete(), 'is complete if state is completed'
test 'isInProgress', ->
ApiProgressBarElement = React.createElement(ApiProgressBar, {
progress_id: @progress_id
})
component = TestUtils.renderIntoDocument(ApiProgressBarElement)
@clock.tick(component.props.delay + 5)
ok component.isInProgress(), 'is in progress if state is queued'
@progress.workflow_state = 'running'
@clock.tick(component.props.delay + 5)
ok component.isInProgress(), 'is in progress if state is running'
@progress.workflow_state = 'completed'
@clock.tick(component.props.delay + 5)
ok !component.isInProgress(), 'is not in progress if state is completed'
test 'poll', ->
ApiProgressBarElement = React.createElement(ApiProgressBar)
component = TestUtils.renderIntoDocument(ApiProgressBarElement)
component.poll()
ok !@storeSpy.called,
'should not fetch from progress store without progress id'
component.setProps(progress_id: @progress_id)
component.poll()
ok @storeSpy.called, 'should fetch when progress id is present'
ReactDOM.unmountComponentAtNode(component.getDOMNode().parentNode)
test 'render', ->
ApiProgressBarElement = React.createElement(ApiProgressBar, {
progress_id: @progress_id
})
component = TestUtils.renderIntoDocument(ApiProgressBarElement)
ok _.isNull(component.getDOMNode()),
'should not render to DOM if is not in progress'
@clock.tick(component.props.delay + 5)
ok !_.isNull(component.getDOMNode()),
'should render to DOM if is not in progress'
ReactDOM.unmountComponentAtNode(component.getDOMNode().parentNode)