-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rip out sinon & mocha into separate modules
This refactor addresses several issues regarding enzyme working in a variety of environments, and the library just generally not making any assumptions about the environment that tests will be run in. For the most part, this amounts to: - remove direct dependency on jsdom - remove direct dependency on sinon - remove assumed dependency on mocha Additionally, this change moves the code that was removed into separate modules that are defined in the packages folder. In this case we have added two modules: "enzyme-mocha" and "enzyme-sinon". This design will allow us to have one repo where all issues, code, and documentation regarding enzyme are centralized, but modules are still separated at the NPM level. This will allow us to make atomic releases across the modules more easily, as well as keep all of the discussion in one place. Left to do for this to be mergable is: [ ] Add a "guides" section in the docs explaining how to use enzyme in different environments [ ] Build a proper npm script to run an npm publish in the proper order for each module [ ] Add more meaningful tests for packages
- Loading branch information
1 parent
2db7c67
commit 338e201
Showing
28 changed files
with
259 additions
and
194 deletions.
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
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
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,19 @@ | ||
{ | ||
"name": "enzyme-mocha", | ||
"version": "1.1.0", | ||
"description": "Make Mocha and Enzyme play nice", | ||
"main": "build/index.js", | ||
"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" | ||
} | ||
} |
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); | ||
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,16 @@ | ||
{ | ||
"name": "enzyme-sinon", | ||
"version": "1.1.0", | ||
"description": "Make Sinon and Enzyme play nice", | ||
"main": "build/index.js", | ||
"repository": "https://github.com/airbnb/enzyme/tree/master/packages/enzyme-mocha", | ||
"keywords": [ | ||
"sinon", | ||
"enzyme" | ||
], | ||
"dependencies": { | ||
"sinon": "^1.17.2" | ||
}, | ||
"author": "Leland Richardson <leland.richardson@airbnb.com>", | ||
"license": "MIT" | ||
} |
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 | ||
# this cookie was getting too long for a JSON prop | ||
# builds the Phoenix yavascripst from src to lib | ||
|
||
# die on error | ||
setopt -e | ||
|
||
for pkg in packages/*/ ; do | ||
babel ${pkg}/src --out-dir ${pkg}/build | ||
done |
File renamed without changes.
Oops, something went wrong.