Skip to content

Jest mock firebase auth (Operation not supported) #26

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

Open
sniperadmin opened this issue Jul 6, 2020 · 0 comments
Open

Jest mock firebase auth (Operation not supported) #26

sniperadmin opened this issue Jul 6, 2020 · 0 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation help wanted Extra attention is needed

Comments

@sniperadmin
Copy link
Owner

sniperadmin commented Jul 6, 2020

Bug Story

This operation is not supported in the environment this application is running on. "location.protocol" must be http, https or chrome-extension and web storage must be enabled.

Details

here is the method I would like to test:

// @/components/logins/FbLogin.vue

        methods: {
            fbLogin() {
                var provider = new firebase.auth.FacebookAuthProvider();

                fbAuth.auth().signInWithPopup(provider).then(result => {
                // This gives you a Facebook Access Token. You can use it to access the Facebook API.
                var token = result.credential.accessToken;
                // The signed-in user info.
                var user = result.user;
                // ...
                console.log(user)
                }).catch(error => {
                    let errorCode = error.code,
                        errorMessage = error.message,
                        email = error.email,
                        credential = error.credential;
                    console.log(errorMessage)
                });
            }
        }

Expected test behavior

This is my trial to mock firebase for that purpose

import firebase from 'firebase'

let wrapper

const signInWithPopup = jest.fn(() => {
  return Promise.resolve('results for signInWithPopup')
})

const initializeApp = jest.spyOn(firebase, 'initializeApp')
  .mockImplementation(() => {
    return {
      auth: () => {
        return {
          signInWithPopup
        }
      }
    }
  })

initializeApp()

// this is the test
  it('tests methods', () => {
    const mockfbLogin = jest.spyOn(wrapper.vm, 'fbLogin')

    wrapper.vm.$on('click', mockfbLogin)
    wrapper.vm.$emit('click')

    expect(mockfbLogin).toHaveBeenCalled()
    expect(signInWithPopup).toHaveBeenCalled()
  })

This mock resulted in the bug described above!
As proposed, I would like to cover 100% of the code

@sniperadmin sniperadmin changed the title Cannot mock firebase auth login Jest mock firebase auth () Jul 6, 2020
@sniperadmin sniperadmin changed the title Jest mock firebase auth () Jest mock firebase auth (Operation not supported) Jul 6, 2020
@sniperadmin sniperadmin added bug Something isn't working documentation Improvements or additions to documentation help wanted Extra attention is needed labels Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant