Skip to content

Commit 389f324

Browse files
committed
Adding accounts select unit tests
1 parent 3b1684a commit 389f324

2 files changed

Lines changed: 213 additions & 7 deletions

File tree

test/unit/controllers/controllerApp.unit.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
var $controller,
22
$rootScope,
33
$timeout,
4-
$scope;
4+
$scope,
5+
$window;
56
var keypather;
67
var apiMocks = require('../apiMocks/index');
78

89
describe('controllerApp'.bold.underline.blue, function () {
910
var ctx = {};
10-
function setup(stateParams) {
11+
function setup(stateParams, heap, intercom, olark) {
1112
angular.mock.module('app');
1213
ctx.fakeuser = {
1314
attrs: angular.copy(apiMocks.user),
@@ -46,20 +47,33 @@ describe('controllerApp'.bold.underline.blue, function () {
4647
_$controller_,
4748
_$rootScope_,
4849
_$timeout_,
49-
_keypather_
50+
_keypather_,
51+
_$window_
5052
) {
5153
$controller = _$controller_;
5254
$rootScope = _$rootScope_;
5355
$scope = $rootScope.$new();
5456
$timeout = _$timeout_;
5557
keypather = _keypather_;
56-
58+
$window = _$window_;
5759
$rootScope.safeApply = function(cb) {
5860
$timeout(function() {
5961
$scope.$digest();
6062
});
6163
};
6264
});
65+
if (heap) {
66+
$window.heap = {
67+
identify: sinon.spy()
68+
};
69+
}
70+
if (intercom) {
71+
$window.initIntercom = sinon.spy();
72+
}
73+
if (olark) {
74+
$window.olark = sinon.spy();
75+
}
76+
6377
var ca = $controller('ControllerApp', {
6478
'$scope': $scope
6579
});
@@ -121,7 +135,7 @@ describe('controllerApp'.bold.underline.blue, function () {
121135
$rootScope.$digest();
122136
});
123137
it('should select org1, matching it from the stateParams ', function (done) {
124-
setup({});
138+
setup({}, false, false, true);
125139
var listFetchSpy = sinon.spy(function(event, name) {
126140
expect(name).to.equal(ctx.fakeOrg1.oauthName());
127141
expect($scope.dataApp.data.activeAccount).to.be.an.Object;
@@ -137,7 +151,7 @@ describe('controllerApp'.bold.underline.blue, function () {
137151
});
138152
});
139153
it('should not switch accounts if active account matches', function () {
140-
setup({});
154+
setup({}, true);
141155
var listFetchSpy = sinon.spy();
142156
keypather.set($scope, 'dataApp.data.activeAccount', ctx.fakeOrg1);
143157
$scope.$on('INSTANCE_LIST_FETCH', listFetchSpy);
@@ -151,7 +165,7 @@ describe('controllerApp'.bold.underline.blue, function () {
151165
sinon.assert.notCalled(listFetchSpy);
152166
});
153167
it('should switch accounts if active account does not match url', function (done) {
154-
setup({});
168+
setup({}, false, true);
155169
var listFetchSpy = sinon.spy(function(event, name) {
156170
expect(name).to.equal(ctx.fakeOrg2.oauthName());
157171
expect($scope.dataApp.data.activeAccount).to.be.an.Object;
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
describe('directiveAccountsSelect'.bold.underline.blue, function() {
2+
var element;
3+
var $scope, $elScope;
4+
var $rootScope;
5+
var ctx;
6+
var apiMocks = require('../apiMocks/index');
7+
8+
function makeDefaultScope() {
9+
ctx.fakeuser = {
10+
attrs: angular.copy(apiMocks.user),
11+
oauthName: function () {
12+
return 'user';
13+
},
14+
gravitar: function () {
15+
return true;
16+
}
17+
};
18+
ctx.fakeOrg1 = {
19+
attrs: angular.copy(apiMocks.user),
20+
oauthName: function () {
21+
return 'org1';
22+
},
23+
gravitar: function () {
24+
return true;
25+
}
26+
};
27+
ctx.fakeOrg2 = {
28+
attrs: angular.copy(apiMocks.user),
29+
oauthName: function () {
30+
return 'org2';
31+
},
32+
gravitar: function () {
33+
return true;
34+
}
35+
};
36+
return {
37+
data: {
38+
activeAccount: ctx.fakeuser,
39+
orgs: {models: [ctx.fakeOrg1, ctx.fakeOrg2]},
40+
user: ctx.fakeuser
41+
}
42+
};
43+
}
44+
function initState (addToScope) {
45+
ctx = {};
46+
if (!addToScope) {
47+
addToScope = makeDefaultScope();
48+
}
49+
angular.mock.module('app');
50+
ctx.stateMock = {
51+
'$current': {
52+
name: 'instance.instanceEdit'
53+
},
54+
go: function () {}
55+
};
56+
angular.mock.module('app', function ($provide) {
57+
$provide.value('$state', ctx.stateMock);
58+
$provide.value('$stateParams', {
59+
userName: 'username',
60+
instanceName: 'instanceName'
61+
});
62+
});
63+
angular.mock.inject(function($compile, _$rootScope_, $timeout){
64+
$rootScope = _$rootScope_;
65+
$scope = $rootScope.$new();
66+
67+
$rootScope.safeApply = function(cb) {
68+
$timeout(function () {
69+
$scope.$digest();
70+
});
71+
};
72+
73+
var tpl = directiveTemplate('accounts-select', {
74+
'data': 'data'
75+
});
76+
77+
Object.keys(addToScope).forEach(function (key) {
78+
$scope[key] = addToScope[key];
79+
});
80+
81+
ctx.element = $compile(tpl)($scope);
82+
$scope.$digest();
83+
});
84+
$elScope = ctx.element.isolateScope();
85+
}
86+
87+
describe('directive logic'.bold.blue, function() {
88+
it('should set isChangeAccount to false', function () {
89+
initState();
90+
$scope.$digest();
91+
expect($elScope.isChangeAccount).to.be.false;
92+
$elScope.isChangeAccount = true;
93+
$scope.$apply();
94+
$rootScope.$broadcast('app-document-click');
95+
expect($elScope.isChangeAccount).to.be.false;
96+
});
97+
it('should not emit signal and change state on when isChangeAccount to false', function () {
98+
initState();
99+
var instanceFetchSpy = sinon.spy();
100+
$rootScope.$on('INSTANCE_LIST_FETCH', instanceFetchSpy);
101+
$scope.$digest();
102+
expect($elScope.isChangeAccount).to.be.false;
103+
$elScope.selectActiveAccount(ctx.fakeOrg1);
104+
$scope.$apply();
105+
sinon.assert.notCalled(instanceFetchSpy);
106+
});
107+
it('should emit signal and change state on account change', function (done) {
108+
initState();
109+
ctx.stateMock.go = sinon.spy(function (location, state) {
110+
expect(state).to.deep.equal({
111+
userName: ctx.fakeOrg1.oauthName(),
112+
instanceName: ''
113+
});
114+
done();
115+
});
116+
var instanceFetchSpy = sinon.spy(function (event, username) {
117+
expect(username).to.equal(ctx.fakeOrg1.oauthName());
118+
});
119+
$rootScope.$on('INSTANCE_LIST_FETCH', instanceFetchSpy);
120+
$elScope.isChangeAccount = true;
121+
$scope.$digest();
122+
$elScope.selectActiveAccount(ctx.fakeOrg1);
123+
$scope.$apply();
124+
expect($scope.data.activeAccount).to.equal(ctx.fakeOrg1);
125+
});
126+
});
127+
128+
describe('directive logic'.bold.blue, function() {
129+
afterEach(function() {
130+
$rootScope.$destroy();
131+
});
132+
it('should display with an active account', function () {
133+
initState();
134+
$scope.$digest();
135+
expect(ctx.element[0].classList.contains('ng-hide')).to.not.be.ok;
136+
expect(getAccountsGroupItemsList().length).to.equal(3);
137+
expect(getAccountSelectorElement().classList.contains('in')).to.not.be.ok;
138+
});
139+
it('shouldn\'t display without an active account', function () {
140+
var scope = makeDefaultScope();
141+
delete scope.data.activeAccount;
142+
initState(scope);
143+
$scope.$digest();
144+
expect(ctx.element[0].classList.contains('ng-hide')).to.be.ok;
145+
});
146+
it('should display selector after click', function () {
147+
initState();
148+
$rootScope.$digest();
149+
click(getAccountSelectorElement());
150+
$rootScope.$digest();
151+
expect(getAccountSelectorElement().classList.contains('in')).to.be.ok;
152+
expect(getAccountsGroupItemsList().length).to.equal(3);
153+
click(getAccountSelectorElement());
154+
$scope.$digest();
155+
expect(getAccountSelectorElement().classList.contains('in')).to.not.be.ok;
156+
});
157+
it('should be able to select one of the selectors to change the account', function () {
158+
initState();
159+
$elScope.selectActiveAccount = sinon.spy();
160+
$rootScope.$digest();
161+
click(getAccountSelectorElement());
162+
$rootScope.$digest();
163+
expect(getAccountSelectorElement().classList.contains('in')).to.be.ok;
164+
expect(getAccountsGroupItemsList().length).to.equal(3);
165+
click(getAccountsGroupItemsList()[1]);
166+
$scope.$digest();
167+
sinon.assert.calledWith($elScope.selectActiveAccount, ctx.fakeOrg1);
168+
});
169+
});
170+
171+
function getAccountSelectorElement() {
172+
return ctx.element[0]
173+
.querySelector('ol.accounts-group');
174+
}
175+
function getAccountsGroupItemsList() {
176+
return ctx.element[0]
177+
.querySelectorAll('li.accounts-group-item');
178+
}
179+
});
180+
181+
function click(el){
182+
var ev = document.createEvent('MouseEvent');
183+
ev.initMouseEvent(
184+
'click',
185+
true /* bubble */, true /* cancelable */,
186+
window, null,
187+
0, 0, 0, 0, /* coordinates */
188+
false, false, false, false, /* modifier keys */
189+
0 /*left*/, null
190+
);
191+
el.dispatchEvent(ev);
192+
}

0 commit comments

Comments
 (0)