Skip to content

Commit

Permalink
Merge pull request #665 from magento-performance/MAGETWO-56071
Browse files Browse the repository at this point in the history
[Performance] Unnecessary AJAX request for product reviews on product details page
  • Loading branch information
Alexander Akimov authored Dec 13, 2016
2 parents 732d445 + 6611218 commit f3fb417
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/Review/view/frontend/templates/review.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
{
"*": {
"Magento_Review/js/process-reviews": {
"productReviewUrl": "<?php echo $block->escapeJs($block->escapeUrl($block->getProductReviewUrl())); ?>"
"productReviewUrl": "<?php echo $block->escapeJs($block->escapeUrl($block->getProductReviewUrl())); ?>",
"reviewsTabSelector": "#tab-label-reviews"
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions app/code/Magento/Review/view/frontend/web/js/process-reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ define([
$.ajax({
url: url,
cache: true,
dataType: 'html'
dataType: 'html',
showLoader: true,
loaderContext: $('.product.data.items')
}).done(function (data) {
$('#product-review-container').html(data);
$('[data-role="product-review"] .pages a').each(function (index, element) {
Expand All @@ -30,7 +32,17 @@ define([
}

return function (config, element) {
processReviews(config.productReviewUrl);
var reviewTab = $(config.reviewsTabSelector);
var requiredReviewTabRole = 'tab';

if (reviewTab.attr('role') === requiredReviewTabRole && reviewTab.hasClass('active')) {
processReviews(config.productReviewUrl);
} else {
reviewTab.one('beforeOpen', function () {
processReviews(config.productReviewUrl);
});
}

$(function () {
$('.product-info-main .reviews-actions a').click(function (event) {
event.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

/*eslint max-nested-callbacks: 0*/
/*jscs:disable jsDoc*/

define([
'jquery',
'Magento_Review/js/process-reviews'
], function ($, reviewProcessor) {
'use strict';

describe('Test product page reviews processor', function () {
var element,
config = {
reviewsTabSelector: '#review-tab'
};

beforeEach(function () {
element = $('<div id="review-tab" role="tab"></div>');

$('body').append(element);
});

afterEach(function () {
element.remove();
});

it('Should automatically load reviews after page load if review tab is active', function () {
element.addClass('active');

spyOn($, 'ajax').and.callFake(function () {
var d = $.Deferred();

d.promise().complete = function () {};

return d.promise();
});

reviewProcessor(config, null);

expect($.ajax).toHaveBeenCalled();
});

it('Should not automatically load reviews after page load if review tab is not active', function () {
spyOn($, 'ajax').and.callFake(function () {
var d = $.Deferred();

d.promise().complete = function () {};

return d.promise();
});

reviewProcessor(config, null);

expect($.ajax).not.toHaveBeenCalled();
});

it('Should load reviews if non active review tab was opened', function () {
spyOn($, 'ajax').and.callFake(function () {
var d = $.Deferred();

d.promise().complete = function () {};

return d.promise();
});

reviewProcessor(config, null);
element.trigger('beforeOpen');

expect($.ajax).toHaveBeenCalled();
});
});
});

0 comments on commit f3fb417

Please sign in to comment.