Skip to content

Commit

Permalink
Fixing tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
manelgavalda committed Nov 11, 2023
1 parent cfe5fd3 commit cba198a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
2 changes: 1 addition & 1 deletion app/Livewire/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function reloadTokens()
$this->tokens->shift();

$this->tokens->prepend(
$tokens->sortBy(fn ($token) => $token->price * $token->balance)->values()
$tokens->sortBy(fn ($token) => $token->price * $token->balance)
);

$this->balances['prices'][] = $result->ethereumPrice->usd;
Expand Down
60 changes: 28 additions & 32 deletions tests/Feature/Livewire/TokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

use App\Models\User;
use Livewire\Livewire;
use Carbon\CarbonPeriod;
use App\Livewire\Tokens;
use App\Services\SupabaseService;
use Illuminate\Support\Facades\Http;

beforeEach(function () {
config([
Expand All @@ -26,40 +25,37 @@
$this->get('/')->assertSeeLivewire(Tokens::class);
});

it('dispatches_an_event_when_tokens_are_loaded', function () {
$supabaseService = new SupabaseService('fake-api-key', 'https://fake-url.supabase.co');

Livewire::test(Tokens::class)
->assertDispatched('tokens-loaded')
->assertSet('tokens', $supabaseService->getTokens())
->assertSet('balances', $supabaseService->getHistoricalBalances());
});

it('refreshes_the_tokens_when_the_event_is_called', function () {
$dates = collect(
CarbonPeriod::create('Oct 09', 'Nov 08')
)->map->format('M d')->toArray();

config(['tokens.api_url' => 'https://fake-tokens-url.com']);

fakeRequest('https://fake-tokens-url.com', 'new_tokens');

$result = Http::get(config('tokens.api_url'))->object();

$newTokens = collect($result->balances)->sortBy(fn ($token) => $token->price * $token->balance)->values();

$supabaseService = new SupabaseService('fake-api-key', 'https://fake-url.supabase.co');

$oldTokens = $supabaseService->getTokens();
$oldBalances = $supabaseService->getHistoricalBalances();

Livewire::test(Tokens::class)
->dispatch('tokens-loaded')
->assertSet('tokens', fn ($tokens) =>
$tokens->get(0) == $newTokens &&
$tokens->get(1) == $oldTokens->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) &&
round(end($balances['totals_eur']), 9) == round(collect($newTokens)->sum(fn ($token) => $token->price_eur * $token->balance), 9) &&
end($balances['ethereum']) == collect($newTokens)->sum(fn ($token) => $token->price * $token->balance / $result->ethereumPrice->usd)
);
->assertSet('tokens', fn ($tokens) => $tokens->first()->first() == (object) [
'pool' => 'Token 4',
'price' => 2000000,
'price_eur' => 1800000,
'balance' => 0,
"parent" => null,
"created_at" => "2023-11-08T00:00:00.000000+00:00"
])->assertSet('balances', fn ($balances) =>
$balances['dates'] == $dates && end($balances['prices']) == 1884.82
)->assertDispatched('tokens-loaded')
->dispatch('tokens-loaded')
->assertSet('tokens', fn ($tokens) => $tokens->first()->first() == (object) [
'pool' => 'Token 4',
'price' => 1000000,
'price_eur' => 900000,
'balance' => 0
] && count($tokens) == 30)->assertSet('balances', fn ($balances) =>
end($balances['prices']) == 2074.88 &&
end($balances['prices_eur']) == 1942.25 &&
round(end($balances['totals']), 7) == 1150032.9456886 &&
round(end($balances['totals_eur']), 7) == 1076552.8135102 &&
round(end($balances['ethereum']), 7) == 554.2647988
);
});
6 changes: 3 additions & 3 deletions tests/responses/new_tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
},
{
"pool":"Token 4",
"price":1994980.3255706767,
"price_eur":1867455.3340406618,
"price":1000000,
"price_eur":900000,
"balance":0
},
{
"pool":"Token 5",
"price":2074.88,
"price_eur":1942.25,
"balance":0.05213835262402924
"balance":521.3835262402924
},
{
"pool":"Token 14",
Expand Down
34 changes: 17 additions & 17 deletions tests/responses/tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,119 +5,119 @@
"price_eur": 1.41,
"balance": 103.359,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 2",
"price": 1884.82,
"price_eur": 1763.24,
"balance": 17.3519,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 3",
"price": 3.39,
"price_eur": 3.17,
"balance": 7.10523,
"parent": "Token 2",
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 4",
"price": 2000960,
"price_eur": 1871910,
"price": 2000000,
"price_eur": 1800000,
"balance": 0,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 5",
"price": 1884.82,
"price_eur": 1763.24,
"balance": 0.00238218,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 6",
"price": 1884.82,
"price_eur": 1763.24,
"balance": 0.0521384,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 7",
"price": 6.25,
"price_eur": 5.85,
"balance": 33.0963,
"parent": "Token 2",
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 8",
"price": 0.539818,
"price_eur": 0.504998,
"balance": 460.061,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 9",
"price": 1.001,
"price_eur": 0.936309,
"balance": 129.514,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 10",
"price": 0.557084,
"price_eur": 0.52115,
"balance": 356.367,
"parent": "Token 2",
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 11",
"price": 1.95,
"price_eur": 1.83,
"balance": 245.371,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 12",
"price": 3.39,
"price_eur": 3.17,
"balance": 127.802,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 13",
"price": 1884.82,
"price_eur": 1763.24,
"balance": 10.7087,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 14",
"price": 1.078,
"price_eur": 1.009,
"balance": 7000,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 15",
"price": 1884.82,
"price_eur": 1763.24,
"balance": 0.00407832,
"parent": null,
"created_at": "2023-11-08T04:07:13.621314+00:00"
"created_at": "2023-11-08T00:00:00.000000+00:00"
},
{
"pool": "Token 15",
Expand Down

0 comments on commit cba198a

Please sign in to comment.