1. 'use strict';
    2. jest.mock('../fetchCurrentUser');
    3. test('displays a user after a click', () => {
    4. // Set up our document body
    5. document.body.innerHTML =
    6. '<div>' +
    7. ' <span id="username" />' +
    8. ' <button id="button" />' +
    9. '</div>';
    10. // This module has a side-effect
    11. require('../displayUser');
    12. const $ = require('jquery');
    13. const fetchCurrentUser = require('../fetchCurrentUser');
    14. // Tell the fetchCurrentUser mock function to automatically invoke
    15. // its callback with some data
    16. fetchCurrentUser.mockImplementation(cb => {
    17. cb({
    18. fullName: 'Johnny Cash',
    19. });
    20. // Use jquery to emulate a click on our button
    21. $('#button').click();
    22. // Assert that the fetchCurrentUser function was called, and that the
    23. // #username span's inner text was updated as we'd expect it to.
    24. expect(fetchCurrentUser).toBeCalled();
    25. expect($('#username').text()).toEqual('Johnny Cash - Logged In');
    26. });