forked from gee-community/geemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basemap_selector.spec.ts
75 lines (64 loc) · 2.71 KB
/
basemap_selector.spec.ts
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
import { AnyModel } from "@anywidget/types";
import "../js/basemap_selector";
import { default as selectorRender, BasemapSelector, BasemapSelectorModel } from "../js/basemap_selector";
import { FakeAnyModel } from "./fake_anywidget";
describe("<basemap-selector>", () => {
let selector: BasemapSelector;
async function makeSelector(model: AnyModel<BasemapSelectorModel>) {
const container = document.createElement("div");
selectorRender.render({
model, el: container, experimental: {
invoke: () => new Promise(() => [model, []]),
}
});
const element = container.firstElementChild as BasemapSelector;
document.body.appendChild(element);
await element.updateComplete;
return element;
}
beforeEach(async () => {
selector = await makeSelector(new FakeAnyModel<BasemapSelectorModel>({
basemaps: ["select", "default", "bounded"],
value: "default",
}));
});
afterEach(() => {
Array.from(document.querySelectorAll("basemap-selector")).forEach((el) => {
el.remove();
})
});
it("can be instantiated.", () => {
expect(selector.shadowRoot?.querySelector("select")?.textContent).toContain("bounded");
});
it("renders the basemap options.", () => {
const options = selector.shadowRoot?.querySelectorAll("option")!;
expect(options.length).toBe(3);
expect(options[0].textContent).toContain("select");
expect(options[1].textContent).toContain("default");
expect(options[2].textContent).toContain("bounded");
});
it("setting the value on model changes the value on select.", async () => {
selector.value = "select";
await selector.updateComplete;
expect(selector.selectElement.value).toBe("select");
});
it("sets value on model when option changes.", async () => {
const setSpy = spyOn(FakeAnyModel.prototype, "set");
const saveSpy = spyOn(FakeAnyModel.prototype, "save_changes");
selector.selectElement.value = "select";
selector.selectElement.dispatchEvent(new Event('change'));
await selector.updateComplete;
expect(setSpy).toHaveBeenCalledOnceWith("value", "select");
expect(saveSpy).toHaveBeenCalledTimes(1);
});
it("emits close event when clicked.", async () => {
const sendSpy = spyOn(FakeAnyModel.prototype, "send");
// Close button emits an event.
(selector.shadowRoot?.querySelector(".close-button") as HTMLButtonElement).click();
await selector.updateComplete;
expect(sendSpy).toHaveBeenCalledOnceWith({
type: "click",
id: "close"
});
});
});