Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Tests! #77

Merged
merged 40 commits into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
67a03d7
Added mocha to package.json
Feb 16, 2016
fcc7bf6
feat(babel): add babel
Feb 16, 2016
859ec8b
Added mocha test command to package.json
Feb 16, 2016
9c6eba5
Added mocha to package.json
Feb 16, 2016
9f474e1
feat(babel): add babel
Feb 16, 2016
c8cde7a
Added mocha test command to package.json
Feb 16, 2016
f20a01e
Added <label> linked to <input>
Feb 17, 2016
f4bf11a
Corrected reference to labelText property
Feb 17, 2016
593a041
Added labelText and <label> styles to static-data example
Feb 17, 2016
95192ce
Added labelText and <label> styles to async-data example
Feb 17, 2016
f7f1b0d
Added labelText and <label> styles to custom-menu example
Feb 17, 2016
26e788b
Corrected spelling
Feb 17, 2016
c103c60
Removed testing references leftover from different branch
Feb 17, 2016
5e32893
Added accessible name to examples
Feb 17, 2016
defc469
Added test command and babel dependency
Feb 17, 2016
51b08d0
Initialized a wrapper for Autocomplete component in test
Feb 18, 2016
b4426b1
Added ref attribute to <label>
Feb 18, 2016
adfcd19
Put babel preset config behind 'test' environment variable
Feb 18, 2016
c01e9ac
Added jsdom setup file for tests
Feb 19, 2016
f6f89d4
Added global.window context to getComputedStyle() reference
Feb 19, 2016
add2aff
Added '|| 0' fallback in case getComputedStyle() returns empty string
Feb 19, 2016
cc99d9a
Moved utils.js into lib to be used also in tests
Feb 19, 2016
89abc2c
Added jsdom setup file to 'npm test' mocha command
Feb 19, 2016
190352e
Added two tests
Feb 19, 2016
16c7dca
Improved comment clarity
Feb 22, 2016
08b0c2a
Added tests and improved comments for Autocomplete kewDown->Enter eve…
Feb 22, 2016
0f4fc24
Changed keyUp simulated key to Backspace
Feb 22, 2016
a3873ce
Added test for keyDown->Escape event handlers
Feb 22, 2016
d7597b4
Added tests for Autocomplete kewDown->ArrowDown event handlers
Feb 22, 2016
69dea4a
Added tests for Autocomplete kewDown->ArrowUp event handlers
Feb 23, 2016
cfffd89
Added comments
Feb 23, 2016
16de503
Added unit tests for Autocomplete#renderMenu
Feb 23, 2016
dd0ae74
Added code coverage script
Feb 23, 2016
98001a2
Changed code coverage option to produce lcov detailed artifacts
Feb 23, 2016
a8306c4
Changed an expect() to check state instead of an arbitrary object pro…
Feb 24, 2016
b8d1d9a
Added test instructions to README
Feb 24, 2016
cd70cea
Added test for closing menu when input is blurred
Feb 24, 2016
001fb52
Fixed broken test order
Feb 24, 2016
6adb6a6
Removed unused devDependencies and added two needed root-level ones
Mar 9, 2016
bc725ff
Added back a missing parameter
Mar 9, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"test": {
"presets": ["es2015", "stage-0", "react"]
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
examples/**/*-bundle.js
node_modules/
coverage/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ Stuff we need help with:
pretty lean, on purpose, apps should style this however they'd like)
- tests (eventually)

# Tests!
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sprjr README test instructions. Is there more information that would be useful here?


Run them:
`npm test`

Write them:
`lib/__tests__/Autocomplete-test.js`

Check your work:
`npm run coverage`
5 changes: 5 additions & 0 deletions examples/async-data/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ body {
.example {
padding: 0 25px;
}

label {
display: block;
margin: 5px 0;
}
4 changes: 3 additions & 1 deletion examples/async-data/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import Autocomplete from '../../lib/index'
import { getStates, matchStateToTerm, sortStates, styles, fakeRequest } from '../utils'
import { getStates, matchStateToTerm, sortStates, styles, fakeRequest } from '../../lib/utils'

let App = React.createClass({

Expand All @@ -24,6 +24,8 @@ let App = React.createClass({
</p>

<Autocomplete
labelText="Choose a state from the US"
inputProps={{name: "US state"}}
ref="autocomplete"
items={this.state.unitedStates}
getItemValue={(item) => item.name}
Expand Down
5 changes: 5 additions & 0 deletions examples/custom-menu/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ body {
.example {
padding: 0 25px;
}

label {
display: block;
margin: 5px 0;
}
4 changes: 3 additions & 1 deletion examples/custom-menu/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import Autocomplete from '../../lib/index'
import { getStates, matchStateToTerm, sortStates, styles, fakeRequest } from '../utils'
import { getStates, matchStateToTerm, sortStates, styles, fakeRequest } from '../../lib/utils'

let App = React.createClass({

Expand All @@ -21,6 +21,8 @@ let App = React.createClass({
letter of the alphabet.
</p>
<Autocomplete
labelText="Choose a state from the US"
inputProps={{name: "US state"}}
items={this.state.unitedStates}
getItemValue={(item) => item.name}
onSelect={() => this.setState({ unitedStates: [] }) }
Expand Down
5 changes: 5 additions & 0 deletions examples/static-data/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ body {
.example {
padding: 0 25px;
}

label {
display: block;
margin: 5px 0;
}
4 changes: 3 additions & 1 deletion examples/static-data/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { getStates, matchStateToTerm, sortStates, styles } from '../utils'
import { getStates, matchStateToTerm, sortStates, styles } from '../../lib/utils'
import Autocomplete from '../../lib/index'

let App = React.createClass({
Expand All @@ -15,6 +15,8 @@ let App = React.createClass({

<Autocomplete
initialValue="Ma"
labelText="Choose a state from the US"
inputProps={{name: "US state"}}
items={getStates()}
getItemValue={(item) => item.name}
shouldItemRender={matchStateToTerm}
Expand Down
27 changes: 18 additions & 9 deletions lib/Autocomplete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const React = require('react')
const lodash = require('lodash')
const scrollIntoView = require('dom-scroll-into-view')

let _debugStates = []
Expand All @@ -12,12 +13,14 @@ let Autocomplete = React.createClass({
shouldItemRender: React.PropTypes.func,
renderItem: React.PropTypes.func.isRequired,
menuStyle: React.PropTypes.object,
inputProps: React.PropTypes.object
inputProps: React.PropTypes.object,
labelText: React.PropTypes.string
},

getDefaultProps () {
return {
inputProps: {},
labelText: '',
onChange () {},
onSelect (value, item) {},
renderMenu (items, value, style) {
Expand Down Expand Up @@ -46,6 +49,7 @@ let Autocomplete = React.createClass({
},

componentWillMount () {
this.id = lodash.uniqueId('autocomplete-');
this._ignoreBlur = false
this._performAutoCompleteOnUpdate = false
this._performAutoCompleteOnKeyUp = false
Expand Down Expand Up @@ -103,7 +107,7 @@ let Autocomplete = React.createClass({
},

keyDownHandlers: {
ArrowDown () {
ArrowDown (event) {
event.preventDefault()
var { highlightedIndex } = this.state
var index = (
Expand Down Expand Up @@ -133,18 +137,19 @@ let Autocomplete = React.createClass({

Enter (event) {
if (this.state.isOpen === false) {
// already selected this, do nothing
// menu is closed so there is no selection to accept -> do nothing
return
}
else if (this.state.highlightedIndex == null) {
// hit enter after focus but before typing anything so no autocomplete attempt yet
// input has focus but no menu item is selected + enter is hit -> close the menu, highlight whatever's in input
this.setState({
isOpen: false
}, () => {
this.refs.input.select()
})
}
else {
// text entered + menu item has been highlighted + enter is hit -> update value to that of selected menu item, close the menu
var item = this.getFilteredItems()[this.state.highlightedIndex]
this.setState({
value: this.props.getItemValue(item),
Expand Down Expand Up @@ -216,10 +221,10 @@ let Autocomplete = React.createClass({
setMenuPositions () {
var node = this.refs.input
var rect = node.getBoundingClientRect()
var computedStyle = getComputedStyle(node)
var marginBottom = parseInt(computedStyle.marginBottom, 10)
var marginLeft = parseInt(computedStyle.marginLeft, 10)
var marginRight = parseInt(computedStyle.marginRight, 10)
var computedStyle = global.window.getComputedStyle(node)
var marginBottom = parseInt(computedStyle.marginBottom, 10) || 0;
var marginLeft = parseInt(computedStyle.marginLeft, 10) || 0;
var marginRight = parseInt(computedStyle.marginRight, 10) || 0;
this.setState({
menuTop: rect.bottom + marginBottom,
menuLeft: rect.left + marginLeft,
Expand Down Expand Up @@ -297,8 +302,11 @@ let Autocomplete = React.createClass({
state: this.state
})
}
return (
return (
<div style={{display: 'inline-block'}}>
<label htmlFor={this.id} ref="label">
{this.props.labelText}
</label>
<input
{...this.props.inputProps}
role="combobox"
Expand All @@ -311,6 +319,7 @@ let Autocomplete = React.createClass({
onKeyUp={(event) => this.handleKeyUp(event)}
onClick={this.handleInputClick}
value={this.state.value}
id={this.id}
/>
{this.state.isOpen && this.renderMenu()}
{this.props.debug && (
Expand Down
6 changes: 6 additions & 0 deletions lib/__tests__/.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// .setup.js
import { jsdom } from 'jsdom';

global.document = jsdom('<!doctype html><html><body></body></html>');
global.window = document.defaultView;
global.navigator = global.window.navigator;
Loading