@panoramax/web-viewer
Version:
Panoramax web viewer for geolocated pictures
111 lines (98 loc) • 2.81 kB
JavaScript
import SearchBar from "../../../src/components/ui/SearchBar";
describe("_onIconClick", () => {
it("resets on error", () => {
const sb = new SearchBar();
sb.reset = jest.fn();
sb._icon = "warn";
sb._onIconClick();
expect(sb.reset.mock.calls).toMatchSnapshot();
});
it("resets on clear", () => {
const sb = new SearchBar();
sb.reset = jest.fn();
sb._icon = "empty";
sb._onIconClick();
expect(sb.reset.mock.calls).toMatchSnapshot();
});
it("reduces if reduceable & extended", () => {
const sb = new SearchBar();
sb.reduceable = true;
sb.reduced = false;
sb._onIconClick();
expect(sb.reduced).toBe(true);
});
it("expands if reduceable & reduced", () => {
const sb = new SearchBar();
sb.reduceable = true;
sb.reduced = true;
sb._onIconClick();
expect(sb.reduced).toBe(false);
});
});
describe("_onResultClick", () => {
it("works on classic", () => {
const sb = new SearchBar();
sb.dispatchEvent = jest.fn();
sb._onResultClick({title: "res1", subtitle: "sub1", data: "coucou"});
expect(sb.dispatchEvent.mock.calls).toMatchSnapshot();
expect(sb.value).toEqual("res1");
expect(sb._icon).toEqual("empty");
expect(sb._results).toBe(null);
});
it("works on reduced", () => {
const sb = new SearchBar();
sb.dispatchEvent = jest.fn();
sb.reduceable = true;
sb._onResultClick({title: "res1", subtitle: "sub1", data: "coucou"});
expect(sb.dispatchEvent.mock.calls).toMatchSnapshot();
expect(sb.value).toEqual("");
expect(sb._icon).toEqual("search");
expect(sb._results).toBe(null);
expect(sb.reduced).toBe(true);
});
it("resets on null value", () => {
const sb = new SearchBar();
sb.dispatchEvent = jest.fn();
sb._onResultClick(null);
expect(sb.dispatchEvent.mock.calls).toMatchSnapshot();
expect(sb.value).toEqual("");
expect(sb._icon).toEqual("search");
expect(sb._results).toBe(null);
});
});
describe("_search", () => {
it("skips if value is empty", () => {
const sb = new SearchBar();
sb.reset = jest.fn();
sb._search();
expect(sb.reset.mock.calls).toMatchSnapshot();
});
it("skips if no searcher fct", () => {
global.console = { warn: jest.fn() };
const sb = new SearchBar();
sb.value = "test";
sb._search();
expect(global.console.warn.mock.calls).toMatchSnapshot();
});
it("works with actual data", () => {
const sb = new SearchBar();
return new Promise(resolve => {
sb.searcher = value => {
expect(value).toEqual("my search");
expect(sb._icon).toEqual("loading");
expect(sb._results).toBe(null);
resolve();
};
sb.value = "my search";
sb._search();
});
});
});
describe("reset", () => {
it("works", () => {
const sb = new SearchBar();
sb._onResultClick = jest.fn();
sb.reset();
expect(sb._onResultClick.mock.calls).toMatchSnapshot();
});
});