-
Notifications
You must be signed in to change notification settings - Fork 442
/
ViewSwitcherHits.unit.tsx
135 lines (104 loc) · 3.67 KB
/
ViewSwitcherHits.unit.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as React from "react";
import {mount} from "enzyme";
import {ViewSwitcherToggle} from "./ViewSwitcherToggle";
import {ViewSwitcherHits} from "./ViewSwitcherHits";
import {ViewSwitcherConfig} from "./ViewSwitcherConfig";
import {Select} from "../../ui";
import {SearchkitManager} from "../../../core";
import {
fastClick, hasClass, printPrettyHtml, htmlClean
} from "../../__test__/TestHelpers"
const map = require("lodash/map")
const MovieHitsGridItem = (props) => {
return (
<div className="grid-item">{props.result.title}</div>
)
}
const MovieHitsListItem = (props) => {
return (
<div className="list-item">{props.result.title}</div>
)
}
const MovieList = (props)=> {
return (
<div className="custom-list">
{map(props.hits, "_id").join(",")}
</div>
)
}
describe("View Switcher Hits component", () => {
describe('renders correctly', () => {
beforeEach(() => {
this.searchkit = SearchkitManager.mock()
this.searchkit.setResults({
hits:{
hits:[{_id:1, title:1},{_id:2,title:2}],
total:2
}
})
this.setWrapper = (props={})=> {
this.wrapper = mount(
<div>
<ViewSwitcherHits searchkit={this.searchkit}
hitComponents = {[
{key:"grid", title:"Grid", itemComponent:MovieHitsGridItem, defaultOption:true},
{key:"list", title:"List", itemComponent:MovieHitsListItem},
{key:"custom-list", title:"Custom List", listComponent:MovieList}
]}
highlightFields={["title"]}
hitsPerPage={12}
sourceFilter={["title"]}/>
<ViewSwitcherToggle searchkit={this.searchkit} translations={{"Grid":"My Grid"}} {...props}/>
</div>
)
}
this.ViewOptionsAccessor = this.searchkit.accessors.accessors[0]
});
it("View Switcher Hits", ()=> {
this.setWrapper()
expect(this.wrapper).toMatchSnapshot()
fastClick(this.wrapper.find(".sk-toggle-option").at(1))
this.wrapper.update()
expect(this.wrapper).toMatchSnapshot()
fastClick(this.wrapper.find(".sk-toggle-option").at(2))
expect(this.wrapper).toMatchSnapshot()
})
it("custom mod, className, listComponent", ()=> {
this.setWrapper({
mod:"my-view-switcher", className:"customClass",
listComponent:Select
})
expect(this.wrapper).toMatchSnapshot()
})
it("Works with ViewSwitcherConfig", ()=> {
this.wrapper = mount(
<div>
<ViewSwitcherConfig searchkit={this.searchkit}
hitComponents = {[
{key:"grid", title:"Grid", itemComponent:MovieHitsGridItem, defaultOption:true},
{key:"list", title:"List", itemComponent:MovieHitsListItem},
{key:"custom-list", title:"Custom List", listComponent:MovieList}
]}
/>
<ViewSwitcherHits searchkit={this.searchkit}
highlightFields={["title"]}
hitsPerPage={12}
sourceFilter={["title"]}/>
<ViewSwitcherToggle searchkit={this.searchkit} translations={{"Grid":"My Grid"}}/>
</div>
)
expect(this.wrapper).toMatchSnapshot()
fastClick(this.wrapper.find("div[data-key='list']"))
expect(this.wrapper.find("div[data-key='list']").hasClass("is-active")).toBe(true)
expect(this.wrapper).toMatchSnapshot()
})
it("renders null when no accessor available", ()=> {
this.wrapper = mount(
<div>
<ViewSwitcherToggle searchkit={this.searchkit} translations={{ "Grid": "My Grid" }} />
</div>
)
expect(this.wrapper).toMatchSnapshot()
})
})
})