Skip to content

Commit

Permalink
#2 First set of unit tests setup
Browse files Browse the repository at this point in the history
  • Loading branch information
neilbo committed Apr 21, 2017
1 parent 69a350e commit 5b3cd14
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 20 deletions.
51 changes: 34 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,44 @@
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build",
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve"
"ionic:serve": "ionic-app-scripts serve",
"test": "karma start ./test-config/karma.conf.js"
},
"dependencies": {
"@angular/common": "2.2.1",
"@angular/compiler": "2.2.1",
"@angular/compiler-cli": "2.2.1",
"@angular/core": "2.2.1",
"@angular/forms": "2.2.1",
"@angular/http": "2.2.1",
"@angular/platform-browser": "2.2.1",
"@angular/platform-browser-dynamic": "2.2.1",
"@angular/platform-server": "2.2.1",
"@ionic/storage": "1.1.7",
"ionic-angular": "2.0.0",
"ionic-native": "2.4.1",
"@angular/common": "2.4.8",
"@angular/compiler": "2.4.8",
"@angular/compiler-cli": "2.4.8",
"@angular/core": "2.4.8",
"@angular/forms": "2.4.8",
"@angular/http": "2.4.8",
"@angular/platform-browser": "2.4.8",
"@angular/platform-browser-dynamic": "2.4.8",
"@angular/platform-server": "2.4.8",
"@ionic-native/core": "3.1.0",
"@ionic-native/splash-screen": "3.1.0",
"@ionic-native/status-bar": "3.1.0",
"@ionic/storage": "2.0.0",
"ionic-angular": "2.3.0",
"ionicons": "3.0.0",
"rxjs": "5.0.0-beta.12",
"zone.js": "0.6.26",
"sw-toolbox": "3.4.0"
"rxjs": "5.0.1",
"sw-toolbox": "3.4.0",
"zone.js": "0.7.2"
},
"devDependencies": {
"@ionic/app-scripts": "1.0.0",
"@ionic/app-scripts": "1.2.2",
"@types/jasmine": "^2.5.41",
"@types/node": "^7.0.13",
"angular2-template-loader": "^0.6.2",
"html-loader": "^0.4.5",
"jasmine": "^2.5.3",
"karma": "^1.6.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.3",
"null-loader": "^0.1.1",
"ts-loader": "^2.0.3",
"typescript": "2.0.9"
},
"cordovaPlugins": [
Expand All @@ -49,3 +65,4 @@
],
"description": "six-minute-walk-distance: An Ionic project"
}

42 changes: 42 additions & 0 deletions src/providers/conversion-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { async, TestBed, inject } from '@angular/core/testing';
import { IonicModule } from "ionic-angular/index";
import { ConversionService } from './conversion-service';

describe('MyApp Component', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [],
imports: [
],
providers: [
ConversionService
]
})
}));

describe('feetToInches', () => {
it('should convert feet to inches', inject([ConversionService], (ConversionService) => {
expect(ConversionService.feetToInches(5)).toBe(60);
}));
});

describe('cmToInches', () => {
it('should convert cm to inches', inject([ConversionService], (ConversionService) => {
expect(ConversionService.cmToInches(3)).toBe(7.62);
}));
});

describe('lbsToKg', () => {
it('should convert lbs to kgs', inject([ConversionService], (ConversionService) => {
expect(ConversionService.lbsToKg(210)).toBe('95.25');
}));
});

describe('metresToInches', () => {
it('should convert metres to inches', inject([ConversionService], (ConversionService) => {
expect(ConversionService.metresToInches(10)).toBe('393.70');
}));
});

});
4 changes: 2 additions & 2 deletions src/providers/conversion-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class ConversionService {
}

lbsToKg(lbs) {
return lbs * 0.453592;
return (lbs * 0.453592).toFixed(2);
}

metresToInches(m) {
return m * 39.3701;
return (m * 39.3701).toFixed(2);
}

}
21 changes: 21 additions & 0 deletions test-config/karma-test-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Error.stackTraceLimit = Infinity;

require('core-js/es6');
require('core-js/es7/reflect');

require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/proxy');
require('zone.js/dist/sync-test');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');

var appContext = require.context('../src', true, /\.spec\.ts/);

appContext.keys().forEach(appContext);

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.TestBed.initTestEnvironment(browser.BrowserDynamicTestingModule, browser.platformBrowserDynamicTesting());
37 changes: 37 additions & 0 deletions test-config/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var webpackConfig = require('./webpack.test.js');

module.exports = function (config) {
var _config = {
basePath: '',

frameworks: ['jasmine'],

files: [
{pattern: './karma-test-shim.js', watched: true}
],

preprocessors: {
'./karma-test-shim.js': ['webpack', 'sourcemap']
},

webpack: webpackConfig,

webpackMiddleware: {
stats: 'errors-only'
},

webpackServer: {
noInfo: true
},

reporters: ['kjhtml', 'dots'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
};

config.set(_config);
};
44 changes: 44 additions & 0 deletions test-config/webpack.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var webpack = require('webpack');
var path = require('path');

module.exports = {
devtool: 'inline-source-map',

resolve: {
extensions: ['.ts', '.js']
},

module: {
rules: [
{
test: /\.ts$/,
loaders: [
{
loader: 'ts-loader'
} , 'angular2-template-loader'
]
},
{
test: /\.html$/,
loader: 'html-loader'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'null-loader'
}
]
},

plugins: [
new webpack.ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
root('./src'), // location of your src
{} // a map of your routes
)
]
};

function root(localPath) {
return path.resolve(__dirname, localPath);
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"src/**/*.ts"
],
"exclude": [
"node_modules"
"node_modules",
"src/**/*.spec.ts"
],
"compileOnSave": false,
"atom": {
Expand Down

0 comments on commit 5b3cd14

Please sign in to comment.