Skip to content

Commit

Permalink
Make check() function call automatic.
Browse files Browse the repository at this point in the history
  • Loading branch information
rars committed May 1, 2017
1 parent 77dc409 commit de02c7e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 21 deletions.
4 changes: 0 additions & 4 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ angular.module('acme', ['environment']).
}
}
});

// run the environment check, so the comprobation is made
// before controllers and services are built
envServiceProvider.check();
}).
controller('Pages', ['$scope', 'envService', function($scope, envService) {
$scope.environment = envService.get(); // store the current environment
Expand Down
14 changes: 12 additions & 2 deletions dist/angular-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ angular.module('environment', []).
return new RegExp(local.pregQuote(string).replace(/\\\*/g, '.*').replace(/\\\?/g, '.'), 'g');
};

local.hasChecked = false; // true iff the check() function has been called at least once
local.host = undefined; // the host URL of the current page

this.environment = 'development'; // default
this.data = {}; // user defined environments data

Expand Down Expand Up @@ -98,7 +101,7 @@ angular.module('environment', []).
*/
this.check = function() {
var self = this,
location = window.location.host,
location = local.host ? local.host : window.location.host,
matches = [],
keepGoing = true;

Expand All @@ -123,9 +126,16 @@ angular.module('environment', []).
self.environment = v.environment;
}
});

local.hasChecked = true;
};

this.$get = function() {
this.$get = function($location) {
if (!local.hasChecked) {
local.host = $location.host();
this.check();
}
return this;
};
this.$get.$inject = ['$location'];
});
7 changes: 4 additions & 3 deletions dist/angular-environment.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/angular-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ angular.module('environment', []).
return new RegExp(local.pregQuote(string).replace(/\\\*/g, '.*').replace(/\\\?/g, '.'), 'g');
};

local.hasChecked = false; // true iff the check() function has been called at least once
local.host = undefined; // the host URL of the current page

this.environment = 'development'; // default
this.data = {}; // user defined environments data

Expand Down Expand Up @@ -98,7 +101,7 @@ angular.module('environment', []).
*/
this.check = function() {
var self = this,
location = window.location.host,
location = local.host ? local.host : window.location.host,
matches = [],
keepGoing = true;

Expand All @@ -123,9 +126,16 @@ angular.module('environment', []).
self.environment = v.environment;
}
});

local.hasChecked = true;
};

this.$get = function() {
this.$get = function($location) {
if (!local.hasChecked) {
local.host = $location.host();
this.check();
}
return this;
};
this.$get.$inject = ['$location'];
});
91 changes: 81 additions & 10 deletions test/angular-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe('Test environment provider', function () {

var theEnvServiceProvider;
var envService;
var $locationMock;

beforeEach(function () {
// Initialise the provider by injecting it into a fake module's
Expand All @@ -15,21 +16,39 @@ describe('Test environment provider', function () {
// Initialise the test.app injector.
module('environment', 'test.app.environment');
inject(function () {});
envService = theEnvServiceProvider.$get();
});

describe('with development environment by default', function () {
beforeEach(function () {
$locationMock = {
host: function () {
return 'localhost';
}
};
spyOn($locationMock, 'host').and.callThrough();

envService.config({
vars: {
development: {
backend: 'https://backend-dev/'
theEnvServiceProvider.config({
domains: {
development: ['localhost'],
production: ['app.*.com']
},
production: {
backend: 'https://backend/'
vars: {
development: {
backend: 'https://backend-dev/'
},
production: {
backend: 'https://backend/'
}
}
}
});

envService = theEnvServiceProvider.$get($locationMock);
});

it('should call $locationMock.host to obtain host URL', function () {
expect($locationMock.host).toHaveBeenCalledTimes(1);
});
});

describe('with envService provider', function () {
it('tests the envService provider can be created', function () {
expect(theEnvServiceProvider).toBeDefined();
});
Expand All @@ -53,4 +72,56 @@ describe('Test environment provider', function () {
expect(envService.read('backend')).toBe('https://backend/');
});
});

describe('tests matching domains', function () {
var i = 0, testCases = [
[true, 'app.web.com', 'app.*.com'],
[true, 'app.a.com', 'app.*.com'],
[true, 'app.web.com', 'app.web.com'],
[true, 'app.web.com', '*.com'],
[true, 'a.com', '*.com'],
[true, 'app.web.com', 'app.*'],
[true, 'app.a', 'app.*'],
[false, 'a.w.com', 'app.web.com']
];

for (i = 0; i < testCases.length; i++) {
(function (expectedMatch, actualHost, domainPattern) {
it('should ' + (expectedMatch ? '' : 'not') + ' identify production environment'
+ ' when host=' + actualHost + ' and domain pattern=' + domainPattern,
function () {
$locationMock = {
host: function () {
return actualHost;
}
};
spyOn($locationMock, 'host').and.callThrough();

theEnvServiceProvider.config({
domains: {
development: ['localhost'],
production: [domainPattern]
},
vars: {
development: {
backend: 'https://backend-dev/'
},
production: {
backend: 'https://backend/'
}
},
defaults: {
development: {},
production: {}
}
});

envService = theEnvServiceProvider.$get($locationMock);

expect(envService.is('production')).toBe(expectedMatch);
expect($locationMock.host).toHaveBeenCalledTimes(1);
});
})(testCases[i][0], testCases[i][1], testCases[i][2]);
}
});
});

0 comments on commit de02c7e

Please sign in to comment.