Refresh checkout totals after validation if needed

Refreshing the totals the same way as WC does this.
This commit is contained in:
Alex P 2023-04-04 11:35:16 +03:00
parent c03b3d86b5
commit 3c557907f2
No known key found for this signature in database
GPG key ID: 54487A734A204D71
5 changed files with 47 additions and 11 deletions

View file

@ -11,6 +11,7 @@ use WooCommerce\PayPalCommerce\Button\Exception\ValidationException;
use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
class ValidateCheckoutEndpointTest extends TestCase
{
@ -21,6 +22,8 @@ class ValidateCheckoutEndpointTest extends TestCase
private $logger;
private $sut;
private $session = [];
public function setUp(): void
{
parent::setUp();
@ -36,6 +39,10 @@ class ValidateCheckoutEndpointTest extends TestCase
);
$this->requestData->expects('read_request')->andReturn(['form' => ['field1' => 'value']]);
when('WC')->alias(function () {
return (object) ['session' => (object) $this->session];
});
}
public function testValid()
@ -54,7 +61,21 @@ class ValidateCheckoutEndpointTest extends TestCase
->andThrow($exception);
expect('wp_send_json_error')->once()
->with(['message' => $exception->getMessage(), 'errors' => ['Invalid value']]);
->with(['message' => $exception->getMessage(), 'errors' => ['Invalid value'], 'refresh' => false]);
$this->sut->handle_request();
}
public function testInvalidAndRefresh()
{
$exception = new ValidationException(['Invalid value']);
$this->formValidator->expects('validate')->once()
->andThrow($exception);
$this->session['refresh_totals'] = true;
expect('wp_send_json_error')->once()
->with(['message' => $exception->getMessage(), 'errors' => ['Invalid value'], 'refresh' => true]);
$this->sut->handle_request();
}