Skip to content

Commit 52a547b

Browse files
PB-77: Product Carousel Appearance
- fix static tests - add js unit tests
1 parent acaeb5c commit 52a547b

File tree

9 files changed

+244
-7
lines changed

9 files changed

+244
-7
lines changed

app/code/Magento/PageBuilder/Block/WidgetInitializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function __construct(
4747

4848
/**
4949
* Returns config for widgets initializer component.
50+
*
5051
* @return string
5152
* @api
5253
*/

app/code/Magento/PageBuilder/Model/WidgetInitializerConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public function getConfig(): array
6767
*
6868
* @return array
6969
*/
70-
public function getBreakpoints(): array {
70+
public function getBreakpoints(): array
71+
{
7172
return $this->viewConfig->getViewConfig()->getVarValue(
7273
'Magento_PageBuilder',
7374
'breakpoints'

app/code/Magento/PageBuilder/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"magento/module-rule": "*",
1818
"magento/module-directory": "*",
1919
"magento/module-email": "*",
20+
"magento/module-theme": "*",
21+
"magento/module-wishlist": "*",
2022
"magento/module-require-js": "*",
2123
"php": "~7.1.3||~7.2.0||~7.3.0"
2224
},

app/code/Magento/PageBuilder/view/adminhtml/web/css/source/content-type/products/_carousel.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@
6565
}
6666
}
6767
}
68-
}
68+
}

app/code/Magento/PageBuilder/view/frontend/templates/catalog/product/widget/content/carousel.phtml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
use Magento\Framework\App\Action\Action;
77

8+
// phpcs:disable Magento2.Templates.ThisInTemplate
9+
810
/** @var \Magento\CatalogWidget\Block\Product\ProductsList $block */
911
?>
1012
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint-disable max-nested-callbacks */
7+
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
8+
define([
9+
'jquery',
10+
'Magento_PageBuilder/js/content-type/products/mass-converter/carousel-widget-directive'
11+
], function ($, WidgetDirective) {
12+
'use strict';
13+
14+
describe('Magento_PageBuilder/js/content-type/products/mass-converter/carousel-widget-directive', function () {
15+
var model;
16+
17+
beforeEach(function () {
18+
model = new WidgetDirective();
19+
});
20+
21+
describe('toDom', function () {
22+
it('Should return an empty string when conditions_encoded is null', function () {
23+
var data = {
24+
products_count: 123,
25+
conditions_encoded: null
26+
},
27+
config = {
28+
html_variable: 'myhtml'
29+
},
30+
result = model.toDom(data, config);
31+
32+
expect(result.myhtml).toBe(undefined);
33+
});
34+
it('Should return an empty string when conditions_encoded is false', function () {
35+
var data = {
36+
products_count: 123,
37+
conditions_encoded: false
38+
},
39+
config = {
40+
html_variable: 'myhtml'
41+
},
42+
result = model.toDom(data, config);
43+
44+
expect(result.myhtml).toBe(undefined);
45+
});
46+
it('Should return an empty string when conditions_encoded is empty string', function () {
47+
var data = {
48+
products_count: 123,
49+
conditions_encoded: ''
50+
},
51+
config = {
52+
html_variable: 'myhtml'
53+
},
54+
result = model.toDom(data, config);
55+
56+
expect(result.myhtml).toBe(undefined);
57+
});
58+
it('Should return an empty string when conditions_encoded is empty undefined', function () {
59+
var data = {
60+
products_count: 123
61+
},
62+
config = {
63+
html_variable: 'myhtml'
64+
},
65+
result = model.toDom(data, config);
66+
67+
expect(result.myhtml).toBe(undefined);
68+
});
69+
it('Should transform regular properties', function () {
70+
var data = {
71+
carousel_products_count: 123,
72+
conditions_encoded: '[]'
73+
},
74+
config = {
75+
html_variable: 'myhtml'
76+
},
77+
result = model.toDom(data, config);
78+
79+
expect(result.myhtml).toMatch(/^\{\{widget\s.*\}\}$/);
80+
expect(result.myhtml).toContain(' products_count="123"');
81+
expect(result.myhtml).toContain(' conditions_encoded="[]"');
82+
expect(result.myhtml).toContain(' type="Magento\\CatalogWidget\\Block\\Product\\ProductsList"');
83+
expect(result.myhtml)
84+
.toContain(' template="Magento_PageBuilder::catalog/product/widget/content/carousel.phtml"');
85+
expect(result.myhtml).toContain(' anchor_text=""');
86+
expect(result.myhtml).toContain(' id_path=""');
87+
expect(result.myhtml).toContain(' show_pager="0"');
88+
expect(result.myhtml).toContain(' type_name="Catalog Products Carousel"');
89+
});
90+
it('Should encode conditions_encoded', function () {
91+
var data = {
92+
carousel_products_count: 123,
93+
conditions_encoded: '{"1":{"type":"My\\\\Type","aggregator":"all"}}'
94+
},
95+
config = {
96+
html_variable: 'myhtml'
97+
},
98+
result = model.toDom(data, config),
99+
matchText = '^[`1`:^[`type`:`My||Type`,`aggregator`:`all`^]^]';
100+
101+
expect(result.myhtml).toContain(' conditions_encoded="' + matchText + '"');
102+
expect(result.myhtml).toContain(' type="Magento\\CatalogWidget\\Block\\Product\\ProductsList"');
103+
expect(result.myhtml)
104+
.toContain(' template="Magento_PageBuilder::catalog/product/widget/content/carousel.phtml"');
105+
expect(result.myhtml).toContain(' anchor_text=""');
106+
expect(result.myhtml).toContain(' id_path=""');
107+
expect(result.myhtml).toContain(' show_pager="0"');
108+
expect(result.myhtml).toContain(' type_name="Catalog Products Carousel"');
109+
});
110+
});
111+
describe('fromDom', function () {
112+
it('Should parse regular properties without conditions_encoded', function () {
113+
var expected = {
114+
carousel_products_count: '123',
115+
conditions_encoded: ''
116+
},
117+
config = {
118+
html_variable: 'myhtml'
119+
},
120+
attributes = {
121+
myhtml: '{{widget products_count="123"}}'
122+
},
123+
result = model.fromDom(attributes, config);
124+
125+
expect(result.carousel_products_count).toBe(expected.carousel_products_count);
126+
expect(result.conditions_encoded).toBe(expected.conditions_encoded);
127+
// assert the automatically added properties do not get returned.
128+
expect(result.type).toBe(undefined);
129+
expect(result.id_path).toBe(undefined);
130+
});
131+
it('Should parse conditions_encoded', function () {
132+
var expected = {
133+
carousel_products_count: '123',
134+
conditions_encoded: '{"1":{"type":"My\\\\Type","aggregator":"all"}}'
135+
},
136+
config = {
137+
html_variable: 'myhtml'
138+
},
139+
attributes = {
140+
myhtml: '{{widget ' +
141+
'products_count="123" ' +
142+
'conditions_encoded="^[`1`:^[`type`:`My||Type`,`aggregator`:`all`^]^]"}}'
143+
},
144+
result = model.fromDom(attributes, config);
145+
146+
expect(result.carousel_products_count).toBe(expected.carousel_products_count);
147+
expect(result.conditions_encoded).toBe(expected.conditions_encoded);
148+
// assert the automatically added properties do not get returned.
149+
expect(result.type).toBe(undefined);
150+
expect(result.id_path).toBe(undefined);
151+
});
152+
});
153+
});
154+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([
6+
'Magento_PageBuilder/js/content-type/products/appearance/carousel/widget',
7+
'jquery'
8+
], function (sliderWidgetInitializer, $) {
9+
'use strict';
10+
11+
describe('Magento_PageBuilder/js/content-type/products/appearance/carousel/widget', function () {
12+
var config = {
13+
breakpoints: {
14+
desktop: {
15+
conditions: {
16+
'min-width': '1px'
17+
},
18+
options: {
19+
products: {
20+
slidesToShow: 5
21+
}
22+
}
23+
}
24+
}
25+
},
26+
el;
27+
28+
beforeEach(function () {
29+
var childEl = document.createElement('div');
30+
31+
el = document.createElement('div');
32+
el.setAttribute('data-display', true);
33+
el.appendChild(childEl);
34+
});
35+
36+
it('Should call unslick if element has class slick-initialized', function () {
37+
spyOn($.fn, 'slick');
38+
el.children[0].classList.add('slick-initialized');
39+
40+
sliderWidgetInitializer(config, el);
41+
42+
expect($.fn.slick).toHaveBeenCalledWith('unslick');
43+
});
44+
45+
it('Should call slick on element based on its data', function () {
46+
spyOn($.fn, 'slick');
47+
48+
el.setAttribute('data-slide-all', 'true');
49+
el.setAttribute('data-autoplay', 'false');
50+
el.setAttribute('data-autoplay-speed', 4000);
51+
el.setAttribute('data-infinite-loop', 'false');
52+
el.setAttribute('data-show-arrows', 'false');
53+
el.setAttribute('data-show-dots', 'true');
54+
el.setAttribute('data-center-mode', 'false');
55+
56+
sliderWidgetInitializer(config, el);
57+
58+
expect($.fn.slick).toHaveBeenCalledWith({
59+
slideAll: true,
60+
autoplay: false,
61+
autoplaySpeed: 4000,
62+
infinite: false,
63+
arrows: false,
64+
dots: true,
65+
centerMode: false,
66+
slidesToShow: 5,
67+
slidesToScroll: 5
68+
});
69+
});
70+
});
71+
});

dev/tests/js/jasmine/tests/app/code/Magento/PageBuilder/view/frontend/web/js/widget-initializer.test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,22 @@ define([
5656
data = {
5757
config: {
5858
'.unique-element-class-attr': {
59-
awesome: true,
60-
cool: true
59+
awesome: false,
60+
cool: {}
6161
}
62-
}
62+
},
63+
breakpoints: {}
6364
};
6465

6566
widgetInitializer(data);
6667

6768
applyForMock = mocks['mage/apply/main'].applyFor;
68-
expect(applyForMock).toHaveBeenCalledWith(jasmine.any(Object), true, 'awesome');
69-
expect(applyForMock).toHaveBeenCalledWith(jasmine.any(Object), true, 'cool');
69+
expect(applyForMock).toHaveBeenCalledWith(jasmine.any(Object), {
70+
breakpoints: {}
71+
}, 'awesome');
72+
expect(applyForMock).toHaveBeenCalledWith(jasmine.any(Object), {
73+
breakpoints: {}
74+
}, 'cool');
7075
// Due to jQuery objects not being === we must use jQuery's is to validate if the elements are the same
7176
expect(el.is(applyForMock.calls.mostRecent().args[0])).toBeTruthy();
7277
});

dev/tests/static/testsuite/Magento/Test/Integrity/Xml/ExtendsBaseFormTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private function getXmlFiles(): array
6565
'pagebuilder_slide_form.xml' => 'pagebuilder_base_form_with_background_attributes',
6666
'pagebuilder_tab_item_form.xml' => 'pagebuilder_base_form_with_background_attributes',
6767
'pagebuilder_row_form.xml' => 'pagebuilder_base_form_with_background_attributes',
68+
'pagebuilder_products_carousel_form.xml' => 'pagebuilder_products_form',
6869
];
6970
$componentRegistrar = new ComponentRegistrar();
7071
$modulePath = $componentRegistrar->getPath(ComponentRegistrar::MODULE, 'Magento_PageBuilder');

0 commit comments

Comments
 (0)