Skip to content

Commit d54eb85

Browse files
authored
fix: exclude tables if simple product (#55)
1 parent dadbf54 commit d54eb85

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/templates/html/HTMLTemplate.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ ${attributes.map((attr) => /* html */`\
234234
* @returns {string}
235235
*/
236236
renderProductOptions(options) {
237-
return /* html */ `\
237+
return options.length > 0 ? /* html */ `\
238238
<div class="product-options">
239239
${options.map((opt) => /* html */ `\
240240
<div>
@@ -246,7 +246,7 @@ ${options.map((opt) => /* html */ `\
246246
<div>${opt.required === true ? 'required' : ''}</div>
247247
</div>
248248
${HTMLTemplate.indent(this.renderProductItems(opt.items), 2)}`).join('\n')}
249-
</div>`;
249+
</div>` : '';
250250
}
251251

252252
/**
@@ -280,7 +280,7 @@ ${HTMLTemplate.indent(this.renderProductItems(opt.items), 2)}`).join('\n')}
280280
* @returns {string}
281281
*/
282282
renderProductVariants() {
283-
if (!this.variants) {
283+
if (!this.variants || this.variants.length === 0) {
284284
return '';
285285
}
286286

@@ -306,7 +306,7 @@ ${HTMLTemplate.indent(this.renderVariantImages(v.images), 6)}
306306
* @returns {string}
307307
*/
308308
renderProductVariantsAttributes() {
309-
if (!this.variants) {
309+
if (!this.variants || this.variants.length === 0) {
310310
return '';
311311
}
312312

test/templates/html/index.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ describe('Render Product HTML', () => {
233233
});
234234
});
235235

236+
it('should not render product options table if there are none', () => {
237+
product.options = [];
238+
const html = htmlTemplateFromContext(DEFAULT_CONTEXT({ config }), product, variations).render();
239+
dom = new JSDOM(html);
240+
document = dom.window.document;
241+
242+
const optionsTable = document.querySelector('.product-options');
243+
assert.strictEqual(optionsTable, null, 'Options table should not be rendered');
244+
});
245+
246+
it('should not render product variants table if there are none', () => {
247+
variations = [];
248+
const html = htmlTemplateFromContext(DEFAULT_CONTEXT({ config }), product, variations).render();
249+
dom = new JSDOM(html);
250+
document = dom.window.document;
251+
252+
const variantsTable = document.querySelector('.product-variants');
253+
assert.strictEqual(variantsTable, null, 'Variants table should not be rendered');
254+
});
255+
256+
it('should not render variant attributes table if there are none', () => {
257+
const html = htmlTemplateFromContext(DEFAULT_CONTEXT({ config }), product, []).render();
258+
dom = new JSDOM(html);
259+
document = dom.window.document;
260+
const variantAttributesTable = document.querySelector('.variant-attributes');
261+
assert.strictEqual(variantAttributesTable, null, 'Variant attributes table should not be rendered');
262+
});
263+
236264
it('template should allow for missing prices', () => {
237265
config.confMap = {
238266
'/us/p/{{urlkey}}/{{sku}}': {},

0 commit comments

Comments
 (0)