├── .gitignore ├── .node-version ├── .travis.yml ├── LICENSE ├── README.md ├── bower.json ├── package.json ├── rquery.js └── test ├── events.spec.js ├── index.html ├── rquery.spec.js └── selectors.spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | /bower_components -------------------------------------------------------------------------------- /.node-version: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "5" 4 | script: 5 | - npm test 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Andrew Hanna 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Archived 2 | 3 | This repository is no longer being actively developed. Many newer, more modern, and well-supported alternatives have become available since this project was first created. 4 | 5 | # rquery 6 | A [React](http://facebook.github.io/react/) tree traversal utility similar to 7 | jQuery, which can be useful for making assertions on your components in your 8 | tests. 9 | 10 | ## Vision 11 | [`chai-react`](https://github.com/percyhanna/chai-react/) was originally built 12 | to help with test assertions of React components. However, it quickly started 13 | adding too much complexity because it was attempting to solve two problems: 1) 14 | making assertions of properties/rendered content and 2) traversing the rendered 15 | React tree to make those assertions. 16 | 17 | `rquery` is meant to take over the rendered tree traversing responsibility from 18 | `chai-react`, which will allow it to be used with any testing framework. It will 19 | also provide convenience wrappers for various common test actions, such as event 20 | dispatching. 21 | 22 | ## React Version Support 23 | 24 | * For React v15, use `rquery` version 5+ 25 | * For React v0.14, use `rquery` version 4.x 26 | 27 | ## Setup 28 | 29 | ### Node.js, Webpack, Browserify 30 | 31 | ```javascript 32 | var _ = require('lodash'); 33 | var React = require('react'); 34 | var ReactDOM = require('react-dom'); 35 | var TestUtils = require('react-addons-test-utils'); 36 | var $R = require('rquery')(_, React, ReactDOM, TestUtils); 37 | ``` 38 | 39 | ### Browser with Scripttags 40 | 41 | Include React, lodash, and rquery in the page, then you get the `$R` global. 42 | 43 | Sample usage: 44 | 45 | ```html 46 | 47 | 48 | 49 | 50 | 66 | ``` 67 | 68 | ## Usage 69 | 70 | ### $R Factory 71 | 72 | The `$R` factory method returns a new instance of an `rquery` object. 73 | 74 | **Example**: 75 | 76 | ```javascript 77 | var $r = $R(component); 78 | ``` 79 | 80 | ### Class Methods 81 | 82 | #### `.extend` 83 | 84 | ```javascript 85 | $R.extend({ 86 | customMethod: function () { 87 | // your own custom method 88 | } 89 | }); 90 | ``` 91 | 92 | The `extend` method allows you to add extra methods to the `rquery` prototype. It 93 | does not allow you to override internal methods, though. 94 | 95 | #### `.isRQuery` 96 | 97 | ```javascript 98 | $R.isRQuery('abc'); // false 99 | $R.isRQuery($R([])); // true 100 | ``` 101 | 102 | Returns `true` if the provided argument is an instance of the `rquery` 103 | prototype. 104 | 105 | ### *Instance Methods* 106 | 107 | An instance of the `rquery` class contains an array of components, and provides 108 | an `Array`-like interface to directly access each component. 109 | 110 | **Example**: 111 | 112 | ```javascript 113 | var $r = $R([component1, component2 /* , componentN */]); 114 | $r.length === 2; // true 115 | $r[0] === component1; // true 116 | $r[1] === component2; // true 117 | ``` 118 | 119 | #### `#find` 120 | 121 | ```javascript 122 | $r.find(selector) 123 | ``` 124 | 125 | Returns a new `rquery` instance with the components that match the provided 126 | selector (see [Selector](#selectors) documentation). 127 | 128 | #### `#prop` 129 | 130 | ```javascript 131 | $r.prop('a') 132 | ``` 133 | 134 | Returns the value for the given prop for the first component in the scope. It 135 | throws an error if the scope has no components. 136 | 137 | #### `#style` 138 | 139 | ```javascript 140 | $r.style('a') 141 | ``` 142 | 143 | Returns the value for the given style for the first component in the scope. The 144 | style value is loaded from the style property. It throws an error if the scope 145 | has no components. 146 | 147 | #### `#state` 148 | 149 | ```javascript 150 | $r.state('a') 151 | ``` 152 | 153 | Returns the value for the given state for the first component in the scope. It 154 | throws an error if the scope has no components. 155 | 156 | #### `#nodes` 157 | 158 | ```javascript 159 | $r.nodes() 160 | ``` 161 | 162 | Returns an array of each DOM node in the current scope. 163 | 164 | #### `#text` 165 | 166 | ```javascript 167 | $r.text() 168 | ``` 169 | 170 | Returns the text contents of the component(s) in the `$r` object. Similar to 171 | jQuery's `text()` method (read-only). 172 | 173 | #### `#html` 174 | 175 | ```javascript 176 | $r.html() 177 | ``` 178 | 179 | Returns the HTML contents of the component(s) in the `$r` object. Similar to 180 | jQuery's `html()` method (read-only). 181 | 182 | #### `#simulateEvent` 183 | 184 | ```javascript 185 | simulateEvent(eventName, eventData) 186 | ``` 187 | 188 | Simulates triggering the `eventName` DOM event on the component(s) in the rquery 189 | object. 190 | 191 | #### Event helpers 192 | 193 | ```javascript 194 | [eventName](eventData) 195 | ``` 196 | 197 | Convenience helper methods to trigger any supported React DOM event. See the 198 | [React documentation](http://facebook.github.io/react/docs/events.html) to read 199 | about the events that are currently supported. 200 | 201 | ## Selectors 202 | 203 | ### Scope Selectors 204 | 205 | #### Descendant Selector 206 | 207 | **Example**: 208 | 209 | ```javascript 210 | $R(component).find('div p'); 211 | ``` 212 | 213 | **Description**: 214 | 215 | Finds all elements that are descendants of a parent component/node. Note that 216 | the root element of a component will be matched as a descendant of it 217 | (e.g. `MyComponent > div` will match `