Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Jest DOM Manipulation

'use strict';

jest.mock('../fetchCurrentUser');

test('displays a user after a click', () => {
  // Set up our document body
  document.body.innerHTML =
    '<div>' +
    '  <span id="username" />' +
    '  <button id="button" />' +
    '</div>';

  // This module has a side-effect
  require('../displayUser');

  const $ = require('jquery');
  const fetchCurrentUser = require('../fetchCurrentUser');

  // Tell the fetchCurrentUser mock function to automatically invoke
  // its callback with some data
  fetchCurrentUser.mockImplementation(cb => {
    cb({
      fullName: 'Johnny Cash',
      loggedIn: true,
    });
  });

  // Use jquery to emulate a click on our button
  $('#button').click();

  // Assert that the fetchCurrentUser function was called, and that the
  // #username span's inner text was updated as we'd expect it to.
  expect(fetchCurrentUser).toBeCalled();
  expect($('#username').text()).toEqual('Johnny Cash - Logged In');
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: call function javascript from asp net 
Javascript :: gradle error react native 
Javascript :: delete js 
Javascript :: ReactComponent 
Javascript :: javascript get last 2 item in array 
Javascript :: convert javascript to ruby 
Javascript :: react electron desktop app 
Javascript :: react hook form 
Javascript :: js ?. 
Javascript :: how to dynamically populate pdf with pdfmake node 
Javascript :: apollo client mutation without component 
Javascript :: asynchronous function using function constructor 
Javascript :: link external file by url using javascript 
Javascript :: inline null check javascript 
Javascript :: object literals and array literals in javascript 
Javascript :: change app name in react native android 
Javascript :: qr code generator with js 
Javascript :: js setinterval run immediately 
Javascript :: routes in angular 
Javascript :: ejs layout 
Javascript :: async await nodejs 
Javascript :: string.regex match 
Javascript :: express 
Javascript :: inline style to change background color react 
Javascript :: How to add multiple classes to a ReactJS Component 
Javascript :: react native position 
Javascript :: browserrouter invalid hook call 
Javascript :: plus in javascript 
Javascript :: document.getelementsbyname 
Javascript :: react hook will mount 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =