Skip to content

Commit

Permalink
Refactor(refactor-history-test): Rewrite history.test.js to remove Ho…
Browse files Browse the repository at this point in the history
…und CI errors
  • Loading branch information
andela-oolutola committed Oct 4, 2016
1 parent cd113ad commit c28db1b
Showing 1 changed file with 60 additions and 76 deletions.
136 changes: 60 additions & 76 deletions test/app/history.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
beforeEach(function() {
describe('History functionality test', function(){
var HistoryFac;
var $httpBackend;
var $q;
var GlobalMock;
userId = '57ea45dd18be84c20f000001';

api = '/api/users/'+userId+'/history';
response = [
var userId = '57ea45dd18be84c20f000001';
var api = '/api/users/'+userId+'/history';
var response = [
{
'_id': '57f26306fc81500d5b000001',
'gameId': 'qwerty123456',
Expand Down Expand Up @@ -100,9 +99,6 @@ beforeEach(function() {
]
}
];
});

describe('History factory', function() {

// before each test load the angular module for history controller
// and history factory
Expand All @@ -115,86 +111,74 @@ describe('History factory', function() {
angular.mock.module('mean.system')
);

// Set our injected History service (_Users_) to our local History variable
beforeEach(inject(function(_HistoryFac_){
HistoryFac = _HistoryFac_;
}));

// Load HistoryFac, $httpBackend(mocks $http service) and $q service
beforeEach(inject(function(_HistoryFac_, _$httpBackend_, _$q_){
HistoryFac = _HistoryFac_;
$httpBackend = _$httpBackend_;
$q = _$q_;
}));

// A simple test to verify the History service exists
it('should exist', function() {
expect(HistoryFac).toBeDefined();
});

it('should expect getHistory() to be defined', function() {
expect(typeof HistoryFac.getHistory).toBe('function');
});

it('should expect getHistory() to make a get request to /api/users/:id/history', function() {
spyOn(HistoryFac, 'getHistory').and.callThrough();
describe('History factory', function() {

// create a mock http get request and supply it with a sample response
$httpBackend.whenGET(api).respond(200, $q.when(response));
result = {};
// Load HistoryFac, $httpBackend(mocks $http service) and $q service
beforeEach(inject(function(_HistoryFac_, _$httpBackend_, _$q_){
HistoryFac = _HistoryFac_;
$httpBackend = _$httpBackend_;
$q = _$q_;
}));

expect(result).toEqual({});
// A simple test to verify the History service exists
it('should exist', function() {
expect(HistoryFac).toBeDefined();
});

HistoryFac.getHistory(userId).then(function(res){
result = res;
it('should expect getHistory() to be defined', function() {
expect(typeof HistoryFac.getHistory).toBe('function');
});

$httpBackend.flush();
it('should expect getHistory() to make a get request to /api/users/:id/history', function() {
spyOn(HistoryFac, 'getHistory').and.callThrough();

expect(HistoryFac.getHistory).toHaveBeenCalledWith(userId);
});
});
// create a mock http get request and supply it with a sample response
$httpBackend.whenGET(api).respond(200, $q.when(response));
var result = {};

// mock uo the Global service provided by the mean.system module
beforeEach(function(){
GlobalMock = {
user: {
_id: userId
},
authenticated: true
};
});
expect(result).toEqual({});

describe('History Controller', function() {
var $controller;
var HistoryCtr;
var Global;
HistoryFac.getHistory(userId).then(function(res){
result = res;
});

// load the angular module
beforeEach(
angular.mock.module('services.History')
);
$httpBackend.flush();

beforeEach(inject(function(_$controller_, _HistoryFac_, _$httpBackend_, _$q_){
$controller = _$controller_;
HistoryFac = _HistoryFac_;
HistoryCtr = $controller('HistoryController', {HistoryFac: HistoryFac, Global: GlobalMock});
$httpBackend = _$httpBackend_;
$q = _$q_;
}));

it('should be defined', function() {
expect(HistoryCtr).toBeDefined();
expect(HistoryFac.getHistory).toHaveBeenCalledWith(userId);
});
});

it('call the getHistory() function of HistoryFac service', function() {
spyOn(HistoryFac, 'getHistory').and.callThrough();
HistoryCtr = $controller('HistoryController', {HistoryFac: HistoryFac, Global: GlobalMock});

$httpBackend.whenGET(api).respond(200, $q.when(response));
$httpBackend.flush();
describe('History Controller', function() {
var GlobalMock = {
user: {
_id: '57ea45dd18be84c20f000001'
},
authenticated: true
};
var $controller;
var HistoryCtr;

beforeEach(inject(function(_$controller_, _HistoryFac_, _$httpBackend_, _$q_){
$controller = _$controller_;
HistoryFac = _HistoryFac_;
HistoryCtr = $controller('HistoryController', {HistoryFac: HistoryFac, Global: GlobalMock});
$httpBackend = _$httpBackend_;
$q = _$q_;
}));

it('should be defined', function() {
expect(HistoryCtr).toBeDefined();
});

expect(HistoryFac.getHistory).toHaveBeenCalledWith(userId);
expect(HistoryCtr.histories).toEqual(response);
it('call the getHistory() function of HistoryFac service', function() {
spyOn(HistoryFac, 'getHistory').and.callThrough();
HistoryCtr = $controller('HistoryController', {HistoryFac: HistoryFac, Global: GlobalMock});

$httpBackend.whenGET(api).respond(200, $q.when(response));
$httpBackend.flush();

expect(HistoryFac.getHistory).toHaveBeenCalledWith(userId);
expect(HistoryCtr.histories).toEqual(response);
});
});
});

0 comments on commit c28db1b

Please sign in to comment.