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

E2e jsdom fix #1470

Merged
merged 3 commits into from
Jan 30, 2017
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"babel-preset-latest": "6.16.0",
"babel-register": "6.18.0",
"babel-register": "6.22.0",
"babel-polyfill": "6.20.0",
"chai": "3.5.0",
"jsdom": "9.8.3",
Expand Down
23 changes: 7 additions & 16 deletions packages/react-scripts/fixtures/kitchensink/integration/initDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const path = require('path')

let getMarkup
let resourceLoader
// this value could be tweaked in order to let the resource
// retriever get every file and jsdom execute react
let timeToWaitForJsToExecute

if (process.env.E2E_FILE) {
const file = path.isAbsolute(process.env.E2E_FILE)
Expand All @@ -21,8 +18,6 @@ if (process.env.E2E_FILE) {
null,
fs.readFileSync(path.join(path.dirname(file), resource.url.pathname), 'utf8')
)

timeToWaitForJsToExecute = 0
} else if (process.env.E2E_URL) {
getMarkup = () => new Promise(resolve => {
http.get(process.env.E2E_URL, (res) => {
Expand All @@ -32,11 +27,7 @@ if (process.env.E2E_FILE) {
})
})

resourceLoader = (resource, callback) => {
return resource.defaultFetch(callback)
}

timeToWaitForJsToExecute = 100
resourceLoader = (resource, callback) => resource.defaultFetch(callback)
} else {
it.only('can run jsdom (at least one of "E2E_FILE" or "E2E_URL" environment variables must be provided)', () => {
expect(new Error('This isn\'t the error you are looking for.')).toBeUndefined()
Expand All @@ -47,16 +38,16 @@ export default feature => new Promise(async resolve => {
const markup = await getMarkup()
const host = process.env.E2E_URL || 'http://localhost:3000'
const doc = jsdom.jsdom(markup, {
features : {
FetchExternalResources : ['script', 'css'],
ProcessExternalResources : ['script'],
features: {
FetchExternalResources: ['script', 'css'],
ProcessExternalResources: ['script'],
},
created: (_, win) => win.addEventListener('ReactFeatureDidMount', () => resolve(doc), true),
deferClose: true,
resourceLoader,
url: `${host}#${feature}`,
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
})

doc.defaultView.addEventListener('load', () => {
setTimeout(() => resolve(doc), timeToWaitForJsToExecute)
}, false)
doc.close()
})
24 changes: 23 additions & 1 deletion packages/react-scripts/fixtures/kitchensink/src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import React from 'react';

class BuiltEmitter extends React.Component {
constructor(props) {
super(props)

this.callWhenDone = done => done();
}

componentDidMount() {
this.callWhenDone(() => document.dispatchEvent(new Event('ReactFeatureDidMount')));
}

render() {
const feature = React.cloneElement(React.Children.only(this.props.children), {
setCallWhenDone: done => {
this.callWhenDone = done;
}
});

return <div>{feature}</div>;
}
}

class App extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -96,7 +118,7 @@ class App extends React.Component {

render() {
const Feature = this.state.feature;
return Feature ? <Feature /> : null;
return Feature ? <BuiltEmitter><Feature /></BuiltEmitter> : null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty complicated. Could you explain why just passing onDone as a prop, and then calling it in setState callback not enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a way to allow functional components.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand. Can you give me an example?

this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load();
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load();
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load([{ id: 42, name: '42' }]);
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = await load();
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load('user_');
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load();
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load();
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const { users } = await load();
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

Expand All @@ -20,7 +25,7 @@ export default class extends React.Component {
for (let user of load(4)) {
users.push(user);
}
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load();
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load({ age: 42 });
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

componentDidMount() {
load().then(users => {
this.setState({ users });
this.setState({ users }, () => this.done());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load();
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load({ id: 0, user: { id: 42, name: '42' } });
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export default class extends React.Component {
constructor(props) {
super(props);

this.done = () => {};
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
this.done = done;
});

this.state = { users: [] };
}

async componentDidMount() {
const users = load('user_');
this.setState({ users });
this.setState({ users }, () => this.done());
}

render() {
Expand Down
Loading