Skip to content

Instantly share code, notes, and snippets.

@joelmukuthu
Last active December 1, 2016 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelmukuthu/deb1f4bd656b4ab51a42440a816ca057 to your computer and use it in GitHub Desktop.
Save joelmukuthu/deb1f4bd656b4ab51a42440a816ca057 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { createRenderer } from 'react-addons-test-utils';
import unexpected from 'unexpected';
import unexpectedReact from 'unexpected-react';
class Foo extends Component {
constructor(props) {
super(props);
this.state = {
focus: false
};
this.handleFocus = this.handleFocus.bind(this);
this.handleBlur = this.handleBlur.bind(this);
}
handleFocus(e) {
this.setState({ focus: true });
}
handleBlur(e) {
this.setState({ focus: false });
}
render() {
const {
focus
} = this.state;
return (
<div tabIndex='0' onFocus={this.handleFocus} onBlur={this.handleBlur}>
{focus &&
<div className='with-focus'>
<span>with focus</span>
</div>
}
</div>
);
}
};
const expect = unexpected
.clone()
.use(unexpectedReact);
describe('Foo', function () {
let renderer;
before(() => {
expect.addAssertion('<ReactShallowRenderer> when focussed <assertion>', (expect, renderer) => {
const { props: { onFocus } } = renderer.getRenderOutput();
onFocus();
return expect.shift(renderer);
});
expect.addAssertion('<ReactShallowRenderer> when blurred <assertion>', (expect, renderer) => {
const { props: { onBlur } } = renderer.getRenderOutput();
onBlur();
return expect.shift(renderer);
});
});
beforeEach(() => {
renderer = createRenderer();
});
describe('with event', function () {
it(`renders div.with-focus on focus`, function () {
renderer.render(<Foo />);
return expect(renderer, 'with event focus', 'to contain', (
<div className='with-focus'>
<span>with focus</span>
</div>
));
});
it(`does not render div.with-focus' on blur`, function () {
renderer.render(<Foo />);
return expect(renderer, 'with event focus', 'with event blur', 'not to contain', (
<div className='with-focus' />
));
});
});
describe('custom', function () {
it(`renders div.with-focus on focus`, function () {
renderer.render(<Foo />);
return expect(renderer, 'when focussed', 'to contain', (
<div className='with-focus'>
<span>with focus</span>
</div>
));
});
it(`does not render div.with-focus on blur`, function () {
renderer.render(<Foo />);
return expect(renderer, 'when focussed', 'when blurred', 'not to contain', (
<div className='with-focus' />
));
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment