/
radio-group.spec.js
140 lines (113 loc) · 4.27 KB
/
radio-group.spec.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
modules.define(
'spec',
['radio-group', 'i-bem__dom', 'jquery', 'BEMHTML', 'chai', 'sinon'],
function(provide, RadioGroup, BEMDOM, $, BEMHTML, chai, sinon) {
var expect = chai.expect;
describe('radio-group', function() {
var radioGroup;
function buildRadioGroup(bemjson) {
return BEMDOM.init($(BEMHTML.apply(bemjson)).appendTo('body'))
.bem('radio-group');
}
beforeEach(function() {
radioGroup = buildRadioGroup({
block : 'radio-group',
name : 'name',
options : [
{ val : 'val1', label : 'label1' },
{ val : 'val2', label : 'label2' },
{ val : 'val3', label : 'label3', disabled : true }
],
val : 'val2'
});
});
afterEach(function() {
BEMDOM.destruct(radioGroup.domElem);
});
describe('value', function() {
it('should have correct initial value', function() {
radioGroup.getVal().should.be.equal('val2');
});
it('should have correct initial undefined value', function() {
BEMDOM.destruct(radioGroup.domElem);
radioGroup = buildRadioGroup({
block : 'radio-group',
name : 'name',
options : [{ val : 'val1', label : 'label1' }]
});
expect(radioGroup.getVal()).to.be.undefined;
});
it('should properly set allowed value', function() {
var options = radioGroup.getRadios(),
spy = sinon.spy();
radioGroup
.on('change', spy)
.setVal('val1');
options[0].hasMod('checked').should.be.true;
options[1].hasMod('checked').should.be.false;
radioGroup.getVal().should.be.equal('val1');
spy.should.have.been.calledOnce;
});
it('should not set wrong value', function() {
radioGroup.setVal('val44');
radioGroup.getVal().should.be.equal('val2');
});
it('should properly unset value', function() {
radioGroup.setVal(undefined);
expect(radioGroup.getVal()).to.be.undefined;
radioGroup.getRadios().forEach(function(option) {
option.hasMod('checked').should.be.false;
});
});
it('should not change "checked" mod on set wrong value', function() {
var options = radioGroup.getRadios();
radioGroup.setVal('val44');
options[1].hasMod('checked').should.be.true;
});
it('should not trigger "change" event on set wrong value', function() {
var spy = sinon.spy();
radioGroup
.on('change', spy)
.setVal('val44');
spy.should.not.have.been.called;
});
});
describe('focus/blur', function() {
it('should focus first enabled radio', function() {
BEMDOM.destruct(radioGroup.domElem);
radioGroup = buildRadioGroup({
block : 'radio-group',
name : 'name',
options : [
{ val : 'val1', label : 'label1', disabled : true },
{ val : 'val2', label : 'label2' }
]
});
radioGroup.setMod('focused');
radioGroup.getRadios()[0].hasMod('focused').should.be.false;
radioGroup.getRadios()[1].hasMod('focused').should.be.true;
});
it('should remove "focused" mod from currently focused radio', function() {
radioGroup.getRadios()[1].setMod('focused');
radioGroup.delMod('focused');
radioGroup.getRadios()[1].hasMod('focused').should.be.false;
});
});
describe('disabled', function() {
it('should disable all radios in group', function() {
radioGroup.setMod('disabled');
var disabled = true;
radioGroup.getRadios().forEach(function(radio) {
radio.hasMod('disabled') || (disabled = false);
});
disabled.should.be.true;
});
});
describe('getName()', function() {
it('should return right name', function() {
radioGroup.getName().should.be.equal('name');
});
});
});
provide();
});