Skip to content

Commit 251b480

Browse files
authored
Merge pull request #7 from risal-shefin/hybrid-unit-testing-attempt
Hybrid unit testing.
2 parents e755656 + 5e84b38 commit 251b480

13 files changed

+188
-67
lines changed

angular.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"zone.js/testing"
9494
],
9595
"tsConfig": "tsconfig.spec.json",
96+
"karmaConfig": "karma.conf.cjs",
9697
"assets": [
9798
"app/img",
9899
"app/phones",
@@ -109,8 +110,7 @@
109110
"app/app.animations.css"
110111
],
111112
"scripts": [
112-
"node_modules/jquery/dist/jquery.js",
113-
"build-webpack-ngJs/ngJsScripts.bundle.min.js"
113+
"node_modules/jquery/dist/jquery.js"
114114
]
115115
}
116116
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Currently, using the directives that extend UpgradeComponent, we can only test component creation.
2+
3+
import {Component} from '@angular/core';
4+
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
5+
6+
import {Phone, PhoneData} from '../../services/phone.service';
7+
8+
import { PhoneListComponentWrapper } from './phone-list.component.wrapper';
9+
import {UpgradeModule } from '@angular/upgrade/static';
10+
11+
class MockPhone {
12+
queryByPromise(): Promise<PhoneData[] | undefined> {
13+
let mockList = [
14+
{name: 'Nexus S', snippet: '', images: [], age: 1},
15+
{name: 'Motorola DROID', snippet: '', images: [], age: 2}
16+
];
17+
18+
return new Promise((resolve) => {
19+
resolve(mockList);
20+
})
21+
}
22+
}
23+
24+
@Component({
25+
template: `<phone-list></phone-list>`
26+
})
27+
class MockPhoneListComponent { }
28+
29+
let fixture: ComponentFixture<MockPhoneListComponent>;
30+
31+
describe('PhoneList', () => {
32+
33+
beforeEach(waitForAsync(() => {
34+
TestBed
35+
.configureTestingModule({
36+
declarations: [
37+
PhoneListComponentWrapper,
38+
MockPhoneListComponent
39+
],
40+
providers: [
41+
{provide: Phone, useClass: MockPhone},
42+
{provide: '$scope', useExisting: '$rootScope'}
43+
],
44+
imports: [
45+
UpgradeModule
46+
]
47+
})
48+
.compileComponents();
49+
50+
// Bootstrap Angular JS
51+
TestBed.inject(UpgradeModule).bootstrap(document.documentElement, ['phonecatApp']);
52+
}));
53+
54+
beforeEach(() => {
55+
fixture = TestBed.createComponent(MockPhoneListComponent);
56+
fixture.detectChanges(); // Trigger initial data binding
57+
});
58+
59+
it('should create a component', () => {
60+
expect(fixture).toBeTruthy();
61+
const component = fixture.componentInstance;
62+
expect(component).toBeTruthy();
63+
});
64+
});

app/angular-area/services/phone.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export class Phone {
2121
get(id: string): Observable<PhoneData> {
2222
return this.http.get<PhoneData>(`phones/${id}.json`);
2323
}
24+
25+
queryByPromise(): Promise<PhoneData[] | undefined> {
26+
return this.query().toPromise();
27+
}
28+
29+
getByPromise(id: string): Promise<PhoneData | undefined> {
30+
return this.get(id).toPromise();
31+
}
2432
}
2533

2634
angular.module('core.phone')

app/phone-list/phone-list.component.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ angular.
88
template: `<ng-include src="'phone-list/phone-list.template.html'"></ng-include>`,
99
controller: ['phone',
1010
function PhoneListController(phone) {
11-
phone.query().subscribe(phones => {
11+
this.phones = [];
12+
phone.queryByPromise().then(phones => {
1213
this.phones = phones;
1314
});
1415
this.orderProp = 'age';

app/phone-list/phone-list.component.spec.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,61 @@ describe('phoneList', function() {
77

88
// Test the controller
99
describe('PhoneListController', function() {
10+
var $httpBackend, ctrl, mockPhoneService, $http, $q;
1011

11-
// Due to upgrading the phone service to angular,
12-
// the mock component initialization is not working because of unexpected provider-related errors.
13-
/*
14-
var $httpBackend, ctrl;
12+
// Create an empty object to mock the phone service and inject it into the provider.
13+
// We'll implement the mock service later because we should't inject anything
14+
// before injecting this mock service in the mock module.
15+
// Otherwise, we'll get "Error: Injector already created, can not register a module!".
16+
beforeEach(function() {
17+
mockPhoneService = {
18+
queryByPromise: function() {}
19+
};
1520

16-
beforeEach(inject(function($componentController, _$httpBackend_) {
21+
angular.mock.module(function($provide) {
22+
$provide.value("phone", mockPhoneService);
23+
})
24+
});
25+
26+
beforeEach(inject(function($componentController, _$q_, _$http_, _$httpBackend_) {
27+
$q = _$q_;
28+
$http = _$http_;
1729
$httpBackend = _$httpBackend_;
1830
$httpBackend.expectGET('phones/phones.json')
1931
.respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
2032

33+
// Implement mock query of the mock service here.
34+
mockPhoneService.queryByPromise = function() {
35+
var deferred = $q.defer();
36+
$http.get('phones/phones.json')
37+
.then(function(response) {
38+
deferred.resolve(response.data);
39+
});
40+
return deferred.promise;
41+
};
42+
2143
ctrl = $componentController('phoneList');
2244
}));
2345

2446
it('should create a `phones` property with 2 phones fetched with `$http`', function() {
2547
jasmine.addCustomEqualityTester(angular.equals);
2648

2749
expect(ctrl.phones).toEqual([]);
28-
50+
2951
$httpBackend.flush();
3052
expect(ctrl.phones).toEqual([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
3153
});
3254

3355
it('should set a default value for the `orderProp` property', function() {
3456
expect(ctrl.orderProp).toBe('age');
3557
});
36-
*/
3758

59+
// Minimal test to verify if the test runner is working.
3860
it('should subtract', function() {
3961
var a = 3, b = 7;
4062
var sub = a - b;
4163
expect(sub).toEqual(-4);
4264
});
43-
4465
});
4566

4667
});

build-webpack-ngJs/ngJsScripts.bundle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angularjs-phonecat-to-angular/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
<div ng-view class="view-frame"></div>
1111
</div>
1212

13-
<script src="runtime.9c04bd24e47ac341.js" type="module"></script><script src="polyfills.f5e663f919440dc5.js" type="module"></script><script src="scripts.3b643bbe4b5e8993.js" defer></script><script src="main.7b26605e243c4fcf.js" type="module"></script></body>
13+
<script src="runtime.9c04bd24e47ac341.js" type="module"></script><script src="polyfills.f5e663f919440dc5.js" type="module"></script><script src="scripts.2db18f04beaedee2.js" defer></script><script src="main.71a8ff9d0f7a71d5.js" type="module"></script></body>
1414
</html>

dist/angularjs-phonecat-to-angular/main.7b26605e243c4fcf.js renamed to dist/angularjs-phonecat-to-angular/main.71a8ff9d0f7a71d5.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angularjs-phonecat-to-angular/scripts.3b643bbe4b5e8993.js renamed to dist/angularjs-phonecat-to-angular/scripts.2db18f04beaedee2.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

karma.angularJS.conf.cjs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//jshint strict: false
2+
module.exports = function(config) {
3+
config.set({
4+
5+
basePath: './app',
6+
7+
files: [
8+
'lib/angular/angular.js',
9+
'lib/angular-animate/angular-animate.js',
10+
'lib/angular-resource/angular-resource.js',
11+
'lib/angular-route/angular-route.js',
12+
'../node_modules/angular-mocks/angular-mocks.js',
13+
'**/*.module.*js',
14+
'**/*.service.*js',
15+
'*!(.module|.spec).js',
16+
'!(lib)/**/*!(.module|.spec).js',
17+
'**/*.spec.js',
18+
],
19+
20+
autoWatch: true,
21+
22+
frameworks: ['jasmine'],
23+
24+
browsers: ['Chrome'],
25+
26+
plugins: [
27+
'karma-chrome-launcher',
28+
'karma-firefox-launcher',
29+
'karma-jasmine',
30+
'karma-webpack',
31+
],
32+
33+
webpack: {
34+
// karma watches the test entry points
35+
// Do NOT specify the entry option
36+
// webpack watches dependencies
37+
38+
// webpack configuration
39+
"mode": "development",
40+
},
41+
42+
preprocessors: {
43+
// process your `esmodule` syntax of your files
44+
'**/*.js': ['webpack'],
45+
'**/*.spec.js': ['webpack']
46+
},
47+
48+
});
49+
};

karma.conf.cjs

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
1-
//jshint strict: false
2-
module.exports = function(config) {
3-
config.set({
4-
5-
basePath: './app',
6-
7-
files: [
8-
'lib/angular/angular.js',
9-
'lib/angular-animate/angular-animate.js',
10-
'lib/angular-resource/angular-resource.js',
11-
'lib/angular-route/angular-route.js',
12-
'../node_modules/angular-mocks/angular-mocks.js',
13-
'**/*.module.*js',
14-
'**/*.service.*js',
15-
'*!(.module|.spec).js',
16-
'!(lib)/**/*!(.module|.spec).js',
17-
'**/*.spec.js',
18-
],
19-
20-
autoWatch: true,
21-
22-
frameworks: ['jasmine'],
23-
24-
browsers: ['Chrome'],
25-
26-
plugins: [
27-
'karma-chrome-launcher',
28-
'karma-firefox-launcher',
29-
'karma-jasmine',
30-
'karma-webpack',
31-
],
32-
33-
webpack: {
34-
// karma watches the test entry points
35-
// Do NOT specify the entry option
36-
// webpack watches dependencies
37-
38-
// webpack configuration
39-
"mode": "development",
40-
},
41-
42-
preprocessors: {
43-
// process your `esmodule` syntax of your files
44-
'**/*.js': ['webpack'],
45-
'**/*.spec.js': ['webpack']
46-
},
47-
48-
});
49-
};
1+
// Karma configuration file, see link for more information
2+
// https://karma-runner.github.io/1.0/config/configuration-file.html
3+
4+
module.exports = function (config) {
5+
config.set({
6+
basePath: '',
7+
frameworks: ['jasmine', '@angular-devkit/build-angular'],
8+
files: [
9+
'build-webpack-ngJs/ngJsScripts.bundle.min.js',
10+
'node_modules/angular-mocks/angular-mocks.js',
11+
'app/**/*.spec.js'
12+
],
13+
plugins: [
14+
require('karma-jasmine'),
15+
require('karma-chrome-launcher'),
16+
require('@angular-devkit/build-angular/plugins/karma')
17+
],
18+
client: {
19+
clearContext: false // leave Jasmine Spec Runner output visible in browser
20+
},
21+
browsers: ['Chrome'],
22+
restartOnFileChange: true
23+
});
24+
};
25+

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@
8787
"preupdate-webdriver": "npm install",
8888
"protractor": "protractor e2e-tests/protractor.conf.cjs",
8989
"start-http-server": "http-server ./ -a localhost -p 8000 -c-1",
90-
"test": "karma start karma.conf.cjs",
91-
"test-single-run": "karma start karma.conf.cjs --single-run",
90+
"test": "karma start karma.angularJS.conf.cjs",
91+
"test-single-run": "karma start karma.angularJS.conf.cjs --single-run",
9292
"tsc": "tsc",
9393
"tsc:w": "tsc -w",
9494
"update-webdriver": "webdriver-manager update"

tsconfig.spec.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"outDir": "./out-tsc/spec",
66
"types": [
77
"angular",
8-
"jasmine"
8+
"jasmine",
9+
"node",
10+
"angular-mocks"
911
],
1012
},
1113
"include": [

0 commit comments

Comments
 (0)