Skip to content

Commit

Permalink
add tests for Tokens component
Browse files Browse the repository at this point in the history
  • Loading branch information
manelgavalda committed Nov 10, 2023
1 parent 6a24397 commit b5902d4
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 8 deletions.
56 changes: 48 additions & 8 deletions tests/Feature/Livewire/TokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
use App\Models\User;
use Livewire\Livewire;
use App\Livewire\Tokens;
use App\Services\SupabaseService;

beforeEach(function () {
config([
'wise.profile_id' => 'fake_profile_id',
'supabase.url' => 'https://fake-url.supabase.co'
'supabase.url' => 'https://fake-url.supabase.co',
]);

fakeRequest('https://api.transferwise.com/v1/profiles/fake_profile_id/activities', 'latest_transactions');
Expand All @@ -17,16 +18,55 @@
});

it('exists_on_the_page', function() {
$user = User::factory()->create();
$this->actingAs(
User::factory()->create()
);

$this->actingAs($user);
$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');

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

it('tokens_are_correctly_set', function () {
it('tokens_are_refreshed_when_the_event_is_called', function () {
config(['tokens.api_url' => 'https://fake-tokens-url.com']);

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

Livewire::test(Tokens::class)
->assertViewHas('tokens')
->assertDispatched('tokens-loaded');
->assertViewHas('tokens', function ($tokens) {
expect(count($tokens))->toBe(30);

return true;
})
->assertViewHas('balances', function ($balances) {
expect(count($balances['prices']))->toBe(31);
expect(count($balances['totals']))->toBe(31);
expect(count($balances['ethereum']))->toBe(31);
expect(count($balances['prices_eur']))->toBe(31);
expect(count($balances['totals_eur']))->toBe(31);

return true;
})
->dispatch('tokens-loaded')
->assertViewHas('tokens', function ($tokens) {
expect(count($tokens))->toBe(31);

return true;
})
->assertViewHas('balances', function ($balances) {
expect(count($balances['prices']))->toBe(32);
expect(count($balances['totals']))->toBe(32);
expect(count($balances['ethereum']))->toBe(32);
expect(count($balances['prices_eur']))->toBe(32);
expect(count($balances['totals_eur']))->toBe(32);

return true;
});
});
101 changes: 101 additions & 0 deletions tests/responses/new_tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"balances":[
{
"pool":"Token 6",
"price":2074.88,
"price_eur":1942.25,
"balance":0.004078315527207583
},
{
"pool":"Token 9",
"price":1.13,
"price_eur":1.062,
"balance":7000
},
{
"pool":"Token 2",
"price":2074.88,
"price_eur":1942.25,
"balance":0.002382175250036868
},
{
"pool":"Token 1",
"price":1.65,
"price_eur":1.55,
"balance":103.3593082948141
},
{
"pool":"Token 11",
"price":2.24,
"price_eur":2.1,
"balance":245.37090418591836
},
{
"pool":"Token 4",
"price":1994980.3255706767,
"price_eur":1867455.3340406618,
"balance":0
},
{
"pool":"Token 5",
"price":2074.88,
"price_eur":1942.25,
"balance":0.05213835262402924
},
{
"pool":"Token 14",
"price":0.999868,
"price_eur":0.935953,
"balance":129.513934
},
{
"pool":"Token 13",
"price":2074.88,
"price_eur":1942.25,
"balance":10.72073036
},
{
"pool":"Token 7",
"price":6.8,
"price_eur":6.37,
"balance":34.36476725975825,
"parent":"Token 15"
},
{
"pool":"Token 8",
"price":0.571653,
"price_eur":0.535111,
"balance":374.85899681599767,
"parent":"Token 15"
},
{
"pool":"Token 3",
"price":3.7,
"price_eur":3.47,
"balance":7.437910160047613,
"parent":"Token 15"
},
{
"pool":"Token 15",
"price":2074.88,
"price_eur":1942.25,
"balance":17.351891141359467
},
{
"pool":"Token 10",
"price":0.556324,
"price_eur":0.520762,
"balance":460.0607141184403
},
{
"pool":"Token 12",
"price":3.7,
"price_eur":3.47,
"balance":127.80157438718044
}
],
"ethereumPrice":{
"usd":2074.88,
"eur":1942.25
}
}

0 comments on commit b5902d4

Please sign in to comment.