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

CU-86drumcht - Implement NeonEventListener tests #33

Merged
merged 1 commit into from
Apr 8, 2024
Merged

Conversation

luc10921
Copy link
Contributor

@luc10921 luc10921 commented Apr 1, 2024

No description provided.

@luc10921 luc10921 requested a review from melanke April 1, 2024 18:09
@luc10921 luc10921 self-assigned this Apr 1, 2024
@melanke
Copy link
Contributor

melanke commented Apr 1, 2024

Copy link

github-actions bot commented Apr 1, 2024

Coverage Report

Commit: e35bcc1
Base: main@ea06b86

Type This PR
Total Statements Coverage  93.58%
Total Branches Coverage  81.38%
Total Functions Coverage  93.82%
Total Lines Coverage  94.93%
Details (changed files)
FileStatementsBranchesFunctionsLines
Details (all files)
FileStatementsBranchesFunctionsLines
packages/neon-dappkit/src/NeonEventListener.ts 97.95% 71.42% 100% 97.91%
packages/neon-dappkit/src/NeonInvoker.ts 87.2% 85.29% 88.46% 90.43%
packages/neon-dappkit/src/NeonParser.ts 95.74% 94.92% 96.29% 96.7%
packages/neon-dappkit/src/NeonSigner.ts 95.65% 44.44% 92.3% 95.65%
packages/neon-dappkit/src/index.ts 100% 100% 100% 100%

eventListener.removeAllEventListenersOfEvent(gasScriptHash, eventName)
}

eventListener.addEventListener(gasScriptHash, eventName, callBack)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a better way to work with callbacks on mocha.

You could create a Promise that is resolved on the callback. So instead of having the "called" control variable, you would simply await for the promise inside the test and mocha would work perfectly. Something like this:

it('adds an eventListener', async () => {
    const eventName = 'Transfer'
    
    const eventPromise = new Promise((resolve) => {
      const callBack: Neo3EventListenerCallback = (notification: Neo3EventWithState) => {
        resolve(notification)

        eventListener.removeAllEventListenersOfEvent(gasScriptHash, eventName)
      }

      eventListener.addEventListener(gasScriptHash, eventName, callBack)
    })

    const sender = accountWithGas
    const receiver = new wallet.Account()

    const neoInvoker = await NeonInvoker.init({ rpcAddress, account: sender })

    const txId = await neoInvoker.invokeFunction(transferInvocation(sender, receiver, '100'))
    assert(txId, 'Transaction ID should be returned')

    const notification = await eventPromise
    
    assert(notification.contract === gasScriptHash, 'Notification should be sent by NeoToken')
    assert(notification.eventname === eventName, `Notification should be ${eventName}`)
    assert(notification.state !== undefined, 'Notification should return value')
    assert(TypeChecker.isStackTypeArray(notification.state), 'Notification value should be an array')
  })

@melanke melanke merged commit 4292188 into main Apr 8, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants