1 | connect = require '../lib/connect'
|
2 | path = require 'path'
|
3 |
|
4 | xhrs = []
|
5 | window = undefined
|
6 |
|
7 | class XMLHttpRequest
|
8 |
|
9 | @clear: ->
|
10 | xhrs = []
|
11 |
|
12 | constructor: ->
|
13 | xhrs.push @
|
14 |
|
15 | @opened = no
|
16 | @sent = no
|
17 | @responseText = no
|
18 |
|
19 | open: -> @opened = [].splice.call arguments, 0
|
20 | send: ->
|
21 | @sent = yes
|
22 | @responseText = "h.push('#{@opened[1]}');"
|
23 |
|
24 | describe 'connect', ->
|
25 |
|
26 | it 'should be a function', ->
|
27 | expect(typeof connect).toBe 'function'
|
28 |
|
29 | describe 'using express', ->
|
30 |
|
31 | handler = undefined
|
32 | beforeEach ->
|
33 | XMLHttpRequest.clear()
|
34 | window =
|
35 | eval: jasmine.createSpy "eval"
|
36 |
|
37 | handler = connect path.join(__dirname, 'fixtures/jspackle.json'), '/javascripts/project_name.js'
|
38 |
|
39 | it 'should return a function', ->
|
40 | expect(typeof handler).toBe 'function'
|
41 |
|
42 | describe 'handling a request', ->
|
43 |
|
44 | req = res = next = undefined
|
45 | beforeEach ->
|
46 | next = jasmine.createSpy 'next'
|
47 | req =
|
48 | url: '/something/else'
|
49 | res =
|
50 | statusCode: undefined
|
51 | setHeader: jasmine.createSpy "res.setHeader"
|
52 | end: jasmine.createSpy "res.end"
|
53 |
|
54 | describe 'request does not match', ->
|
55 |
|
56 | beforeEach ->
|
57 | handler req, res, next
|
58 |
|
59 | it 'moves to next middleware', ->
|
60 | expect(next).toHaveBeenCalled()
|
61 |
|
62 | describe 'request matches exactly', ->
|
63 |
|
64 | h = undefined
|
65 | beforeEach ->
|
66 | h = []
|
67 | req.url = '/javascripts/project_name.js'
|
68 | handler req, res, next
|
69 |
|
70 | it 'should send a JavaScript result', ->
|
71 | expect(res.statusCode).toEqual 200
|
72 | expect(res.setHeader).toHaveBeenCalled()
|
73 | expect(res.setHeader.calls.length).toEqual 1
|
74 | expect(res.setHeader.calls[0].args[0]).toEqual 'Content-type'
|
75 | expect(res.setHeader.calls[0].args[1]).toEqual 'text/javascript'
|
76 | expect(res.end).toHaveBeenCalled()
|
77 |
|
78 | describe 'using the JavaScript result', ->
|
79 |
|
80 | beforeEach ->
|
81 | eval res.end.calls[0].args[0]
|
82 |
|
83 | it 'should have made XHRs', ->
|
84 | expect(xhrs.length).toEqual 3
|
85 | expect(xhrs[0].opened).toEqual ['GET', '/javascripts/project_name.js/foo.js', false]
|
86 | expect(xhrs[1].opened).toEqual ['GET', '/javascripts/project_name.js/bar.js', false]
|
87 | expect(xhrs[2].opened).toEqual ['GET', '/javascripts/project_name.js/baz.js', false]
|
88 |
|
89 | it 'should have sent all XHRs', ->
|
90 | expect(xhrs[0].sent).toBeTruthy()
|
91 | expect(xhrs[1].sent).toBeTruthy()
|
92 | expect(xhrs[2].sent).toBeTruthy()
|
93 |
|
94 | it 'should have evaluated all XHR response', ->
|
95 | expect(window.eval).toHaveBeenCalled()
|
96 | expect(window.eval.calls.length).toEqual 3
|
97 | expect(window.eval.calls[0].args[0]).toEqual "h.push('#{xhrs[0].opened[1]}');"
|
98 | expect(window.eval.calls[1].args[0]).toEqual "h.push('#{xhrs[1].opened[1]}');"
|
99 | expect(window.eval.calls[2].args[0]).toEqual "h.push('#{xhrs[2].opened[1]}');"
|
100 |
|
101 |
|
102 | describe 'request only matches begining of URL', ->
|
103 | h = undefined
|
104 | beforeEach ->
|
105 | h = []
|
106 | req.url = '/javascripts/project_name.js/foo.js'
|
107 | handler req, res, next
|
108 |
|
109 | it 'should send a JavaScript result', ->
|
110 | waits 10
|
111 | runs ->
|
112 | expect(res.statusCode).toEqual 200
|
113 | expect(res.setHeader).toHaveBeenCalled()
|
114 | expect(res.setHeader.calls.length).toEqual 1
|
115 | expect(res.setHeader.calls[0].args[0]).toEqual 'Content-type'
|
116 | expect(res.setHeader.calls[0].args[1]).toEqual 'text/javascript'
|
117 | expect(res.end).toHaveBeenCalled()
|
118 | eval res.end.calls[0].args[0]
|
119 | expect(h[0]).toEqual 1000
|
120 |
|
121 | describe 'request matches a coffee-script file', ->
|
122 | h = undefined
|
123 | beforeEach ->
|
124 | h = []
|
125 | req.url = '/javascripts/project_name.js/baz.coffee'
|
126 | handler req, res, next
|
127 |
|
128 | it 'should send a JavaScript result', ->
|
129 | waits 10
|
130 | runs ->
|
131 | expect(res.statusCode).toEqual 200
|
132 | expect(res.setHeader).toHaveBeenCalled()
|
133 | expect(res.setHeader.calls.length).toEqual 1
|
134 | expect(res.setHeader.calls[0].args[0]).toEqual 'Content-type'
|
135 | expect(res.setHeader.calls[0].args[1]).toEqual 'text/javascript'
|
136 | expect(res.end).toHaveBeenCalled()
|
137 | eval res.end.calls[0].args[0]
|
138 | expect(h[0]).toEqual 1000
|
139 |
|
140 | describe 'request matches a file with querystring', ->
|
141 | h = undefined
|
142 | beforeEach ->
|
143 | h = []
|
144 | req.url = '/javascripts/project_name.js/baz.coffee?version=1.0.3'
|
145 | handler req, res, next
|
146 |
|
147 | it 'should send a JavaScript result', ->
|
148 | waits 10
|
149 | runs ->
|
150 | expect(res.statusCode).toEqual 200
|
151 | expect(res.setHeader).toHaveBeenCalled()
|
152 | expect(res.setHeader.calls.length).toEqual 1
|
153 | expect(res.setHeader.calls[0].args[0]).toEqual 'Content-type'
|
154 | expect(res.setHeader.calls[0].args[1]).toEqual 'text/javascript'
|
155 | expect(res.end).toHaveBeenCalled()
|
156 | eval res.end.calls[0].args[0]
|
157 | expect(h[0]).toEqual 1000
|