{"size": 2088, "ext": "js", "max_stars_repo_path": "__tests__/index.js", "max_stars_repo_name": "frontity/mobx-react-wrapper", "max_stars_repo_stars_event_min_datetime": "2019-01-17T08:22:05.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-17T08:22:05.000Z", "max_issues_repo_path": "__tests__/index.js", "max_issues_repo_name": "frontity/mobx-react-wrapper", "max_issues_repo_issues_event_min_datetime": "2019-01-13T22:08:19.000Z", "max_issues_repo_issues_event_max_datetime": "2019-02-06T16:11:28.000Z", "max_forks_repo_path": "__tests__/index.js", "max_forks_repo_name": "frontity/mobx-react-wrapper", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 8, "max_forks_count": null, "avg_line_length": 36.6315789474, "max_line_length": 79, "alphanum_fraction": 0.6623563218}
import 'react-testing-library/cleanup-after-each';
import 'jest-dom/extend-expect';
import React from 'react';
import { render, fireEvent } from 'react-testing-library';
import { types } from 'mobx-state-tree';
import { Dumb as DumbComponent } from '../example/components/Dumb';
import { Store as DumbStore } from '../example/store';
import wrap, { setStore } from '../src';

beforeEach(() => {
  setStore(null); // Reset the store to its default value.
});

test("Wrapped component throws if setStore wasn't used", () => {
  spyOn(console, 'error'); // Avoid priting error on screen.
  const WrappedDumb = wrap(() => {})(DumbComponent);
  expect(() => render(<WrappedDumb />)).toThrow(
    'Please, use setStore before your ReactDOM.render call'
  );
});

test('setStore should work with only one store', () => {
  const store = { name: 'Peter' };
  setStore(store);
  const WrappedDumb = wrap(({ name }) => ({ name }))(DumbComponent);
  const { getByTestId } = render(<WrappedDumb />);
  expect(getByTestId('name')).toHaveTextContent('Peter');
});

test('setStore should work with more than one store', () => {
  const userStore = { name: 'Peter' };
  const uiStore = {};
  setStore({ user: userStore, ui: uiStore });
  const WrappedDumb = wrap(({ user }) => ({ name: user.name }))(DumbComponent);
  const { getByTestId } = render(<WrappedDumb />);
  expect(getByTestId('name')).toHaveTextContent('Peter');
});

test('Component should be rerendred when an observable changes', () => {
  const store = DumbStore.create();
  setStore(store);
  const WrappedDumb = wrap(({ index, addIndex }) => ({ index, addIndex }))(
    DumbComponent
  );
  const { getByTestId } = render(<WrappedDumb />);
  expect(getByTestId('index')).toHaveTextContent('1');
  fireEvent.click(getByTestId('addIndex'));
  expect(getByTestId('index')).toHaveTextContent('2');
});

test('wrap should pass ownProps', () => {
  setStore({});
  const WrappedDumb = wrap(() => ({}))(DumbComponent);
  const { getByTestId } = render(<WrappedDumb name="John" />);
  expect(getByTestId('name')).toHaveTextContent('John');
});
