2015-03-06 08:00:56 +08:00
|
|
|
# This tests the basic Contenteditable component. For various modules of
|
|
|
|
# the contenteditable (such as selection, tooltip, quoting, etc) see the
|
|
|
|
# related test files.
|
|
|
|
#
|
2015-05-20 07:06:59 +08:00
|
|
|
_ = require "underscore"
|
2015-06-09 03:41:31 +08:00
|
|
|
fs = require 'fs'
|
2015-02-19 06:24:16 +08:00
|
|
|
React = require "react/addons"
|
|
|
|
ReactTestUtils = React.addons.TestUtils
|
2015-10-31 08:03:33 +08:00
|
|
|
Contenteditable = require "../../src/components/contenteditable/contenteditable",
|
2015-02-19 06:24:16 +08:00
|
|
|
|
2015-10-30 05:20:41 +08:00
|
|
|
describe "Contenteditable", ->
|
2015-02-19 06:24:16 +08:00
|
|
|
beforeEach ->
|
|
|
|
@onChange = jasmine.createSpy('onChange')
|
|
|
|
html = 'Test <strong>HTML</strong>'
|
|
|
|
@component = ReactTestUtils.renderIntoDocument(
|
2015-10-30 05:20:41 +08:00
|
|
|
<Contenteditable html={html} onChange={@onChange}/>
|
2015-02-19 06:24:16 +08:00
|
|
|
)
|
2015-11-26 08:07:50 +08:00
|
|
|
|
2015-04-25 02:33:10 +08:00
|
|
|
@editableNode = React.findDOMNode(ReactTestUtils.findRenderedDOMComponentWithAttr(@component, 'contentEditable'))
|
2015-02-19 06:24:16 +08:00
|
|
|
|
|
|
|
describe "render", ->
|
|
|
|
it 'should render into the document', ->
|
2015-10-30 05:20:41 +08:00
|
|
|
expect(ReactTestUtils.isCompositeComponentWithType @component, Contenteditable).toBe true
|
2015-02-19 06:24:16 +08:00
|
|
|
|
|
|
|
it "should include a content-editable div", ->
|
2015-03-06 08:15:29 +08:00
|
|
|
expect(@editableNode).toBeDefined()
|
2015-02-19 06:24:16 +08:00
|
|
|
|
|
|
|
describe "when the html is changed", ->
|
|
|
|
beforeEach ->
|
|
|
|
@changedHtmlWithoutQuote = 'Changed <strong>NEW 1 HTML</strong><br>'
|
|
|
|
|
2015-03-06 08:00:56 +08:00
|
|
|
@performEdit = (newHTML, component = @component) =>
|
2015-03-06 08:15:29 +08:00
|
|
|
@editableNode.innerHTML = newHTML
|
2015-02-19 06:24:16 +08:00
|
|
|
|
|
|
|
it "should fire `props.onChange`", ->
|
2015-11-26 08:07:50 +08:00
|
|
|
runs =>
|
|
|
|
@performEdit('Test <strong>New HTML</strong>')
|
|
|
|
waitsFor =>
|
|
|
|
@onChange.calls.length > 0
|
|
|
|
runs =>
|
|
|
|
expect(@onChange).toHaveBeenCalled()
|
2015-02-19 06:24:16 +08:00
|
|
|
|
2015-03-03 07:33:58 +08:00
|
|
|
# One day we may make this more efficient. For now we aggressively
|
|
|
|
# re-render because of the manual cursor positioning.
|
|
|
|
it "should fire if the html is the same", ->
|
2015-02-19 06:24:16 +08:00
|
|
|
expect(@onChange.callCount).toBe(0)
|
2015-11-26 08:07:50 +08:00
|
|
|
runs =>
|
|
|
|
@performEdit(@changedHtmlWithoutQuote)
|
|
|
|
@performEdit(@changedHtmlWithoutQuote)
|
|
|
|
waitsFor =>
|
|
|
|
@onChange.callCount > 0
|
|
|
|
runs =>
|
|
|
|
expect(@onChange).toHaveBeenCalled()
|
2015-05-12 09:07:06 +08:00
|
|
|
|
2015-06-09 03:41:31 +08:00
|
|
|
describe "pasting", ->
|
|
|
|
beforeEach ->
|
|
|
|
|
|
|
|
describe "when a file item is present", ->
|
|
|
|
beforeEach ->
|
|
|
|
@mockEvent =
|
|
|
|
preventDefault: jasmine.createSpy('preventDefault')
|
|
|
|
clipboardData:
|
|
|
|
items: [{
|
|
|
|
kind: 'file'
|
|
|
|
type: 'image/png'
|
|
|
|
getAsFile: -> new Blob(['12341352312411'], {type : 'image/png'})
|
|
|
|
}]
|
|
|
|
|
|
|
|
it "should save the image to a temporary file and call `onFilePaste`", ->
|
|
|
|
onPaste = jasmine.createSpy('onPaste')
|
|
|
|
@component = ReactTestUtils.renderIntoDocument(
|
2015-10-30 05:20:41 +08:00
|
|
|
<Contenteditable html={''} onChange={@onChange} onFilePaste={onPaste} />
|
2015-06-09 03:41:31 +08:00
|
|
|
)
|
|
|
|
@editableNode = React.findDOMNode(ReactTestUtils.findRenderedDOMComponentWithAttr(@component, 'contentEditable'))
|
|
|
|
runs ->
|
|
|
|
ReactTestUtils.Simulate.paste(@editableNode, @mockEvent)
|
|
|
|
waitsFor ->
|
|
|
|
onPaste.callCount > 0
|
|
|
|
runs ->
|
|
|
|
path = require('path')
|
|
|
|
file = onPaste.mostRecentCall.args[0]
|
|
|
|
expect(path.basename(file)).toEqual('Pasted File.png')
|
|
|
|
contents = fs.readFileSync(file)
|
|
|
|
expect(contents.toString()).toEqual('12341352312411')
|
|
|
|
|