This repository has been archived by the owner on Feb 14, 2018. It is now read-only.
/
pageHandler.coffee
110 lines (83 loc) · 3.39 KB
/
pageHandler.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
pageHandler = require '../lib/pageHandler.coffee'
mockServer = require './mockServer.coffee'
# Fakes for things still stuck in legacy.coffee
# TODO: Remove these ASAP
wiki.useLocalStorage = -> false
wiki.addToJournal = ->
describe 'pageHandler.get', ->
it 'should have an empty context', ->
expect(pageHandler.context).to.eql([])
pageInformationWithoutSite = {
wasServerGenerated: false
slug: 'slugName'
rev: 'revName'
}
genericPageInformation = _.extend( {}, pageInformationWithoutSite, {site: 'siteName'} )
genericPageData = {
journal: []
}
describe 'ajax fails', ->
before ->
mockServer.simulatePageNotFound()
after ->
jQuery.ajax.restore()
it "should tell us when it can't find a page (server specified)", ->
whenGotten = sinon.spy()
whenNotGotten = sinon.spy()
pageHandler.get
pageInformation: _.clone( genericPageInformation )
whenGotten: whenGotten
whenNotGotten: whenNotGotten
expect( whenGotten.called ).to.be.false
expect( whenNotGotten.called ).to.be.true
it "should tell us when it can't find a page (server unspecified)", ->
whenGotten = sinon.spy()
whenNotGotten = sinon.spy()
pageHandler.get
pageInformation: _.clone( pageInformationWithoutSite )
whenGotten: whenGotten
whenNotGotten: whenNotGotten
expect( whenGotten.called ).to.be.false
expect( whenNotGotten.called ).to.be.true
describe 'ajax, success', ->
before ->
sinon.stub(jQuery, "ajax").yieldsTo('success', genericPageData)
$('<div id="pageHandler5" data-site="foo" />').appendTo('body')
it 'should get a page from specific site', ->
whenGotten = sinon.spy()
pageHandler.get
pageInformation: _.clone( genericPageInformation )
whenGotten: whenGotten
expect(whenGotten.calledOnce).to.be.true
expect(jQuery.ajax.calledOnce).to.be.true
expect(jQuery.ajax.args[0][0]).to.have.property('type', 'GET')
expect(jQuery.ajax.args[0][0].url).to.match(///^http://siteName/slugName\.json\?random=[a-z0-9]{8}$///)
after ->
jQuery.ajax.restore()
describe 'ajax, search', ->
before ->
mockServer.simulatePageNotFound()
pageHandler.context = ['view', 'example.com', 'asdf.test', 'foo.bar']
it 'should search through the context for a page', ->
pageHandler.get
pageInformation: _.clone( pageInformationWithoutSite )
whenGotten: sinon.stub()
whenNotGotten: sinon.stub()
expect(jQuery.ajax.args[0][0].url).to.match(///^/slugName\.json\?random=[a-z0-9]{8}$///)
expect(jQuery.ajax.args[1][0].url).to.match(///^http://example.com/slugName\.json\?random=[a-z0-9]{8}$///)
expect(jQuery.ajax.args[2][0].url).to.match(///^http://asdf.test/slugName\.json\?random=[a-z0-9]{8}$///)
expect(jQuery.ajax.args[3][0].url).to.match(///^http://foo.bar/slugName\.json\?random=[a-z0-9]{8}$///)
after ->
jQuery.ajax.restore()
describe 'pageHandler.put', ->
before ->
$('<div id="pageHandler3" />').appendTo('body')
sinon.stub(jQuery, "ajax").yieldsTo('success')
it 'should save an action', (done) ->
action = {type: 'edit', id: 1, item: {id:1}}
wiki.addToJournal = ->
expect(jQuery.ajax.args[0][0].data).to.eql({action: JSON.stringify(action)})
done()
pageHandler.put $('#pageHandler3'), action
after ->
jQuery.ajax.restore()