Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit d356862

Browse files
committed
rename variables; add test for the function getSettingWithCoercion
1 parent dc52b2f commit d356862

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

assets/js/blocks/cart-checkout/mini-cart/block.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ const MiniCartBlock = ( {
120120
isBoolean
121121
);
122122

123-
const labelTaxName = getSettingWithCoercion(
124-
'labelIncludingTax',
125-
'',
126-
isString
127-
);
123+
const taxLabel = getSettingWithCoercion( 'taxLabel', '', isString );
128124

129125
const subTotal = showIncludingTax
130126
? parseInt( cartTotals.total_items, 10 ) +
@@ -167,9 +163,9 @@ const MiniCartBlock = ( {
167163
getCurrencyFromPriceResponse( cartTotals )
168164
) }
169165
</span>
170-
{ labelTaxName !== '' && subTotal !== 0 && (
171-
<small className="wc-block-mini-cart__tax_label">
172-
{ labelTaxName }
166+
{ taxLabel !== '' && subTotal !== 0 && (
167+
<small className="wc-block-mini-cart__tax-label">
168+
{ taxLabel }
173169
</small>
174170
) }
175171
<QuantityBadge
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { isBoolean, isString } from '@woocommerce/types';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import { ADMIN_URL } from '../default-constants';
10+
import { getSettingWithCoercion } from '..';
11+
12+
describe( 'getSettingWithCoercion', () => {
13+
it( 'returns provided default for non available setting', () => {
14+
expect(
15+
getSettingWithCoercion( 'nada', 'really nada', isString )
16+
).toBe( 'really nada' );
17+
} );
18+
it( 'returns expected value for existing setting', () => {
19+
expect(
20+
getSettingWithCoercion( 'adminUrl', 'not this', isString )
21+
).toEqual( ADMIN_URL );
22+
} );
23+
it( "returns expected value for existing setting even if it's a falsy value", () => {
24+
expect(
25+
getSettingWithCoercion( 'currentUserIsAdmin', true, isBoolean )
26+
).toBe( false );
27+
} );
28+
} );

src/BlockTypes/MiniCart.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class MiniCart extends AbstractBlock {
3434
*
3535
* @var string
3636
*/
37-
protected $label_including_tax = '';
37+
protected $tax_label = '';
3838

3939
/**
4040
* Visibility of price including tax.
@@ -110,15 +110,12 @@ protected function enqueue_data( array $attributes = [] ) {
110110

111111
$label_info = $this->get_tax_label();
112112

113-
$label_including_tax = $label_info['label_including_tax'];
114-
$display_cart_prices_including_tax = $label_info['display_cart_prices_including_tax'];
115-
116-
$this->label_including_tax = $label_including_tax;
117-
$this->display_cart_prices_including_tax = $display_cart_prices_including_tax;
113+
$this->tax_label = $label_info['tax_label'];
114+
$this->display_cart_prices_including_tax = $label_info['display_cart_prices_including_tax'];
118115

119116
$this->asset_data_registry->add(
120-
'labelIncludingTax',
121-
$this->label_including_tax,
117+
'taxLabel',
118+
$this->tax_label,
122119
''
123120
);
124121

@@ -249,7 +246,7 @@ protected function get_include_tax_label_markup() {
249246
$cart = $cart_controller->get_cart_instance();
250247
$cart_contents_total = $cart->get_subtotal();
251248

252-
return ( ! empty( $this->label_including_tax ) && 0 !== $cart_contents_total ) ? ( "<small class='wc-block-mini-cart__tax_label'>" . $this->label_including_tax . '</small>' ) : '';
249+
return ( ! empty( $this->tax_label ) && 0 !== $cart_contents_total ) ? ( "<small class='wc-block-mini-cart__tax-label'>" . esc_html( $this->tax_label ) . '</small>' ) : '';
253250
}
254251

255252
/**
@@ -425,10 +422,10 @@ protected function get_tax_label() {
425422

426423
if ( $cart->display_prices_including_tax() ) {
427424
if ( ! wc_prices_include_tax() ) {
428-
$label_including_tax = WC()->countries->inc_tax_or_vat();
425+
$tax_label = WC()->countries->inc_tax_or_vat();
429426
$display_cart_prices_including_tax = true;
430427
return array(
431-
'label_including_tax' => $label_including_tax,
428+
'tax_label' => $tax_label,
432429
'display_cart_prices_including_tax' => $display_cart_prices_including_tax,
433430
);
434431
}
@@ -439,16 +436,15 @@ protected function get_tax_label() {
439436
}
440437

441438
if ( wc_prices_include_tax() ) {
442-
$label_including_tax = WC()->countries->ex_tax_or_vat();
443-
$display_cart_prices_including_tax = true;
439+
$tax_label = WC()->countries->ex_tax_or_vat();
444440
return array(
445-
'label_including_tax' => $label_including_tax,
441+
'tax_label' => $tax_label,
446442
'display_cart_prices_including_tax' => false,
447443
);
448444
};
449445

450446
return array(
451-
'label_including_tax' => '',
447+
'tax_label' => '',
452448
'display_cart_prices_including_tax' => false,
453449
);
454450
}

0 commit comments

Comments
 (0)