How to use jest-mock - 10 common examples

To help you get started, we’ve selected a few jest-mock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github conveyal / analysis-ui / lib / components / __tests__ / edit-region.js View on Github external
it('renders correctly', () => {
    const props = {
      region: mockRegion,
      save: mock.fn(),
      load: mock.fn(),
      create: mock.fn(),
      deleteRegion: mock.fn(),
      setLocally: mock.fn(),
      setCenter: mock.fn()
    }

    // mount component
    const {snapshot} = mockWithProvider()
    expect(snapshot()).toMatchSnapshot()
    const noCalls = [
      'save',
      'load',
      'setLocally',
      'setCenter'
    ]
    noCalls.forEach(fn => {
      expect(props[fn]).not.toBeCalled()
github mikechabot / redux-entity / test / unit / test-thunk.js View on Github external
it('Stage AFTER_SUCCESS', (done) => {
                const afterSuccess = {
                    _runAfterSuccess(dispatch, getState, data) {
                        return dispatch({type: 'foo', data});
                    }
                };

                const spy = mock.spyOn(afterSuccess, '_runAfterSuccess');

                const configOptions = {
                    processors: {
                        [PROCESSOR_STAGE.AFTER_SUCCESS]: afterSuccess._runAfterSuccess
                    }
                };

                // Under test
                store
                    .dispatch(loadEntity(entity, Promise.resolve({}), configOptions))
                    .then(() => {
                        expect(spy).toHaveBeenCalled();
                    })
                    .then(done)
                    .catch(done);
            });
github mikechabot / redux-entity / test / unit / test-thunk.js View on Github external
it('Stage BEFORE_SUCCESS', (done) => {
                const beforeSuccess = {
                    _runBeforeSuccess(dispatch, getState, data) {
                        return dispatch({type: 'foo', data});
                    }
                };

                const spy = mock.spyOn(beforeSuccess, '_runBeforeSuccess');

                const configOptions = {
                    processors: {
                        [PROCESSOR_STAGE.BEFORE_SUCCESS]: beforeSuccess._runBeforeSuccess
                    }
                };

                // Under test
                store
                    .dispatch(loadEntity(entity, Promise.resolve({}), configOptions))
                    .then(() => {
                        expect(spy).toHaveBeenCalled();
                    })
                    .then(done)
                    .catch(done);
            });
github mikechabot / redux-entity / test / unit / test-thunk.js View on Github external
it('Stage BEFORE_FAILURE', (done) => {
                const beforeFailure = {
                    _runBeforeFailure(dispatch, getState, data) {
                        return dispatch({type: 'foo', data});
                    }
                };

                const spy = mock.spyOn(beforeFailure, '_runBeforeFailure');

                const configOptions = {
                    processors: {
                        [PROCESSOR_STAGE.BEFORE_FAILURE]: beforeFailure._runBeforeFailure
                    }
                };

                // Under test
                store
                    .dispatch(loadEntity(entity, Promise.reject(new Error('Fake error')), configOptions))
                    .catch(() => {
                        expect(spy).toHaveBeenCalled();
                        done();
                    });
            });
            it('Stage BEFORE_FAILURE should return a new object', (done) => {
github mikechabot / redux-entity / test / unit / test-thunk.js View on Github external
it('Stage AFTER_FAILURE', (done) => {
                const afterFailure = {
                    _runAfterFailure(dispatch, getState, data) {
                        return dispatch({type: 'foo', data});
                    }
                };

                const spy = mock.spyOn(afterFailure, '_runAfterFailure');

                const configOptions = {
                    processors: {
                        [PROCESSOR_STAGE.BEFORE_FAILURE]: afterFailure._runAfterFailure
                    }
                };

                // Under test
                store
                    .dispatch(loadEntity(entity, Promise.reject(new Error('Fake Error')), configOptions))
                    .catch(() => {
                        expect(spy).toHaveBeenCalled();
                        done();
                    });
            });
        });
github thymikee / jest-preset-angular / testEnvironment.js View on Github external
constructor(config) {
    // lazy require
    const {JSDOM} = require('jsdom');
    
    this.document = new JSDOM('', {
      url: config.testURL,
      runScripts: 'dangerously'
    });
    const global = (this.global = this.document.window.document.defaultView);
    // Node's error-message stack size is limited at 10, but it's pretty useful
    // to see more than that when a test fails.
    global.Error.stackTraceLimit = 100;
    installCommonGlobals(global, config.globals);

    this.moduleMocker = new mock.ModuleMocker(global);
    this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);
  }
github NikhilVerma / jest-environment-node-debug-fixed / index.js View on Github external
constructor(config) {

        // jest doesn't need full global before runScript, but to reduce possible issues we create full copy here
        this.global = assign({}, global, deepCopy(config.globals));

        this.moduleMocker = new ModuleMocker(global);
        this.fakeTimers = new FakeTimers(this.global, this.moduleMocker, config);
    }
github sourcegraph / sourcegraph / shared / dev / jest-environment.js View on Github external
const originalAddListener = global.addEventListener
    const originalRemoveListener = global.removeEventListener
    global.addEventListener = function(...args) {
      if (args[0] === 'error') {
        userErrorListenerCount++
      }
      return originalAddListener.apply(this, args)
    }
    global.removeEventListener = function(...args) {
      if (args[0] === 'error') {
        userErrorListenerCount--
      }
      return originalRemoveListener.apply(this, args)
    }
    /* eslint-enable @typescript-eslint/unbound-method */
    this.moduleMocker = new ModuleMocker(global)
    const timerConfig = {
      idToRef: id => id,
      refToId: ref => ref,
    }
    this.fakeTimers = new JestFakeTimers({
      config,
      global,
      moduleMocker: this.moduleMocker,
      timerConfig,
    })
    this.global.jsdom = this.dom
  }
  setup() {
github kinvolk / nebraska / frontend / src / js / __tests__ / Applications / ApplicationItemGroupItem.spec.js View on Github external
import { MuiThemeProvider } from '@material-ui/core/styles';
import { getByText, render, waitForDomChange } from '@testing-library/react';
import jest from 'jest-mock';
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import API from '../../api/API';
import ApplicationItemGroupItem from '../../components/Applications/ApplicationItemGroupItem';
import { theme } from '../../TestHelpers/theme';

function mockResolver() {
  return Promise.resolve(1);
}
const mockAjax = jest.fn(() => mockResolver);
describe('Application Item Group Item', () => {
  const minProps = {
    group: {
      name: 'ABC',
      application_id: '123',
      id: '1',
      channel: {
        name: 'main',
      },
    },
    appName: 'FlatCar',
  };
  beforeEach(() => {
    API.getInstancesCount = mockAjax();
  });
  it('should render correct link and correct total instances', async () => {
github conveyal / analysis-ui / lib / components / map / __tests__ / edit-bounds.js View on Github external
it('renders correctly', () => {
    const tree = renderer.create(
      <map>
        
      </map>
    ).toJSON()
    expect(tree).toMatchSnapshot()
  })
})

jest-mock

**Note:** More details on user side API can be found in [Jest documentation](https://jestjs.io/docs/mock-function-api).

MIT
Latest version published 1 year ago

Package Health Score

88 / 100
Full package analysis