Skip to content

Commit

Permalink
Merge pull request #112 from bangnokia/main
Browse files Browse the repository at this point in the history
merge initialStep with allStepState
  • Loading branch information
freekmurze authored Apr 22, 2024
2 parents 0d0412c + 5e24a26 commit 90053e3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Components/Concerns/MountsWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function mountMountsWizard(?string $showStep = null, array $initialState

$initialState = $initialState ?? $this->initialState() ?? [];

$initialState = array_merge($initialState, $this->allStepState);

$this->showStep($stepName, $initialState[$stepName] ?? []);

foreach ($initialState as $stepName => $state) {
Expand Down
12 changes: 12 additions & 0 deletions tests/InitialStateTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Spatie\LivewireWizard\Tests\TestSupport\Components\Steps\FirstStepComponent;
use Spatie\LivewireWizard\Tests\TestSupport\Components\WizardWithInitialState;
Expand Down Expand Up @@ -63,3 +64,14 @@
->assertSuccessful()
->assertNotSet('name', 'Freek');
});

it('state is not persisted when wizard recreated', function () {
$this->wizard->call('setStepState', 'first-step', ['order' => 456]);

$wizard = Testable::create(WizardWithInitialState::class, [
'order' => 123,
]);

Livewire::test(FirstStepComponent::class, $wizard->getStepState(FirstStepComponent::class))
->assertSet('order', 123);
});
33 changes: 33 additions & 0 deletions tests/PersistStateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Spatie\LivewireWizard\Tests\TestSupport\Components\Steps\FirstStepComponent;
use Spatie\LivewireWizard\Tests\TestSupport\Components\WizardWithPersistState;

it('can restore the persisted state', function () {
$wizard = Testable::create(WizardWithPersistState::class, [
'initialState' => [
'first-step' => [
'order' => 123
]
]
]);

$wizard->call('setStepState', 'first-step', ['order' => 456]);

Livewire::test(FirstStepComponent::class, $wizard->getStepState(FirstStepComponent::class))
->assertSet('order', 456);

// recreate the wizard
$wizard = Testable::create(WizardWithPersistState::class, [
'initialState' => [
'first-step' => [
'order' => 123
]
]
]);

Livewire::test(FirstStepComponent::class, $wizard->getStepState(FirstStepComponent::class))
->assertSet('order', 456);
});
31 changes: 31 additions & 0 deletions tests/TestSupport/Components/WizardWithPersistState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spatie\LivewireWizard\Tests\TestSupport\Components;

use Livewire\Attributes\Session;
use Spatie\LivewireWizard\Components\WizardComponent;
use Spatie\LivewireWizard\Tests\TestSupport\Components\Steps\FirstStepComponent;
use Spatie\LivewireWizard\Tests\TestSupport\Components\Steps\SecondStepComponent;

class WizardWithPersistState extends WizardComponent
{
#[Session]
public array $allStepState = [];

public function steps(): array
{
return [
FirstStepComponent::class,
SecondStepComponent::class,
];
}

public function initialState(): ?array
{
return [
'first-step' => [
'order' => 123
]
];
}
}

0 comments on commit 90053e3

Please sign in to comment.