Skip to content

Commit

Permalink
Example unit test for spots srvice, run with npm test
Browse files Browse the repository at this point in the history
remove commented out code

remove console.log

added to see if function is called
  • Loading branch information
John Chen committed Feb 24, 2017
1 parent 4045b5a commit 91fcab1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
11 changes: 6 additions & 5 deletions karma-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ require('ts-helpers');

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

/*
Ok, this is kinda crazy. We can use the the context method on
Expand All @@ -30,7 +32,6 @@ appContext.keys().forEach(appContext);
// Select BrowserDomAdapter.
// see https://github.com/AngularClass/angular2-webpack-starter/issues/124
// Somewhere in the test setup
var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.setBaseTestProviders(browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
// var testing = require('@angular/core/testing');
// var browser = require('@angular/platform-browser-dynamic/testing');
// testing.setBaseTestProviders(browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
6 changes: 3 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function (config) {

// list of files / patterns to load in the browser
files: [
{ pattern: './karma-shim.js', watched: false }
{ pattern: './karma-shim.js', watched: false },
],

// list of files to exclude
Expand Down Expand Up @@ -75,15 +75,15 @@ module.exports = function (config) {
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
autoWatch: true,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'], // you can also use Chrome

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
singleRun: false
};

config.set(_config);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/turf": "^3.5.32",
"angularfire2": "^2.0.0-beta.8",
"core-js": "^2.4.1",
"es6-shim": "^0.35.3",
"firebase": "^3.6.9",
"hammerjs": "^2.0.8",
"mapbox-gl": "^0.32.1",
Expand Down
2 changes: 1 addition & 1 deletion src/app/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './geolocation.service';
export * from './maplocation.service';
export * from './spots.service';
export * from './spots/spots.service';
export * from './distance.service';
export * from './rulesinfo.service';
export * from './editspotstate.service';
Expand Down
17 changes: 17 additions & 0 deletions src/app/services/spots/spots.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SpotsService } from './spots.service';
import { AngularFire } from 'angularfire2';

describe('when spots service is called', () => {

it('should call for database value and save it', () => {
let mockAngularFire : any = {
database: {
list : jasmine.createSpy('list').and.returnValue(3)
}
};

let serviceToTest = new SpotsService(mockAngularFire as AngularFire);
expect(mockAngularFire.database.list).toHaveBeenCalledTimes(1);
expect(serviceToTest.get()).toEqual(3);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Observable } from 'rxjs/Observable';
import { Position } from '~/util';

// If the user is offline, use this data:
const data = require('./data/spots$.json');
const data = require('../data/spots$.json');

@Injectable()
export class SpotsService {
Expand Down

0 comments on commit 91fcab1

Please sign in to comment.