Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/tests/search_bar.spec.ts
2313 views
1
import { AnyModel } from "@anywidget/types";
2
import { default as searchBarRender, SearchBar, SearchBarModel } from "../js/search_bar";
3
import { FakeAnyModel } from "./fake_anywidget";
4
5
import "../js/search_bar";
6
7
describe("<search-bar>", () => {
8
let searchBar: SearchBar;
9
10
async function makeSearchBar(model: AnyModel<SearchBarModel>) {
11
const container = document.createElement("div");
12
searchBarRender.render({
13
model, el: container, experimental: {
14
invoke: () => new Promise(() => [model, []]),
15
}
16
});
17
const element = container.firstElementChild as SearchBar;
18
document.body.appendChild(element);
19
await element.updateComplete;
20
return element;
21
}
22
23
beforeEach(async () => {
24
searchBar = await makeSearchBar(new FakeAnyModel<SearchBarModel>({
25
collapsed: true,
26
tab_index: 0,
27
location_model: JSON.stringify({
28
search: "",
29
results: [],
30
selected: "",
31
additional_html: "",
32
}),
33
dataset_model: JSON.stringify({
34
search: "",
35
results: [],
36
selected: "",
37
additional_html: "",
38
}),
39
}));
40
});
41
42
afterEach(() => {
43
Array.from(document.querySelectorAll("search-bar")).forEach((el) => {
44
el.remove();
45
})
46
});
47
48
it("can be instantiated.", () => {
49
expect(searchBar.shadowRoot?.querySelector("tab-panel")).toBeDefined();
50
});
51
52
it("renders and updates the name address search", async () => {
53
searchBar.locationModel = JSON.stringify({
54
search: "my city",
55
results: ["my city 1", "my city 2"],
56
selected: "my city 1",
57
additional_html: `<p class="name-address-extra">An extra message </p>`,
58
})
59
await searchBar.updateComplete;
60
expect(searchBar.locationSearch).toBeDefined();
61
expect(searchBar.locationResults).toBeDefined();
62
expect(searchBar.locationResults[0].checked).toBeTrue();
63
const results = Array.from(searchBar.locationResults).map((el) => el.value);
64
expect(results).toEqual(["my city 1", "my city 2"]);
65
expect(searchBar.shadowRoot!.querySelector(".name-address-extra")).toBeDefined();
66
expect(searchBar.shadowRoot!.querySelector(".name-address-container .reset-button")).toBeDefined();
67
68
jasmine.clock().install();
69
searchBar.locationSearch.value = "my new search";
70
searchBar.locationSearch.dispatchEvent(new KeyboardEvent('keydown', { 'key': 'Enter' }));
71
jasmine.clock().tick(500);
72
await searchBar.updateComplete;
73
expect(JSON.parse(searchBar.locationModel).search).toBe("my new search");
74
jasmine.clock().uninstall();
75
76
searchBar.locationResults[1].checked = true;
77
searchBar.locationResults[1].dispatchEvent(new Event("input"));
78
expect(JSON.parse(searchBar.locationModel).selected).toBe("my city 2");
79
});
80
81
it("renders and updates the lat-lon search", async () => {
82
searchBar.locationModel = JSON.stringify({
83
search: "40, -100",
84
results: ["my cool city"],
85
selected: "my cool city",
86
additional_html: `<p class="lat-lon-extra">An extra message </p>`,
87
})
88
await searchBar.updateComplete;
89
expect(searchBar.locationSearch).toBeDefined();
90
expect(searchBar.locationResults).toBeDefined();
91
expect(searchBar.locationResults[0].checked).toBeTrue();
92
const results = Array.from(searchBar.locationResults).map((el) => el.value);
93
expect(results).toEqual(["my cool city"]);
94
expect(searchBar.shadowRoot!.querySelector(".lat-lon-extra")).toBeDefined();
95
expect(searchBar.shadowRoot!.querySelector(".lat-lon-container .reset-button")).toBeDefined();
96
97
jasmine.clock().install();
98
searchBar.locationSearch.value = "my new search";
99
searchBar.locationSearch.dispatchEvent(new KeyboardEvent('keydown', { 'key': 'Enter' }));
100
jasmine.clock().tick(500);
101
await searchBar.updateComplete;
102
expect(JSON.parse(searchBar.locationModel).search).toBe("my new search");
103
jasmine.clock().uninstall();
104
});
105
106
it("renders and updates the dataset search", async () => {
107
searchBar.datasetModel = JSON.stringify({
108
search: "elevation",
109
results: ["dataset 1", "dataset 2"],
110
selected: "dataset 1",
111
additional_html: `<p class="dataset-extra">A cool dataset</p>`,
112
})
113
await searchBar.updateComplete;
114
expect(searchBar.datasetSearch).toBeDefined();
115
expect(searchBar.shadowRoot!.querySelector(".dataset-extra")).toBeDefined();
116
117
jasmine.clock().install();
118
searchBar.datasetSearch.value = "my new search";
119
searchBar.datasetSearch.dispatchEvent(new KeyboardEvent('keydown', { 'key': 'Enter' }));
120
jasmine.clock().tick(500);
121
await searchBar.updateComplete;
122
expect(JSON.parse(searchBar.datasetModel).search).toBe("my new search");
123
jasmine.clock().uninstall();
124
});
125
});
126