-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathtoggler.js
161 lines (125 loc) · 5.85 KB
/
toggler.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
describe('Toggler', function() {
var plugin;
var $html;
afterEach(function() {
plugin.destroy();
$html.remove();
});
function appendTriggers() {
return $(`<div>
<a data-open="toggler">Open</a>
<a data-close="toggler">Close</a>
<a data-toggle="toggler">Toggle</a>
</div>`).appendTo('body')
}
describe('constructor()', function() {
it('stores the element and plugin options', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
plugin = new Foundation.Toggler($html, {});
plugin.$element.should.be.an('object');
plugin.options.should.be.an('object');
});
});
describe('init()', function() {
it('stores the class defined on the data-toggler attribute', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
plugin = new Foundation.Toggler($html, {});
plugin.className.should.equal('class');
});
it('stores the class defined on the data-toggler attribute (with leading dot)', function() {
$html = $('<div id="toggler" data-toggler=".class"></div>').appendTo('body');
plugin = new Foundation.Toggler($html, {});
plugin.className.should.equal('class');
});
it('stores defined animation classes', function() {
$html = $('<div id="toggler" data-toggler data-animate="fade-in fade-out"></div>').appendTo('body');
plugin = new Foundation.Toggler($html, {});
plugin.animationIn.should.equal('fade-in');
plugin.animationOut.should.equal('fade-out');
});
it('adds Aria attributes to click triggers', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-controls', 'toggler');
$triggers.remove();
});
it('sets aria-expanded to false if the element has toggler class and no animate and toggler class not in class set', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'false');
$triggers.remove();
});
it('sets aria-expanded to true if the element has toggler class and no animate and toggler class is in class set', function() {
$html = $('<div id="toggler" class="class" data-toggler="class"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'true');
$triggers.remove();
});
it('sets aria-expanded to true if the element is visible and animate set', function() {
$html = $('<div id="toggler" data-animate="hinge-in-from-top spin-out"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'true');
$triggers.remove();
});
it('sets aria-expanded to false if the element is invisible and animate set', function() {
var $css = $('<style>#toggler { display: none }</style>').appendTo('body');
$html = $('<div id="toggler" data-animate="hinge-in-from-top spin-out"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'false');
$triggers.remove();
$css.remove();
});
});
describe('toggle()', function() {
it('calls Toggler._toggleClass() if the element toggles with a class');
it('calls Toggler._toggleAnimate() if the element toggles with animation');
});
describe('toggleClass()', function() {
it('toggles a class on the element', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
plugin = new Foundation.Toggler($html, {});
plugin._toggleClass();
$('#toggler').should.have.class('class');
plugin._toggleClass();
$('#toggler').should.not.have.class('class');
});
it('updates aria-expanded after the class is toggled', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});
plugin._toggleClass();
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'true');
plugin._toggleClass();
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'false');
$triggers.remove();
});
});
// [TODO] Re-enable this if you can get it working in PhantomJS
xdescribe('toggleAnimate()', function() {
it('animates an invisible element in', function(done) {
var $css = $('<style>#toggler { display: none; }</style>').appendTo('body');
$html = $('<div id="toggler" data-toggler data-animate="fade-in fade-out"></div>').appendTo('body');
plugin = new Foundation.Toggler($html, {});
$html.on('on.zf.toggler', function() {
$('#toggler').should.be.visible;
$css.remove();
done();
});
plugin._toggleAnimate();
});
it('animates a visible element out', function(done) {
$html = $('<div id="toggler" data-toggler data-animate="fade-in fade-out"></div>').appendTo('body');
plugin = new Foundation.Toggler($html, {});
$html.on('off.zf.toggler', function() {
$('#toggler').should.be.hidden;
done();
});
plugin._toggleAnimate();
});
});
});