forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathionicModal.unit.js
165 lines (137 loc) · 5.32 KB
/
ionicModal.unit.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
162
163
164
165
describe('Ionic Modal', function() {
var modal, q, timeout, ionicPlatform, rootScope;
beforeEach(module('ionic.service.modal'));
beforeEach(module('ionic.service.platform'));
beforeEach(inject(function($ionicModal, $q, $templateCache, $timeout, $ionicPlatform, $rootScope) {
q = $q;
modal = $ionicModal;
timeout = $timeout;
ionicPlatform = $ionicPlatform;
rootScope = $rootScope;
$templateCache.put('modal.html', '<div class="modal"></div>');
}));
it('Should show for static template', function() {
var template = '<div class="modal"></div>';
var modalInstance = modal.fromTemplate(template);
modalInstance.show();
expect(modalInstance.el.classList.contains('modal-backdrop')).toBe(true);
expect(modalInstance.modalEl.classList.contains('modal')).toBe(true);
expect(modalInstance.modalEl.classList.contains('slide-in-up')).toBe(true);
});
it('Should show for dynamic template', function() {
var template = '<div class="modal"></div>';
var done = false;
var modalInstance = modal.fromTemplateUrl('modal.html', function(modalInstance) {
done = true;
modalInstance.show();
expect(modalInstance.el.classList.contains('modal-backdrop')).toBe(true);
expect(modalInstance.modalEl.classList.contains('modal')).toBe(true);
expect(modalInstance.modalEl.classList.contains('active')).toBe(true);
});
timeout.flush();
expect(done).toBe(true);
});
it('should set isShown on show/hide', function() {
var m = modal.fromTemplate('<div class="modal">hello</div>');
expect(m.isShown()).toBe(false);
m.show();
expect(m.isShown()).toBe(true);
m.hide();
expect(m.isShown()).toBe(false);
});
it('should set isShown on remove', function() {
var m = modal.fromTemplate('<div class="modal">hello</div>');
expect(m.isShown()).toBe(false);
m.show();
expect(m.isShown()).toBe(true);
m.remove();
expect(m.isShown()).toBe(false);
});
it('show & remove should add .model-open to body', inject(function() {
var m = modal.fromTemplate('<div class="modal">hi</div>');
m.show();
expect(angular.element(document.body).hasClass('modal-open')).toBe(true);
m.remove();
timeout.flush();
expect(angular.element(document.body).hasClass('modal-open')).toBe(false);
}));
it('show & hide should add .model-open body', inject(function() {
var m = modal.fromTemplate('<div class="modal">hi</div>');
m.show();
expect(angular.element(document.body).hasClass('modal-open')).toBe(true);
m.hide();
timeout.flush();
expect(angular.element(document.body).hasClass('modal-open')).toBe(false);
}));
it('should animate leave and destroy scope on remove', inject(function($animate) {
var m = modal.fromTemplate('<div class="modal"></div>');
spyOn($animate, 'leave').andCallFake(function(el, cb) { cb(); });
spyOn(m.scope, '$destroy');
m.remove();
timeout.flush();
expect(m.scope.$destroy).toHaveBeenCalled();
}));
it('Should close on hardware back button', function() {
var template = '<div class="modal"></div>';
var modalInstance = modal.fromTemplate(template);
modalInstance.show();
timeout.flush();
expect(modalInstance.isShown()).toBe(true);
expect( Object.keys(rootScope.$backButtonActions).length ).toEqual(1);
ionicPlatform.hardwareBackButtonClick();
expect(modalInstance.isShown()).toBe(false);
});
it('should broadcast "modal.shown" on show', function() {
var template = '<div class="modal"></div>';
var m = modal.fromTemplate(template, {});
spyOn(m.scope.$parent, '$broadcast');
m.show();
timeout.flush();
expect(m.scope.$parent.$broadcast).toHaveBeenCalledWith('modal.shown');
});
it('should broadcast "modal.hidden" on hide', function() {
var template = '<div class="modal"></div>';
var m = modal.fromTemplate(template, {});
spyOn(m.scope.$parent, '$broadcast');
m.hide();
expect(m.scope.$parent.$broadcast).toHaveBeenCalledWith('modal.hidden');
});
it('should broadcast "modal.removed" on remove', inject(function($animate) {
var template = '<div class="modal"></div>';
var m = modal.fromTemplate(template, {});
var broadcastedModal;
var done = false;
//By the time m.remove() is done, our scope will be destroyed. so we have to save the modal
//it gives us
spyOn(m.scope.$parent, '$broadcast').andCallFake(function(e, modal) {
broadcastedModal = modal;
});
m.remove();
expect(m.scope.$parent.$broadcast).toHaveBeenCalledWith('modal.removed');
timeout.flush();
}));
it('show should return a promise resolved on hide', function() {
var template = '<div class="modal"></div>';
var m = modal.fromTemplate(template, {});
var done = false;
m.hide().then(function() {
done = true;
});
expect(m.el.classList.contains('hide')).toBe(false);
timeout.flush();
expect(m.el.classList.contains('hide')).toBe(true);
expect(done).toBe(true);
});
it('show should return a promise resolved on remove', function() {
var template = '<div class="modal"></div>';
var m = modal.fromTemplate(template, {});
var done = false;
m.remove().then(function() {
done = true;
});
spyOn(m.scope, '$destroy');
timeout.flush();
expect(m.scope.$destroy).toHaveBeenCalled();
expect(done).toBe(true);
});
});