-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsearch-bar_test.ts
More file actions
154 lines (142 loc) · 4.6 KB
/
Copy pathsearch-bar_test.ts
File metadata and controls
154 lines (142 loc) · 4.6 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {SearchaliciousBar} from '../search-bar';
import {fixture, assert, expect} from '@open-wc/testing';
import {html} from 'lit/static-html.js';
suite('searchalicious-bar', () => {
test('is defined', () => {
const el = document.createElement('searchalicious-bar');
assert.instanceOf(el, SearchaliciousBar);
});
test('renders with default values', async () => {
const el = await fixture(html` <searchalicious-bar></searchalicious-bar>`);
assert.shadowDom.equal(
el,
`
<div class="search-bar" part="wrapper">
<input
class="search-input"
autocomplete="off"
name="q"
part="input"
placeholder="Search..."
type="text"
>
</div>
</div>
`
);
const input = el.shadowRoot!.querySelector('input');
expect(input).to.have.value('');
const bar = el as SearchaliciousBar;
assert.equal(bar.query, '');
assert.equal(bar.index, undefined);
});
test('renders with custom attributes', async () => {
const el = await fixture(
html` <searchalicious-bar
placeholder="Try it !"
index="foo"
></searchalicious-bar>`
);
//const input = (el.getElementsByTagName('input')[0] as HTMLInputElement);
//assert.equal(input.value, 'fixme');
assert.shadowDom.equal(
el,
`
<div class="search-bar" part="wrapper">
<input
autocomplete="off"
name="q"
class="search-input"
part="input"
placeholder="Try it !"
type="text"
>
</div>
`
);
const bar = el as SearchaliciousBar;
assert.equal(bar.query, '');
assert.equal(bar.index, 'foo');
});
test('text input in query', async () => {
const el = await fixture(html` <searchalicious-bar></searchalicious-bar>`);
const input = el.shadowRoot!.querySelector('input');
input!.value = 'test';
input!.dispatchEvent(new Event('input'));
const bar = el as SearchaliciousBar;
assert.equal(bar.query, 'test');
});
test('_searchUrl computation', async () => {
const el = await fixture(
html` <searchalicious-bar index="foo"></searchalicious-bar>`
);
const input = el.shadowRoot!.querySelector('input');
input!.value = 'test';
input!.dispatchEvent(new Event('input'));
const bar = el as SearchaliciousBar;
const searchParams = (bar as any)['_searchUrl']();
assert.equal(searchParams.searchUrl, '/search');
console.log(searchParams.params);
assert.deepEqual(searchParams.params, {
boost_phrase: false,
index_id: 'foo',
langs: ['en'],
page_size: '10',
q: 'test',
});
assert.equal(
(bar as any)._paramsToQueryStr(searchParams.params),
'index_id=foo&langs=en&page_size=10&q=test'
);
});
test('_searchUrl computation boost phrase', async () => {
const el = await fixture(
html` <searchalicious-bar index="foo" boost-phrase></searchalicious-bar>`
);
const input = el.shadowRoot!.querySelector('input');
input!.value = 'test';
input!.dispatchEvent(new Event('input'));
const bar = el as SearchaliciousBar;
const searchParams = (bar as any)['_searchUrl']();
assert.equal(searchParams.searchUrl, '/search');
assert.deepEqual(searchParams.params, {
boost_phrase: true,
index_id: 'foo',
langs: ['en'],
page_size: '10',
q: 'test',
});
// not present in search query string
assert.equal(
(bar as any)._paramsToQueryStr(searchParams.params),
'boost_phrase=true&index_id=foo&langs=en&page_size=10&q=test'
);
});
test('submitSuggestion resets facets on direct text submit', async () => {
// create search bar qnad get it
const el = await fixture(html` <searchalicious-bar></searchalicious-bar>`);
const bar = el as SearchaliciousBar;
let resetFacetsCalled = false;
const originalResetFacets = (bar as any).resetFacets.bind(bar);
(bar as any).resetFacets = (launchSearch: boolean) => {
resetFacetsCalled = true;
assert.equal(launchSearch, false);
originalResetFacets(launchSearch);
};
// mock search API
(bar as any).search = () => {};
// mimic option selection
(bar as any).selectedOption = {
value: 'categories:pastas',
label: 'categories:pastas',
id: '--direct-suggestion--categories:pastas',
input: 'categories:pastas',
};
bar.submitSuggestion(false);
assert.isTrue(
resetFacetsCalled,
'resetFacets should be called when submitting a non-suggestion query'
);
assert.equal(bar.query, 'categories:pastas');
});
});