mark
mark

Reputation: 5070

how to mock navigator.userAgentData.getHighEntropyValues with jest

I would like to mock the resolved value of navigator.userAgentData.getHighEntropyValues in my test to be a sample object.

How can I do this?

I tried:

  beforeEach(() => {
    jest.mock(global.navigator.userAgentData);
  });
  it('uaFullVersion is defined', async () => {
     global.navigator.userAgentData.getHighEntropyValues.mockResolvedValueOnce({uaFullVersion: '1.2.3'});
     const hev = await myFunc();
     expect(hev.uaFullVersion).toBeDefined();
  });

myFunc:

async function myFunc() {
  const hev = await navigator.userAgentData.getHighEntropyValues(["uaFullVersion"]);
  return hev;
}

but I get the error TypeError: Cannot read properties of undefined (reading 'getHighEntropyValues')

Upvotes: 0

Views: 1106

Answers (1)

have you tried using jest.spyOn instead of the jest.mock at the beforeEach?

like:

jest.spyOn(global.navigator.userAgentData,  
    'getHighEntropyValues').mockResolvedValueOnce({})

?

Upvotes: 0

Related Questions