From 1aa44b24b7c90c98e7be7bbd0668dbdd958bd3f1 Mon Sep 17 00:00:00 2001 From: manel Date: Fri, 10 Nov 2023 23:24:30 +0100 Subject: [PATCH] Fix tests --- app/Livewire/Tokens.php | 2 ++ tests/Feature/Livewire/TokensTest.php | 37 +++++++++++++++++---------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app/Livewire/Tokens.php b/app/Livewire/Tokens.php index cfb001e..0b686f5 100644 --- a/app/Livewire/Tokens.php +++ b/app/Livewire/Tokens.php @@ -29,6 +29,8 @@ public function reloadTokens() $tokens = $result->balances; + $this->tokens->shift(); + $this->tokens->prepend( collect($tokens)->sortBy(fn ($token) => $token->price * $token->balance) ); diff --git a/tests/Feature/Livewire/TokensTest.php b/tests/Feature/Livewire/TokensTest.php index 5e43fe2..6533248 100644 --- a/tests/Feature/Livewire/TokensTest.php +++ b/tests/Feature/Livewire/TokensTest.php @@ -4,6 +4,7 @@ use Livewire\Livewire; use App\Livewire\Tokens; use App\Services\SupabaseService; +use Illuminate\Support\Facades\Http; beforeEach(function () { config([ @@ -39,20 +40,28 @@ fakeRequest('https://fake-tokens-url.com', 'new_tokens'); + $result = Http::get(config('tokens.api_url'))->object(); + + $newTokens = $result->balances; + + $supabaseService = new SupabaseService('fake-api-key', 'https://fake-url.supabase.co'); + + $oldTokens = $supabaseService->getTokens(); + $oldBalances = $supabaseService->getHistoricalBalances(); + Livewire::test(Tokens::class) - ->assertViewHasAll(tokenAndBalances(30, 31)) ->dispatch('tokens-loaded') - ->assertViewHasAll(tokenAndBalances(31, 32)); + ->assertSet('tokens', fn ($tokens) => + count($tokens) == count($oldTokens) + && $newTokens[0] == $tokens->first()->get(0) + && $oldTokens->get(0) != $tokens->get(0) + && $oldTokens->get(1) == $tokens->get(1) + )->assertSet('balances', fn ($balances) => + end($oldBalances['prices']) == $balances['prices'][count($balances['prices']) - 2] + && end($balances['prices']) == $result->ethereumPrice->usd + && end($balances['prices_eur']) == $result->ethereumPrice->eur + && end($balances['totals']) == collect($newTokens)->sum(fn ($token) => $token->price * $token->balance) + && end($balances['totals_eur']) == collect($newTokens)->sum(fn ($token) => $token->price_eur * $token->balance) + && end($balances['ethereum']) == collect($newTokens)->sum(fn ($token) => $token->price * $token->balance / $result->ethereumPrice->usd) + ); }); - -function tokenAndBalances($tokensCount, $balancesCount) -{ - return [ - 'tokens' => fn ($tokens) => count($tokens) == $tokensCount, - 'balances' => fn ($balances) => count($balances['prices']) == $balancesCount - && count($balances['totals']) == $balancesCount - && count($balances['ethereum']) == $balancesCount - && count($balances['prices_eur']) == $balancesCount - && count($balances['totals_eur']) == $balancesCount - ]; -}