Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change state assigment #870

Merged
merged 2 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/rsg-components/Preview/Preview.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import noop from 'lodash/noop';
import { transform } from 'buble';
import PlaygroundError from 'rsg-components/PlaygroundError';
import Wrapper from 'rsg-components/Wrapper';

/* eslint-disable react/no-multi-comp */

const noop = () => {};
const compileCode = (code, config) => transform(code, config).code;

// Wrap everything in a React component to leverage the state management of this component
Expand All @@ -23,10 +23,9 @@ class PreviewComponent extends Component {
this.setInitialState = this.setInitialState.bind(this);
}

// Synchronously set initial state, so it will be ready before first render
// Ignore all consequent calls
// Set the initial state, ignore all consequent calls
setInitialState(initialState) {
Object.assign(this.state, initialState);
this.setState(initialState);
this.setInitialState = noop;
}

Expand Down
43 changes: 42 additions & 1 deletion src/rsg-components/Preview/Preview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import Preview from '../Preview';
/* eslint-disable no-console */

const evalInContext = a =>
new Function('require', 'const React = require("react");' + a).bind(null, require); // eslint-disable-line no-new-func
// eslint-disable-next-line no-new-func
new Function(
'require',
'state',
'setState',
'__setInitialState',
'const React = require("react");' + a
).bind(null, require);
const code = '<button>OK</button>';
const options = {
context: {
Expand Down Expand Up @@ -65,3 +72,37 @@ it('should clear console on second mount', () => {
});
expect(console.clear).toHaveBeenCalledTimes(1);
});

// XXX: “Warning: Cannot update during an existing state transition” doesn’t
// happend in the browser, only in tests
it('should set initialState before the first render', () => {
const code = `
initialState = {count:1};
<span>{state.count}</span>
`;
const actual = mount(<Preview code={code} evalInContext={evalInContext} />, options);
expect(actual.html()).toMatchSnapshot();
});

it('should update state on setState', done => {
const code = `
initialState = {count:1};
setTimeout(() => state.count === 1 && setState({count:2}));
<button>{state.count}</button>
`;
const actual = mount(<Preview code={code} evalInContext={evalInContext} />, options);

actual
.instance()
.mountNode.querySelector('button')
.click();

setTimeout(() => {
try {
expect(actual.html()).toMatchSnapshot();
done();
} catch (err) {
done.fail(err);
}
});
});
24 changes: 24 additions & 0 deletions src/rsg-components/Preview/__snapshots__/Preview.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,27 @@ exports[`should render component renderer 1`] = `
<div />
</div>
`;

exports[`should set initialState before the first render 1`] = `

<div>
<div>
<span>
1
</span>
</div>
</div>

`;

exports[`should update state on setState 1`] = `

<div>
<div>
<button>
2
</button>
</div>
</div>

`;