forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaginatedListSpec.coffee
More file actions
97 lines (79 loc) · 2.69 KB
/
Copy pathpaginatedListSpec.coffee
File metadata and controls
97 lines (79 loc) · 2.69 KB
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
define [
'jquery'
'compiled/PaginatedList'
], ($, PaginatedList) ->
paginatedListFixture = """
<h3>Paginated List Spec</h3>
<div id="list-wrapper">
<ul></ul>
</div>
"""
QUnit.module 'PaginatedList',
setup: ->
# server response
@response = [200, { 'Content-Type': 'application/json' }, '[{ "value": "one" }, { "value": "two" }]']
# fake template (mimics a handlebars function)
@template = (opts) ->
tpl = (opt) ->
"<li>#{opt['value']}</li>"
(tpl(opt) for opt in opts).join ''
@fixture = $(paginatedListFixture).appendTo('#fixtures')
@el =
wrapper: $('#list-wrapper')
list: $('#list-wrapper').find('ul')
@clock = sinon.useFakeTimers()
@server = sinon.fakeServer.create()
teardown: ->
@clock.restore()
@server.restore()
@fixture.remove()
test 'should fetch and display results', ->
@server.respondWith(/.+/, @response)
new PaginatedList @el.wrapper,
template: @template
url: '/api/v1/test.json'
@server.respond()
@clock.tick 500
equal @el.list.children().length, 2
test 'should display a view more link if next page is available', ->
@server.respondWith(/.+/, [@response[0], { 'Content-Type': 'application/json', 'Link': 'rel="next"' }, @response[2]])
new PaginatedList @el.wrapper,
template: @template
url: '/api/v1/test.json'
@server.respond()
@clock.tick 500
ok @el.wrapper.find('.view-more-link').length > 0
test 'should not display a view more link if there is no next page', ->
@server.respondWith(/.+/, @response)
new PaginatedList @el.wrapper,
template: @template
url: '/api/v1/test.json'
@server.respond()
@clock.tick 500
ok @el.wrapper.find('.view-more-link').length is 0
test 'should accept a template function', ->
@server.respondWith(/.+/, @response)
new PaginatedList @el.wrapper,
template: @template
url: '/api/v1/test.json'
@server.respond()
@clock.tick 500
equal @el.list.find('li:first-child').text(), 'one'
equal @el.list.find('li:last-child').text(), 'two'
test 'should accept a presenter function', ->
@server.respondWith(/.+/, @response)
new PaginatedList @el.wrapper,
presenter: (list) ->
({ value: 'changed' } for l in list)
template: @template
url: '/api/v1/test.json'
@server.respond()
@clock.tick 500
equal @el.list.find('li:first-child').text(), 'changed'
test 'should allow user to defer getJSON', ->
@spy($, 'getJSON')
new PaginatedList @el.wrapper,
start: false
template: @template,
url: '/api/v1/not-called.json'
equal $.getJSON.callCount, 0