-
Notifications
You must be signed in to change notification settings - Fork 46
/
rxModalAction.spec.js
182 lines (125 loc) · 4.75 KB
/
rxModalAction.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* jshint node: true */
describe('rxModalAction', function () {
var el, scope, compile, rootScope, mockModal, modalApi, instanceApi, instanceMock, controller;
var validTemplate = '<rx-modal-action ' +
'template-url="test.html" ' +
'post-hook="post()" ' +
'pre-hook="pre()" ' +
'>Title</rx-modal-action>';
var setupModalCtrl = function (ctrl) {
controller(ctrl, {
$scope: scope,
$modalInstance: instanceApi
});
};
beforeEach(function () {
// Load the directive's module
module('encore.ui.rxModalAction');
// load the template
module('templates/rxModalAction.html');
module('templates/rxModalActionForm.html');
modalApi = {
open: sinon.stub().returns({
result: {
then: function (onSubmit, onCancel) {
instanceApi.close = onSubmit;
instanceApi.dismiss = onCancel;
}
}
})
};
instanceApi = {
close: function () {},
dismiss: function () {}
};
instanceMock = sinon.mock(instanceApi);
// Provide any mocks needed
module(function ($provide) {
mockModal = sinon.mock(modalApi);
$provide.value('$modal', modalApi);
});
// Inject in angular constructs
inject(function ($rootScope, $compile, $controller) {
rootScope = $rootScope;
scope = $rootScope.$new();
scope.postAction = sinon.spy();
compile = $compile;
controller = $controller;
});
el = helpers.createDirective(validTemplate, compile, scope);
scope.$digest();
});
afterEach(function () {
// zero out link element
el = null;
});
it('should create a link to open a modal', function () {
var link = el.find('a');
expect(link).to.exist;
expect(link.text()).to.equal('Title');
});
it('should create a new modal on click', function () {
var link = el.find('a')[0];
helpers.clickElement(link);
sinon.assert.calledOnce(modalApi.open);
expect(modalApi.open.args[0][0]).to.include.keys(['templateUrl','controller','scope']);
});
it('should close on submit', function () {
var link = el.find('a')[0];
helpers.clickElement(link);
instanceMock.expects('close').once();
instanceMock.expects('dismiss').never();
setupModalCtrl(modalApi.open.getCall(0).args[0].controller);
scope.submit();
instanceMock.verify();
});
it('should dismiss modal on cancel', function () {
var link = el.find('a')[0];
helpers.clickElement(link);
instanceMock.expects('close').never();
instanceMock.expects('dismiss').once();
setupModalCtrl(modalApi.open.getCall(0).args[0].controller);
scope.cancel();
instanceMock.verify();
});
it('should dismiss modal on routeChange', function () {
var link = el.find('a')[0];
helpers.clickElement(link);
instanceMock.expects('close').never();
instanceMock.expects('dismiss').once();
setupModalCtrl(modalApi.open.getCall(0).args[0].controller);
// fake a route change
rootScope.$broadcast('$routeChangeSuccess');
instanceMock.verify();
});
it('should focus on calling link after submitting', function () {
var link = el.find('a')[0];
helpers.clickElement(link);
sinon.spy(link, 'focus');
setupModalCtrl(modalApi.open.getCall(0).args[0].controller);
scope.submit();
sinon.assert.calledOnce(link.focus);
});
it('should focus on calling link after cancelling or submitting', function () {
var link = el.find('a')[0];
helpers.clickElement(link);
sinon.spy(link, 'focus');
setupModalCtrl(modalApi.open.getCall(0).args[0].controller);
scope.cancel();
helpers.clickElement(link);
scope.submit();
sinon.assert.calledTwice(link.focus);
});
it('should run prehook function before open', function () {
scope.pre = sinon.spy();
el.scope().showModal({ preventDefault: function () {}});
expect(scope.pre.callCount).to.equal(1);
});
it('should run posthook function after close', function () {
scope.post = sinon.spy();
el.scope().showModal({ preventDefault: function () {}});
setupModalCtrl(modalApi.open.getCall(0).args[0].controller);
scope.submit();
expect(scope.post.callCount).to.equal(1);
});
});