-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
143 additions
and
30 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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,55 @@ | ||
import React from 'react'; | ||
import ReactTestUtils from 'react-addons-test-utils'; | ||
import { createRenderer, renderIntoDocument, Simulate } from 'react-addons-test-utils'; | ||
|
||
import SearchForm from 'search/components/SearchForm'; | ||
|
||
const render = ReactTestUtils.renderIntoDocument; | ||
const findByTag = ReactTestUtils.findRenderedDOMComponentWithTag; | ||
if (typeof window === 'undefined' && typeof global !== 'undefined') { | ||
global.sinon = require('sinon'); | ||
global.assert = require('chai').assert; | ||
} | ||
|
||
function browser(message, test) { | ||
if (typeof document === 'undefined') { | ||
xit(message, test); | ||
} else { | ||
it(message, test); | ||
} | ||
} | ||
|
||
function render(component) { | ||
const renderer = createRenderer(); | ||
renderer.render(component); | ||
return renderer.getRenderOutput(); | ||
} | ||
|
||
describe('<SearchForm />', () => { | ||
let onSearch; | ||
let root; | ||
|
||
beforeEach(() => { | ||
onSearch = sinon.spy(); | ||
root = render(<SearchForm onSearch={onSearch} />); | ||
}); | ||
|
||
it('is a form', () => { | ||
assert.equal(root.type, 'form'); | ||
assert.equal(root.props.className, 'search-form'); | ||
}); | ||
|
||
it('renders a form', () => { | ||
const root = render(<SearchForm onSearch={sinon.spy()} />); | ||
const form = findByTag(root, 'form'); | ||
assert.ok(form.classList.contains('search-form')); | ||
assert.include(root.props.className, 'search-form'); | ||
}); | ||
|
||
it('renders a search input', () => { | ||
const root = render(<SearchForm onSearch={sinon.spy()} />); | ||
const input = findByTag(root, 'input'); | ||
assert.equal(input.placeholder, 'Search'); | ||
assert.equal(input.type, 'text'); | ||
const query = root.props.children.filter(({ ref }) => ref === 'query')[0]; | ||
assert.equal(query.props.placeholder, 'Search'); | ||
assert.equal(query.props.type, 'search'); | ||
}); | ||
|
||
it('calls onSearch with a search query', () => { | ||
const onSearch = sinon.spy(); | ||
const root = render(<SearchForm onSearch={onSearch} />); | ||
browser('calls onSearch with a search query', () => { | ||
root = renderIntoDocument(<SearchForm onSearch={onSearch} />); | ||
root.refs.query.value = 'adblock'; | ||
ReactTestUtils.Simulate.submit(root.refs.form); | ||
Simulate.submit(root.refs.form); | ||
assert.ok(onSearch.calledWith('adblock')); | ||
}); | ||
}); |
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,66 @@ | ||
import React from 'react'; | ||
import ReactTestUtils from 'react-addons-test-utils'; | ||
|
||
import SearchResults from 'search/components/SearchResults'; | ||
|
||
const render = ReactTestUtils.renderIntoDocument; | ||
const findByTag = ReactTestUtils.findRenderedDOMComponentWithTag; | ||
const findByClass = ReactTestUtils.findRenderedDOMComponentWithClass; | ||
const isDOMComponent = ReactTestUtils.isDOMComponent; | ||
|
||
if (typeof window === 'undefined' && typeof global !== 'undefined') { | ||
global.sinon = require('sinon'); | ||
global.assert = require('chai').assert; | ||
} | ||
|
||
function browser(message, test) { | ||
if (typeof document === 'undefined') { | ||
xit(message, test); | ||
} else { | ||
it(message, test); | ||
} | ||
} | ||
|
||
|
||
describe('<SearchResults />', () => { | ||
function renderResults(props) { | ||
return render(<SearchResults {...props} />); | ||
} | ||
|
||
browser('renders empty search results container', () => { | ||
const root = renderResults(); | ||
const searchResults = findByClass(root, 'search-results'); | ||
assert.ok(isDOMComponent(searchResults)); | ||
assert.equal(searchResults.childNodes.length, 0); | ||
}); | ||
|
||
browser('renders error when query is an empty string', () => { | ||
const root = renderResults({query: ''}); | ||
const searchResultsMsg = findByTag(root, 'p'); | ||
assert.include(searchResultsMsg.firstChild.nodeValue, 'supply a valid search'); | ||
}); | ||
|
||
browser('renders error when no results and valid query', () => { | ||
const root = renderResults({query: 'test'}); | ||
const searchResultsMsg = findByTag(root, 'p'); | ||
// Using textContent here since we want to see the text inside the p. | ||
// Since it has dynamic content is wrapped in a span implicitly. | ||
assert.include(searchResultsMsg.textContent, 'No results were found'); | ||
}); | ||
|
||
browser('renders search results when supplied', () => { | ||
const root = renderResults({ | ||
query: 'test', | ||
results: [ | ||
{title: 'result 1'}, | ||
{title: 'result 2'}, | ||
], | ||
}); | ||
const searchResultsMsg = findByTag(root, 'p'); | ||
assert.include(searchResultsMsg.textContent, 'Your search for "test" returned 2'); | ||
|
||
const searchResultsList = findByTag(root, 'ul'); | ||
assert.include(searchResultsList.textContent, 'result 1'); | ||
assert.include(searchResultsList.textContent, 'result 2'); | ||
}); | ||
}); |