Skip to content

Commit 16c81e9

Browse files
committed
Merge branch 'ACP2E-3454' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-VK-2024-11-27-CE
2 parents 5b47ddd + 0e5e2a5 commit 16c81e9

File tree

6 files changed

+291
-25
lines changed

6 files changed

+291
-25
lines changed

app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Copyright © Magento, Inc. All rights reserved.
3-
* See COPYING.txt for license details.
2+
* Copyright 2012 Adobe
3+
* All Rights Reserved.
44
*/
55
/**
66
* @api
@@ -356,7 +356,13 @@ define([
356356
* @private
357357
*/
358358
_addFotoramaVideoEvents: function (isInitial) {
359-
if (_.isUndefined($.mage.AddFotoramaVideoEvents)) {
359+
if (_.isUndefined($.mage.AddFotoramaVideoEvents)
360+
|| !$(this.options.mediaGallerySelector).AddFotoramaVideoEvents('instance')
361+
) {
362+
$(this.options.mediaGallerySelector).on('addfotoramavideoeventscreate', function () {
363+
this._addFotoramaVideoEvents(isInitial);
364+
}.bind(this));
365+
360366
return;
361367
}
362368

@@ -521,7 +527,7 @@ define([
521527

522528
if (this.options.spConfig.canDisplayShowOutOfStockStatus) {
523529
filteredSalableProducts = $(this.options.spConfig.salable[attributeId][options[i].id]).
524-
filter(options[i].allowedProducts);
530+
filter(options[i].allowedProducts);
525531
canDisplayOutOfStockProducts = filteredSalableProducts.length === 0;
526532
}
527533

app/code/Magento/ProductVideo/view/frontend/web/js/fotorama-add-video-events.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Copyright © Magento, Inc. All rights reserved.
3-
* See COPYING.txt for license details.
2+
* Copyright 2015 Adobe
3+
* All Rights Reserved.
44
*/
55

66
define([
@@ -137,16 +137,6 @@ define([
137137
* @private
138138
*/
139139
_create: function () {
140-
$(this.element).data('gallery') ?
141-
this._onGalleryLoaded() :
142-
$(this.element).on('gallery:loaded', this._onGalleryLoaded.bind(this));
143-
},
144-
145-
/**
146-
*
147-
* @private
148-
*/
149-
_initialize: function () {
150140
if (!this.defaultVideoData.length) {
151141
this.defaultVideoData = this.options.videoData;
152142
}
@@ -156,7 +146,14 @@ define([
156146
if (!this.defaultVideoData.length && !this.options.videoData.length) {
157147
this.defaultVideoData = this.options.videoData = this.videoDataPlaceholder;
158148
}
149+
this._initializeOnGalleryLoaded();
150+
},
159151

152+
/**
153+
*
154+
* @private
155+
*/
156+
_initialize: function () {
160157
this.clearEvents();
161158

162159
if (this._checkForVideoExist()) {
@@ -168,6 +165,17 @@ define([
168165
}
169166
},
170167

168+
/**
169+
* Initializes after gallery is loaded
170+
*
171+
* @private
172+
*/
173+
_initializeOnGalleryLoaded: function () {
174+
$(this.element).data('gallery') ?
175+
this._onGalleryLoaded() :
176+
$(this.element).on('gallery:loaded', this._onGalleryLoaded.bind(this));
177+
},
178+
171179
/**
172180
* Callback which fired after gallery gets initialized.
173181
*/
@@ -203,7 +211,7 @@ define([
203211
}
204212

205213
this._loadVideoData(options);
206-
this._initialize();
214+
this._initializeOnGalleryLoaded();
207215
},
208216

209217
/**
@@ -473,7 +481,8 @@ define([
473481
elem.removeClass(this.TI);
474482
}
475483

476-
if (this.options.videoData[i].mediaType === this.VID &&
484+
if (this.options.videoData[i] &&
485+
this.options.videoData[i].mediaType === this.VID &&
477486
fotorama.data[i].type === this.VID &&
478487
fotorama.options.nav === 'thumbs') {
479488
elem.addClass(this.TI);

app/code/Magento/Swatches/view/base/web/js/swatch-renderer.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Copyright © Magento, Inc. All rights reserved.
3-
* See COPYING.txt for license details.
2+
* Copyright 2015 Adobe
3+
* All Rights Reserved.
44
*/
55

66
define([
@@ -1334,7 +1334,13 @@ define([
13341334
* @private
13351335
*/
13361336
_addFotoramaVideoEvents: function (isInitial) {
1337-
if (_.isUndefined($.mage.AddFotoramaVideoEvents)) {
1337+
if (_.isUndefined($.mage.AddFotoramaVideoEvents)
1338+
|| !$(this.options.mediaGallerySelector).AddFotoramaVideoEvents('instance')
1339+
) {
1340+
$(this.options.mediaGallerySelector).on('addfotoramavideoeventscreate', function () {
1341+
this._addFotoramaVideoEvents(isInitial);
1342+
}.bind(this));
1343+
13381344
return;
13391345
}
13401346

dev/tests/js/jasmine/tests/app/code/Magento/ConfigurableProduct/view/frontend/js/configurable.test.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Copyright © Magento, Inc. All rights reserved.
3-
* See COPYING.txt for license details.
2+
* Copyright 2020 Adobe
3+
* All Rights Reserved.
44
*/
55

66
/* eslint-disable max-nested-callbacks */
@@ -184,5 +184,72 @@ define([
184184
widget._getSimpleProductId(selectElement);
185185
expect(widget).toBeTruthy();
186186
});
187+
188+
describe('_addFotoramaVideoEvents', function () {
189+
let mageAddFotoramaVideoEventsWidget,
190+
jQueryFnAddFotoramaVideoEvents,
191+
jQueryFnOn,
192+
galleryElement;
193+
194+
beforeEach(function () {
195+
mageAddFotoramaVideoEventsWidget = $.mage.AddFotoramaVideoEvents;
196+
jQueryFnAddFotoramaVideoEvents = $.fn.AddFotoramaVideoEvents;
197+
jQueryFnOn = $.fn.on;
198+
galleryElement = window.document.createElement('div');
199+
galleryElement.id = 'gallery-' + new Date().getTime();
200+
window.document.body.append(galleryElement);
201+
});
202+
203+
afterEach(function () {
204+
$.mage.AddFotoramaVideoEvents = mageAddFotoramaVideoEventsWidget;
205+
$.fn.AddFotoramaVideoEvents = jQueryFnAddFotoramaVideoEvents;
206+
$.fn.on = jQueryFnOn;
207+
window.document.body.removeChild(galleryElement);
208+
galleryElement = null;
209+
});
210+
211+
it('Should not call AddFotoramaVideoEvents if $.mage.AddFotoramaVideoEvents is undefined', function () {
212+
$.mage.AddFotoramaVideoEvents = undefined;
213+
$.fn.AddFotoramaVideoEvents = jasmine.createSpy().and.returnValue({});
214+
$.fn.on = jasmine.createSpy();
215+
216+
widget.options.mediaGallerySelector = '#' + galleryElement.id;
217+
widget._addFotoramaVideoEvents();
218+
219+
expect($.fn.AddFotoramaVideoEvents).not.toHaveBeenCalled();
220+
expect($.fn.on).toHaveBeenCalledWith('addfotoramavideoeventscreate', jasmine.any(Function));
221+
});
222+
223+
it('Should not call AddFotoramaVideoEvents if the element has not been initialized', function () {
224+
$.mage.AddFotoramaVideoEvents = jasmine.createSpy();
225+
$.fn.AddFotoramaVideoEvents = jasmine.createSpy().and.returnValue(undefined);
226+
$.fn.on = jasmine.createSpy();
227+
228+
widget.options.mediaGallerySelector = '#' + galleryElement.id;
229+
widget._addFotoramaVideoEvents();
230+
231+
expect($.fn.AddFotoramaVideoEvents).toHaveBeenCalledTimes(1);
232+
expect($.fn.AddFotoramaVideoEvents).toHaveBeenCalledWith('instance');
233+
expect($.fn.on).toHaveBeenCalledWith('addfotoramavideoeventscreate', jasmine.any(Function));
234+
});
235+
236+
it('Should call AddFotoramaVideoEvents immediately if the element has already been initialized',
237+
function () {
238+
$.mage.AddFotoramaVideoEvents = jasmine.createSpy();
239+
$.fn.AddFotoramaVideoEvents = jasmine.createSpy().and.returnValue({});
240+
241+
widget.options.mediaGallerySelector = '#' + galleryElement.id;
242+
widget.options.gallerySwitchStrategy = 'prepend';
243+
widget.simpleProduct = 1;
244+
widget._addFotoramaVideoEvents();
245+
246+
expect($.fn.AddFotoramaVideoEvents).toHaveBeenCalledTimes(2);
247+
expect($.fn.AddFotoramaVideoEvents).toHaveBeenCalledWith('instance');
248+
expect($.fn.AddFotoramaVideoEvents).toHaveBeenCalledWith({
249+
selectedOption: 1,
250+
dataMergeStrategy: widget.options.gallerySwitchStrategy
251+
});
252+
});
253+
});
187254
});
188255
});
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright 2024 Adobe
3+
* All Rights Reserved.
4+
*/
5+
6+
/*eslint-disable max-nested-callbacks*/
7+
define([
8+
'jquery',
9+
'Magento_ProductVideo/js/fotorama-add-video-events'
10+
], function ($) {
11+
'use strict';
12+
13+
describe('Magento_ProductVideo/js/fotorama-add-video-events.js', function () {
14+
let element;
15+
16+
beforeEach(function () {
17+
element = document.createElement('div');
18+
document.body.appendChild(element);
19+
});
20+
21+
afterEach(function () {
22+
document.body.removeChild(element);
23+
element = null;
24+
});
25+
26+
describe('_create', function () {
27+
let _initialize, _defaultVideoData;
28+
29+
beforeEach(function () {
30+
_initialize = $.mage.AddFotoramaVideoEvents.prototype._initialize;
31+
_defaultVideoData = $.mage.AddFotoramaVideoEvents.prototype.defaultVideoData;
32+
});
33+
34+
afterEach(function () {
35+
$.mage.AddFotoramaVideoEvents.prototype._initialize = _initialize;
36+
$.mage.AddFotoramaVideoEvents.prototype.defaultVideoData = _defaultVideoData;
37+
});
38+
it('Should set defaultVideoData to videoData if defaultVideoData is empty', function () {
39+
let videoData = [{}, {}];
40+
41+
$(element).AddFotoramaVideoEvents({videoData});
42+
expect($(element).AddFotoramaVideoEvents('instance').defaultVideoData).toEqual(videoData);
43+
});
44+
it('Should set defaultVideoData and videoData to videoDataPlaceholder if they are empty',
45+
function () {
46+
$(element).AddFotoramaVideoEvents();
47+
expect($(element).AddFotoramaVideoEvents('instance').defaultVideoData).toEqual(
48+
$.mage.AddFotoramaVideoEvents.prototype.videoDataPlaceholder
49+
);
50+
expect($(element).AddFotoramaVideoEvents('instance').options.videoData).toEqual(
51+
$.mage.AddFotoramaVideoEvents.prototype.videoDataPlaceholder
52+
);
53+
});
54+
it('Should not set defaultVideoData to videoData if defaultVideoData is not empty', function () {
55+
let videoData = [{}, {}], defaultVideoData = [{}];
56+
57+
$.mage.AddFotoramaVideoEvents.prototype.defaultVideoData = defaultVideoData;
58+
$(element).AddFotoramaVideoEvents({videoData, defaultVideoData});
59+
expect($(element).AddFotoramaVideoEvents('instance').defaultVideoData).toEqual(defaultVideoData);
60+
});
61+
it('Should call _initialize immediately if gallery is already loaded', function () {
62+
$.mage.AddFotoramaVideoEvents.prototype._initialize = jasmine.createSpy();
63+
$(element).data('gallery', true);
64+
$(element).AddFotoramaVideoEvents();
65+
expect($.mage.AddFotoramaVideoEvents.prototype._initialize).toHaveBeenCalled();
66+
});
67+
it('Should call _initialize only after gallery is loaded', function () {
68+
$.mage.AddFotoramaVideoEvents.prototype._initialize = jasmine.createSpy();
69+
$(element).AddFotoramaVideoEvents();
70+
expect($.mage.AddFotoramaVideoEvents.prototype._initialize).not.toHaveBeenCalled();
71+
$(element).trigger('gallery:loaded');
72+
expect($.mage.AddFotoramaVideoEvents.prototype._initialize).toHaveBeenCalled();
73+
});
74+
});
75+
describe('_setOptions', function () {
76+
let _initialize;
77+
78+
beforeEach(function () {
79+
_initialize = $.mage.AddFotoramaVideoEvents.prototype._initialize;
80+
});
81+
82+
afterEach(function () {
83+
$.mage.AddFotoramaVideoEvents.prototype._initialize = _initialize;
84+
});
85+
it('Should call _initialize immediately if gallery is already loaded', function () {
86+
$.mage.AddFotoramaVideoEvents.prototype._initialize = jasmine.createSpy();
87+
// initialize
88+
$(element).AddFotoramaVideoEvents();
89+
expect($.mage.AddFotoramaVideoEvents.prototype._initialize).not.toHaveBeenCalled();
90+
$(element).data('gallery', true);
91+
$(element).trigger('gallery:loaded');
92+
// set options after gallery is loaded
93+
$(element).AddFotoramaVideoEvents({videoData: []});
94+
expect($.mage.AddFotoramaVideoEvents.prototype._initialize).toHaveBeenCalledTimes(2);
95+
});
96+
it('Should call _initialize only after gallery is loaded', function () {
97+
$.mage.AddFotoramaVideoEvents.prototype._initialize = jasmine.createSpy();
98+
// initialize
99+
$(element).AddFotoramaVideoEvents();
100+
expect($.mage.AddFotoramaVideoEvents.prototype._initialize).not.toHaveBeenCalled();
101+
// set options after gallery is loaded
102+
$(element).AddFotoramaVideoEvents({videoData: []});
103+
expect($.mage.AddFotoramaVideoEvents.prototype._initialize).not.toHaveBeenCalled();
104+
$(element).data('gallery', true);
105+
$(element).trigger('gallery:loaded');
106+
expect($.mage.AddFotoramaVideoEvents.prototype._initialize).toHaveBeenCalledTimes(2);
107+
});
108+
});
109+
});
110+
});

0 commit comments

Comments
 (0)