From 2c6e7d859938badb5e96be8f92acb18111dbacdf Mon Sep 17 00:00:00 2001 From: Albert Scherman Date: Sat, 25 Mar 2023 13:31:59 +0100 Subject: [PATCH 1/4] Added hasPreviousStep and hasNextStep to StepComponent --- src/Components/StepComponent.php | 10 ++++++++++ tests/WizardTest.php | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Components/StepComponent.php b/src/Components/StepComponent.php index de93a03..8f0df4a 100644 --- a/src/Components/StepComponent.php +++ b/src/Components/StepComponent.php @@ -32,6 +32,16 @@ public function showStep(string $stepName) $this->emitUp('showStep', $stepName, $this->state()->currentStep()); } + public function hasPreviousStep() + { + return $this->allStepNames[0] !== Livewire::getAlias(static::class); + } + + public function hasNextStep() + { + return end($this->allStepNames) !== Livewire::getAlias(static::class); + } + public function stepInfo(): array { return []; diff --git a/tests/WizardTest.php b/tests/WizardTest.php index 11e73be..7ab96ed 100644 --- a/tests/WizardTest.php +++ b/tests/WizardTest.php @@ -142,3 +142,17 @@ assertMatchesHtmlSnapshot($navigationHtml); }); + +it('does have the correct has step states', function () { + $this->secondStep = Livewire::test(SecondStepComponent::class); + $this->thirdStep = Livewire::test(ThirdStepComponent::class); + + $this->assertTrue($this->firstStep->call('hasNextStep')); + $this->assertFalse($this->firstStep->call('hasPreviousStep')); + + $this->assertTrue($this->secondStep->call('hasNextStep')); + $this->assertTrue($this->secondStep->call('hasPreviousStep')); + + $this->assertFalse($this->thirdStep->call('hasNextStep')); + $this->assertTrue($this->thirdStep->call('hasPreviousStep')); +}); From 39ba7976b6804c37598264d6134b26c12a82a19d Mon Sep 17 00:00:00 2001 From: Albert Scherman Date: Sat, 25 Mar 2023 13:39:02 +0100 Subject: [PATCH 2/4] Updated documentation --- docs/usage/navigating-steps.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/usage/navigating-steps.md b/docs/usage/navigating-steps.md index ab7e767..eacdd86 100644 --- a/docs/usage/navigating-steps.md +++ b/docs/usage/navigating-steps.md @@ -60,6 +60,13 @@ You can also call it in your view. ``` +You can also check if a next or previous step exists directly from the step component. + +```blade +$this->hasNextStep(); +$this->hasPreviousStep(); +``` + ## Start at a specific step If you want the wizard to display a specific step when it is rendered first, you can pass the step name to the `show-step` property. From ceead823baaf71cf4ea7a36ea1fb4a17f42e1d64 Mon Sep 17 00:00:00 2001 From: Albert Scherman Date: Mon, 3 Apr 2023 13:50:45 +0200 Subject: [PATCH 3/4] Added tests --- tests/WizardTest.php | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/tests/WizardTest.php b/tests/WizardTest.php index 7ab96ed..731acb1 100644 --- a/tests/WizardTest.php +++ b/tests/WizardTest.php @@ -143,16 +143,30 @@ assertMatchesHtmlSnapshot($navigationHtml); }); -it('does have the correct has step states', function () { - $this->secondStep = Livewire::test(SecondStepComponent::class); - $this->thirdStep = Livewire::test(ThirdStepComponent::class); +it('has the correct has step states', function () { + // Set up the step names array to match the expected steps + $stepNames = [ + Livewire::getAlias(FirstStepComponent::class), + Livewire::getAlias(SecondStepComponent::class), + Livewire::getAlias(ThirdStepComponent::class), + ]; - $this->assertTrue($this->firstStep->call('hasNextStep')); - $this->assertFalse($this->firstStep->call('hasPreviousStep')); + // Create instances of each step component and set the allStepNames property on them + $this->firstStep = new FirstStepComponent(); + $this->firstStep->allStepNames = $stepNames; - $this->assertTrue($this->secondStep->call('hasNextStep')); - $this->assertTrue($this->secondStep->call('hasPreviousStep')); + $this->secondStep = new SecondStepComponent(); + $this->secondStep->allStepNames = $stepNames; - $this->assertFalse($this->thirdStep->call('hasNextStep')); - $this->assertTrue($this->thirdStep->call('hasPreviousStep')); + $this->thirdStep = new ThirdStepComponent(); + $this->thirdStep->allStepNames = $stepNames; + + $this->assertFalse($this->firstStep->hasPreviousStep()); + $this->assertTrue($this->firstStep->hasNextStep()); + + $this->assertTrue($this->secondStep->hasPreviousStep()); + $this->assertTrue($this->secondStep->hasNextStep()); + + $this->assertTrue($this->thirdStep->hasPreviousStep()); + $this->assertFalse($this->thirdStep->hasNextStep()); }); From 9c5d5e17cd69e1b40d3dc8ad7e6f0336b1991eb7 Mon Sep 17 00:00:00 2001 From: Albert Scherman Date: Mon, 3 Apr 2023 14:57:42 +0200 Subject: [PATCH 4/4] Changed PHPUnit assertions with Pest assertions --- tests/WizardTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/WizardTest.php b/tests/WizardTest.php index 731acb1..a8309c1 100644 --- a/tests/WizardTest.php +++ b/tests/WizardTest.php @@ -161,12 +161,12 @@ $this->thirdStep = new ThirdStepComponent(); $this->thirdStep->allStepNames = $stepNames; - $this->assertFalse($this->firstStep->hasPreviousStep()); - $this->assertTrue($this->firstStep->hasNextStep()); + expect($this->firstStep->hasPreviousStep())->toBeFalse(); + expect($this->firstStep->hasNextStep())->toBeTrue(); - $this->assertTrue($this->secondStep->hasPreviousStep()); - $this->assertTrue($this->secondStep->hasNextStep()); + expect($this->secondStep->hasPreviousStep())->toBeTrue(); + expect($this->secondStep->hasNextStep())->toBeTrue(); - $this->assertTrue($this->thirdStep->hasPreviousStep()); - $this->assertFalse($this->thirdStep->hasNextStep()); + expect($this->thirdStep->hasPreviousStep())->toBeTrue(); + expect($this->thirdStep->hasNextStep())->toBeFalse(); });