Last active
February 5, 2019 05:49
-
-
Save junibrosas/488b5d5c0af5ea87ddcf15ae48a8b64c to your computer and use it in GitHub Desktop.
AngularJS unit testing snippet compilation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is only a code snippet and should only provide a guide of testing scenarios. | |
describe('rmaLeasingLocationAwards', () => { | |
let $q; | |
let $componentController; | |
let $rootScope; | |
let controller; | |
beforeEach(() => { | |
angular.mock.module('RateMyAgent.Profiles.Locations.Leasing'); | |
inject((_$q_, _$componentController_, _$rootScope_) => { | |
$componentController = _$componentController_; | |
$q = _$q_; | |
$rootScope = _$rootScope_; | |
}); | |
// initialize the controller with corresponding services, providers | |
controller = $componentController('rmaLeasingLocationAwards', {}); | |
}); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is only a code snippet and should only provide a guide of testing scenarios. | |
describe('rmaSalesLocationFilter', () => { | |
let $q; | |
let $componentController; | |
let $rootScope; | |
let controller; | |
let stubLeasingReviewService; | |
beforeEach(() => { | |
angular.mock.module('RateMyAgent.Profiles.Locations.Sales'); | |
// sample of spying object using Jasmine | |
stubSalesLocationService = jasmine.createSpyObj('SalesLocationService', ['getLocationAgents']); | |
stubTranslate = jasmine.createSpy(); | |
inject((_$q_, _$componentController_, _$rootScope_) => { | |
$componentController = _$componentController_; | |
$q = _$q_; | |
$rootScope = _$rootScope_; | |
}); | |
// sample of stubbing a method with returned Promise using Sinon | |
stubLeasingReviewService = { | |
getSimilarReviews: sinon.stub().returns($q.defer().promise) | |
}; | |
// sample of spying a method with returned Promise | |
stubTranslate.and.returnValue($q.resolve({ | |
Vendor: 'mickey mouse' | |
})); | |
// sample of stubbing a moment function using Sinon | |
stubMoment = sinon.stub().returns({ | |
fromNow: sinon.stub().returns('3 months ago') | |
}); | |
// sample of stub method with returned Promise | |
stubSalesLocationService.getLocationAgents.and.returnValue($q.defer().promise); | |
// initialize the controller with corresponding services, providers | |
controller = $componentController('rmaSimilarReviews', { | |
SalesLocationService: stubSalesLocationService, | |
stubLeasingReviewService: stubLeasingReviewService, | |
moment: stubMoment | |
}); | |
}); | |
describe('onInit', () => { | |
beforeEach(() => { | |
controller.getSimilarReviews = sinon.spy(); | |
}); | |
it('should getSimilarReviews get called', () => { | |
controller.$onInit(); | |
expect(controller.getSimilarReviews.called).toBeTruthy(); | |
}); | |
}); | |
describe('getSimilarReviews', () => { | |
it('should isReviewsDisplay false if no result', () => { | |
const result = []; | |
controller.getSimilarReviews(); | |
$q.defer().resolve(result); | |
// always rerender component when stubbing a method with returned Promise to apply the changes | |
$rootScope.$apply(); | |
expect(controller.isReviewsDisplay).toBeFalsy(); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('SalesLocationSErvice', () => { | |
let targetService; | |
let stubStateParams = {}; | |
let stubTranslate = {}; | |
let stubrmaHttpService = {}; | |
let stubrmaProperties = {}; | |
let stubPropertyListingsService = {}; | |
let stubSponsoredService = {}; | |
let stubMetaService = {}; | |
let stubAutoSearchApiService = {}; | |
let stubAgentShortlistService = {}; | |
let stubProfilePaginationService = {}; | |
let stubFeatureProviderService = {}; | |
beforeEach(() => { | |
angular.mock.module('RateMyAgent.Profiles.Locations.Sales'); | |
angular.mock.module(($provide) => { | |
$provide.value('$stateParams', stubStateParams); | |
$provide.value('$translate', stubTranslate); | |
$provide.value('rmaHttpService', stubrmaHttpService); | |
$provide.value('rmaProperties', stubrmaProperties); | |
$provide.value('PropertyListingsService', stubPropertyListingsService); | |
$provide.value('SponsoredService', stubSponsoredService); | |
$provide.value('MetaService', stubMetaService); | |
$provide.value('AutoSearchApiService', stubAutoSearchApiService); | |
$provide.value('AgentShortlistService', stubAgentShortlistService); | |
$provide.value('ProfilePaginationService', stubProfilePaginationService); | |
$provide.value('FeatureProviderService', stubFeatureProviderService); | |
}); | |
inject((SalesLocationService) => { | |
targetService = SalesLocationService; | |
}); | |
}); | |
describe('setDefaultStatisticsType', () => { | |
let stateParams, location; | |
beforeEach(() => { | |
stateParams = { OrderBy: '' }; | |
location = { ReviewCount: 5, TotalSales: 5 }; | |
}); | |
it('should have correct statistics type if there are no reviews', () => { | |
let expectedValue = 'SoldListings'; | |
location.ReviewCount = 0; | |
expect(targetService.setDefaultStatisticsType(stateParams, location)).toBe(expectedValue); | |
}); | |
it('should have correct statistics type if there are no reviews and total sales', () => { | |
let expectedValue = 'ActiveListings'; | |
location.ReviewCount = 0; | |
location.TotalSales = 0; | |
expect(targetService.setDefaultStatisticsType(stateParams, location)).toBe(expectedValue); | |
}); | |
it('should have correct statistics type if there is state params and reviews', () => { | |
let expectedValue = 'TotalRecommendations'; | |
stateParams = { OrderBy: expectedValue }; | |
expect(targetService.setDefaultStatisticsType(stateParams, location)).toBe(expectedValue); | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment