Skip to content

Commit

Permalink
fix type_spec for chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
kuceb committed Apr 24, 2019
1 parent 1525713 commit 7914f68
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,44 @@ Keyboard = Cypress.Keyboard
Promise = Cypress.Promise
$selection = require("../../../../../src/dom/selection")

## trim new lines at the end of innerText
## due to changing browser versions implementing
## this differently
trimInnerText = ($el) ->
_.trimEnd($el.get(0).innerText, "\n")

describe "src/cy/commands/actions/type", ->
before ->
cy
.visit("/fixtures/dom.html")
.then (win) ->
@body = win.document.body.outerHTML

el = cy.$$('[contenteditable]:first').get(0)

innerHtml = el.innerHTML

## by default... the last new line by itself
## will only ever count as a single new line...
## but new lines above it will count as 2 new lines...
## so by adding "3" new lines, the last counts as 1
## and the first 2 count as 2...
el.innerHTML = '<div><br></div>'.repeat(3)

## browsers changed their implementation
## of the number of newlines that <div><br></div>
## create. newer versions of chrome set 2 new lines
## per set - whereas older ones create only 1 new line.
## so we grab the current sets for the assertion later
## so this test is browser version agnostic
newLines = el.innerText

## disregard the last new line, and divide by 2...
## this tells us how many multiples of new lines
## the browser inserts for new lines other than
## the last new line
@multiplierNumNewLines = (newLines.length - 1) / 2

beforeEach ->
doc = cy.state("document")

Expand Down Expand Up @@ -1038,29 +1069,29 @@ describe "src/cy/commands/actions/type", ->
cy.$$('[contenteditable]:first').get(0).innerHTML = '<div>foo</div>'
cy.get("[contenteditable]:first")
.type("bar").then ($div) ->
expect($div.get(0).innerText).to.eql("foobar\n")
expect(trimInnerText($div)).to.eql("foobar")
expect($div.get(0).textContent).to.eql("foobar")
expect($div.get(0).innerHTML).to.eql("<div>foobar</div>")

it "can type into [contenteditable] with existing <p>", ->
cy.$$('[contenteditable]:first').get(0).innerHTML = '<p>foo</p>'
cy.get("[contenteditable]:first")
.type("bar").then ($div) ->
expect($div.get(0).innerText).to.eql("foobar\n\n")
expect(trimInnerText($div)).to.eql("foobar")
expect($div.get(0).textContent).to.eql("foobar")
expect($div.get(0).innerHTML).to.eql("<p>foobar</p>")

it "collapses selection to start on {leftarrow}", ->
cy.$$('[contenteditable]:first').get(0).innerHTML = '<div>bar</div>'
cy.get("[contenteditable]:first")
.type("{selectall}{leftarrow}foo").then ($div) ->
expect($div.get(0).innerText).to.eql("foobar\n")
expect(trimInnerText($div)).to.eql("foobar")

it "collapses selection to end on {rightarrow}", ->
cy.$$('[contenteditable]:first').get(0).innerHTML = '<div>bar</div>'
cy.get("[contenteditable]:first")
.type("{selectall}{leftarrow}foo{selectall}{rightarrow}baz").then ($div) ->
expect($div.get(0).innerText).to.eql("foobarbaz\n")
expect(trimInnerText($div)).to.eql("foobarbaz")

it "can remove a placeholder <br>", ->
cy.$$('[contenteditable]:first').get(0).innerHTML = '<div><br></div>'
Expand Down Expand Up @@ -1451,7 +1482,7 @@ describe "src/cy/commands/actions/type", ->

cy.get("[contenteditable]:first")
.type("{home}11{uparrow}{home}22{uparrow}{home}33").then ($div) ->
expect($div.get(0).innerText).to.eql("33foo\n22bar\n11baz\n")
expect(trimInnerText($div)).to.eql("33foo\n22bar\n11baz")

context "{end}", ->
it "sets which and keyCode to 35 and does not fire keypress events", (done) ->
Expand Down Expand Up @@ -1501,7 +1532,7 @@ describe "src/cy/commands/actions/type", ->

cy.get("[contenteditable]:first")
.type("{end}11{uparrow}{end}22{uparrow}{end}33").then ($div) ->
expect($div.get(0).innerText).to.eql("foo33\nbar22\nbaz11\n")
expect(trimInnerText($div)).to.eql("foo33\nbar22\nbaz11")

context "{uparrow}", ->
beforeEach ->
Expand Down Expand Up @@ -1540,7 +1571,7 @@ describe "src/cy/commands/actions/type", ->

cy.get("[contenteditable]:first")
.type("{leftarrow}{leftarrow}{uparrow}11{uparrow}22{downarrow}{downarrow}33").then ($div) ->
expect($div.get(0).innerText).to.eql("foo22\nb11ar\nbaz33\n")
expect(trimInnerText($div)).to.eql("foo22\nb11ar\nbaz33")

it "uparrow ignores current selection", ->
ce = cy.$$('[contenteditable]:first').get(0)
Expand All @@ -1556,7 +1587,7 @@ describe "src/cy/commands/actions/type", ->

cy.get("[contenteditable]:first")
.type("{uparrow}11").then ($div) ->
expect($div.get(0).innerText).to.eql("11foo\nbar\nbaz\n")
expect(trimInnerText($div)).to.eql("11foo\nbar\nbaz")

it "up and down arrow on textarea", ->
cy.$$('textarea:first').get(0).value = 'foo\nbar\nbaz'
Expand All @@ -1570,7 +1601,6 @@ describe "src/cy/commands/actions/type", ->
.type('{uparrow}{uparrow}')
.should('have.value', '14')


context "{downarrow}", ->
beforeEach ->
cy.$$("#comments").val("foo\nbar\nbaz")
Expand Down Expand Up @@ -1626,7 +1656,7 @@ describe "src/cy/commands/actions/type", ->

cy.get("[contenteditable]:first")
.type("{downarrow}22").then ($div) ->
expect($div.get(0).innerText).to.eql("foo\n22bar\nbaz\n")
expect(trimInnerText($div)).to.eql("foo\n22bar\nbaz")

context "{selectall}{del}", ->
it "can select all the text and delete", ->
Expand Down Expand Up @@ -1688,14 +1718,16 @@ describe "src/cy/commands/actions/type", ->
it "inserts new line into [contenteditable] ", ->
cy.get("#input-types [contenteditable]:first").invoke("text", "foo")
.type("bar{enter}baz{enter}{enter}{enter}quux").then ($div) ->
expect($div.get(0).innerText).to.eql("foobar\nbaz\n\n\nquux\n")
conditionalNewLines = "\n\n".repeat(@multiplierNumNewLines)

expect(trimInnerText($div)).to.eql("foobar\nbaz#{conditionalNewLines}\nquux")
expect($div.get(0).textContent).to.eql("foobarbazquux")
expect($div.get(0).innerHTML).to.eql("foobar<div>baz</div><div><br></div><div><br></div><div>quux</div>")

it "inserts new line into [contenteditable] from midline", ->
cy.get("#input-types [contenteditable]:first").invoke("text", "foo")
.type("bar{leftarrow}{enter}baz{leftarrow}{enter}quux").then ($div) ->
expect($div.get(0).innerText).to.eql("fooba\nba\nquuxzr\n")
expect(trimInnerText($div)).to.eql("fooba\nba\nquuxzr")
expect($div.get(0).textContent).to.eql("foobabaquuxzr")
expect($div.get(0).innerHTML).to.eql("fooba<div>ba</div><div>quuxzr</div>")

Expand Down Expand Up @@ -2312,7 +2344,6 @@ describe "src/cy/commands/actions/type", ->
.then ->
expect(changed).to.eql 0


describe "caret position", ->

it "respects being formatted by input event handlers"
Expand Down Expand Up @@ -2380,32 +2411,35 @@ describe "src/cy/commands/actions/type", ->
el.innerHTML = 'start'+
'<div>middle</div>'+
'<div>end</div>'

cy.get('[contenteditable]:first')
## move cursor to beginning of div
.type('{selectall}{leftarrow}')
.type('{rightarrow}'.repeat(14)+'[_I_]').then ->
expect(cy.$$('[contenteditable]:first').get(0).innerText).to.eql('start\nmiddle\ne[_I_]nd\n')
.type('{rightarrow}'.repeat(14)+'[_I_]').then ($el) ->
expect(trimInnerText($el)).to.eql('start\nmiddle\ne[_I_]nd')

it "can wrap cursor to prev line in [contenteditable] with {leftarrow}", ->
$el = cy.$$('[contenteditable]:first')
el = $el.get(0)

el.innerHTML = 'start'+
'<div>middle</div>'+
'<div>end</div>'
cy.get('[contenteditable]:first').type('{leftarrow}'.repeat(12)+'[_I_]').then ->
expect(cy.$$('[contenteditable]:first').get(0).innerText).to.eql('star[_I_]t\nmiddle\nend\n')

cy.get('[contenteditable]:first').type('{leftarrow}'.repeat(12)+'[_I_]').then ($el) ->
expect(trimInnerText($el)).to.eql('star[_I_]t\nmiddle\nend')

it "can wrap cursor to next line in [contenteditable] with {rightarrow} and empty lines", ->
$el = cy.$$('[contenteditable]:first')
el = $el.get(0)
el.innerHTML = '<div><br></div>'.repeat(4)+
'<div>end</div>'
el.innerHTML = '<div><br></div>'.repeat(4) + '<div>end</div>'

newLines = "\n\n\n".repeat(@multiplierNumNewLines)

cy.get('[contenteditable]:first')
.type('{selectall}{leftarrow}')
# .type('foobar'+'{rightarrow}'.repeat(6)+'[_I_]').then ->
# expect(cy.$$('[contenteditable]:first').get(0).innerText).to.eql('foobar\n\n\n\nen[_I_]d\n')
.type('foobar'+'{rightarrow}'.repeat(6)+'[_I_]').then ->
expect(trimInnerText($el)).to.eql("foobar#{newLines}\nen[_I_]d")

it "can use {rightarrow} and nested elements", ->
$el = cy.$$('[contenteditable]:first')
Expand All @@ -2415,19 +2449,19 @@ describe "src/cy/commands/actions/type", ->
cy.get('[contenteditable]:first')
.type('{selectall}{leftarrow}')
.type('{rightarrow}'.repeat(3)+'[_I_]').then ->
expect(cy.$$('[contenteditable]:first').get(0).innerText).to.eql('sta[_I_]rt\n')
expect(trimInnerText($el)).to.eql('sta[_I_]rt')

it "enter and \\n should act the same for [contenteditable]", ->

cleanseText = (text) ->
text.replace(/ /g, ' ')
## non breaking white space
text.split('\u00a0').join(' ')

expectMatchInnerText = ($el , innerText) ->
expect(cleanseText($el.get(0).innerText)).to.eql(innerText)
expect(cleanseText(trimInnerText($el))).to.eql(innerText)

## NOTE: this may only pass in Chrome since the whitespace may be different in other browsers
## even if actual and expected appear the same.
expected = "{\n foo: 1\n bar: 2\n baz: 3\n}\n"
expected = "{\n foo: 1\n bar: 2\n baz: 3\n}"
cy.get('[contenteditable]:first')
.invoke('html', '<div><br></div>')
.type('{{}{enter} foo: 1{enter} bar: 2{enter} baz: 3{enter}}')
Expand All @@ -2438,7 +2472,6 @@ describe "src/cy/commands/actions/type", ->
.should ($el) ->
expectMatchInnerText($el, expected)


it "enter and \\n should act the same for textarea", ->
expected = "{\n foo: 1\n bar: 2\n baz: 3\n}"
cy.get('textarea:first')
Expand All @@ -2449,8 +2482,6 @@ describe "src/cy/commands/actions/type", ->
.type('{{}\n foo: 1\n bar: 2\n baz: 3\n}')
.should('have.prop', 'value', expected)



describe "{enter}", ->
beforeEach ->
@$forms = cy.$$("#form-submits")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe "src/cy/commands/navigation", ->
expect(stub2).to.be.calledOnce
expect(stub3).to.be.calledOnce

# Array(100).fill().map ->
it "removes listeners", ->
win = cy.state("window")

Expand All @@ -100,6 +101,14 @@ describe "src/cy/commands/navigation", ->
expect(rel).to.be.calledWith("beforeunload")
expect(rel).to.be.calledWith("unload")


# cy.reload().then ->
# cy.wrap(null).should ->
# expect(rel).to.be.calledWith("beforeunload")
# expect(rel).to.be.calledWith("unload")



describe "errors", ->
beforeEach ->
Cypress.config("defaultCommandTimeout", 50)
Expand Down Expand Up @@ -149,6 +158,7 @@ describe "src/cy/commands/navigation", ->
expect(win.foo).to.be.undefined

it "throws when reload times out", (done) ->
console.time('foo')
locReload = cy.spy(Cypress.utils, "locReload")

cy
Expand All @@ -167,6 +177,8 @@ describe "src/cy/commands/navigation", ->
cy.on "fail", (err) ->
expected = true

console.timeEnd('foo')

expect(err.message).to.include "Your page did not fire its 'load' event within '1ms'."

.reload({timeout: 1})
Expand Down Expand Up @@ -232,17 +244,18 @@ describe "src/cy/commands/navigation", ->
$(doc.body).empty().html(@body)

## TODO: fix this
it.skip "(FLAKY) sets timeout to Cypress.config(pageLoadTimeout)", ->
timeout = cy.spy Promise.prototype, "timeout"
Cypress.config("pageLoadTimeout", 4567)
# it.skip "(FLAKY) sets timeout to Cypress.config(pageLoadTimeout)", ->
# timeout = cy.spy Promise.prototype, "timeout"
# Cypress.config("pageLoadTimeout", 4567)

cy
.visit("/fixtures/jquery.html")
.go("back").then ->
expect(timeout).to.be.calledWith(4567, "go")
# cy
# .visit("/fixtures/jquery.html")
# .go("back").then ->
# expect(timeout).to.be.calledWith(4567, "go")

it "removes listeners", ->
cy
.visit("/fixtures/generic.html")
.visit("/fixtures/jquery.html")
.then ->
winLoadListeners = cy.listeners("window:load")
Expand All @@ -266,6 +279,7 @@ describe "src/cy/commands/navigation", ->
stub3 = cy.stub()

cy
.visit("/fixtures/generic.html")
.visit("/fixtures/jquery.html")
.then ->
cy.on("stability:changed", stub1)
Expand All @@ -279,6 +293,7 @@ describe "src/cy/commands/navigation", ->

it "removes listeners from window", ->
cy
.visit("/fixtures/generic.html")
.visit("/fixtures/jquery.html")
.then (win) ->
rel = cy.stub(win, "removeEventListener")
Expand Down Expand Up @@ -369,6 +384,7 @@ describe "src/cy/commands/navigation", ->

it "logs go", ->
cy
.visit("/fixtures/generic.html")
.visit("/fixtures/jquery.html")
.go("back").then ->
lastLog = @lastLog
Expand All @@ -378,12 +394,14 @@ describe "src/cy/commands/navigation", ->

it "can turn off logging", ->
cy
.visit("/fixtures/generic.html")
.visit("/fixtures/jquery.html")
.go("back", {log: false}).then ->
expect(@lastLog).to.be.undefined

it "does not log 'Page Load' events", ->
cy
.visit("/fixtures/generic.html")
.visit("/fixtures/jquery.html")
.go("back").then ->
@logs.slice(0).forEach (log) ->
Expand All @@ -393,6 +411,7 @@ describe "src/cy/commands/navigation", ->
beforeunload = false

cy
.visit("/fixtures/generic.html")
.visit("/fixtures/jquery.html")
.window().then (win) ->
cy.on "window:before:unload", =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe "return values", ->

it "stringifies function bodies", (done) ->
cy.on "fail", (err) ->
expect(err.message).to.include("> function () {")
expect(err.message).to.include("> function")
expect(err.message).to.include("return \"foo\";")
expect(err.message).to.include("Cypress detected that you invoked one or more cy commands but returned a different value.")

Expand Down Expand Up @@ -78,7 +78,7 @@ describe "return values", ->
expect(lastLog.get("name")).to.eq("foo")
expect(lastLog.get("error")).to.eq(err)
expect(err.message).to.include("> cy.foo()")
expect(err.message).to.include("> function () {")
expect(err.message).to.include("> function")
expect(err.message).to.include("return \"bar\";")
expect(err.message).to.include("Cypress detected that you invoked one or more cy commands in a custom command but returned a different value.")

Expand Down
1 change: 1 addition & 0 deletions packages/runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"lib"
],
"devDependencies": {
"@babel/plugin-proposal-decorators": "7.4.0",
"@babel/plugin-proposal-object-rest-spread": "7.4.0",
"@cypress/react-tooltip": "0.4.0",
"bin-up": "1.1.0",
Expand Down

0 comments on commit 7914f68

Please sign in to comment.