Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ <h3>Tótems de búsqueda</h3>
<script src="js/mall/mall-assistant-catalog-search.js?v=20261003-local-catalog-search" defer></script>
<script src="js/mall/mall-store-assistant.js?v=20261003-local-catalog-search" defer></script>
<script src="js/mall/mall-npc.js?v=20261004-third-person-camera-v1" defer></script>
<script src="js/mall/mall-purchases.js?v=20261014-shipping-rate-id-guard-v1" defer></script>
<script src="js/mall/mall-purchases.js?v=20261127-hide-purchase-shipping-rate-table-v1" defer></script>
<script src="js/mall/mall-guided-tour.js?v=20260929-guided-tour" defer></script>
<script src="js/mall/mall-quick-store-picker.js?v=20261011-third-person-store-catalog-v1" defer></script>
<script src="js/mall/mall-main.js?v=20260929-guided-tour" defer></script>
Expand Down
35 changes: 2 additions & 33 deletions js/mall/mall-purchases.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@
<strong id="purchase-product-price"></strong>
</div>
</div>
<section class="purchase-shipping-section" aria-labelledby="purchase-shipping-title">
<h3 id="purchase-shipping-title">Costos de despacho de este local</h3>
<div id="purchase-shipping-table" class="purchase-shipping-table-wrap">
<p class="purchase-muted">Cargando tarifas...</p>
</div>
</section>
<form id="purchase-form" class="purchase-form">
<div class="purchase-form-heading">
<h3>Datos del comprador y despacho</h3>
Expand Down Expand Up @@ -336,19 +330,16 @@
window.closePurchaseForm = closePurchaseForm;

function renderShippingRates(rates = []) {
const tableContainer = document.getElementById('purchase-shipping-table');
const select = document.getElementById('purchase-delivery-commune');
const submit = document.getElementById('purchase-submit');
if (!tableContainer || !select || !submit) return;
if (!select || !submit) return;

const selectedCommune = select.value;
tableContainer.innerHTML = '';
select.innerHTML = '';
select.appendChild(createElement('option', 'Selecciona una comuna'));
select.firstElementChild.value = '';

if (!rates.length) {
tableContainer.appendChild(createElement('p', 'Este local aún no ha configurado costos de despacho.', 'purchase-muted'));
SANTIAGO_COMMUNES.forEach((commune) => {
const option = createElement('option', commune);
option.value = commune;
Expand All @@ -360,32 +351,12 @@
return;
}

const table = document.createElement('table');
table.className = 'purchase-shipping-table';
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
headerRow.appendChild(createElement('th', 'Comuna'));
headerRow.appendChild(createElement('th', 'Costo de despacho'));
thead.appendChild(headerRow);
const tbody = document.createElement('tbody');
const ratesByCommune = new Map(rates.map((rate) => [rate.commune.toLocaleLowerCase('es-CL'), rate]));
rates.forEach((rate) => {
const row = document.createElement('tr');
row.appendChild(createElement('td', rate.commune));
row.appendChild(createElement('td', formatCurrency(rate.shipping_cost)));
tbody.appendChild(row);
});
SANTIAGO_COMMUNES.forEach((commune) => {
const rate = ratesByCommune.get(commune.toLocaleLowerCase('es-CL'));
const option = createElement('option', rate
? `${commune} - ${formatCurrency(rate.shipping_cost)}`
: `${commune} - Sin despacho configurado`);
const option = createElement('option', commune);
option.value = commune;
select.appendChild(option);
});
if (SANTIAGO_COMMUNES.includes(selectedCommune)) select.value = selectedCommune;
table.append(thead, tbody);
tableContainer.appendChild(table);
select.disabled = false;
submit.disabled = true;
}
Expand Down Expand Up @@ -418,7 +389,6 @@

async function loadPurchaseRates(product, requestId) {
const client = getClient();
const table = document.getElementById('purchase-shipping-table');
const select = document.getElementById('purchase-delivery-commune');
const submit = document.getElementById('purchase-submit');
if (!client || !product?.storeId) {
Expand All @@ -429,7 +399,6 @@
return;
}

if (table) table.innerHTML = '<p class="purchase-muted">Cargando tarifas...</p>';
if (select) select.disabled = false;
if (submit) submit.disabled = true;
purchaseState.ratesLoading = true;
Expand Down
24 changes: 19 additions & 5 deletions tests/unit/purchase-payment-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ const purchases = fs.readFileSync(path.join(root, 'js/mall/mall-purchases.js'),
const migration = fs.readFileSync(path.join(root, 'supabase/store_payment_options_20260927.sql'), 'utf8');
const ui = fs.readFileSync(path.join(root, 'js/mall/mall-ui.js'), 'utf8');

assert.doesNotMatch(purchases, /purchase-shipping-table|purchase-shipping-table-wrap/,
'The buyer checkout must not expose the full store commune and shipping-rate table.');
assert.doesNotMatch(purchases, /Costos de despacho de este local/,
'The buyer checkout must not show the removed shipping-rate section title.');
assert.match(purchases, /id="purchase-delivery-commune"/,
'The buyer keeps the commune selector.');
assert.match(purchases, /id="purchase-shipping-summary"/,
'The buyer keeps the selected shipping-cost summary.');

const optionBlock = purchases.match(/const PAYMENT_OPTIONS = Object\.freeze\(\[([\s\S]*?)\]\);/);
assert.ok(optionBlock, 'The checkout must declare its supported payment alternatives.');
const optionValues = [...optionBlock[1].matchAll(/value: '([^']+)'/g)].map((match) => match[1]);
Expand Down Expand Up @@ -74,11 +83,9 @@ function makeElement(tagName) {
return element;
}

const shippingTable = makeElement('div');
const communeSelect = makeElement('select');
const purchaseButton = makeElement('button');
const shippingElements = {
'purchase-shipping-table': shippingTable,
'purchase-delivery-commune': communeSelect,
'purchase-submit': purchaseButton
};
Expand All @@ -104,12 +111,15 @@ renderContext.renderShippingRates([]);
assert.equal(communeSelect.disabled, false, 'The commune selector stays enabled when the tenant has no rates yet.');
assert.equal(communeSelect.children.length, 53, 'All 52 communes remain available beside the placeholder.');
assert.ok(communeSelect.children.slice(1).every((option) => !option.disabled), 'No-rate communes remain selectable.');
assert.ok(communeSelect.children.slice(1).every((option) => option.textContent === option.value),
'Commune options show only their name when the tenant has no rates yet.');
assert.equal(purchaseButton.disabled, true, 'A purchase cannot be sent without a shipping price.');

renderContext.renderShippingRates([{commune: 'Providencia', shipping_cost: 2500}]);
assert.equal(communeSelect.disabled, false);
assert.ok(communeSelect.children.slice(1).every((option) => !option.disabled), 'Communes remain selectable while rates are partial.');
assert.match(communeSelect.children.find((option) => option.value === 'Vitacura').textContent, /Sin despacho configurado/);
assert.ok(communeSelect.children.slice(1).every((option) => option.textContent === option.value),
'Commune options do not expose rates or availability details.');

const shippingSummary = makeElement('p');
const selectedCommune = makeElement('select');
Expand All @@ -125,6 +135,7 @@ const summaryContext = {
})[id]
},
purchaseState: {rates: [{commune: 'Providencia', shipping_cost: 2500}]},
formatCurrency: (value) => `$${value}`,
updatePurchasePaymentSummary: () => { paymentSummaryUpdated = true; }
};
const summaryStart = purchases.indexOf(' function updatePurchaseShippingSummary(');
Expand All @@ -136,7 +147,11 @@ assert.match(shippingSummary.textContent, /no ha configurado el costo de despach
assert.equal(summaryButton.disabled, true);
assert.equal(paymentSummaryUpdated, true, 'Payment details refresh when the selected commune has no rate.');

const loadingTable = makeElement('div');
selectedCommune.value = 'Providencia';
summaryContext.updatePurchaseShippingSummary();
assert.match(shippingSummary.textContent, /Despacho a Providencia: \$2500/);
assert.equal(summaryButton.disabled, false, 'A purchase can continue when the selected commune has a valid rate.');

const loadingSelect = makeElement('select');
loadingSelect.disabled = true;
const loadingSubmit = makeElement('button');
Expand All @@ -151,7 +166,6 @@ const loadingState = {requestId: 7, rates: [], ratesLoading: false};
const loadingContext = {
document: {
getElementById: (id) => ({
'purchase-shipping-table': loadingTable,
'purchase-delivery-commune': loadingSelect,
'purchase-submit': loadingSubmit
})[id]
Expand Down