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
1 parent
7f7c86e
commit 5f10fa9
Showing
5 changed files
with
317 additions
and
22 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
199 changes: 199 additions & 0 deletions
199
src/components/search/filters/input-filter/InputFilter.unit.tsx
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,199 @@ | ||
import * as React from "react"; | ||
import {mount} from "enzyme"; | ||
import { InputFilter } from "./InputFilter"; | ||
import { SearchkitManager } from "../../../../core"; | ||
const bem = require("bem-cn"); | ||
import { | ||
fastClick, hasClass, jsxToHTML, printPrettyHtml | ||
} from "../../../__test__/TestHelpers" | ||
|
||
import * as sinon from "sinon"; | ||
|
||
describe("Searchbox tests", () => { | ||
|
||
beforeEach(() => { | ||
|
||
this.searchkit = SearchkitManager.mock() | ||
|
||
this.searchkit.translateFunction = (key)=> { | ||
return { | ||
"searchbox.placeholder":"search placeholder", | ||
}[key] | ||
} | ||
|
||
this.createWrapper = (searchOnChange=false, queryFields=null, prefixQueryFields=null, otherProps={}) => { | ||
this.wrapper = mount( | ||
<InputFilter searchkit={this.searchkit} | ||
id="test_id" | ||
title="Test title" | ||
searchOnChange={searchOnChange} | ||
queryFields={queryFields} | ||
prefixQueryFields={prefixQueryFields} | ||
{...otherProps} /> | ||
); | ||
this.accessor = this.searchkit.accessors.getAccessors()[0] | ||
} | ||
|
||
this.setResults = ()=> { | ||
this.searchkit.setResults({ | ||
hits:{ | ||
hits:[{_id:1, title:1},{_id:2,title:2}], | ||
total:2 | ||
} | ||
}) | ||
} | ||
|
||
this.setEmptyResults = () => { | ||
this.searchkit.setResults({ | ||
hits: { | ||
total: 0 | ||
} | ||
}) | ||
} | ||
|
||
this.typeSearch = (value)=> { | ||
this.wrapper.find(".sk-input-filter__text") | ||
.simulate("input", {target:{value}}) | ||
} | ||
|
||
}); | ||
|
||
it("render", () => { | ||
this.createWrapper() | ||
expect(this.wrapper.find(".sk-input-filter__text").get(0).placeholder).toBe("search placeholder") | ||
}) | ||
|
||
it("toggles visibility", () => { | ||
let spy = sinon.spy() | ||
this.searchkit.performSearch = spy | ||
this.createWrapper(true) | ||
|
||
this.setEmptyResults() | ||
expect(hasClass(this.wrapper.find(".sk-panel"), "is-disabled")).toBe(true) | ||
|
||
this.setResults() | ||
expect(hasClass(this.wrapper.find(".sk-panel"), "is-disabled")).toBe(false) | ||
|
||
// Don't hide if active filter | ||
this.typeSearch("noresults") | ||
this.setEmptyResults() | ||
expect(hasClass(this.wrapper.find(".sk-panel"), "is-disabled")).toBe(false) | ||
}) | ||
|
||
it("should allow custom mod and className", () => { | ||
this.createWrapper(false, null, null, { | ||
mod: "my-input", className: "my-class" | ||
}) | ||
this.setResults() | ||
expect(this.wrapper.html()).toEqual(jsxToHTML( | ||
<div className="sk-panel filter--test_id"> | ||
<div className="sk-panel__header">Test title</div> | ||
<div className="sk-panel__content"> | ||
<div className="my-input"> | ||
<form> | ||
<div className="my-input__icon" /> | ||
<input type="text" data-qa="input-filter" className="my-input__text" placeholder="search placeholder" value=""/> | ||
<input type="submit" value="search" className="my-input__action" data-qa="submit" /> | ||
<div data-qa="loader" className="my-input__loader sk-spinning-loader is-hidden"></div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
)) | ||
}) | ||
|
||
it("search on change", () => { | ||
let spy = sinon.spy() | ||
this.searchkit.performSearch = spy | ||
this.createWrapper(true) | ||
this.typeSearch("m") | ||
expect(this.accessor.state.getValue()).toBe("m") | ||
expect(spy.callCount).toBe(1) | ||
this.typeSearch("ma") | ||
expect(this.accessor.state.getValue()).toBe("ma") | ||
expect(spy.callCount).toBe(1) // throttling should block it | ||
this.wrapper.node.throttledSearch.flush() | ||
expect(spy.callCount).toBe(2) | ||
}) | ||
|
||
it("search on change with clock", ()=> { | ||
jasmine.clock().install() | ||
let queries = [] | ||
this.searchkit.performSearch = ()=> { | ||
queries.push(this.searchkit.buildQuery()) | ||
} | ||
expect(this.wrapper.node.props.searchThrottleTime).toBe(200) | ||
this.createWrapper(true) | ||
this.typeSearch("m") | ||
jasmine.clock().tick(100) | ||
expect(queries.length).toBe(1) | ||
expect(queries[0].getQueryString()).toBe("m") | ||
this.typeSearch("ma") | ||
jasmine.clock().tick(100) | ||
expect(queries.length).toBe(1) | ||
jasmine.clock().tick(300) | ||
expect(queries.length).toBe(2) | ||
expect(queries[1].getQueryString()).toBe("ma") | ||
jasmine.clock().uninstall() | ||
}) | ||
|
||
it("search on submit", () => { | ||
let spy = sinon.spy() | ||
this.searchkit.performSearch = spy | ||
|
||
this.createWrapper(false) | ||
this.typeSearch('m') | ||
this.typeSearch('ma') | ||
expect(this.accessor.state.getValue()).toBe("ma") | ||
expect(spy.callCount).toBe(0) | ||
this.wrapper.find("form").simulate("submit") | ||
expect(spy.callCount).toBe(1) | ||
}) | ||
|
||
it("should configure accessor defaults correctly", ()=> { | ||
this.createWrapper(false, ["title"]) | ||
|
||
expect(this.accessor.key).toBe("test_id") | ||
let options = this.accessor.options | ||
expect(options).toEqual({ | ||
title: "Test title", | ||
addToFilters: true, | ||
"queryFields": ["title"], | ||
prefixQueryFields:false, | ||
"queryOptions": {} | ||
}) | ||
|
||
}) | ||
|
||
it("should configure accessor search on change correctly", ()=> { | ||
this.createWrapper(true, ["title"]) | ||
|
||
expect(this.accessor.key).toBe("test_id") | ||
let options = this.accessor.options | ||
expect(options).toEqual({ | ||
title: "Test title", | ||
addToFilters: true, | ||
queryFields: ["title"], | ||
prefixQueryFields:["title"], | ||
queryOptions: {} | ||
}) | ||
|
||
}) | ||
|
||
it("should configure accessor + prefix", ()=> { | ||
this.createWrapper(true, ["title"], ["prefix"]) | ||
|
||
expect(this.accessor.key).toBe("test_id") | ||
let options = this.accessor.options | ||
expect(options).toEqual({ | ||
title: "Test title", | ||
addToFilters: true, | ||
"queryFields": ["title"], | ||
prefixQueryFields:["prefix"], | ||
"queryOptions": {} | ||
}) | ||
|
||
}) | ||
|
||
|
||
}); |
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
Oops, something went wrong.