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

Cannot test realtime database #125

Open
balazsorban44 opened this issue Dec 21, 2018 · 2 comments
Open

Cannot test realtime database #125

balazsorban44 opened this issue Dec 21, 2018 · 2 comments

Comments

@balazsorban44
Copy link

balazsorban44 commented Dec 21, 2018

Hi @soumak77
I have the following function in my source:

export const fetchData = async (ref, shouldSort) => {
  let result
  try {
    result = await ref.once("value")
    result = await result.val()
    if (shouldSort) {
      Object.keys(result).forEach(path => {
        result[path] = Object.values(result[path]).sort((a, b) => a.order - b.order)
      })
    }
    return result
  } catch (error) {
    sendNotification("error", error.message)
    return false
  }
}

In my test, I am trying to test the scenario when sholudSort is false, and the function should return the .val() of the database ref.

my firebase.mock.js:

import firebasemock from 'firebase-mock'

const mockdatabase = new firebasemock.MockFirebase()
const mockfirestore = new firebasemock.MockFirestore()

export default new firebasemock.MockFirebaseSdk(
  // RTDB
  path => path ? mockdatabase.child(path) : mockdatabase,
  () => null,
  // FIRESTORE
  () => mockfirestore,
  () => null,
  () => null
)

my test file:

import mocksdk from "./firebase.mock.js"

jest.mock("../../../lib/firebase", () => mocksdk)

describe("data fetched", () => {
  const fakeRef = firebase.database().ref("fake")
  const fakeData = {id: 1}

  beforeAll(() => {
    fakeRef.push(fakeData)
    fakeRef.flush()
  })

  it("don't sort", () => {
    const result = fetchData(fakeRef)
    expect(result).toBe(fakeRef.getData())
  })
})

In my console though, I get the following error:

image

If .getData can verify that the fake data has been pushed to the fake database, why does my function only return {}? Since it returns a Promise, I tried to test with async, then I get the following in the console:

image

@NullSoldier
Copy link

NullSoldier commented Jan 22, 2019

You need to call autoFlush. Your firebase requests don't digest automatically.

mockdatabase.autoFlush(1)
mockfirestore.autoFlush(1)

@RezaRahmati
Copy link

@NullSoldier I receive the below error
TypeError: mockfirebase.ref is not a function

Please see this #151

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

No branches or pull requests

3 participants