Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manelgavalda committed Nov 10, 2023
1 parent 0d05951 commit 1aa44b2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/Livewire/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down
37 changes: 23 additions & 14 deletions tests/Feature/Livewire/TokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Livewire\Livewire;
use App\Livewire\Tokens;
use App\Services\SupabaseService;
use Illuminate\Support\Facades\Http;

beforeEach(function () {
config([
Expand Down Expand Up @@ -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
];
}

0 comments on commit 1aa44b2

Please sign in to comment.