forked from urish/angular-spinner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
151 lines (132 loc) · 6.34 KB
/
tests.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
/* License: MIT.
* Copyright (C) 2013, 2014, Uri Shaked and contributors.
*/
'use strict';
describe('Directive: us-spinner', function () {
var Spinner;
beforeEach(module(function ($provide) {
Spinner = jasmine.createSpy('Spinner');
Spinner.prototype.spin = jasmine.createSpy('Spinner.spin');
Spinner.prototype.stop = jasmine.createSpy('Spinner.stop');
$provide.value('$window', {
location: angular.copy(window.location),
navigator: angular.copy(window.navigator),
document: window.document,
Spinner: Spinner
});
}));
beforeEach(module('angularSpinner'));
it('should create a spinner object', inject(function ($rootScope, $compile) {
var element = angular.element('<div us-spinner></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner).toHaveBeenCalled();
}));
it('should start spinning the spinner automatically', inject(function ($rootScope, $compile) {
var element = angular.element('<div us-spinner></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner.prototype.spin).toHaveBeenCalled();
expect(Spinner.prototype.stop).not.toHaveBeenCalled();
}));
it('should start spinning the second spinner without stopping the first one', inject(function ($rootScope, $compile) {
var element = angular.element('<div us-spinner></div>');
element = $compile(element)($rootScope);
var secondElement = angular.element('<div us-spinner></div>');
secondElement = $compile(secondElement)($rootScope);
$rootScope.$digest();
expect(Spinner.prototype.spin.calls.count()).toBe(2);
expect(Spinner.prototype.stop).not.toHaveBeenCalled();
}));
it('should set spinner options as given in attribute', inject(function ($rootScope, $compile) {
var element = angular.element('<div us-spinner="{width:15}"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner).toHaveBeenCalledWith({width: 15});
}));
it('should update spinner options in response to scope updates', inject(function ($rootScope, $compile) {
$rootScope.actualWidth = 25;
var element = angular.element('<div us-spinner="{width:actualWidth}"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner).toHaveBeenCalledWith({width: 25});
expect(Spinner.prototype.stop).not.toHaveBeenCalled();
$rootScope.actualWidth = 72;
$rootScope.$digest();
expect(Spinner).toHaveBeenCalledWith({width: 72});
expect(Spinner.prototype.stop).toHaveBeenCalled();
expect(Spinner.prototype.spin.calls.count()).toBe(2);
}));
it('should stop the spinner when the scope is destroyed', inject(function ($rootScope, $compile) {
var scope = $rootScope.$new();
var element = angular.element('<div us-spinner></div>');
element = $compile(element)(scope);
$rootScope.$digest();
expect(Spinner.prototype.stop).not.toHaveBeenCalled();
scope.$destroy();
expect(Spinner.prototype.stop).toHaveBeenCalled();
}));
it('should not start spinning automatically', inject(function ($rootScope, $compile) {
var element = angular.element('<div us-spinner spinner-key="spinner"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner.prototype.spin).not.toHaveBeenCalled();
}));
it('should start spinning when service trigger the spin event', inject(function ($rootScope, $compile, usSpinnerService) {
var element = angular.element('<div us-spinner spinner-key="spinner"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner.prototype.spin).not.toHaveBeenCalled();
usSpinnerService.spin('spinner');
expect(Spinner.prototype.spin).toHaveBeenCalled();
}));
it('should start spinning the spinner automatically and stop when service trigger the stop event', inject(function ($rootScope, $compile, usSpinnerService) {
var element = angular.element('<div us-spinner spinner-key="spinner" spinner-start-active="1"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner.prototype.spin).toHaveBeenCalled();
usSpinnerService.stop('spinner');
expect(Spinner.prototype.stop).toHaveBeenCalled();
}));
it('should not start spinning the spinner automatically from binding', inject(function ($rootScope, $compile) {
$rootScope.spinnerActive = false;
var element = angular.element('<div us-spinner spinner-key="spinner" spinner-start-active="spinnerActive"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner.prototype.spin).not.toHaveBeenCalled();
}));
it('should start spinning the spinner automatically from binding', inject(function ($rootScope, $compile) {
$rootScope.spinnerActive = true;
var element = angular.element('<div us-spinner spinner-key="spinner" spinner-start-active="spinnerActive"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(Spinner.prototype.spin).toHaveBeenCalled();
}));
it('should start spinning the second spinner without starting the first one', inject(function ($rootScope, $compile, usSpinnerService) {
var element = angular.element('<div us-spinner spinner-key="spinner"></div>');
element = $compile(element)($rootScope);
var secondElement = angular.element('<div us-spinner spinner-key="spinner2"></div>');
secondElement = $compile(secondElement)($rootScope);
$rootScope.$digest();
usSpinnerService.spin('spinner2');
expect(Spinner.prototype.spin.calls.count()).toBe(1);
expect(Spinner.prototype.stop).not.toHaveBeenCalled();
}));
it('should start spinning the spinners with the same key', inject(function ($rootScope, $compile, usSpinnerService) {
$compile('<div us-spinner spinner-key="spinner"></div>')($rootScope);
$compile('<div us-spinner spinner-key="spinner2"></div>')($rootScope);
$compile('<div us-spinner spinner-key="spinner"></div>')($rootScope);
$compile('<div us-spinner spinner-key="spinner2"></div>')($rootScope);
$compile('<div us-spinner spinner-key="spinner"></div>')($rootScope);
$rootScope.$digest();
usSpinnerService.spin('spinner');
expect(Spinner.prototype.spin.calls.count()).toBe(3);
expect(Spinner.prototype.stop).not.toHaveBeenCalled();
usSpinnerService.stop('spinner');
expect(Spinner.prototype.stop.calls.count()).toBe(3);
usSpinnerService.spin('spinner2');
expect(Spinner.prototype.spin.calls.count()).toBe(5);
usSpinnerService.stop('spinner2');
expect(Spinner.prototype.stop.calls.count()).toBe(5);
}));
});