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
var $ = require('jquery'); | |
var _ = require('underscore'); | |
var Backbone = require('backbone'); | |
var Marionette = require('backbone.marionette'); | |
var ProductShowController = require('apps/products/show/productShowController.js'); | |
var App = require('src/app.js'); | |
var productsShowChannel = Backbone.Wreqr.radio.channel('productsShow'); | |
describe('productShowController', function () { | |
beforeEach(function () { | |
spyOn(ProductShowController.prototype, 'initialize'); | |
this.productShowController = new ProductShowController(); | |
this.deferred = $.Deferred(); | |
this.fakeRegion = {show: jasmine.createSpy('show')}; | |
this.fakeModel = new Backbone.Model(); | |
this.fakeCollection = new Backbone.Collection(); | |
this.productShowController.layout = { | |
map: this.fakeRegion, | |
header: this.fakeRegion, | |
gallery: this.fakeRegion, | |
navBar: this.fakeRegion, | |
flights: this.fakeRegion, | |
rooms: this.fakeRegion, | |
info: this.fakeRegion, | |
reviews: this.fakeRegion | |
}; | |
}); | |
describe('#initialize', function () { | |
beforeEach(function () { | |
ProductShowController.prototype.initialize.and.callThrough(); | |
spyOn(this.productShowController, 'callServices'); | |
spyOn(this.productShowController, 'showLayout'); | |
spyOn(productsShowChannel.commands, 'execute'); | |
this.productShowController.initialize(); | |
}); | |
it('should call the services and show the layout', function () { | |
expect(this.productShowController.callServices).toHaveBeenCalled(); | |
expect(this.productShowController.showLayout).toHaveBeenCalled(); | |
}); | |
it('should show the loading', function () { | |
expect(productsShowChannel.commands.execute).toHaveBeenCalledWith('show:loading'); | |
}); | |
}); | |
describe('#showLayout', function () { | |
beforeEach(function () { | |
this.fakeLayout = 'Fake layout'; | |
this.containerRegion = { | |
show: jasmine.createSpy('show') | |
}; | |
spyOn(App, 'request').and.returnValue(this.fakeLayout); | |
spyOn(App, 'getRegion').and.returnValue(this.containerRegion); | |
this.productShowController.showLayout(); | |
}); | |
it('should show product details layout', function () { | |
expect(this.containerRegion.show).toHaveBeenCalledWith(this.fakeLayout); | |
}); | |
}); | |
describe('#callServices', function () { | |
beforeEach(function () { | |
spyOn(this.productShowController, 'changeFlightRequest').and.returnValue(this.deferred); | |
spyOn(this.productShowController, 'productRequest').and.returnValue(this.deferred); | |
spyOn(this.productShowController, 'responseSuccess'); | |
spyOn(this.productShowController, 'responseFailure'); | |
}); | |
it('should request data from the change flight service when the selected flight has been changed', function () { | |
this.productShowController.flightData = true; | |
this.productShowController.callServices(); | |
expect(this.productShowController.changeFlightRequest).toHaveBeenCalled(); | |
}); | |
it('should request data from the product service when the selected flight has not been changed', function () { | |
this.productShowController.flightData = false; | |
this.productShowController.callServices(); | |
expect(this.productShowController.productRequest).toHaveBeenCalled(); | |
}); | |
it('should call "responseSuccess" when the promise is resolved', function () { | |
this.productShowController.callServices(); | |
this.deferred.resolve(); | |
expect(this.productShowController.responseSuccess).toHaveBeenCalled(); | |
expect(this.productShowController.responseFailure).not.toHaveBeenCalled(); | |
}); | |
it('should call "responseFailure" when the promise is rejected', function () { | |
this.productShowController.callServices(); | |
this.deferred.reject(); | |
expect(this.productShowController.responseFailure).toHaveBeenCalled(); | |
expect(this.productShowController.responseSuccess).not.toHaveBeenCalled(); | |
}); | |
}); | |
describe('#changeFlightRequest', function () { | |
beforeEach(function () { | |
this.fakeFlightData = { | |
newFlightId: 'Fake newFlightId', | |
oldFlightId: 'Fake oldFlightId' | |
}; | |
this.productShowController.flightData = this.fakeFlightData; | |
spyOn(App, 'request'); | |
this.productShowController.changeFlightRequest(); | |
}); | |
it('should request data from the change flight service, passing it flight data', function () { | |
expect(App.request).toHaveBeenCalledWith('service:changeFlight', { | |
data: { | |
newFlightId: this.fakeFlightData.newFlightId, | |
oldFlightId: this.fakeFlightData.oldFlightId | |
} | |
}); | |
}); | |
}); | |
describe('#productRequest', function () { | |
beforeEach(function () { | |
spyOn(App, 'request'); | |
App.handshakeData = { | |
sessionId: 'any session id' | |
}; | |
App.searchId = 'any search id'; | |
this.productShowController.productId = 'any product id'; | |
this.productShowController.productRequest(); | |
}); | |
it('should request data from the product service', function () { | |
expect(App.request).toHaveBeenCalledWith('service:product', { | |
sessionId: 'any session id', | |
searchId: 'any search id', | |
productId: 'any product id' | |
}); | |
}); | |
}); | |
describe('#responseSuccess', function () { | |
beforeEach(function () { | |
spyOn(this.productShowController, 'responseFailure'); | |
spyOn(this.productShowController, 'responseValid'); | |
}); | |
it('should add the service response to the scope of the controller', function () { | |
this.fakeServiceResponse = 'Fake response'; | |
this.productShowController.responseSuccess(this.fakeServiceResponse); | |
expect(this.productShowController.serviceResponse).toEqual(this.fakeServiceResponse); | |
}); | |
it('should handle a response error', function () { | |
this.fakeServiceResponse = {error: 'Fake error'}; | |
this.productShowController.responseSuccess(this.fakeServiceResponse); | |
expect(this.productShowController.responseFailure).toHaveBeenCalled(); | |
expect(this.productShowController.responseValid).not.toHaveBeenCalled(); | |
}); | |
it('should handle a valid response', function () { | |
this.fakeServiceResponse = {error: null}; | |
this.productShowController.responseSuccess(this.fakeServiceResponse); | |
expect(this.productShowController.responseValid).toHaveBeenCalled(); | |
expect(this.productShowController.responseFailure).not.toHaveBeenCalled(); | |
}); | |
}); | |
describe('#responseValid', function () { | |
beforeEach(function () { | |
spyOn(this.productShowController, 'setDictionary'); | |
spyOn(this.productShowController, 'showProductDetails'); | |
spyOn(App, 'execute'); | |
spyOn(productsShowChannel.commands, 'execute'); | |
this.productShowController.responseValid(); | |
}); | |
it('should hide the waiting page', function () { | |
expect(App.execute).toHaveBeenCalledWith('hide:waiting'); | |
}); | |
it('should hide the loading', function () { | |
expect(productsShowChannel.commands.execute).toHaveBeenCalledWith('hide:loading'); | |
}); | |
it('should set the dictionary response and call "showProducts"', function () { | |
expect(this.productShowController.setDictionary).toHaveBeenCalled(); | |
expect(this.productShowController.showProductDetails).toHaveBeenCalled(); | |
}); | |
}); | |
describe('#setDictionary', function () { | |
beforeEach(function () { | |
this.fakeServiceResponse = {dictionary: {translations: 'Fake translations'}}; | |
this.productShowController.serviceResponse = this.fakeServiceResponse; | |
this.productShowController.setDictionary(); | |
}); | |
it('should set the translations returned from the service on the global app object', function () { | |
expect(App.dictionary).toEqual(this.fakeServiceResponse.dictionary.translations); | |
}); | |
}); | |
describe('#showProductDetails', function () { | |
var productEntity = 'product entity'; | |
var imagesCollection = 'images collection'; | |
var servicesCollection = 'services collection'; | |
var roomsSummaryModel = 'rooms summary model'; | |
var mealPlansCollection = 'meal plans collection'; | |
beforeEach(function () { | |
this.fakeServiceResponse = { | |
selectedHotel: {}, | |
selectedTransportMode: 'Fake selectedTransportMode' | |
}; | |
this.handleFakeRequest = function (param1) { | |
var responseMap = { | |
'product:entity': productEntity, | |
'new:hotelImagesCollection': imagesCollection, | |
'new:hotelServicesCollection': servicesCollection, | |
'new:roomsSummaryModel': roomsSummaryModel, | |
'new:mealPlansCollection': mealPlansCollection | |
}; | |
return responseMap[param1]; | |
}; | |
spyOn(App, 'request').and.callFake(this.handleFakeRequest); | |
spyOn(this.productShowController, 'showHeader'); | |
spyOn(this.productShowController, 'showGallery'); | |
spyOn(this.productShowController, 'showNavBar'); | |
spyOn(this.productShowController, 'showFlights'); | |
spyOn(this.productShowController, 'showRooms'); | |
spyOn(this.productShowController, 'showInfo'); | |
spyOn(this.productShowController, 'showReviews'); | |
this.productShowController.serviceResponse = this.fakeServiceResponse; | |
this.productShowController.showProductDetails(); | |
}); | |
it('should execute all required functionality to render product details', function () { | |
expect(this.productShowController.showHeader).toHaveBeenCalledWith({ | |
model: productEntity, | |
region: this.fakeRegion | |
}); | |
expect(this.productShowController.showGallery).toHaveBeenCalledWith({ | |
collection: imagesCollection, | |
region: this.fakeRegion | |
}); | |
expect(this.productShowController.showNavBar).toHaveBeenCalledWith({ | |
model: productEntity, | |
region: this.fakeRegion | |
}); | |
expect(this.productShowController.showRooms).toHaveBeenCalledWith({ | |
model: roomsSummaryModel, | |
collection: mealPlansCollection, | |
region: this.fakeRegion | |
}); | |
expect(this.productShowController.showInfo).toHaveBeenCalledWith({ | |
model: productEntity, | |
collection: servicesCollection, | |
region: this.fakeRegion | |
}); | |
expect(this.productShowController.showReviews).toHaveBeenCalledWith({ | |
model: productEntity, | |
region: this.fakeRegion | |
}); | |
}); | |
it('should show the selected flights in the correct region', function () { | |
expect(this.productShowController.showFlights).toHaveBeenCalledWith({ | |
data: this.fakeServiceResponse.selectedTransportMode, | |
region: this.productShowController.layout.flights | |
}); | |
}); | |
}); | |
describe('#showHeader', function () { | |
beforeEach(function () { | |
this.fakeView = new Marionette.ItemView(); | |
spyOn(App, 'request').and.returnValue(this.fakeView); | |
spyOn(this.productShowController, 'listenTo'); | |
this.productShowController.showHeader({model: this.fakeModel, region: this.fakeRegion}); | |
}); | |
it('should request the Product Header View', function () { | |
expect(App.request).toHaveBeenCalledWith('new:productHeaderView', this.fakeModel); | |
}); | |
it('should show the header view in the region passed', function () { | |
expect(this.fakeRegion.show).toHaveBeenCalledWith(this.fakeView); | |
}); | |
it('should listen to the "navigate:search:results" event from the view', function () { | |
expect(this.productShowController.listenTo).toHaveBeenCalledWith(this.fakeView, 'navigate:search:results', this.productShowController.navigateSearchResults); | |
}); | |
}); | |
describe('#showGallery', function () { | |
beforeEach(function () { | |
spyOn(App, 'request').and.returnValue(this.fakeView); | |
spyOn(this.productShowController, 'initCarousel'); | |
this.galleryCollection = new Backbone.Collection(); | |
this.productShowController.showGallery({ | |
collection: this.galleryCollection, | |
region: this.fakeRegion | |
}); | |
}); | |
it('should create a gallery view', function () { | |
expect(App.request).toHaveBeenCalledWith('new:productGalleryView', this.galleryCollection); | |
}); | |
it('should show view in the region', function () { | |
expect(this.fakeRegion.show).toHaveBeenCalledWith(this.fakeView); | |
}); | |
it('should call initCarousel', function () { | |
expect(this.productShowController.initCarousel).toHaveBeenCalledWith(this.fakeRegion.el); | |
}); | |
}); | |
describe('#showNavBar', function () { | |
beforeEach(function () { | |
spyOn(App, 'request').and.returnValue(new Marionette.ItemView()); | |
this.productShowController.showNavBar({model: this.fakeModel, region: this.fakeRegion}); | |
}); | |
it('should request the navigation bar view', function () { | |
expect(App.request).toHaveBeenCalledWith('new:productNavBarView', this.fakeModel); | |
}); | |
it('should show the navigation bar', function () { | |
expect(this.fakeRegion.show.calls.argsFor(0)[0] instanceof Marionette.ItemView).toBeTruthy(); | |
}); | |
}); | |
describe('#showFlights', function () { | |
beforeEach(function () { | |
this.fakeData = ''; | |
this.fakeView = new Marionette.ItemView(); | |
this.requestMap = function (request) { | |
var requests = { | |
'new:selectedFlight:entity': this.fakeModel, | |
'new:productFlightsView': this.fakeView | |
}; | |
return requests[request]; | |
}; | |
spyOn(this.productShowController, 'navigateAvailableFlights'); | |
spyOn(App, 'request').and.callFake(_.bind(this.requestMap, this)); | |
this.productShowController.showFlights({data: this.fakeData, region: this.fakeRegion}); | |
}); | |
it('should request the flight model', function () { | |
expect(App.request.calls.argsFor(0)).toEqual(['new:selectedFlight:entity', this.fakeData]); | |
}); | |
it('should request the view with the model', function () { | |
expect(App.request.calls.argsFor(1)).toEqual(['new:productFlightsView', this.fakeModel]); | |
}); | |
it('should show the selected flights card', function () { | |
expect(this.fakeRegion.show).toHaveBeenCalledWith(this.fakeView); | |
}); | |
it('should navigate to the available flights when the change flight button is clicked on the view', function () { | |
this.fakeView.trigger('change:flight'); | |
expect(this.productShowController.navigateAvailableFlights).toHaveBeenCalled(); | |
}); | |
}); | |
describe('#showRooms', function () { | |
beforeEach(function () { | |
spyOn(App, 'request').and.callFake(_.bind(function (attr1, attr2) { | |
return new Marionette.ItemView({model: attr2}); | |
}, this)); | |
this.productShowController.showRooms({model: this.fakeModel, region: this.fakeRegion}); | |
}); | |
it('should show the room information', function () { | |
expect(this.fakeRegion.show.calls.argsFor(0)[0] instanceof Marionette.ItemView).toBeTruthy(); | |
expect(this.fakeRegion.show.calls.argsFor(0)[0].model).toBe(this.fakeModel); | |
}); | |
}); | |
describe('#showInfo', function () { | |
beforeEach(function () { | |
spyOn(App, 'request').and.callFake(_.bind(function (attr1, attr2, attr3) { | |
return new Marionette.CompositeView({model: attr2, collection: attr3}); | |
}, this)); | |
this.productShowController.showInfo({ | |
model: this.fakeModel, | |
collection: this.fakeCollection, | |
region: this.fakeRegion | |
}); | |
}); | |
it('should show the hotel info', function () { | |
expect(this.fakeRegion.show.calls.argsFor(0)[0] instanceof Marionette.CompositeView).toBeTruthy(); | |
expect(this.fakeRegion.show.calls.argsFor(0)[0].model).toBe(this.fakeModel); | |
expect(this.fakeRegion.show.calls.argsFor(0)[0].collection).toBe(this.fakeCollection); | |
}); | |
}); | |
describe('#showReviews', function () { | |
beforeEach(function () { | |
this.fakeUrl = 'Fake url'; | |
spyOn(App, 'request').and.returnValue(new Marionette.ItemView()); | |
}); | |
it('should do nothing if there is no iFrame url', function () { | |
this.fakeModel.set('iFrameUrl', undefined); | |
this.productShowController.showReviews({model: this.fakeModel, region: this.fakeRegion}); | |
expect(App.request).not.toHaveBeenCalled(); | |
expect(this.fakeRegion.show).not.toHaveBeenCalled(); | |
}); | |
it('should request the reviews view if there is iFrame url', function () { | |
this.fakeModel.set('iFrameUrl', this.fakeUrl); | |
this.productShowController.showReviews({model: this.fakeModel, region: this.fakeRegion}); | |
expect(App.request).toHaveBeenCalledWith('new:productReviewsView', this.fakeModel); | |
}); | |
it('should show the room information if there is iFrame url', function () { | |
this.fakeModel.set('iFrameUrl', this.fakeUrl); | |
this.productShowController.showReviews({model: this.fakeModel, region: this.fakeRegion}); | |
expect(this.fakeRegion.show.calls.argsFor(0)[0] instanceof Marionette.ItemView).toBeTruthy(); | |
}); | |
}); | |
describe('#navigateAvailableFlights', function () { | |
beforeEach(function () { | |
spyOn(App.commands, 'execute'); | |
this.productShowController.navigateAvailableFlights(); | |
}); | |
it('should navigate to the available flights page', function () { | |
expect(App.commands.execute).toHaveBeenCalledWith('show:availableFlights'); | |
}); | |
}); | |
describe('#navigateSearchResults', function () { | |
beforeEach(function () { | |
spyOn(App, 'execute'); | |
this.productShowController.navigateSearchResults(); | |
}); | |
it('should navigate to search results page', function () { | |
expect(App.execute).toHaveBeenCalledWith('show:searchResults'); | |
}); | |
}); | |
describe('#responseFailure', function () { | |
beforeEach(function () { | |
spyOn(productsShowChannel.commands, 'execute'); | |
spyOn(App, 'execute'); | |
this.productShowController.responseFailure(); | |
}); | |
it('should hide the loading', function () { | |
expect(productsShowChannel.commands.execute).toHaveBeenCalledWith('hide:loading'); | |
}); | |
it('should execute the command to show the error with default error information', function () { | |
this.productShowController.responseFailure(); | |
expect(App.execute).toHaveBeenCalledWith('show:global:error', { | |
errorType: 'ERROR_SERVICE_NO_RESPONSE' | |
}); | |
}); | |
it('should execute the command to show the error with response error information', function () { | |
this.fakeServiceResponse = {error: {errorType: 'Fake error type'}}; | |
this.productShowController.serviceResponse = this.fakeServiceResponse; | |
this.productShowController.responseFailure(); | |
expect(App.execute).toHaveBeenCalledWith('show:global:error', this.fakeServiceResponse.error); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment