diff --git a/app/Http/Livewire/BlockTable.php b/app/Http/Livewire/BlockTable.php index dde693b8f8..1b60e78257 100644 --- a/app/Http/Livewire/BlockTable.php +++ b/app/Http/Livewire/BlockTable.php @@ -15,6 +15,9 @@ final class BlockTable extends Component { use HasPagination; + /** @phpstan-ignore-next-line */ + protected $listeners = ['currencyChanged' => '$refresh']; + public function render(): View { return view('livewire.block-table', [ diff --git a/app/Http/Livewire/BlockTransactionsTable.php b/app/Http/Livewire/BlockTransactionsTable.php index 8c80db1121..588a5f1c15 100644 --- a/app/Http/Livewire/BlockTransactionsTable.php +++ b/app/Http/Livewire/BlockTransactionsTable.php @@ -16,6 +16,9 @@ final class BlockTransactionsTable extends Component public string $blockId; + /** @phpstan-ignore-next-line */ + protected $listeners = ['currencyChanged' => '$refresh']; + public function mount(string $blockId): void { $this->blockId = $blockId; diff --git a/app/Http/Livewire/LatestRecords.php b/app/Http/Livewire/LatestRecords.php index d6651fea8b..2df364dd26 100644 --- a/app/Http/Livewire/LatestRecords.php +++ b/app/Http/Livewire/LatestRecords.php @@ -16,6 +16,9 @@ final class LatestRecords extends Component use ManagesLatestBlocks; use ManagesLatestTransactions; + /** @phpstan-ignore-next-line */ + protected $listeners = ['currencyChanged' => 'currencyChanged']; + public array $state = [ 'selected' => 'transactions', 'type' => 'all', @@ -34,6 +37,15 @@ public function render(): View return $this->renderTransactions(); } + public function currencyChanged(): void + { + if ($this->state['selected'] === 'blocks') { + $this->pollBlocks(); + } else { + $this->pollTransactions(); + } + } + private function renderBlocks(): View { if (is_null($this->blocks)) { diff --git a/app/Http/Livewire/TransactionTable.php b/app/Http/Livewire/TransactionTable.php index a015f117c4..57b38fe90a 100644 --- a/app/Http/Livewire/TransactionTable.php +++ b/app/Http/Livewire/TransactionTable.php @@ -15,6 +15,9 @@ final class TransactionTable extends Component { use HasPagination; + /** @phpstan-ignore-next-line */ + protected $listeners = ['currencyChanged' => '$refresh']; + public array $state = [ 'type' => 'all', ]; diff --git a/app/Http/Livewire/WalletBalance.php b/app/Http/Livewire/WalletBalance.php index 358f8947a0..e5c1cf5b8e 100644 --- a/app/Http/Livewire/WalletBalance.php +++ b/app/Http/Livewire/WalletBalance.php @@ -12,6 +12,11 @@ final class WalletBalance extends Component { + /** @phpstan-ignore-next-line */ + protected $listeners = [ + 'currencyChanged' => '$refresh', + ]; + public string $walletAddress; public function mount(Wallet $wallet): void diff --git a/app/Http/Livewire/WalletTransactionTable.php b/app/Http/Livewire/WalletTransactionTable.php index 8f607a2b0e..36cefa5de0 100644 --- a/app/Http/Livewire/WalletTransactionTable.php +++ b/app/Http/Livewire/WalletTransactionTable.php @@ -28,6 +28,7 @@ final class WalletTransactionTable extends Component /** @phpstan-ignore-next-line */ protected $listeners = [ 'filterTransactionsByDirection', + 'currencyChanged' => '$refresh', ]; public function mount(string $address, bool $isCold, ?string $publicKey): void diff --git a/app/Http/Livewire/WalletVoterTable.php b/app/Http/Livewire/WalletVoterTable.php index 39f47a241e..9dc2b56eb6 100644 --- a/app/Http/Livewire/WalletVoterTable.php +++ b/app/Http/Livewire/WalletVoterTable.php @@ -15,6 +15,9 @@ final class WalletVoterTable extends Component { use HasPagination; + /** @phpstan-ignore-next-line */ + protected $listeners = ['currencyChanged' => '$refresh']; + public string $publicKey; public string $username; diff --git a/resources/views/components/tables/desktop/blocks.blade.php b/resources/views/components/tables/desktop/blocks.blade.php index 3061c3f648..f60fdef692 100644 --- a/resources/views/components/tables/desktop/blocks.blade.php +++ b/resources/views/components/tables/desktop/blocks.blade.php @@ -14,7 +14,7 @@ @foreach($blocks as $block) - + diff --git a/resources/views/components/tables/desktop/transactions.blade.php b/resources/views/components/tables/desktop/transactions.blade.php index cc7b8f8b03..d96098f02d 100644 --- a/resources/views/components/tables/desktop/transactions.blade.php +++ b/resources/views/components/tables/desktop/transactions.blade.php @@ -14,7 +14,7 @@ - + diff --git a/resources/views/components/tables/mobile/blocks.blade.php b/resources/views/components/tables/mobile/blocks.blade.php index 5a6de8b813..675493435e 100644 --- a/resources/views/components/tables/mobile/blocks.blade.php +++ b/resources/views/components/tables/mobile/blocks.blade.php @@ -1,6 +1,6 @@
@foreach ($blocks as $block) -
+
id(), Settings::currency()) }}"> diff --git a/tests/Feature/Http/Livewire/BlockTableTest.php b/tests/Feature/Http/Livewire/BlockTableTest.php index 49d5f8bf26..f80c824144 100644 --- a/tests/Feature/Http/Livewire/BlockTableTest.php +++ b/tests/Feature/Http/Livewire/BlockTableTest.php @@ -6,8 +6,12 @@ use App\Http\Livewire\BlockTable; use App\Models\Block; use App\Models\Scopes\OrderByHeightScope; +use App\Services\Cache\CryptoCompareCache; use App\Services\NumberFormatter; +use App\Services\Settings; use App\ViewModels\ViewModelFactory; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Session; use Livewire\Livewire; use function Tests\configureExplorerDatabase; @@ -28,3 +32,33 @@ $component->assertSee(NumberFormatter::currency($block->fee(), Network::currency())); } }); + +it('should update the records fiat tooltip when currency changed', function () { + Config::set('explorer.networks.development.canBeExchanged', true); + + (new CryptoCompareCache())->setPrices('USD', collect([ + '2020-10-19' => 24210, + ])); + + (new CryptoCompareCache())->setPrices('BTC', collect([ + '2020-10-19' => 0.1234567, + ])); + + Block::factory()->create([ + 'total_amount' => 499 * 1e8, + ]); + + $component = Livewire::test(BlockTable::class); + + $component->assertSeeHtml('data-tippy-content="12,080,790 USD"'); + $component->assertDontSeeHtml('data-tippy-content="61.6048933 BTC"'); + + $settings = Settings::all(); + $settings['currency'] = 'BTC'; + Session::put('settings', json_encode($settings)); + + $component->emit('currencyChanged', 'BTC'); + + $component->assertDontSeeHtml('data-tippy-content="12,080,790 USD"'); + $component->assertSeeHtml('data-tippy-content="61.6048933 BTC"'); +}); diff --git a/tests/Feature/Http/Livewire/BlockTransactionsTableTest.php b/tests/Feature/Http/Livewire/BlockTransactionsTableTest.php index 8f3948c64e..5fe725eca4 100644 --- a/tests/Feature/Http/Livewire/BlockTransactionsTableTest.php +++ b/tests/Feature/Http/Livewire/BlockTransactionsTableTest.php @@ -6,9 +6,12 @@ use App\Http\Livewire\BlockTransactionsTable; use App\Models\Block; use App\Models\Transaction; +use App\Services\Cache\CryptoCompareCache; use App\Services\NumberFormatter; - +use App\Services\Settings; use App\ViewModels\ViewModelFactory; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Session; use Livewire\Livewire; it('should list the first transactions for the giving block id', function () { @@ -26,3 +29,37 @@ $component->assertSee(NumberFormatter::currency($transaction->fee(), Network::currency())); } }); + +it('should update the records fiat tooltip when currency changed', function () { + Config::set('explorer.networks.development.canBeExchanged', true); + + (new CryptoCompareCache())->setPrices('USD', collect([ + '2020-10-19' => 24210, + ])); + + (new CryptoCompareCache())->setPrices('BTC', collect([ + '2020-10-19' => 0.1234567, + ])); + + $block = Block::factory()->create(); + + Transaction::factory()->create([ + 'block_id' => $block->id, + 'timestamp' => 112982056, + 'amount' => 499 * 1e8, + ]); + + $component = Livewire::test(BlockTransactionsTable::class, ['blockId' => $block->id]); + + $component->assertSeeHtml('data-tippy-content="12,080,790 USD"'); + $component->assertDontSeeHtml('data-tippy-content="61.6048933 BTC"'); + + $settings = Settings::all(); + $settings['currency'] = 'BTC'; + Session::put('settings', json_encode($settings)); + + $component->emit('currencyChanged', 'BTC'); + + $component->assertDontSeeHtml('data-tippy-content="12,080,790 USD"'); + $component->assertSeeHtml('data-tippy-content="61.6048933 BTC"'); +}); diff --git a/tests/Feature/Http/Livewire/LatestRecordsTest.php b/tests/Feature/Http/Livewire/LatestRecordsTest.php index e73271f27d..36c051a076 100644 --- a/tests/Feature/Http/Livewire/LatestRecordsTest.php +++ b/tests/Feature/Http/Livewire/LatestRecordsTest.php @@ -130,3 +130,22 @@ $component->assertSee($transaction->amount()); } }); + +it('should poll transactions when currency changed', function () { + $transaction = Transaction::factory()->create(); + + Livewire::test(LatestRecords::class) + ->assertDontSee($transaction->id) + ->emit('currencyChanged') + ->assertSee($transaction->id); +}); + +it('should poll blocks when currency changed', function () { + $block = Block::factory()->create(); + + Livewire::test(LatestRecords::class) + ->set('state.selected', 'blocks') + ->assertDontSee($block->id) + ->emit('currencyChanged') + ->assertSee($block->id); +}); diff --git a/tests/Feature/Http/Livewire/TransactionTableTest.php b/tests/Feature/Http/Livewire/TransactionTableTest.php index 5037fabcd3..f8b857000b 100644 --- a/tests/Feature/Http/Livewire/TransactionTableTest.php +++ b/tests/Feature/Http/Livewire/TransactionTableTest.php @@ -8,9 +8,12 @@ use App\Models\Scopes\OrderByTimestampScope; use App\Models\Transaction; use App\Models\Wallet; +use App\Services\Cache\CryptoCompareCache; use App\Services\NumberFormatter; - +use App\Services\Settings; use App\ViewModels\ViewModelFactory; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Session; use Livewire\Livewire; use Ramsey\Uuid\Uuid; use function Tests\configureExplorerDatabase; @@ -109,3 +112,34 @@ $component->assertSee(NumberFormatter::currency($transaction->amount(), Network::currency())); } }); + +it('should update the records fiat tooltip when currency changed', function () { + Config::set('explorer.networks.development.canBeExchanged', true); + + (new CryptoCompareCache())->setPrices('USD', collect([ + '2020-10-19' => 24210, + ])); + + (new CryptoCompareCache())->setPrices('BTC', collect([ + '2020-10-19' => 0.1234567, + ])); + + Transaction::factory()->create([ + 'timestamp' => 112982056, + 'amount' => 499 * 1e8, + ]); + + $component = Livewire::test(TransactionTable::class); + + $component->assertSeeHtml('data-tippy-content="12,080,790 USD"'); + $component->assertDontSeeHtml('data-tippy-content="61.6048933 BTC"'); + + $settings = Settings::all(); + $settings['currency'] = 'BTC'; + Session::put('settings', json_encode($settings)); + + $component->emit('currencyChanged', 'BTC'); + + $component->assertDontSeeHtml('data-tippy-content="12,080,790 USD"'); + $component->assertSeeHtml('data-tippy-content="61.6048933 BTC"'); +}); diff --git a/tests/Feature/Http/Livewire/WalletBalanceTest.php b/tests/Feature/Http/Livewire/WalletBalanceTest.php index 8dc9a34d39..1d514ce46b 100644 --- a/tests/Feature/Http/Livewire/WalletBalanceTest.php +++ b/tests/Feature/Http/Livewire/WalletBalanceTest.php @@ -5,7 +5,9 @@ use App\Http\Livewire\WalletBalance; use App\Models\Wallet; use App\Services\Cache\CryptoCompareCache; +use App\Services\Settings; use Carbon\Carbon; +use Illuminate\Support\Facades\Session; use Livewire\Livewire; it('should show the balance of the wallet', function () { @@ -17,3 +19,23 @@ Livewire::test(WalletBalance::class, ['wallet' => $wallet])->assertSee('0.01 USD'); }); + +it('updates the balance when currency changes', function () { + (new CryptoCompareCache())->setPrices('USD', collect([ + Carbon::now()->format('Y-m-d') => 10, + ])); + + (new CryptoCompareCache())->setPrices('BTC', collect([ + Carbon::now()->format('Y-m-d') => 0.1234567, + ])); + + $wallet = Wallet::factory()->create(['balance' => 125456]); + + $component = Livewire::test(WalletBalance::class, ['wallet' => $wallet])->assertSee('0.01 USD'); + + $settings = Settings::all(); + $settings['currency'] = 'BTC'; + Session::put('settings', json_encode($settings)); + + $component->emit('currencyChanged', 'BTC')->assertSee('0.00015488 BTC'); +});