Skip to content

Commit 9edb28c

Browse files
committed
Merge pull request sayanee#84 from simobasso/improve_test
Unit test with karma
2 parents d2998dc + 578c4d0 commit 9edb28c

10 files changed

+186
-8
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
language: node_js
22
node_js:
33
- 0.10
4+
before_script:
5+
- npm install -g bower
6+
- bower install

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
- Code code code and amend the file `dist/angular-pdf.js`
1010
- Run `npm run build` to create the build minified and example files
1111
- Code code code
12-
- Run `npm test` to ensure the [build](https://travis-ci.org/sayanee/angularjs-pdf) will pass
12+
- Run `npm test` to ensure the [build](https://travis-ci.org/sayanee/angularjs-pdf) and the tests will pass
1313
- Write a [good commit message](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#commit-message-format)
1414
- [Pull request](https://help.github.com/articles/using-pull-requests) using the new feature/patch branch
15-
- Ensure the [Travis build](https://travis-ci.org/sayanee/angularjs-pdf) passes
15+
- Ensure the [Travis build and tests](https://travis-ci.org/sayanee/angularjs-pdf) passes
1616

1717
##Make a Release (for maintainers only)
1818

Gruntfile.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ module.exports = function(grunt) {
112112
'dist/angular-pdf.min.js': [ 'dist/angular-pdf.js' ]
113113
}
114114
}
115+
},
116+
karma: {
117+
unit: {
118+
configFile: 'test/karma.conf.js'
119+
}
115120
}
116121
});
117122

@@ -122,19 +127,22 @@ module.exports = function(grunt) {
122127
grunt.loadNpmTasks('grunt-jscs');
123128
grunt.loadNpmTasks('grunt-jsonlint');
124129
grunt.loadNpmTasks('grunt-contrib-uglify');
130+
grunt.loadNpmTasks('grunt-karma');
125131

126132
grunt.registerTask('default', [
127133
'clean',
128134
'jsonlint',
129135
'jscs',
130136
'jshint',
131137
'copy',
132-
'uglify'
138+
'uglify',
139+
'karma'
133140
]);
134141

135142
grunt.registerTask('check', [
136143
'jsonlint',
137144
'jscs',
138-
'jshint'
145+
'jshint',
146+
'karma'
139147
]);
140148
};

bower.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"angular": "1.4.8",
1010
"pdfjs-dist": "1.3.76"
1111
},
12+
"devDependencies": {
13+
"angular-mocks": "1.4.7"
14+
},
1215
"ignore": [
1316
"**/.*",
1417
"package.json",

example/index.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ <h1>{{pdfName}}</h1>
1919

2020
<script src="js/lib/angular.min.js"></script>
2121

22-
<script>
23-
var app = angular.module('App', ['pdf']);
24-
</script>
22+
<script scr="js/app.js"></script>
2523
<script src="js/controllers/docCtrl.js"></script>
2624
<script src="js/directives/angular-pdf.js"></script>
2725

example/js/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var app = angular.module('App', ['pdf']);

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
"grunt-contrib-jshint": "0.11.3",
1818
"grunt-contrib-uglify": "0.10.1",
1919
"grunt-jscs": "2.3.0",
20-
"grunt-jsonlint": "1.0.6"
20+
"grunt-jsonlint": "1.0.6",
21+
"grunt-karma": "^0.12.1",
22+
"jasmine-core": "^2.4.1",
23+
"karma": "^0.13.15",
24+
"karma-jasmine": "^0.3.6",
25+
"karma-ng-html2js-preprocessor": "^0.2.0",
26+
"karma-phantomjs-launcher": "^0.2.1",
27+
"phantomjs": "^1.9.19"
2128
},
2229
"scripts": {
2330
"test": "grunt --verbose",

test/karma.conf.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Karma configuration
2+
// Generated on Tue Dec 15 2015 22:21:55 GMT+0100 (CET)
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+
{pattern: 'example/pdf/relativity.pdf', included: false, served: true},
19+
'bower_components/pdfjs-dist/web/compatibility.js',
20+
'example/js/lib/pdf.js',
21+
'example/js/lib/pdf.worker.js',
22+
'example/js/lib/angular.min.js',
23+
'example/js/app.js',
24+
'example/js/controllers/docCtrl.js',
25+
'dist/angular-pdf.min.js',
26+
'example/partials/*.html',
27+
'bower_components/angular-mocks/angular-mocks.js',
28+
'test/spec/*.js'
29+
],
30+
31+
32+
// list of files to exclude
33+
exclude: [
34+
],
35+
36+
// preprocess matching files before serving them to the browser
37+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
38+
preprocessors: {
39+
'example/partials/*.html': ['ng-html2js']
40+
},
41+
42+
proxies: {
43+
'/pdf': '/base/example/pdf'
44+
},
45+
46+
// test results reporter to use
47+
// possible values: 'dots', 'progress'
48+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
49+
reporters: ['progress'],
50+
51+
52+
// web server port
53+
port: 9876,
54+
55+
// enable / disable colors in the output (reporters and logs)
56+
colors: true,
57+
58+
59+
// level of logging
60+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
61+
logLevel: config.LOG_INFO,
62+
63+
64+
// enable / disable watching file and executing tests whenever any file changes
65+
autoWatch: false,
66+
67+
68+
// start these browsers
69+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
70+
browsers: ['PhantomJS'],
71+
72+
73+
// Continuous Integration mode
74+
// if true, Karma captures browsers, runs the tests and exits
75+
singleRun: true,
76+
77+
// Concurrency level
78+
// how many browser should be started simultanous
79+
concurrency: Infinity,
80+
81+
ngHtml2JsPreprocessor: {
82+
// strip this from the file path
83+
stripPrefix: 'example/',
84+
moduleName: 'my.templates'
85+
}
86+
})
87+
}

test/spec/docCtrlSpec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
describe('docCtrl', function() {
2+
beforeEach(module('App'));
3+
4+
var $controller;
5+
6+
beforeEach(inject(function(_$controller_){
7+
// The injector unwraps the underscores (_) from around the parameter names when matching
8+
$controller = _$controller_;
9+
}));
10+
describe('$scope.getNavStyle', function() {
11+
var $scope, controller;
12+
13+
beforeEach(function() {
14+
$scope = {};
15+
controller = $controller('DocCtrl', { $scope: $scope });
16+
});
17+
18+
it('return "pdf-controls fixed" classes if the scroll is > 100', function() {
19+
var navStyle = $scope.getNavStyle(101);
20+
expect(navStyle).toEqual('pdf-controls fixed');
21+
});
22+
23+
it('return "pdf-controls" class if the scroll is < 100', function() {
24+
var navStyle = $scope.getNavStyle(99);
25+
expect(navStyle).toEqual('pdf-controls');
26+
});
27+
});
28+
});

test/spec/ngPdfSpec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
describe('ngPdf', function() {
2+
console.log = function() {};
3+
var element, $scope;
4+
5+
// Load the myApp module, which contains the directive
6+
beforeEach(module('App'));
7+
8+
beforeEach(module('my.templates'));
9+
10+
beforeEach(inject(function(_$compile_, _$rootScope_, _$document_){
11+
// The injector unwraps the underscores (_) from around the parameter names when matching
12+
var $compile = _$compile_;
13+
var $rootScope = _$rootScope_;
14+
var $document = _$document_;
15+
$scope = $rootScope.$new();
16+
// Compile a piece of HTML containing the directive
17+
var html = '<ng-pdf template-url="partials/viewer.html" canvasid="pdf" scale="page-fit" page=13></ng-pdf>';
18+
var elmnt = angular.element(html);
19+
$document.find('body').append(elmnt);
20+
element = $compile(elmnt)($scope);
21+
$scope.pdfUrl = '/pdf/relativity.pdf';
22+
$scope.$digest();
23+
}));
24+
25+
beforeEach(function(done){
26+
setTimeout(function() {
27+
done();
28+
}, 9000);
29+
}, 10000);
30+
31+
it('ng-pdf maust have 1 canvas', function() {
32+
var canvas = element.find('canvas');
33+
expect(canvas.length).toBe(1);
34+
});
35+
36+
it('goNext and goPrevious change page', function(){
37+
expect($scope.pageNum).toBe(13);
38+
$scope.goPrevious();
39+
expect($scope.pageNum).toBe(12);
40+
$scope.goNext();
41+
expect($scope.pageNum).toBe(13);
42+
});
43+
});

0 commit comments

Comments
 (0)