Skip to content
This repository has been archived by the owner on Sep 20, 2018. It is now read-only.

Commit

Permalink
[added] default context
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 26, 2016
1 parent deef0e4 commit 2de941f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ Returns the DOM nodes for a component instance, if it exists.

Compiles a selector into a function that matches a node

##### `$.defaultContext(context: ?object) => (node) => bool`

You can globally set a context object to be used for each and all renders,
shallow or otherwise. This is helpful for context that is available to all
levels of the application, like the `router`, i18n context, or a Redux Store.

##### `$.fn.length`

Expand Down
5 changes: 5 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


export default {
context: null
};
13 changes: 11 additions & 2 deletions src/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from './utils';

import { createNode } from 'bill/node';
import defaults from './defaults';
import invariant from 'invariant';

let createCallback = (collection, fn) => ()=> fn.call(collection, collection)
Expand Down Expand Up @@ -64,7 +65,12 @@ Object.assign($.fn, {
if (intoDocument)
document.body.appendChild(mount)

let { instance, wrapper } = render(element, mount, { context })
if (defaults.context && context)
context = { ...defaults.context, ...context }

let { instance, wrapper } = render(element, mount, {
context: context || defaults.context
})

let collection = iQuery(instance);

Expand All @@ -87,7 +93,10 @@ Object.assign($.fn, {

let renderer = ReactTestUtils.createRenderer()

renderer.render(element, context);
if (defaults.context && context)
context = { ...defaults.context, ...context }

renderer.render(element, context || defaults.context);

let collection = $(getShallowTreeWithRoot(renderer));

Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import ElementCollection from './element'
import InstanceCollection from './instance'
import commonPrototype from './common';
import warning from 'warning';
import invariant from 'invariant';
import { compile, selector, registerPseudo } from 'bill';
import { createNode, NODE_TYPES } from 'bill/node';
import { qsa, matches } from './utils';
import defaults from './defaults';

import * as utils from './utils';

Expand Down Expand Up @@ -72,4 +74,15 @@ $.createPseudo('textContent', text => node => {
return (!text && !!textContent) || text === textContent
})

$.defaultContext = (obj) => {
invariant(typeof obj === 'object',
'[teaspoon]: Default context must be an object or null.'
);

if (!obj || Object.keys(obj) === 0)
obj = null;

defaults.context = obj
};

module.exports = $
3 changes: 1 addition & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ export function render(element, mount, { props, context }, renderFn) {
element = node.element;
}

if (props) {
if (props)
element = React.cloneElement(element, props);
}

if (context) {
wrapper = element = wrapElement(element, context, prevWrapper)
Expand Down
16 changes: 16 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('common', ()=> {
}
})

afterEach(() => {
$.defaultContext(null);
})

it('should create collection', ()=>{
$(<div/>).length.should.equal(1)
$(<div/>)[0].type.should.equal('div')
Expand Down Expand Up @@ -364,6 +368,18 @@ describe('common', ()=> {
.should.throw('the method `context()` found no matching elements')
})

it('should use default context', ()=> {
let context = { question: ', who dis?'};

$.defaultContext(context);

render(<Example />)
.context('question').should.equal(context.question)

render(<Example />)
.context().should.eql(context)
})

it('should not reuse nodes on rerenders', ()=> {
let Component = (props) => (
<div>
Expand Down
2 changes: 1 addition & 1 deletion test/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('DOM rendering specific', ()=> {
}
}

it.only('should wrap existing mounted component', ()=> {
it('should wrap existing mounted component', ()=> {
class Div extends React.Component {
render(){ return <div {...this.props} /> }
}
Expand Down

0 comments on commit 2de941f

Please sign in to comment.