From 28e7c8bafc74034cd4ec3a4d49242ed8ed7ae446 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Mon, 15 Jan 2024 11:10:54 +0000 Subject: [PATCH] Update StateTest.php --- tests/src/Forms/StateTest.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/src/Forms/StateTest.php b/tests/src/Forms/StateTest.php index 6abc61d0c7..8274b3a2dd 100644 --- a/tests/src/Forms/StateTest.php +++ b/tests/src/Forms/StateTest.php @@ -301,19 +301,37 @@ test('custom logic can be executed after child component state is updated', func }); test('custom logic can be executed only once after nested state is updated', function () { + $calls = 0; + ComponentContainer::make($livewire = Livewire::make()) ->statePath('data') ->components([ (new Component()) ->statePath($statePath = Str::random()) - ->afterStateUpdated(fn (Component $component, $state) => $component->state([strrev($state[0])])), + ->afterStateUpdated(function () use (&$calls) { + $calls++; + }), ]) - ->fill([$statePath => [$state = Str::random()]]) + ->tap(fn (ComponentContainer $container) => $container->callAfterStateUpdated("data.{$statePath}.0")) + ->tap(fn (ComponentContainer $container) => $container->callAfterStateUpdated("data.{$statePath}.1")); + + expect($calls)->toEqual(1); +}); + +test('custom logic can be executed more than once after nested state is updated if the state changes', function () { + ComponentContainer::make($livewire = Livewire::make()) + ->statePath('data') + ->components([ + (new Component()) + ->statePath($statePath = Str::random()) + ->afterStateUpdated(fn (Component $component, $state) => $component->state([$state[0] + 1])), + ]) + ->fill([$statePath => [0]]) ->tap(fn (ComponentContainer $container) => $container->callAfterStateUpdated("data.{$statePath}.0")) ->tap(fn (ComponentContainer $container) => $container->callAfterStateUpdated("data.{$statePath}.1")); expect($livewire) - ->getData()->toBe([$statePath => [strrev($state)]]); + ->getData()->toBe([$statePath => [2]]); }); test('custom logic on parent component can be executed after child component state is updated', function () {