forked from GetmeUK/ContentTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathignition.coffee
More file actions
231 lines (153 loc) · 6.81 KB
/
Copy pathignition.coffee
File metadata and controls
231 lines (153 loc) · 6.81 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# IgnitionUI
describe 'ContentTools.IgnitionUI', () ->
div = null
editor = null
beforeEach ->
# Create an editable region
div = document.createElement('div')
div.setAttribute('class', 'editable')
document.body.appendChild(div)
# Initialize the editor
editor = ContentTools.EditorApp.get()
editor.init('.editable')
afterEach ->
# Shutdown the editor
editor.destroy()
# Remove the editable region
document.body.removeChild(div)
describe 'ContentTools.IgnitionUI()', () ->
it 'should return an instance of a IgnitionUI', () ->
ignition = new ContentTools.IgnitionUI()
expect(ignition instanceof ContentTools.IgnitionUI).toBe true
describe 'ContentTools.IgnitionUI.busy()', () ->
it 'should set/unset the ignition to busy', () ->
ignition = new ContentTools.IgnitionUI()
# Check that the default ignition busy state is false
expect(ignition.state()).toBe 'ready'
# Check we can change it
ignition.busy(true)
expect(ignition.state()).toBe 'busy'
# Check we can change it back
ignition.busy(false)
expect(ignition.state()).toBe 'ready'
describe 'ContentTools.IgnitionUI.cancel()', () ->
it 'should set the ignition to editing and trigger the cancel
event', () ->
# Set the ignition to editing
ignition = new ContentTools.IgnitionUI()
ignition.state('editing')
# Create function we can spy on to ensure the event is triggered
foo = {
handleFoo: () ->
return
}
spyOn(foo, 'handleFoo')
# Bind the spied on function to the event
ignition.addEventListener('cancel', foo.handleFoo)
# Trigger the cancel event
ignition.cancel()
expect(foo.handleFoo).toHaveBeenCalled()
expect(ignition.state()).toBe 'ready'
describe 'ContentTools.IgnitionUI.confim()', () ->
it 'should set the ignition to ready and trigger the confirm
event', () ->
# Set the ignition to editing
ignition = new ContentTools.IgnitionUI()
ignition.state('editing')
# Create function we can spy on to ensure the event is triggered
foo = {
handleFoo: () ->
return
}
spyOn(foo, 'handleFoo')
# Bind the spied on function to the event
ignition.addEventListener('confirm', foo.handleFoo)
# Trigger the confirm event
ignition.confirm()
expect(foo.handleFoo).toHaveBeenCalled()
expect(ignition.state()).toBe 'ready'
describe 'ContentTools.IgnitionUI.edit()', () ->
it 'should set the ignition to editing and trigger the edit
event', () ->
# Set the ignition to editing
ignition = new ContentTools.IgnitionUI()
ignition.state('ready')
# Create function we can spy on to ensure the event is triggered
foo = {
handleFoo: () ->
return
}
spyOn(foo, 'handleFoo')
# Bind the spied on function to the event
ignition.addEventListener('edit', foo.handleFoo)
# Trigger the edit event
ignition.edit()
expect(foo.handleFoo).toHaveBeenCalled()
expect(ignition.state()).toBe 'editing'
describe 'ContentTools.IgnitionUI.mount()', () ->
it 'should mount the component', () ->
ignition = new ContentTools.IgnitionUI()
editor.attach(ignition)
ignition.mount()
expect(ignition.isMounted()).toBe true
describe 'ContentTools.IgnitionUI.state()', () ->
it 'should change the state of the ignition switch', () ->
# Set the ignition to editing
ignition = new ContentTools.IgnitionUI()
ignition.state('ready')
# Create function we can spy on to ensure the event is triggered
foo = {
handleFoo: () ->
return
}
spyOn(foo, 'handleFoo')
# Bind the spied on function to the event
ignition.addEventListener('statechange', foo.handleFoo)
# Trigger the edit event
ignition.state('editing')
expect(foo.handleFoo).toHaveBeenCalled()
expect(ignition.state()).toBe 'editing'
it 'should get the state of the iginition switch', () ->
# Set the ignition to editing
ignition = new ContentTools.IgnitionUI()
expect(ignition.state()).toBe 'ready'
ignition.edit()
expect(ignition.state()).toBe 'editing'
ignition.busy(true)
expect(ignition.state()).toBe 'busy'
describe 'ContentTools.IgnitionUI.unmount()', () ->
it 'should unmount the component', () ->
ignition = new ContentTools.IgnitionUI()
editor.attach(ignition)
ignition.mount()
ignition.unmount()
expect(ignition.isMounted()).toBe false
# Events
describe 'ContentTools.IgnitionUI > Events', () ->
it 'should call `edit` when edit button is clicked', () ->
ignition = editor._ignition
# Spy on the edit methof
spyOn(ignition, 'edit')
# Create a fake click event against the modal's DOM element
clickEvent = document.createEvent('CustomEvent')
clickEvent.initCustomEvent('click', false, false, null)
ignition._domEdit.dispatchEvent(clickEvent)
expect(ignition.edit).toHaveBeenCalled()
it 'should call `cancel` when cancel button is clicked', () ->
ignition = editor._ignition
# Spy on the edit methof
spyOn(ignition, 'cancel')
# Create a fake click event against the modal's DOM element
clickEvent = document.createEvent('CustomEvent')
clickEvent.initCustomEvent('click', false, false, null)
ignition._domCancel.dispatchEvent(clickEvent)
expect(ignition.cancel).toHaveBeenCalled()
it 'should call `confirm` when confirm button is clicked', () ->
ignition = editor._ignition
# Spy on the edit methof
spyOn(ignition, 'confirm')
# Create a fake click event against the modal's DOM element
clickEvent = document.createEvent('CustomEvent')
clickEvent.initCustomEvent('click', false, false, null)
ignition._domConfirm.dispatchEvent(clickEvent)
expect(ignition.confirm).toHaveBeenCalled()