Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 3ff07c9

Browse files
author
James Kleeh
committed
Added unit tests
1 parent 929696a commit 3ff07c9

File tree

7 files changed

+325
-3
lines changed

7 files changed

+325
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.idea
2+
/node_modules

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Apache License
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {yyyy} {name of copyright owner}
189+
Copyright 2015 James Kleeh
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

angular-confirm.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* angular-confirm
33
* http://schlogen.github.io/angular-confirm/
4-
* Version: 1.0.2 - 2015-28-04
4+
* Version: 1.0.4 - 2015-28-04
55
* License: Apache
66
*/
77
angular.module('angular-confirm', ['ui.bootstrap'])
@@ -15,6 +15,7 @@ angular.module('angular-confirm', ['ui.bootstrap'])
1515
$scope.cancel = function () {
1616
$modalInstance.dismiss('cancel');
1717
};
18+
1819
}])
1920
.value('$confirmModalDefaults', {
2021
template: '<div class="modal-header"><h3 class="modal-title">{{data.title}}</h3></div>' +
@@ -58,6 +59,7 @@ angular.module('angular-confirm', ['ui.bootstrap'])
5859
confirmCancel: '@'
5960
},
6061
link: function(scope, element, attrs) {
62+
6163
function reBind(func) {
6264
element.unbind("click").bind("click", function($event) {
6365
$event.preventDefault();
@@ -79,7 +81,7 @@ angular.module('angular-confirm', ['ui.bootstrap'])
7981
$confirm(data).then(scope.ngClick);
8082
}
8183

82-
if ('confirmIf' in attrs) {
84+
if (attrs.confirmIf && attrs.confirmIf != "") {
8385
scope.$watch('confirmIf', function(newVal) {
8486
if (newVal) {
8587
reBind(bindConfirm);

gulpfile.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var gulp = require('gulp'),
2+
karma = require('karma').server;
3+
4+
gulp.task('test-unit', function(done) {
5+
karma.start({
6+
configFile: __dirname + '\\test\\karma.conf.js',
7+
singleRun: true
8+
}, done);
9+
});

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "angular-confirm",
3+
"version": "1.0.5",
4+
"description": "Angular Confirm Modal",
5+
"main": "angular-confirm.js",
6+
"scripts": {
7+
"test": "gulp test-unit"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/Schlogen/angular-confirm.git"
12+
},
13+
"keywords": [
14+
"angular",
15+
"confirm",
16+
"angular-ui",
17+
"$modal"
18+
],
19+
"author": "James Kleeh",
20+
"license": "Apache",
21+
"bugs": {
22+
"url": "https://github.com/Schlogen/angular-confirm/issues"
23+
},
24+
"homepage": "https://github.com/Schlogen/angular-confirm",
25+
"devDependencies": {
26+
"angular": "1.4.2",
27+
"angular-bootstrap": "0.12.0",
28+
"angular-mocks": "^1.4.2",
29+
"gulp": "3.9.0",
30+
"jasmine-core": "^2.3.4",
31+
"karma": "0.12.37",
32+
"karma-chrome-launcher": "^0.2.0",
33+
"karma-jasmine": "^0.3.6"
34+
}
35+
}

test/karma.conf.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Karma configuration
2+
// Generated on Tue Jul 14 2015 10:10:58 GMT-0400 (Eastern Daylight Time)
3+
4+
module.exports = function(config) {
5+
config.set({
6+
7+
// base path that will be used to resolve all patterns (eg. files, exclude)
8+
basePath: '',
9+
10+
11+
// frameworks to use
12+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13+
frameworks: ['jasmine'],
14+
15+
16+
// list of files / patterns to load in the browser
17+
files: [
18+
'../node_modules/angular/angular.js',
19+
'../node_modules/angular-bootstrap/dist/ui-bootstrap-tpls.js',
20+
'../node_modules/angular-mocks/angular-mocks.js',
21+
'../angular-confirm.js',
22+
'unit/**/*Spec.js'
23+
],
24+
25+
26+
// list of files to exclude
27+
exclude: [
28+
],
29+
30+
31+
// preprocess matching files before serving them to the browser
32+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
33+
preprocessors: {
34+
},
35+
36+
37+
// test results reporter to use
38+
// possible values: 'dots', 'progress'
39+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
40+
reporters: ['progress'],
41+
42+
43+
// web server port
44+
port: 9876,
45+
46+
47+
// enable / disable colors in the output (reporters and logs)
48+
colors: true,
49+
50+
51+
// level of logging
52+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
53+
logLevel: config.LOG_INFO,
54+
55+
56+
// enable / disable watching file and executing tests whenever any file changes
57+
autoWatch: true,
58+
59+
60+
// start these browsers
61+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
62+
browsers: ['Chrome'],
63+
64+
65+
// Continuous Integration mode
66+
// if true, Karma captures browsers, runs the tests and exits
67+
singleRun: false
68+
})
69+
}

test/unit/confirmSpec.js

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
describe('angular-confirm', function() {
2+
3+
var $rootScope, $modal;
4+
5+
beforeEach(angular.mock.module('angular-confirm', function ($provide) {
6+
7+
$provide.decorator('$modal', function($delegate) {
8+
$modal = {
9+
open: jasmine.createSpy('$modal.open', function(settings) {
10+
return {result: settings};
11+
})
12+
};
13+
return $modal;
14+
});
15+
16+
$provide.decorator('$confirm', function($delegate) {
17+
return jasmine.createSpy('$confirm', $delegate);
18+
});
19+
20+
}));
21+
22+
beforeEach(angular.mock.inject(function (_$rootScope_) {
23+
$rootScope = _$rootScope_;
24+
}));
25+
26+
describe('ConfirmModalController', function() {
27+
var $scope, controller, data = {testVal: 1}, $modalInstance;
28+
29+
beforeEach(angular.mock.inject(function($controller) {
30+
$scope = $rootScope.$new();
31+
$modalInstance = {
32+
close: jasmine.createSpy('modalInstance.close'),
33+
dismiss: jasmine.createSpy('modalInstance.dismiss'),
34+
result: {
35+
then: jasmine.createSpy('modalInstance.result.then')
36+
}
37+
};
38+
controller = $controller('ConfirmModalController', {"$scope": $scope, "$modalInstance": $modalInstance, "data": data});
39+
}));
40+
41+
it("should copy the data, not use it as a reference", function() {
42+
data.testVal = 2;
43+
expect($scope.data.testVal).toEqual(1);
44+
});
45+
46+
it("should call close when $scope.ok is invoked", function() {
47+
$scope.ok();
48+
expect($modalInstance.close).toHaveBeenCalled();
49+
});
50+
51+
it("should call dismiss when $scope.cancel is invoked", function() {
52+
$scope.cancel();
53+
expect($modalInstance.dismiss).toHaveBeenCalledWith('cancel');
54+
});
55+
56+
});
57+
58+
describe('$confirm factory', function() {
59+
60+
var $confirm, $confirmModalDefaults;
61+
62+
beforeEach(angular.mock.inject(function(_$confirm_, _$confirmModalDefaults_) {
63+
$confirm = _$confirm_;
64+
$confirm.and.callThrough();
65+
$confirmModalDefaults = _$confirmModalDefaults_;
66+
$modal.open.and.callThrough();
67+
}));
68+
69+
it("should call $modal.open", function() {
70+
$confirm();
71+
expect($modal.open).toHaveBeenCalled();
72+
});
73+
74+
it("should override the defaults with settings passed in", function() {
75+
var settings = $confirm({}, {"template": "hello"});
76+
expect(settings.template).toEqual("hello");
77+
});
78+
79+
it("should override the default labels with the data passed in", function() {
80+
var settings = $confirm({title: "Title"});
81+
var data = settings.resolve.data();
82+
expect(data.title).toEqual("Title");
83+
expect(data.ok).toEqual('OK');
84+
});
85+
86+
it("should remove template if templateUrl is passed in", function() {
87+
var settings = $confirm({}, {templateUrl: "abc.txt"});
88+
expect(settings.template).not.toBeDefined();
89+
});
90+
91+
});
92+
93+
describe('confirm directive', function() {
94+
var $scope, element, $confirm, data;
95+
96+
beforeEach(angular.mock.inject(function (_$confirm_, $compile) {
97+
$confirm = _$confirm_;
98+
99+
$confirm.and.callFake(function(d) {
100+
data = d;
101+
return {then: function() {}}
102+
});
103+
104+
$scope = $rootScope.$new();
105+
$scope.click = jasmine.createSpy('$scope.click');
106+
}));
107+
108+
describe('resolve properties in title', function() {
109+
beforeEach(angular.mock.inject(function($compile) {
110+
$scope.name = 'Joe';
111+
element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure, {{name}}?">Delete</button>');
112+
$compile(element)($scope);
113+
$scope.$digest();
114+
}));
115+
116+
it("should resolve the name to the text property", function() {
117+
element.triggerHandler('click');
118+
expect(data.text).toEqual('Are you sure, Joe?');
119+
});
120+
});
121+
122+
describe('without confirmIf', function() {
123+
124+
beforeEach(angular.mock.inject(function($compile) {
125+
element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?">Delete</button>');
126+
$compile(element)($scope);
127+
$scope.$digest();
128+
}));
129+
130+
it("should call confirm on click and not call the function", function() {
131+
element.triggerHandler('click');
132+
expect($scope.click).not.toHaveBeenCalled();
133+
expect($confirm).toHaveBeenCalled();
134+
});
135+
136+
});
137+
138+
describe('with confirmIf option', function() {
139+
140+
beforeEach(angular.mock.inject(function($compile) {
141+
element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-if="truthy">Delete</button>');
142+
$compile(element)($scope);
143+
$scope.$digest();
144+
}));
145+
146+
it("should call confirm on click and not call the function", function() {
147+
$scope.truthy = true;
148+
$scope.$apply();
149+
element.triggerHandler('click');
150+
expect($scope.click).not.toHaveBeenCalled();
151+
expect($confirm).toHaveBeenCalled();
152+
});
153+
154+
it("should call the function", function() {
155+
$scope.truthy = false;
156+
$scope.$apply();
157+
element.triggerHandler('click');
158+
expect($scope.click).toHaveBeenCalled();
159+
expect($confirm).not.toHaveBeenCalled();
160+
});
161+
162+
});
163+
164+
describe('with confirmTitle option', function() {
165+
beforeEach(angular.mock.inject(function($compile) {
166+
element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-title="Hello!">Delete</button>');
167+
$compile(element)($scope);
168+
$scope.$digest();
169+
}));
170+
171+
it("should resolve the confirmTitle to the title property", function() {
172+
element.triggerHandler('click');
173+
expect(data.title).toEqual('Hello!');
174+
});
175+
});
176+
177+
describe('with confirmOk option', function() {
178+
beforeEach(angular.mock.inject(function($compile) {
179+
element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-ok="Okie Dokie!">Delete</button>');
180+
$compile(element)($scope);
181+
$scope.$digest();
182+
}));
183+
184+
it("should resolve the confirmTitle to the title property", function() {
185+
element.triggerHandler('click');
186+
expect(data.ok).toEqual('Okie Dokie!');
187+
});
188+
});
189+
190+
describe('with confirmCancel option', function() {
191+
beforeEach(angular.mock.inject(function($compile) {
192+
element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-cancel="No Way!">Delete</button>');
193+
$compile(element)($scope);
194+
$scope.$digest();
195+
}));
196+
197+
it("should resolve the confirmTitle to the title property", function() {
198+
element.triggerHandler('click');
199+
expect(data.cancel).toEqual('No Way!');
200+
});
201+
});
202+
203+
});
204+
205+
});

0 commit comments

Comments
 (0)