Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var _ = require('underscore');
var Backbone = require('backbone');
var Marionette = require('backbone.marionette');
var App = require('../../../../../src/app');
var ProductItemLayout = require('../../../../../src/lib/components/productCard/layout/productItemLayout');
var productsCardChannel = Backbone.Wreqr.radio.channel('productsCard');
describe('Product Item Layout View', function () {
beforeEach(function () {
this.fakeView = new Marionette.ItemView();
this.fakeEntity = new Backbone.Model();
this.fakeRegion = {
show: jasmine.createSpy('show')
};
this.productItemLayout = new ProductItemLayout();
this.channelRequestMap = function (request) {
var requests = {
'new:productsAllInfo:view': this.fakeView,
'new:hotelImagesCollection': this.fakeEntity,
'new:productGalleryView': this.fakeView
};
return requests[request];
};
spyOn(App, 'request').and.callFake(_.bind(this.channelRequestMap, this));
spyOn(productsCardChannel.reqres, 'request').and.callFake(_.bind(this.channelRequestMap, this));
spyOn(this.productItemLayout, 'getRegion').and.returnValue(this.fakeRegion);
});
describe('Product Item Constructor', function () {
it('should be a layout view', function () {
expect(this.productItemLayout instanceof Marionette.LayoutView).toBeTruthy();
});
it('should have a template defined', function () {
expect(ProductItemLayout.prototype.template).toBeDefined();
});
it('should have regions defined', function () {
var expectedRegions = {
gallery: '[data-region="gallery"]',
allInfo: '[data-region="all-info"]'
};
expect(ProductItemLayout.prototype.regions).toEqual(expectedRegions);
});
});
describe('#onRender', function () {
beforeEach(function () {
spyOn(this.productItemLayout, 'showGallery');
spyOn(this.productItemLayout, 'showAllInfo');
spyOn(this.productItemLayout, 'initCarousel');
this.fakeEl = 'fake gallery region el';
this.productItemLayout.galleryRegion = {el: this.fakeEl};
this.productItemLayout.render();
});
it('should shows the product item views', function () {
expect(this.productItemLayout.showGallery).toHaveBeenCalled();
expect(this.productItemLayout.showAllInfo).toHaveBeenCalled();
expect(this.productItemLayout.initCarousel).toHaveBeenCalledWith(this.fakeEl);
});
});
describe('#showAllInfo', function () {
beforeEach(function () {
this.fakeModel = 'Fake model';
this.productItemLayout.model = this.fakeModel;
this.productItemLayout.showAllInfo();
});
it('should request the productAllInfo view', function () {
expect(productsCardChannel.reqres.request).toHaveBeenCalledWith('new:productsAllInfo:view', this.fakeModel);
});
it('should show the view in the correct region', function () {
expect(this.productItemLayout.getRegion).toHaveBeenCalledWith('allInfo');
expect(this.fakeRegion.show).toHaveBeenCalledWith(this.fakeView);
});
});
describe('#showGallery', function () {
beforeEach(function () {
this.fakeImages = 'Fake images';
this.fakeModel = {
get: jasmine.createSpy('get').and.returnValue(this.fakeImages)
};
this.productItemLayout.model = this.fakeModel;
this.productItemLayout.showGallery();
});
it('should get the images collection', function () {
expect(App.request).toHaveBeenCalledWith('new:hotelImagesCollection', this.fakeImages);
});
it('should request the gallery view', function () {
expect(App.request).toHaveBeenCalledWith('new:productGalleryView', this.fakeEntity);
});
it('should show the view in its region', function () {
expect(this.fakeRegion.show).toHaveBeenCalledWith(this.fakeView);
});
});
describe('#initCarousel', function () {
beforeEach(function () {
this.productItemLayout.Gallery = jasmine.createSpy('Gallery');
this.fakeTarget = 'Fake target';
this.productItemLayout.initCarousel(this.fakeTarget);
});
it('should instantiate the gallery component with the correct parameters', function () {
expect(this.productItemLayout.Gallery).toHaveBeenCalledWith({
parentElement: this.fakeTarget,
thumbnails: false,
defaultImage: 'http://www3.staticroot.com/images/ose/noimage_bkg_4_3.png',
imgsCache: 2
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment