Skip to content

Commit

Permalink
Enable cookie testing on subsequent request (#8087)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmad Fikrizaman <ahmadfikrizaman@gmail.com>
Co-authored-by: Caleb Porzio <calebporzio@gmail.com>
  • Loading branch information
3 people committed Mar 12, 2024
1 parent 44b0280 commit 67c66a0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/Features/SupportTesting/SubsequentRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ function __construct(
protected ComponentState $lastState,
) {}

static function make($requestBroker, $lastState, $calls = [], $updates = [])
static function make($requestBroker, $lastState, $calls = [], $updates = [], $cookies = [])
{
$instance = new static($requestBroker, $lastState);

return $instance->makeSubsequentRequest($calls, $updates);
return $instance->makeSubsequentRequest($calls, $updates, $cookies);
}

function makeSubsequentRequest($calls = [], $updates = []) {
function makeSubsequentRequest($calls = [], $updates = [], $cookies = []) {
$uri = app('livewire')->getUpdateUri();

$encodedSnapshot = json_encode($this->lastState->getSnapshot());
Expand All @@ -31,9 +31,9 @@ function makeSubsequentRequest($calls = [], $updates = []) {
],
];

[$response, $componentInstance, $componentView] = $this->extractComponentAndBladeView(function () use ($uri, $payload) {
return $this->requestBroker->temporarilyDisableExceptionHandlingAndMiddleware(function ($requestBroker) use ($uri, $payload) {
return $requestBroker->withHeaders(['X-Livewire' => true])->post($uri, $payload);
[$response, $componentInstance, $componentView] = $this->extractComponentAndBladeView(function () use ($uri, $payload, $cookies) {
return $this->requestBroker->temporarilyDisableExceptionHandlingAndMiddleware(function ($requestBroker) use ($uri, $payload, $cookies) {
return $requestBroker->addHeaders(['X-Livewire' => true])->call('POST', $uri, $payload, $cookies);
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/Features/SupportTesting/Testable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Livewire\Features\SupportTesting;

use Illuminate\Support\Traits\Macroable;
use Livewire\Features\SupportFileDownloads\TestsFileDownloads;
use Livewire\Features\SupportValidation\TestsValidation;
use Livewire\Features\SupportRedirects\TestsRedirects;
use Livewire\Features\SupportEvents\TestsEvents;
use Illuminate\Support\Traits\Macroable;

/** @mixin \Illuminate\Testing\TestResponse */

Expand Down Expand Up @@ -177,6 +177,7 @@ function update($calls = [], $updates = [])
$this->lastState,
$calls,
$updates,
app('request')->cookies->all()
);

$this->lastState = $newState;
Expand Down
27 changes: 26 additions & 1 deletion src/Features/SupportTesting/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Livewire\Features\SupportTesting;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use PHPUnit\Framework\ExpectationFailedException;
use Illuminate\Support\Facades\Artisan;
Expand All @@ -11,6 +10,7 @@
use Illuminate\Testing\TestView;
use Livewire\Component;
use Livewire\Livewire;
use Closure;

// TODO - Change this to \Tests\TestCase
class UnitTest extends \LegacyTests\Unit\TestCase
Expand Down Expand Up @@ -616,6 +616,31 @@ public function render()
->assertSet('nameHeader', 'Taylor')
;
}

/** @test */
public function can_set_cookies_and_use_it_for_testing_subsequent_request()
{
// Test both the `withCookies` and `withCookie` methods that Laravel normally provides
Livewire::withCookies(['colour' => 'blue'])->withCookie('name', 'Taylor')
->test(new class extends Component {
public $colourCookie = '';
public $nameCookie = '';

public function setTheCookies()
{
$this->colourCookie = request()->cookie('colour');
$this->nameCookie = request()->cookie('name');
}

public function render()
{
return '<div></div>';
}
})
->call('setTheCookies')
->assertSet('colourCookie', 'blue')
->assertSet('nameCookie', 'Taylor');
}
}

class HasMountArguments extends Component
Expand Down

0 comments on commit 67c66a0

Please sign in to comment.