This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
switch.spec.js
98 lines (72 loc) · 3.18 KB
/
switch.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
describe('<md-switch>', function() {
var CHECKED_CSS = 'md-checked';
var $compile, parentScope;
beforeEach(module('ngAria', 'material.components.switch'));
beforeEach(inject(function($injector) {
var $rootScope = $injector.get('$rootScope');
parentScope = $rootScope.$new();
$compile = $injector.get('$compile');
}));
it('should set checked css class and aria-checked attributes', function() {
var template =
'<div>' +
'<md-switch ng-model="blue"></md-switch>' +
'<md-switch ng-model="green"></md-switch>' +
'</div>';
var element = $compile(template)(parentScope);
parentScope.$apply(function(){
parentScope.blue = false;
parentScope.green = true;
});
var switches = angular.element(element[0].querySelectorAll('md-switch'));
expect(switches.eq(0).hasClass(CHECKED_CSS)).toEqual(false);
expect(switches.eq(0).attr('aria-checked')).toEqual('false');
expect(switches.eq(0).attr('role')).toEqual('checkbox');
expect(switches.eq(1).hasClass(CHECKED_CSS)).toEqual(true);
expect(switches.eq(1).attr('aria-checked')).toEqual('true');
expect(switches.eq(1).attr('role')).toEqual('checkbox');
parentScope.$apply(function(){
parentScope.blue = true;
parentScope.green = false;
});
expect(switches.eq(1).hasClass(CHECKED_CSS)).toEqual(false);
expect(switches.eq(0).hasClass(CHECKED_CSS)).toEqual(true);
expect(switches.eq(1).attr('aria-checked')).toEqual('false');
expect(switches.eq(0).attr('aria-checked')).toEqual('true');
expect(switches.eq(1).attr('role')).toEqual('checkbox');
});
it('should have tabindex -1 while disabled', function() {
parentScope.value = false;
var el = $compile('<md-switch ng-disabled="value">')(parentScope);
parentScope.$apply();
expect(el.attr('tabindex')).not.toEqual('-1');
parentScope.$apply('value = true');
expect(el.attr('tabindex')).toEqual('-1');
});
it('should disable via `disabled` attribute', function() {
parentScope.value = false;
var element = $compile('<md-switch disabled>')(parentScope);
parentScope.$apply();
expect(element.attr('tabindex')).toEqual('-1');
});
it('should skip click event if releasing drag over element', function() {
var checkbox = $compile('<md-switch></md-switch>')(parentScope);
var scope = checkbox.scope();
// skipToggle is used here to imitate an ending drag, same behavior as in the component.
scope.skipToggle = true;
scope.$apply();
checkbox.triggerHandler('click');
expect(checkbox[0]).not.toHaveClass('md-checked');
});
it('should correctly invert the switch through attribute', function() {
var element = $compile('<md-switch md-invert="{{ isInverted }}">')(parentScope);
parentScope.$apply('isInverted = true');
expect(element).toHaveClass('md-inverted');
expect(element.children()[0]).toHaveClass('md-label');
expect(element.children()[1]).toHaveClass('md-container');
parentScope.$apply('isInverted = false');
expect(element).not.toHaveClass('md-inverted');
expect(element.children()[0]).toHaveClass('md-container');
expect(element.children()[1]).toHaveClass('md-label');
});
});