Skip to content

Commit ac0f5d8

Browse files
author
Mehdy Dara
committed
Merge pull request #745 from Swiip/fix-test-typescript
Improve boilerplate tests
2 parents ff69fc0 + a6f63b7 commit ac0f5d8

26 files changed

+538
-81
lines changed

app/templates/_karma.conf.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ function listFiles() {
2424
path.join(conf.paths.tmp, '/serve/app/**/*.js'),
2525
path.join(conf.paths.tmp, '/**/*.spec.js'),
2626
path.join(conf.paths.tmp, '/**/*.mock.js'),
27-
<% } else if (props.jsPreprocessor.key === 'typescript') { -%>
28-
path.join(conf.paths.tmp, '/serve/app/index.module.js'),
29-
path.join(conf.paths.src, '/**/*.spec.{js,ts}'),
30-
path.join(conf.paths.src, '/**/*.mock.{js,ts}'),
3127
<% } else { -%>
3228
path.join(conf.paths.tmp, '/serve/app/index.module.js'),
33-
path.join(conf.paths.src, '/**/*.spec.js'),
34-
path.join(conf.paths.src, '/**/*.mock.js'),
3529
<% } -%>
3630
path.join(conf.paths.src, '/**/*.html')
3731
]);
@@ -63,27 +57,8 @@ module.exports = function(config) {
6357
whitelist: [path.join(conf.paths.tmp, '/**/!(*.html|*.spec|*.mock).js')]
6458
<% } -%>
6559
},
66-
67-
reporters: ['progress', 'coverage'],
68-
69-
preprocessors: {
70-
'src/**/*.html': ['ng-html2js'],
71-
<% if (props.jsPreprocessor.key === 'noJsPrepro') { -%>
72-
'src/**/!(*.spec).js': ['coverage']
73-
<% } else { -%>
74-
'.tmp/**/!(*.spec).js': ['coverage']
75-
<% } -%>
76-
},
77-
78-
// optionally, configure the reporter
79-
coverageReporter: {
80-
type : 'html',
81-
dir : 'coverage/'
82-
},
8360
<% } else { -%>
8461
frameworks: ['jasmine'],
85-
86-
reporters: ['progress'],
8762
<% } -%>
8863

8964
<% if(props.jsPreprocessor.key === 'traceur') { -%>
@@ -98,13 +73,40 @@ module.exports = function(config) {
9873
'karma-phantomjs-launcher',
9974
<% } if (props.jsPreprocessor.key === 'noJsPrepro' || props.jsPreprocessor.key === 'coffee') { -%>
10075
'karma-angular-filesort',
101-
'karma-coverage',
10276
<% } -%>
77+
'karma-coverage',
10378
'karma-jasmine',
10479
'karma-ng-html2js-preprocessor'
105-
]
80+
],
81+
82+
coverageReporter: {
83+
type : 'html',
84+
dir : 'coverage/'
85+
},
86+
87+
reporters: ['progress', 'coverage']
10688
};
10789

90+
var preprocessors = {};
91+
var pathSrcHtml = path.join(conf.paths.src, '/**/*.html');
92+
preprocessors[pathSrcHtml] = ['ng-html2js'];
93+
94+
<% if (props.jsPreprocessor.key === 'noJsPrepro') { -%>
95+
var pathSrcJs = path.join(conf.paths.src, '/**/!(*.spec).js');
96+
97+
preprocessors[pathSrcJs] = ['coverage'];
98+
<% } else if (props.jsPreprocessor.key === 'coffee') { -%>
99+
var pathTmpJs = path.join(conf.paths.tmp, '/**/!(*.spec).js');
100+
101+
preprocessors[pathTmpJs] = ['coverage'];
102+
<% } else { -%>
103+
var pathTmpJs = path.join(conf.paths.tmp, '/serve/app/index.module.js');
104+
105+
preprocessors[pathTmpJs] = ['coverage'];
106+
<% } -%>
107+
108+
configuration.preprocessors = preprocessors;
109+
108110
// This block is needed to execute Chrome on Travis
109111
// If you ever plan to use Chrome and Travis, you can keep it
110112
// If not, you can safely remove it

app/templates/_package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@
8383
"karma-phantomjs-launcher": "~0.2.0",
8484
<% } if (props.jsPreprocessor.srcExtension === 'js' || props.jsPreprocessor.srcExtension === 'coffee') { -%>
8585
"karma-angular-filesort": "~0.1.0",
86-
"karma-coverage": "~0.4.1",
8786
<% } -%>
87+
"karma-coverage": "~0.4.1",
8888
"karma-ng-html2js-preprocessor": "~0.1.2",
8989
"concat-stream": "~1.5.0",
9090
"require-dir": "~0.3.0",

app/templates/gulp/_scripts.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ gulp.task('scripts:watch', ['scripts'], function (callback) {
9797
return webpackWrapper(true, false, callback);
9898
});
9999

100+
<% if (props.jsPreprocessor.key === 'typescript') { -%>
101+
gulp.task('scripts:test', ['tsd:install'], function () {
102+
<% } else { -%>
100103
gulp.task('scripts:test', function () {
104+
<% } -%>
101105
return webpackWrapper(false, true);
102106
});
103107

app/templates/src/app/_index.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @ngInject */
2-
export function config($logProvider: ng.ILogProvider, toastrConfig) {
2+
export function config($logProvider: ng.ILogProvider, toastrConfig: any) {
33
// enable log
44
$logProvider.debugEnabled(true);
55
// set options third-party lib
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
describe 'service githubContributor', () ->
2+
3+
beforeEach module '<%- appName %>'
4+
5+
it 'should be registered', inject (githubContributor) ->
6+
expect(githubContributor).not.toEqual null
7+
8+
describe 'apiHost variable', () ->
9+
it 'should exist', inject (githubContributor) ->
10+
expect(githubContributor.apiHost).not.toEqual null
11+
12+
describe 'getContributors function', () ->
13+
it 'should exist', inject (githubContributor) ->
14+
expect(githubContributor.getContributors).not.toEqual null
15+
16+
it 'should return data', inject (githubContributor, $httpBackend) ->
17+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=1').respond 200, [{pprt: 'value'}]
18+
data = undefined
19+
githubContributor.getContributors(1).then (fetchedData) ->
20+
data = fetchedData
21+
$httpBackend.flush()
22+
expect(data).toEqual jasmine.any Array
23+
expect(data.length == 1).toBeTruthy()
24+
expect(data[0]).toEqual jasmine.any Object
25+
26+
it 'should define a limit per page as default value', inject (githubContributor, $httpBackend) ->
27+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=30').respond 200, new Array(30)
28+
data = undefined
29+
githubContributor.getContributors().then (fetchedData) ->
30+
data = fetchedData
31+
$httpBackend.flush()
32+
expect(data).toEqual jasmine.any Array
33+
expect(data.length == 30).toBeTruthy()
34+
35+
it 'should log a error', inject (githubContributor, $httpBackend, $log) ->
36+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=1').respond 500
37+
githubContributor.getContributors 1
38+
$httpBackend.flush()
39+
expect($log.error.logs).toEqual jasmine.stringMatching 'XHR Failed for'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
describe('service githubContributor', () => {
2+
beforeEach(angular.mock.module('<%- appName %>'));
3+
4+
it('should be registered', inject(githubContributor => {
5+
expect(githubContributor).not.toEqual(null);
6+
}));
7+
8+
describe('apiHost variable', () => {
9+
it('should exist', inject(githubContributor => {
10+
expect(githubContributor.apiHost).not.toEqual(null);
11+
}));
12+
});
13+
14+
describe('getContributors function', () => {
15+
it('should exist', inject(githubContributor => {
16+
expect(githubContributor.getContributors).not.toEqual(null);
17+
}));
18+
19+
it('should return data', inject((githubContributor, $httpBackend) => {
20+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=1').respond(200, [{pprt: 'value'}]);
21+
var data;
22+
githubContributor.getContributors(1).then(function(fetchedData) {
23+
data = fetchedData;
24+
});
25+
$httpBackend.flush();
26+
expect(data).toEqual(jasmine.any(Array));
27+
expect(data.length === 1).toBeTruthy();
28+
expect(data[0]).toEqual(jasmine.any(Object));
29+
}));
30+
31+
it('should define a limit per page as default value', inject((githubContributor, $httpBackend) => {
32+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=30').respond(200, new Array(30));
33+
var data;
34+
githubContributor.getContributors().then(function(fetchedData) {
35+
data = fetchedData;
36+
});
37+
$httpBackend.flush();
38+
expect(data).toEqual(jasmine.any(Array));
39+
expect(data.length === 30).toBeTruthy();
40+
}));
41+
42+
it('should log a error', inject((githubContributor, $httpBackend, $log) => {
43+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=1').respond(500);
44+
githubContributor.getContributors(1);
45+
$httpBackend.flush();
46+
expect($log.error.logs).toEqual(jasmine.stringMatching('XHR Failed for'));
47+
}));
48+
});
49+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { GithubContributor } from './githubContributor.service';
2+
3+
describe('service githubContributor', () => {
4+
beforeEach(angular.mock.module('<%- appName %>'));
5+
6+
it('should be registered', inject((githubContributor: GithubContributor) => {
7+
expect(githubContributor).not.toBeNull();
8+
}));
9+
10+
describe('getContributors function', () => {
11+
it('should return data', inject((githubContributor: GithubContributor, $httpBackend: ng.IHttpBackendService) => {
12+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=1').respond(200, [{pprt: 'value'}]);
13+
let data: any[];
14+
githubContributor.getContributors(1).then((fetchedData: any[]) => {
15+
data = fetchedData;
16+
});
17+
$httpBackend.flush();
18+
expect(data.length === 1).toBeTruthy();
19+
expect(data[0]).not.toBeNull();
20+
}));
21+
22+
it('should define a limit per page as default value', inject((githubContributor: GithubContributor, $httpBackend: ng.IHttpBackendService) => {
23+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=30').respond(200, new Array(30));
24+
var data: any[];
25+
githubContributor.getContributors().then((fetchedData: any[]) => {
26+
data = fetchedData;
27+
});
28+
$httpBackend.flush();
29+
expect(data.length === 30).toBeTruthy();
30+
}));
31+
32+
it('should log a error', inject((githubContributor: GithubContributor, $httpBackend: ng.IHttpBackendService, $log: ng.ILogService) => {
33+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=1').respond(500);
34+
githubContributor.getContributors(1);
35+
$httpBackend.flush();
36+
expect($log.error.logs).toEqual(jasmine.stringMatching('XHR Failed for'));
37+
}));
38+
});
39+
});

app/templates/src/app/components/githubContributor/_githubContributor.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ export class GithubContributor {
1010
this.$http = $http;
1111
}
1212

13-
getContributors(limit: number = 30) {
13+
getContributors(limit: number = 30): ng.IPromise<any[]> {
1414
return this.$http.get(this.apiHost + '/contributors?per_page=' + limit)
15-
.then((response: any) => {
15+
.then((response: any): any => {
1616
return response.data;
1717
})
18-
.catch((error: any) => {
18+
.catch((error: any): any => {
1919
this.$log.error('XHR Failed for getContributors.\n', error.data);
2020
});
2121
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
###
2+
@todo Complete the test
3+
This example is not perfect.
4+
The `link` function is not tested.
5+
(malarkey usage, addClass, $watch, $destroy)
6+
###
7+
describe 'directive malarkey', () ->
8+
vm = undefined
9+
element = element
10+
11+
beforeEach module '<%- appName %>'
12+
13+
beforeEach inject ($compile, $rootScope, githubContributor, $q) ->
14+
spyOn(githubContributor, 'getContributors').and.callFake () ->
15+
$q.when [{}, {}, {}, {}, {}, {}]
16+
17+
element = angular.element '<acme-malarkey extra-values="[\'Poney\', \'Monkey\']"></acme-malarkey>'
18+
19+
$compile(element) $rootScope.$new()
20+
$rootScope.$digest()
21+
vm = element.isolateScope().vm
22+
23+
it 'should be compiled', () ->
24+
expect(element.html()).not.toEqual null
25+
26+
it 'should have isolate scope object with instanciate members', () ->
27+
expect(vm).toEqual jasmine.any Object
28+
29+
expect(vm.contributors).toEqual jasmine.any Array
30+
expect(vm.contributors.length).toEqual 6
31+
32+
it 'should log a info', inject ($log) ->
33+
expect($log.info.logs).toEqual jasmine.stringMatching 'Activated Contributors View'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @todo Complete the test
3+
* This example is not perfect.
4+
* The `link` function is not tested.
5+
* (malarkey usage, addClass, $watch, $destroy)
6+
*/
7+
describe('directive malarkey', function() {
8+
let vm;
9+
let element;
10+
11+
beforeEach(angular.mock.module('<%- appName %>'));
12+
13+
beforeEach(inject(($compile, $rootScope, githubContributor, $q) => {
14+
spyOn(githubContributor, 'getContributors').and.callFake(() => {
15+
return $q.when([{}, {}, {}, {}, {}, {}]);
16+
});
17+
18+
element = angular.element(`
19+
<acme-malarkey extra-values="['Poney', 'Monkey']"></acme-malarkey>
20+
`);
21+
22+
$compile(element)($rootScope.$new());
23+
$rootScope.$digest();
24+
vm = element.isolateScope().vm;
25+
}));
26+
27+
it('should be compiled', () => {
28+
expect(element.html()).not.toEqual(null);
29+
});
30+
31+
it('should have isolate scope object with instanciate members', () => {
32+
expect(vm).toEqual(jasmine.any(Object));
33+
34+
expect(vm.contributors).toEqual(jasmine.any(Array));
35+
expect(vm.contributors.length).toEqual(6);
36+
});
37+
38+
it('should log a info', inject($log => {
39+
expect($log.info.logs).toEqual(jasmine.stringMatching('Activated Contributors View'));
40+
}));
41+
});

0 commit comments

Comments
 (0)