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

Unable to mock for test #102

Open
ergunp opened this issue Jan 15, 2020 · 4 comments
Open

Unable to mock for test #102

ergunp opened this issue Jan 15, 2020 · 4 comments

Comments

@ergunp
Copy link

ergunp commented Jan 15, 2020

Hi,

I am trying to mock adalFetch but with no luck. I keep getting,
{ message: 'User login is required', msg: 'login required' }
here is what I have so far

  jest.mock('./../config/adal-config', () => ({
            authContext: () => jest.fn(),
            getToken: () => jest.fn(),
            adalApiFetch: () => Promise.resolve({ json: () => mockedReturnedData }), 
            adalFetch: () => jest.fn() 
          }));

Any help would be greatly appreciated.

@J-theGit
Copy link

J-theGit commented Jan 21, 2020

Hi @ergunp
You're likely missing one of the functions that you need to mock, however that's just a guess
Here's the mock I've generated to be able to test. react-adal.js file in/src/__mocks__dir.

I've set the mockup Azure AD response in mockupPayload

import jwt from 'jsonwebtoken';

const mockupPayload = {
…
};

const generateToken = () => jwt.sign(mockupPayload, 'secret');
const withAdalLogin = () => null;
const adalFetch = () => null;
const runWithAdal = jest.fn((auth, callback) => callback());

class AuthenticationContext {
    getCachedToken = () => generateToken();
    getCachedUser = () => ({
        userName: ‘[email protected]',
        profile: mockupPayload,
    });
}

export { AuthenticationContext, adalFetch, withAdalLogin, runWithAdal };

@ergunp
Copy link
Author

ergunp commented Jan 21, 2020

@J-theGit Thanks I'll give this a shot

@ergunp
Copy link
Author

ergunp commented Jan 23, 2020

Actually found a better way

await act(async () => {
 jest.spyOn(adalConf, 'adalApiFetch').mockImplementation(() => Promise.resolve({ json: () => existingChange }));
 });

@krytical
Copy link

I'm currently stuck on the same error message using testing-library. How did you resolve this issue?

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