Skip to content

Commit

Permalink
Fix tests for file upload, brought implementation up to coding standard.
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Apr 25, 2012
1 parent b1a78e1 commit f58f4f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ zombie.js-changelog(7) -- Changelog
Now requires Node 0.6.x or later. Also upgraded to CoffeeScript 1.3.1, which
helped find a couple of skipped tests.

Added File object in browser (Ian Young)


## Version 0.12.15 2012-02-23

Expand Down
20 changes: 11 additions & 9 deletions lib/zombie/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ History = require("./history")
Interact = require("./interact")
JSDom = require("jsdom")
Mime = require("mime")
PATH = require("path")
Path = require("path")
Resources = require("./resources")
Storages = require("./storage")
URL = require("url")
XHR = require("./xhr")
WebSocket = require("./websocket")

FILE_CLASS = ->

class File

HTML = JSDom.dom.level3.html
MOUSE_EVENT_NAMES = ["mousedown", "mousemove", "mouseup"]
Expand Down Expand Up @@ -187,7 +188,7 @@ class Browser extends EventEmitter
@_ws.extend newWindow
newWindow.screen = new Screen()
newWindow.JSON = JSON
newWindow.File = FILE_CLASS
newWindow.File = File
newWindow.Image = (width, height)->
img = new HTML.HTMLImageElement(newWindow.document)
img.width = width
Expand Down Expand Up @@ -598,19 +599,20 @@ class Browser extends EventEmitter
attach: (selector, filename, callback)->
field = @field(selector)
if field && field.tagName == "INPUT" && field.type == "file"
FS.stat filename, (e, stat) =>
if filename
stat = FS.statSync(filename)
field.value = filename
file = new FILE_CLASS()
file.name = PATH.basename filename
file.type = Mime.lookup filename
file = new File()
file.name = Path.basename(filename)
file.type = Mime.lookup(filename)
file.size = stat.size
field.files ?= []
field.files.push file
@fire "change", field, callback
unless callback
return this
else
throw new Error("No file INPUT matching '#{selector}'")
unless callback
return this

# ### browser.select(selector, value, callback) => this
#
Expand Down
8 changes: 5 additions & 3 deletions spec/forms_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ Vows.describe("Forms").addBatch

"file upload with JS":
topic: ->
brains.get "/forms/upload", (req, res)->
brains.get "/forms/upload-js", (req, res)->
res.send """
<html>
<head>
Expand Down Expand Up @@ -759,10 +759,11 @@ Vows.describe("Forms").addBatch
"""

"text":
Browser.wants "http://localhost:3003/forms/upload"
Browser.wants "http://localhost:3003/forms/upload-js"
topic: (browser)->
filename = __dirname + "/data/random.txt"
filename = "#{__dirname}/data/random.txt"
browser.attach "my_file", filename, @callback

"should call callback": (browser)->
assert.equal browser.text("title"), "Upload done"
"should have filename": (browser)->
Expand All @@ -774,6 +775,7 @@ Vows.describe("Forms").addBatch
"should be of type File": (browser)->
assert.equal browser.text("#is_file"), "true"


.addBatch

"content length":
Expand Down

0 comments on commit f58f4f7

Please sign in to comment.