UNPKG

overmind-react

Version:
121 lines 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const overmind_1 = require("overmind"); const React = require("react"); const react_1 = require("@testing-library/react"); require("@testing-library/jest-dom"); const _1 = require("./"); describe('React', () => { test('should allow using hooks', () => { let renderCount = 0; const doThis = ({ state }) => { state.foo = 'bar2'; }; const state = { foo: 'bar', }; const actions = { doThis, }; const config = { state, actions, }; const app = new overmind_1.Overmind(config); const useState = (0, _1.createStateHook)(); const FooComponent = () => { const state = useState(); renderCount++; return React.createElement("h1", null, state.foo); }; const { container } = (0, react_1.render)(React.createElement(_1.Provider, { value: app }, React.createElement(FooComponent, null))); expect(renderCount).toBe(1); (0, react_1.act)(() => { app.actions.doThis(); }); expect(renderCount).toBe(2); expect(container).toMatchSnapshot(); }); test('should allow using hooks with scoped tracking', () => { let renderCount = 0; const doThis = ({ state }) => { state.foo.push({ foo: 'bar2' }); }; const doThat = ({ state }) => { state.foo[0].foo = 'bar3'; }; const state = { foo: [{ foo: 'bar' }], }; const actions = { doThis, doThat, }; const config = { state, actions, }; const app = new overmind_1.Overmind(config); const useState = (0, _1.createStateHook)(); const FooComponent = () => { const state = useState((state) => state.foo[0]); renderCount++; return React.createElement("h1", null, state.foo); }; const { container } = (0, react_1.render)(React.createElement(_1.Provider, { value: app }, React.createElement(FooComponent, null))); expect(renderCount).toBe(1); (0, react_1.act)(() => { app.actions.doThis(); }); expect(renderCount).toBe(1); (0, react_1.act)(() => { app.actions.doThat(); }); expect(renderCount).toBe(2); // This is not showing the expected result, but logging the rendering does, so must be the // library messing it up expect(container).toMatchSnapshot(); }); test('should allow using mocked Overmind', () => { let renderCount = 0; const doThis = ({ state }) => { state.foo = 'bar2'; }; const state = { foo: 'bar', }; const actions = { doThis, }; const config = { state, actions, }; const useState = (0, _1.createStateHook)(); const FooComponent = () => { const state = useState(); renderCount++; return React.createElement("h1", null, state.foo); }; const mock = (0, overmind_1.createOvermindMock)(config); const { container } = (0, react_1.render)(React.createElement(_1.Provider, { value: mock }, React.createElement(FooComponent, null))); expect(renderCount).toBe(1); expect(container).toMatchSnapshot(); }); test('should throw an error without provider', () => { expect.assertions(1); const useState = (0, _1.createStateHook)(); const FooComponent = () => { const state = useState(); return React.createElement("h1", null, state.foo); }; jest.spyOn(console, 'error'); // suppress the error message expect(() => { (0, react_1.render)(React.createElement(FooComponent, null)); }).toThrow(Error); }); }); //# sourceMappingURL=index.test.js.map