Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/*
/node_modules/*
/coverage/*
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var historyApiFallback = require('connect-history-api-fallback');
gulp.task('browser-sync', function(){
browserSync.init({
server: {
baseDir: "./",
baseDir: "./src",
middleware: [historyApiFallback()]
}
});
Expand Down
85 changes: 85 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Karma configuration
// Generated on Mon Jan 04 2016 21:39:34 GMT+0300 (RTZ 2 (зима))

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.js',
'node_modules/angular-route/angular-route.js',
'node_modules/angular-resource/angular-resource.js',
'node_modules/angular-mocks/angular-mocks.js',
'src/app.js',
'src/*.js',
'src/*.html',
'test/*.js'
],


// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'src/*.html': ['ng-html2js'],
'src/*.js': ['coverage']
},

ngHtml2JsPreprocessor: {
moduleName: 'templates'
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage'],

coverageReporter: {
type: 'html',
dir: 'coverage/'
},

// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


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


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


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

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "mounts-test",
"version": "1.0.0",
"description": "Unit testing thSample application.",
"dependencies": {
"angular": "^1.3.15"
},
"devDependencies": {
"angular-mocks": "~1.4.7",
"karma": "~0.13.10",
"jasmine-core": "~2.3.4",
"karma-jasmine": "~0.3.6",
"browser-sync": "~2.9.10",
"gulp": "~3.9.0",
"connect-history-api-fallback": "~1.1.0",
"karma-chrome-launcher": "~0.2.1",
"karma-ng-html2js-preprocessor": "~0.2.0",
"karma-coverage": "~0.5.2",
"angular-route": "~1.4.7",
"angular-resource": "~1.4.7"
},
"scripts": {
"test": "karma start"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 1 addition & 6 deletions app.js → src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ angular.module('Travel', ['ngRoute', 'ngResource'])
.when('/',{
templateUrl: 'admin_list.html',
controller: 'AdminToursController',
publicAccess: true,
resolve: {
currentUser: function(){
return {name: 'Alex'};
}
}
publicAccess: true
})
.when('/admin/tours/:id',{
templateUrl: 'item.html',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions tour_controller.js → src/tour_controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
angular.module('Travel').controller('TourController', function($scope, $routeParams) {
angular.forEach(allTours, function(tour){
if ($routeParams.slug == tour.slug)
$scope.tour = tour;
if ($routeParams.slug == tour.slug) $scope.tour = tour;
});
});
File renamed without changes.
150 changes: 150 additions & 0 deletions test/admin_tours_controller_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
describe('AdminToursController', function() {
beforeEach(module('Travel'));
var $scope = {};
var $httpBackend = null;
var resoures = ['Tour', 'Country', 'Place', 'Hotel'];
var tourForUpdate = null;
var tourApiUrl = 'https://api.parse.com/1/classes/Tour';
var country = { name: 'Country name', objectId: '1231erw' };
var hotel = { name: 'Hotel name', stars: 5 };
var place = { name: 'Place name'};

beforeEach(inject(function($controller, _$httpBackend_){
$controller('AdminToursController', {$scope: $scope});
$httpBackend = _$httpBackend_;
resoures.forEach(function(i){
$httpBackend.whenGET('https://api.parse.com/1/classes/' + i).respond(200, JSON.stringify({results: []}));
});
}));

describe('initialize controller', function() {
it('sets title to Путешествия', function() {
expect($scope.title).toBe('Путешествия');
});

it('expects to call parse.com for tours list', function(){
$httpBackend.expectGET('https://api.parse.com/1/classes/Tour').respond(200);
expect($httpBackend.verifyNoOutstandingExpectation).not.toThrow();
});
});

describe('CRUD', function() {
describe('update', function() {
beforeEach(function(){
$scope.tours = [{ title: 'Old name', length: 10, objectId: '21re43f' }];
tourForUpdate = $scope.tours[0];
});

it('hides tour form', function(){
spyOn($scope, 'hideEditForm');
$scope.update(tourForUpdate);
expect($scope.hideEditForm).toHaveBeenCalledWith(tourForUpdate);
});

it('updates the tour values', function(){
tourForUpdate.title = 'New name';
$scope.update(tourForUpdate);
expect(tourForUpdate.title).toEqual('New name');
});

it('makes request to parse.com', function(){
$scope.update(tourForUpdate);
$httpBackend.expectPUT('https://api.parse.com/1/classes/Tour/' + tourForUpdate.objectId).respond(200);
expect($httpBackend.verifyNoOutstandingExpectation).not.toThrow();
});
});

describe('create', function() {
var newTour = null;

beforeEach(function(){
var newTour = { title: 'New tour name', length: 10, placeId: place, countryId: country,
hotel: hotel }
$scope.newTour = newTour;
var jsonResponse = JSON.stringify({results: [newTour]});
$httpBackend.whenPOST(tourApiUrl).respond(jsonResponse);
});

it('makes request to api for create', function(){
$httpBackend.expectPOST(tourApiUrl);
$scope.addTour();
expect($httpBackend.verifyNoOutstandingExpectation).not.toThrow();
});

it('adds new tour to scope', function(){
expect($scope.tours.length).toEqual(0)
$scope.addTour();
$httpBackend.flush();
expect($scope.tours.length).toEqual(1);
expect($scope.tours[0].title).toEqual('New tour name');
expect($scope.tours[0].length).toEqual(10);
});
});

describe('delete', function(){
var tourForDelete = null;
var tourForDeleteUrl = null;

beforeEach(function(){
$scope.tours = [ { title: 'Tour for delete', length: 10, objectId: 'ewrwr3e' }];
tourForDelete = $scope.tours[0];
tourForDeleteUrl = tourApiUrl + '/' + tourForDelete.objectId;
$httpBackend.whenGET(tourForDeleteUrl).respond(JSON.stringify({results: [tourForDelete]}));
$httpBackend.whenDELETE(tourForDeleteUrl).respond(200);
});

it('calls api for tour deletion', function(){
$httpBackend.expectGET(tourForDeleteUrl);
$httpBackend.expectDELETE(tourForDeleteUrl);
$scope.delete(tourForDelete);
expect($httpBackend.verifyNoOutstandingExpectation).not.toThrow();
});

it('delete tour from scope', function(){
expect($scope.tours.length).toEqual(1);
$scope.delete(tourForDelete);
$httpBackend.flush();
expect($scope.tours.length).toEqual(0);
});
});
});

describe('$scope.tourCountry', function(){
beforeEach(function(){
$scope.tourCountries = [country];
$scope.tours = [ { title: 'Tour with Country', countryId: country },
{ title: 'Tour without Country'} ];
});

it('returns tour country', function(){
expect($scope.tourCountry($scope.tours[0])).toEqual(country.name);
});

it('returns blank string for tour without country', function(){
expect($scope.tourCountry($scope.tours[1])).toEqual('');
});
});

describe('$scope.cancelEdit', function(){
var tour = null;

beforeEach(function(){
$scope.tours = [ { title: 'Tour name', length: 10, objectId: 'ewrwr3e' }];
tour = $scope.tours[0];
$httpBackend.whenGET(tourApiUrl + '/' + tour.objectId).respond(JSON.stringify({results: [tour]}));
});

it('hides tour form', function(){
spyOn($scope, 'hideEditForm');
$scope.cancelEdit(tour);
expect($scope.hideEditForm).toHaveBeenCalledWith(tour);
});

it('resets tour values', function(){
tour.title = 'New title';
$scope.cancelEdit(tour);
$httpBackend.flush();
expect($scope.tours[0].results[0].title).toEqual('Tour name');
})
});
});