-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Rip out sinon & mocha into separate modules #60
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,5 @@ node_modules | |
.idea | ||
|
||
/build | ||
packages/*/build | ||
_book |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Using Enzyme with Mocha | ||
|
||
```bash | ||
npm i --save-dev enzyme-mocha | ||
``` | ||
|
||
```jsx | ||
import { mount } from 'enzyme'; | ||
import { describeWithDOM } from 'enzyme-mocha'; | ||
|
||
describe('<Foo />', () => { | ||
|
||
it('calls componentDidMount', () => { | ||
spy(Foo.prototype, 'componentDidMount'); | ||
const wrapper = mount(<Foo />); | ||
expect(Foo.prototype.componentDidMount.calledOnce).to.be.true; | ||
}); | ||
|
||
}); | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Left empty in order to include the build directory for npm publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "enzyme-mocha", | ||
"version": "1.1.0", | ||
"description": "Make Mocha and Enzyme play nice", | ||
"main": "build/index.js", | ||
"scripts": { | ||
"build": "babel src --out-dir build", | ||
"test": "mocha --compilers js:babel/register" | ||
}, | ||
"repository": "https://github.com/airbnb/enzyme/tree/master/packages/enzyme-mocha", | ||
"keywords": [ | ||
"mocha", | ||
"enzyme" | ||
], | ||
"author": "Leland Richardson <leland.richardson@airbnb.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"jsdomify": "^1.0.2" | ||
}, | ||
"peerDependencies": { | ||
"react": "0.13.x || 0.14.x", | ||
"mocha": ">= 2.0.0", | ||
"enzyme": "^1.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
let jsdomify; | ||
|
||
try { | ||
jsdomify = require('jsdomify'); | ||
jsdomify.create(); | ||
require('react'); // require react explicitly after jsdomify.create() has been called | ||
jsdomify.destroy(); | ||
} catch (e) { | ||
// jsdom is not supported | ||
} | ||
|
||
export function jsdomSetup() { | ||
if (!jsdomify) return; | ||
jsdomify.create(); | ||
} | ||
|
||
export function jsdomTeardown() { | ||
if (!jsdomify) return; | ||
jsdomify.destroy(); | ||
} | ||
|
||
export function describeWithDOM(a, b) { | ||
describe('(uses jsdom)', () => { | ||
if (typeof jsdom === 'function') { | ||
beforeEach(jsdomSetup); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these globals are also implicitly depended on |
||
afterEach(jsdomTeardown); | ||
describe(a, b); | ||
} else { | ||
// if jsdom isn't available, skip every test in this describe context | ||
describe.skip(a, b); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
describe('jsdom', () => { | ||
it.skip('TODO: set up tests'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Left empty in order to include the build directory for npm publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "enzyme-sinon", | ||
"version": "1.1.0", | ||
"description": "Make Sinon and Enzyme play nice", | ||
"main": "build/index.js", | ||
"scripts": { | ||
"build": "babel src --out-dir build", | ||
"test": "mocha --compilers js:babel/register" | ||
}, | ||
"repository": "https://github.com/airbnb/enzyme/tree/master/packages/enzyme-mocha", | ||
"keywords": [ | ||
"sinon", | ||
"enzyme" | ||
], | ||
"author": "Leland Richardson <leland.richardson@airbnb.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"sinon": "^1.17.2" | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should have a peerDep of enzyme itself |
||
"peerDependencies": { | ||
"react": "0.13.x || 0.14.x", | ||
"mocha": ">= 2.0.0", | ||
"enzyme": "^1.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Sinon from 'sinon'; | ||
import onPrototype from './onPrototype'; | ||
|
||
export let sinon = Sinon.sandbox.create(); | ||
|
||
export function spySetup() { | ||
sinon = Sinon.sandbox.create(); | ||
} | ||
|
||
export function spyTearDown() { | ||
sinon.restore(); | ||
} | ||
|
||
export function spyLifecycle(Component, sinonInstance = sinon) { | ||
onPrototype(Component, (proto, name) => sinonInstance.spy(proto, name)); | ||
} | ||
|
||
export function spyMethods(Component, sinonInstance = sinon) { | ||
onPrototype(Component, null, (proto, name) => sinonInstance.spy(proto, name)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export default function onPrototype(Component, lifecycle, method) { | ||
const proto = Component.prototype; | ||
Object.getOwnPropertyNames(proto).forEach((name) => { | ||
if (typeof proto[name] !== 'function') return; | ||
switch (name) { | ||
case 'componentDidMount': | ||
case 'componentWillMount': | ||
case 'componentDidUnmount': | ||
case 'componentWillUnmount': | ||
case 'componentWillReceiveProps': | ||
case 'componentDidUpdate': | ||
case 'componentWillUpdate': | ||
case 'shouldComponentUpdate': | ||
case 'render': | ||
if (lifecycle) lifecycle(proto, name); | ||
break; | ||
case 'constructor': | ||
// don't spy on the constructor, even though it shows up in the prototype | ||
break; | ||
default: | ||
if (method) method(proto, name); | ||
break; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import onPrototype from '../src/onPrototype'; | ||
|
||
describe('onPrototype', () => { | ||
|
||
it('makes the expected calls', () => { | ||
|
||
class Foo { | ||
a() {} | ||
b() {} | ||
componentDidUpdate() {} | ||
} | ||
|
||
const lifecycleSpy = sinon.spy(); | ||
const methodSpy = sinon.spy(); | ||
|
||
onPrototype(Foo, lifecycleSpy, methodSpy); | ||
|
||
expect(lifecycleSpy.callCount).to.equal(1); | ||
expect(lifecycleSpy.args[0][0]).to.equal(Foo.prototype); | ||
expect(lifecycleSpy.args[0][1]).to.equal('componentDidUpdate'); | ||
|
||
expect(methodSpy.callCount).to.equal(2); | ||
expect(methodSpy.args[0][0]).to.equal(Foo.prototype); | ||
expect(methodSpy.args[0][1]).to.equal('a'); | ||
expect(methodSpy.args[1][1]).to.equal('b'); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env zsh | ||
|
||
# die on error | ||
setopt -e | ||
|
||
for pkg in packages/*/ ; do | ||
pushd ${pkg} > /dev/null | ||
npm run build | ||
popd > /dev/null | ||
done |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens when this is imported outside a test context, and then cached with the global
describe
set toundefined
? I think perhaps this export may need to either takedescribe
as an argument, or, actually be a function that takesdescribe
and returnsdescribeWithDOM
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we aren't caching describe anywhere? Every time
describeWithDOM
is called, it uses thedescribe
that is in the global namespace.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the function retains a lexical reference to the one that's in the global namespace at the moment the function is defined, it's not freshly looked up. do you mean this to be
global.describe
then?