Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix LedgersPage tests #972

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/containers/Ledgers/test/LedgersPage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import prevLedgerMessage from './mock/prevLedger.json'
import ledgerMessage from './mock/ledger.json'
import validationMessage from './mock/validation.json'
import rippledResponses from './mock/rippled.json'
import { QuickHarness } from '../../test/utils'
import { QuickHarness, flushPromises } from '../../test/utils'
import { SelectedValidatorProvider } from '../useSelectedValidator'

function sleep(ms) {
Expand Down Expand Up @@ -164,6 +164,7 @@ describe('Ledgers Page container', () => {
expect(wrapper.find('.ledger').length).toBe(1)

server.send(validationMessage)
await flushPromises()
wrapper.update()
expect(wrapper.find('.validation').length).toBe(1)

Expand Down Expand Up @@ -254,6 +255,7 @@ describe('Ledgers Page container', () => {
expect(wrapper.find('.ledger').length).toBe(1)

server.send(validationMessage)
await flushPromises()
wrapper.update()
expect(wrapper.find('.validation').length).toBe(1)

Expand Down
32 changes: 19 additions & 13 deletions src/containers/shared/components/Streams.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ class Streams extends Component {
}, 100)
}

componentDidMount() {
this.connect()
async componentDidMount() {
await this.connect()
this.updateNegativeUNL()
if (process.env.VITE_ENVIRONMENT !== 'custom') {
this.updateMetricsFromServer()
Expand Down Expand Up @@ -179,8 +179,8 @@ class Streams extends Component {
}

// handle ledger messages
handleLedger(data) {
const ledger = this.addLedger(data)
async handleLedger(data) {
const ledger = await this.addLedger(data)
const {
ledger_hash: ledgerHash,
ledger_index: ledgerIndex,
Expand All @@ -200,15 +200,15 @@ class Streams extends Component {
}

// handle validation messages
handleValidation(data) {
async handleValidation(data) {
const { ledger_hash: ledgerHash, validation_public_key: pubkey } = data
const ledgerIndex = Number(data.ledger_index)

if (!this.isValidatedChain(ledgerIndex)) {
return undefined
}

this.addLedger(data)
await this.addLedger(data)

this.setState((prevState) => {
if (!prevState.allValidators[pubkey]) {
Expand Down Expand Up @@ -330,6 +330,12 @@ class Streams extends Component {
})
}

async setStateAsync(state) {
justinr1234 marked this conversation as resolved.
Show resolved Hide resolved
return new Promise((resolve) => {
this.setState(state, resolve)
})
}

getTruncatedLedgers(max) {
const { ledgers, maxLedger } = this.state
const newMax = Math.max(max, maxLedger)
Expand Down Expand Up @@ -395,10 +401,10 @@ class Streams extends Component {
}

// add the ledger to the cache
addLedger(data) {
async addLedger(data) {
const { ledger_index: ledgerIndex } = data

this.setState((prevState) => {
await this.setStateAsync((prevState) => {
if (!prevState.allLedgers[ledgerIndex]) {
const allLedgers = Object.assign(prevState.allLedgers, {
[ledgerIndex]: {
Expand Down Expand Up @@ -493,14 +499,14 @@ class Streams extends Component {
})
}

connect() {
async connect() {
const rippledSocket = this.context

this.onLedgerWrapper = (streamResult) => {
this.onLedgerWrapper = async (streamResult) => {
if (streamResult.type !== 'ledgerClosed') {
return
}
const { ledger, baseFee } = this.handleLedger(streamResult)
const { ledger, baseFee } = await this.handleLedger(streamResult)
this.onLedger(ledger)
fetchLedger(ledger, rippledSocket)
.then((ledgerSummary) => {
Expand Down Expand Up @@ -535,8 +541,8 @@ class Streams extends Component {

rippledSocket.on('ledger', this.onLedgerWrapper)

this.onValidationWrapper = (streamResult) => {
const data = this.handleValidation(streamResult)
this.onValidationWrapper = async (streamResult) => {
const data = await this.handleValidation(streamResult)
if (data) {
this.onValidation(data)
}
Expand Down
Loading