I am facing an issue with a unit test that fails to read the JSON in the second request properly
This is my implementation of the Config factory
(function() {
'use strict';
angular.module('commercial.factories').factory('config', config);
function config($http) {
var service = {
GetConfig: getConfig
};
return service;
function getConfig(identifier) {
var _config = {};
// Checking for URL match in mapping json file
var urlMap = $http.get('./app/core/urlMap.json').then(function(response) {
for (var i = 0; i < response.data.length; i++) {
if (identifier.toString().toLowerCase().indexOf(response.data[i].url.toLowerCase()) > -1 || response.data[i].clientId === identifier) {
return response.data[i].contentFolder;
}
}
});
// Fetching the config for the related client found in the URL map (above)
return urlMap.then(function(response) {
var contentFolder = response;
return $http.get('./content/' + response + '/config.json')
.then(function(response) {
if (Object.keys(_config).length === 0) {
_config = response.data;
_config.contentFolder = contentFolder;
}
return _config;
});
});
}
}
})();
And here is my test setup...
describe('Config Factory', function() {
var configFactory;
beforeEach(inject(function(_config_) {
configFactory = _config_;
}));
describe('GetConfig()', function() {
it('should fetch the urlMap from the urlMap.json', function() {
var identifier = '_default';
var mockData = [{ url: identifier, contentFolder: '_default' }];
$httpBackend.expectGET('./content/' + identifier + '/config.json');
$httpBackend.expectGET('./app/core/urlMap.json').respond(mockData);
var promise = configFactory.GetConfig(identifier);
$httpBackend.flush(0);
promise.then(function(result) {
expect(result).toEqual(mockData);
})
});
});
Here is the content of the config.json being read...
{
"clientId":34
}
When running the test, I encounter an error message from Karma stating ...
uncaught SyntaxError: Unexpected token : on line 2 of my JSON.
I suspect that the error may be due to having two expectGET's in the same test case, but I need to investigate further to confirm.