-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
isOnlyResult
out of component state
- Loading branch information
Showing
4 changed files
with
58 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {head} from 'lodash'; | ||
|
||
function getIsOnlyResult({allowNew, highlightOnlyResult, results}) { | ||
if (!highlightOnlyResult || allowNew) { | ||
return false; | ||
} | ||
|
||
return results.length === 1 && !head(results).disabled; | ||
} | ||
|
||
export default getIsOnlyResult; |
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,42 @@ | ||
import {expect} from 'chai'; | ||
|
||
import getIsOnlyResult from '../../src/utils/getIsOnlyResult'; | ||
|
||
describe('getIsOnlyResult', () => { | ||
let props; | ||
|
||
beforeEach(() => { | ||
props = { | ||
allowNew: false, | ||
highlightOnlyResult: true, | ||
results: ['The only result!'], | ||
}; | ||
}); | ||
|
||
it('returns true', () => { | ||
expect(getIsOnlyResult(props)).to.equal(true); | ||
}); | ||
|
||
it('returns false when `highlightOnlyResult` is disabled', () => { | ||
props.highlightOnlyResult = false; | ||
expect(getIsOnlyResult(props)).to.equal(false); | ||
}); | ||
|
||
it('returns false when custom options are allowed', () => { | ||
props.allowNew = true; | ||
expect(getIsOnlyResult(props)).to.equal(false); | ||
}); | ||
|
||
it('returns false when there are more or less than one result', () => { | ||
props.results = ['One', 'Two']; | ||
expect(getIsOnlyResult(props)).to.equal(false); | ||
|
||
props.results = []; | ||
expect(getIsOnlyResult(props)).to.equal(false); | ||
}); | ||
|
||
it('returns false when the only result is disabled', () => { | ||
props.results = [{disabled: true}]; | ||
expect(getIsOnlyResult(props)).to.equal(false); | ||
}); | ||
}); |