Commit 28dc8e0e by cahrens

Update unit tests.

parent 7ac1e7bb
<section class="html-edit"> <section class="html-edit">
<textarea class="tiny-mce">dummy</textarea> <textarea class="tiny-mce">dummy</textarea>
<!-- <!--
TODO: DELETE THIS FILE
The text passed in is the escaped version of The text passed in is the escaped version of
&lt;problem> &lt;problem>
&lt;p>&lt;/p> &lt;p>&lt;/p>
......
...@@ -3,53 +3,24 @@ describe 'HTMLEditingDescriptor', -> ...@@ -3,53 +3,24 @@ describe 'HTMLEditingDescriptor', ->
window.baseUrl = "/static/deadbeef" window.baseUrl = "/static/deadbeef"
afterEach -> afterEach ->
delete window.baseUrl delete window.baseUrl
describe 'Read data from server, create Editor, and get data back out', -> describe 'HTML Editor', ->
it 'Does not munge &lt', ->
# This is a test for Lighthouse #22,
# "html names are automatically converted to the symbols they describe"
# A better test would be a Selenium test to avoid duplicating the
# mako template structure in html-edit-formattingbug.html.
# However, we currently have no working Selenium tests.
loadFixtures 'html-edit-formattingbug.html'
@descriptor = new HTMLEditingDescriptor($('.html-edit'))
visualEditorStub =
isDirty: () -> false
spyOn(@descriptor, 'getVisualEditor').andCallFake () ->
visualEditorStub
data = @descriptor.save().data
expect(data).toEqual("""&lt;problem>
&lt;p>&lt;/p>
&lt;multiplechoiceresponse>
<pre>&lt;problem>
&lt;p>&lt;/p></pre>
<div><foo>bar</foo></div>""")
describe 'Saves HTML', ->
beforeEach -> beforeEach ->
loadFixtures 'html-edit.html' loadFixtures 'html-edit.html'
@descriptor = new HTMLEditingDescriptor($('.html-edit')) @descriptor = new HTMLEditingDescriptor($('.html-edit'))
it 'Returns data from Advanced Editor if Visual Editor is not dirty', -> it 'Returns data from Visual Editor if Visual Editor is dirty', ->
visualEditorStub =
isDirty: () -> false
spyOn(@descriptor, 'getVisualEditor').andCallFake () ->
visualEditorStub
expect(@descriptor.showingVisualEditor).toEqual(true)
data = @descriptor.save().data
expect(data).toEqual('Advanced Editor Text')
it 'Returns data from Advanced Editor if Visual Editor is not showing (even if Visual Editor is dirty)', ->
visualEditorStub = visualEditorStub =
isDirty: () -> true isDirty: () -> true
getContent: () -> 'from visual editor'
spyOn(@descriptor, 'getVisualEditor').andCallFake () -> spyOn(@descriptor, 'getVisualEditor').andCallFake () ->
visualEditorStub visualEditorStub
@descriptor.showingVisualEditor = false
data = @descriptor.save().data data = @descriptor.save().data
expect(data).toEqual('Advanced Editor Text') expect(data).toEqual('from visual editor')
it 'Returns data from Visual Editor if Visual Editor is dirty and showing', -> it 'Returns data from Visual Editor even if Visual Editor is not dirty', ->
visualEditorStub = visualEditorStub =
isDirty: () -> true isDirty: () -> false
getContent: () -> 'from visual editor' getContent: () -> 'from visual editor'
spyOn(@descriptor, 'getVisualEditor').andCallFake () -> spyOn(@descriptor, 'getVisualEditor').andCallFake () ->
visualEditorStub visualEditorStub
expect(@descriptor.showingVisualEditor).toEqual(true)
data = @descriptor.save().data data = @descriptor.save().data
expect(data).toEqual('from visual editor') expect(data).toEqual('from visual editor')
it 'Performs link rewriting for static assets when saving', -> it 'Performs link rewriting for static assets when saving', ->
...@@ -58,63 +29,19 @@ describe 'HTMLEditingDescriptor', -> ...@@ -58,63 +29,19 @@ describe 'HTMLEditingDescriptor', ->
getContent: () -> 'from visual editor with /c4x/foo/bar/asset/image.jpg' getContent: () -> 'from visual editor with /c4x/foo/bar/asset/image.jpg'
spyOn(@descriptor, 'getVisualEditor').andCallFake () -> spyOn(@descriptor, 'getVisualEditor').andCallFake () ->
visualEditorStub visualEditorStub
expect(@descriptor.showingVisualEditor).toEqual(true)
@descriptor.base_asset_url = '/c4x/foo/bar/asset/' @descriptor.base_asset_url = '/c4x/foo/bar/asset/'
data = @descriptor.save().data data = @descriptor.save().data
expect(data).toEqual('from visual editor with /static/image.jpg') expect(data).toEqual('from visual editor with /static/image.jpg')
describe 'Can switch to Advanced Editor', -> it 'When showing visual editor links are rewritten to c4x format', ->
beforeEach ->
loadFixtures 'html-edit.html'
@descriptor = new HTMLEditingDescriptor($('.html-edit'))
it 'Populates from Visual Editor if Advanced Visual is dirty', ->
expect(@descriptor.showingVisualEditor).toEqual(true)
visualEditorStub =
isDirty: () -> true
getContent: () -> 'from visual editor'
@descriptor.showAdvancedEditor(visualEditorStub)
expect(@descriptor.showingVisualEditor).toEqual(false)
expect(@descriptor.advanced_editor.getValue()).toEqual('from visual editor')
it 'Does not populate from Visual Editor if Visual Editor is not dirty', ->
expect(@descriptor.showingVisualEditor).toEqual(true)
visualEditorStub =
isDirty: () -> false
getContent: () -> 'from visual editor'
@descriptor.showAdvancedEditor(visualEditorStub)
expect(@descriptor.showingVisualEditor).toEqual(false)
expect(@descriptor.advanced_editor.getValue()).toEqual('Advanced Editor Text')
describe 'Can switch to Visual Editor', ->
it 'Always populates from the Advanced Editor', ->
loadFixtures 'html-edit.html'
@descriptor = new HTMLEditingDescriptor($('.html-edit'))
@descriptor.showingVisualEditor = false
visualEditorStub =
content: 'not set'
startContent: 'not set',
focus: () -> true
isDirty: () -> false
setContent: (x) -> @content = x
getContent: -> @content
@descriptor.showVisualEditor(visualEditorStub)
expect(@descriptor.showingVisualEditor).toEqual(true)
expect(visualEditorStub.getContent()).toEqual('Advanced Editor Text')
expect(visualEditorStub.startContent).toEqual('Advanced Editor Text')
it 'When switching to visual editor links are rewritten to c4x format', ->
loadFixtures 'html-edit-with-links.html'
@descriptor = new HTMLEditingDescriptor($('.html-edit')) @descriptor = new HTMLEditingDescriptor($('.html-edit'))
@descriptor.base_asset_url = '/c4x/foo/bar/asset/' @descriptor.base_asset_url = '/c4x/foo/bar/asset/'
@descriptor.showingVisualEditor = false
visualEditorStub = visualEditorStub =
content: 'not set' content: 'text /static/image.jpg'
startContent: 'not set', startContent: 'text /static/image.jpg'
focus: () -> true focus: ->
isDirty: () -> false
setContent: (x) -> @content = x setContent: (x) -> @content = x
getContent: -> @content getContent: -> @content
@descriptor.showVisualEditor(visualEditorStub) @descriptor.initInstanceCallback(visualEditorStub)
expect(@descriptor.showingVisualEditor).toEqual(true) expect(visualEditorStub.getContent()).toEqual('text /c4x/foo/bar/asset/image.jpg')
expect(visualEditorStub.getContent()).toEqual('Advanced Editor Text with link /c4x/foo/bar/asset/dummy.jpg')
expect(visualEditorStub.startContent).toEqual('Advanced Editor Text with link /c4x/foo/bar/asset/dummy.jpg')
...@@ -58,9 +58,6 @@ class @HTMLEditingDescriptor ...@@ -58,9 +58,6 @@ class @HTMLEditingDescriptor
image : "#{baseUrl}/images/ico-tinymce-code.png", image : "#{baseUrl}/images/ico-tinymce-code.png",
onclick : () -> onclick : () ->
ed.formatter.toggle('code') ed.formatter.toggle('code')
# Without this, the dirty flag does not get set unless the user also types in text.
# Visual Editor must be marked as dirty or else we won't populate the Advanced Editor from it.
ed.isNotDirty = false
}) })
@visualEditor = ed @visualEditor = ed
...@@ -86,18 +83,14 @@ class @HTMLEditingDescriptor ...@@ -86,18 +83,14 @@ class @HTMLEditingDescriptor
initInstanceCallback: (visualEditor) => initInstanceCallback: (visualEditor) =>
@rewriteLinksFromStatic(visualEditor) @rewriteLinksFromStatic(visualEditor)
@focusVisualEditor(visualEditor) visualEditor.focus()
rewriteLinksFromStatic: (visualEditor) => rewriteLinksFromStatic: (visualEditor) =>
visualEditor.setContent(rewriteStaticLinks(visualEditor.getContent({no_events: 1}), '/static/', @base_asset_url)) visualEditor.setContent(rewriteStaticLinks(visualEditor.getContent({no_events: 1}), '/static/', @base_asset_url))
focusVisualEditor: (visualEditor) =>
visualEditor.focus()
getVisualEditor: () -> getVisualEditor: () ->
### ###
Returns the instance of TinyMCE. Returns the instance of TinyMCE.
This is different from the textarea that exists in the HTML template (@tiny_mce_textarea.
Pulled out as a helper method for unit test. Pulled out as a helper method for unit test.
### ###
...@@ -105,6 +98,5 @@ class @HTMLEditingDescriptor ...@@ -105,6 +98,5 @@ class @HTMLEditingDescriptor
save: -> save: ->
visualEditor = @getVisualEditor() visualEditor = @getVisualEditor()
if visualEditor.isDirty() text = rewriteStaticLinks(visualEditor.getContent({no_events: 1}), @base_asset_url, '/static/')
text = rewriteStaticLinks(visualEditor.getContent({no_events: 1}), @base_asset_url, '/static/')
data: text data: text
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment