Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mock window jest js

let windowSpy;

beforeEach(() => {
  windowSpy = jest.spyOn(window, "window", "get");
});

afterEach(() => {
  windowSpy.mockRestore();
});

it('should return https://example.com', () => {
  windowSpy.mockImplementation(() => ({
    location: {
      origin: "https://example.com"
    }
  }));

  expect(window.location.origin).toEqual("https://example.com");
});

it('should be undefined.', () => {
  windowSpy.mockImplementation(() => undefined);

  expect(window).toBeUndefined();
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: simulate click jest 
Javascript :: split a message js 
Javascript :: move dom element to another parent 
Javascript :: Custom jquery validation messages 
Javascript :: size of call stack js 
Javascript :: reset form javascript/jquery 
Javascript :: a <route is only ever to be used as the child of <routes element" 
Javascript :: js promis with ajax 
Javascript :: how get one value of array of object in javascript 
Javascript :: image base64 to file javascript 
Javascript :: javascript format seconds into minutes and second 
Javascript :: call a function when page is loaded 
Javascript :: aos js 
Javascript :: export variables react 
Javascript :: javascript dataurl to blob 
Javascript :: local storage javascript 
Javascript :: multi-line javascript 
Javascript :: isset js 
Javascript :: committing parts of a file git 
Javascript :: delete all node modules 
Javascript :: useLocation in jest 
Javascript :: get element font size javascript 
Javascript :: javascript delete first character from string 
Javascript :: componentdidupdate 
Javascript :: passing data variable using ajax 
Javascript :: electron js development auto refresh 
Javascript :: mongoose schema 
Javascript :: why does my page reloads on form submission 
Javascript :: nextjs custom document 
Javascript :: set background opacity react native 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =