我的App.js文件中有一个函数,如下所示。
function unCamelCase(val) {
return val.replace(/([a-z])([A-Z])/g, '$1 $2')
.replace(/\b([A-Z]+)([A-Z])([a-z])/, '$1 $2$3')
.replace(/^./, function(str){ return str.toUpperCase(); })
}
如何编写上述功能的单元测试。
谢谢。
答案 0 :(得分:0)
您听说过Jest吗?我相信Facebook会用它来测试他们所有的javascript代码。在我的一个项目中,我很少使用它。如果您有兴趣,此页面说明了如何进行设置。
https://jestjs.io/docs/en/getting-started.html
示例实现(unCamelCase.js位于测试文件夹之外,而test_unCamelCase.js位于内部):
?>
<script>
ga('require', 'ecommerce');
<?php
// GET the WC_Order object instance from, the Order ID
$order = wc_get_order( $order_id );
$order_key = $order->get_order_key();
$transaction_id = $order->get_transaction_id(); // Doesn't always exist
$transaction_id = $order_id; // (Or the order key or the transaction ID if it exist)
?>
ga('ecommerce:addTransaction', {
'id': '<?php echo $transaction_id; // To be checked ?>',
'affiliation': '<?php echo 'UA-XXXXX-Y'; // replace by yours ?>',
'revenue': '<?php echo $order->get_total(); ?>',
'shipping': '<?php echo $order->get_shipping_total(); ?>',
'tax': '<?php echo $order->get_total_tax(); ?>',
'currency': '<?php echo get_woocommerce_currency(); // Optional ?>'
}); <?php
// LOOP START: Iterate through order items
foreach( $order->get_items() as $item_id => $item ) :
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get the product categories for the product
$categories = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );
$category = reset($categories); // Keep only the first product category
?>
ga('ecommerce:addItem', {
'id': '<?php echo $transaction_id; ?>',
'name': '<?php echo $item->get_name(); ?>',
'sku': '<?php echo $product->get_sku(); ?>',
'category': '<?php echo $category; ?>',
'price': '<?php echo wc_get_price_excluding_tax($product); // OR wc_get_price_including_tax($product) ?>',
'quantity': '<?php echo $item->get_quantity(); ?>',
'currency': '<?php echo get_woocommerce_currency(); // Optional ?>'
});
<?php
endforeach; // LOOP END
?>
ga('ecommerce:send');
</script>
<?php
// test_unCamelCase.js
const unCamelCase = require('../unCamelCase');
test("test unCamelCase", () => {
expect(unCamelCase("camelCase")).toBe("Camel Case")
expect(unCamelCase("moreThanTwoWords")).toBe("More Than Two Words")
expect(unCamelCase("symb0lsAndNumber$")).toBe("Symb0ls And Number$")
})