From 5b3cd140c4621394974446dede224fcd7de49c36 Mon Sep 17 00:00:00 2001 From: Neil Estandarte Date: Fri, 21 Apr 2017 22:51:14 +1000 Subject: [PATCH] #2 First set of unit tests setup --- package.json | 51 ++++++++++++++++-------- src/providers/conversion-service.spec.ts | 42 +++++++++++++++++++ src/providers/conversion-service.ts | 4 +- test-config/karma-test-shim.js | 21 ++++++++++ test-config/karma.conf.js | 37 +++++++++++++++++ test-config/webpack.test.js | 44 ++++++++++++++++++++ tsconfig.json | 3 +- 7 files changed, 182 insertions(+), 20 deletions(-) create mode 100644 src/providers/conversion-service.spec.ts create mode 100755 test-config/karma-test-shim.js create mode 100755 test-config/karma.conf.js create mode 100755 test-config/webpack.test.js diff --git a/package.json b/package.json index 3f7561f..88e8362 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -49,3 +65,4 @@ ], "description": "six-minute-walk-distance: An Ionic project" } + diff --git a/src/providers/conversion-service.spec.ts b/src/providers/conversion-service.spec.ts new file mode 100644 index 0000000..312c75a --- /dev/null +++ b/src/providers/conversion-service.spec.ts @@ -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'); + })); + }); + +}); \ No newline at end of file diff --git a/src/providers/conversion-service.ts b/src/providers/conversion-service.ts index e1e8e63..5a532b0 100644 --- a/src/providers/conversion-service.ts +++ b/src/providers/conversion-service.ts @@ -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); } } diff --git a/test-config/karma-test-shim.js b/test-config/karma-test-shim.js new file mode 100755 index 0000000..1c1040c --- /dev/null +++ b/test-config/karma-test-shim.js @@ -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()); \ No newline at end of file diff --git a/test-config/karma.conf.js b/test-config/karma.conf.js new file mode 100755 index 0000000..261e4bf --- /dev/null +++ b/test-config/karma.conf.js @@ -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); +}; diff --git a/test-config/webpack.test.js b/test-config/webpack.test.js new file mode 100755 index 0000000..27d1dd0 --- /dev/null +++ b/test-config/webpack.test.js @@ -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); +} diff --git a/tsconfig.json b/tsconfig.json index 2e450f9..f9695c9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,8 @@ "src/**/*.ts" ], "exclude": [ - "node_modules" + "node_modules", + "src/**/*.spec.ts" ], "compileOnSave": false, "atom": {