Commit 2f9ee9cd authored by Michael Underwood's avatar Michael Underwood
Browse files

Add basic tests for LControlAttribution

parent f8836dcf
Loading
Loading
Loading
Loading
+4 −0
+33 −18
Original line number Diff line number Diff line
@@ -3,35 +3,50 @@ import type { InjectionKey } from "vue";

import type { IControlDefinition, ILayerDefinition } from "./interfaces";

export const UseGlobalLeafletInjection = Symbol() as InjectionKey<boolean>;
export const AddLayerInjection = Symbol() as InjectionKey<
export const UseGlobalLeafletInjection = Symbol(
  "useGlobalLeaflet"
) as InjectionKey<boolean>;

export const AddLayerInjection = Symbol("addLayer") as InjectionKey<
  (layer: ILayerDefinition) => void
>;
export const RemoveLayerInjection = Symbol() as InjectionKey<

export const RemoveLayerInjection = Symbol("removeLayer") as InjectionKey<
  (layer: ILayerDefinition) => void
>;
export const RegisterControlInjection = Symbol() as InjectionKey<
  (control: IControlDefinition) => void
>;
export const RegisterLayerControlInjection = Symbol() as InjectionKey<
  (control: IControlDefinition<L.Control.Layers>) => void
>;

export const CanSetParentHtmlInjection = Symbol() as InjectionKey<
  () => boolean
>;
export const SetParentHtmlInjection = Symbol() as InjectionKey<
export const RegisterControlInjection = Symbol(
  "registerControl"
) as InjectionKey<(control: IControlDefinition) => void>;

export const RegisterLayerControlInjection = Symbol(
  "registerLayerControl"
) as InjectionKey<(control: IControlDefinition<L.Control.Layers>) => void>;

export const CanSetParentHtmlInjection = Symbol(
  "canSetParentHtml"
) as InjectionKey<() => boolean>;

export const SetParentHtmlInjection = Symbol("setParentHtml") as InjectionKey<
  (html: string) => void
>;
export const SetIconInjection = Symbol() as InjectionKey<

export const SetIconInjection = Symbol("setIcon") as InjectionKey<
  (newIcon: L.DivIcon | L.Icon | undefined) => L.Marker<any> | undefined
>;

export const BindPopupInjection = Symbol() as InjectionKey<
export const BindPopupInjection = Symbol("bindPopup") as InjectionKey<
  (leafletObject: L.Layer | undefined) => void
>;
export const BindTooltipInjection = Symbol() as InjectionKey<

export const BindTooltipInjection = Symbol("bindTooltip") as InjectionKey<
  (leafletObject: L.Layer | undefined) => void
>;
export const UnbindPopupInjection = Symbol() as InjectionKey<() => void>;
export const UnbindTooltipInjection = Symbol() as InjectionKey<() => void>;

export const UnbindPopupInjection = Symbol("unbindPopup") as InjectionKey<
  () => void
>;

export const UnbindTooltipInjection = Symbol("unbindTooltip") as InjectionKey<
  () => void
>;
+5 −3
Original line number Diff line number Diff line
@@ -137,10 +137,12 @@ export const WINDOW_OR_GLOBAL =
  (typeof global === "object" && global.global === global && global) ||
  globalThis;

export const assertInject = <T>(key: InjectionKey<T> | string) => {
export const assertInject = <T>(key: InjectionKey<T>) => {
  const value = inject<T>(key);
  if (!value) {
    throw new Error(`Attempt to inject ${key} before it was provided.`);
  if (value === undefined) {
    throw new Error(
      `Attempt to inject ${key.description} before it was provided.`
    );
  }

  return value;
+86 −0
Original line number Diff line number Diff line
import { flushPromises, shallowMount } from "@vue/test-utils";
import "leaflet";
import { beforeEach, describe, expect, it, vi } from "vitest";

import LControlAttribution from "@src/components/LControlAttribution.vue";
import {
  RegisterControlInjection,
  UseGlobalLeafletInjection,
} from "@src/types/injectionKeys";

describe("LControlAttribution", () => {
  const mockRegisterControl = vi.fn();
  const getWrapper = async () => {
    const wrapper = shallowMount(LControlAttribution, {
      propsData: {
        position: "topright",
        prefix: "Hello there",
      },
      global: {
        provide: {
          [UseGlobalLeafletInjection as symbol]: true,
          [RegisterControlInjection as symbol]: mockRegisterControl,
        },
      },
    });
    await flushPromises();

    return wrapper;
  };

  beforeEach(() => {
    mockRegisterControl.mockReset();
  });

  it("emits a ready event", async () => {
    const wrapper = await getWrapper();

    expect(wrapper.emitted("ready")).to.have.length(1);
  });

  it("creates a Leaflet object with expected properties", async () => {
    const wrapper = await getWrapper();

    expect(wrapper.vm.leafletObject).to.exist;
    const leafletObject = wrapper.vm.leafletObject!;
    expect(leafletObject!.options.prefix).to.equal("Hello there");
    expect(leafletObject?.options.position).to.equal("topright");
  });

  it("registers its Leaflet object", async () => {
    const wrapper = await getWrapper();

    expect(mockRegisterControl).toHaveBeenCalledTimes(1);
    expect(mockRegisterControl).toHaveBeenCalledWith({
      leafletObject: wrapper.vm.leafletObject,
    });
  });

  it("sets the prefix value", async () => {
    const wrapper = await getWrapper();
    expect(wrapper.vm.leafletObject?.options.prefix);

    wrapper.setProps({ prefix: "new prefix" });

    await flushPromises();
    expect(wrapper.vm.leafletObject?.options.prefix).to.equal("new prefix");
  });

  it("sets a new position value", async () => {
    const wrapper = await getWrapper();

    wrapper.setProps({ position: "bottomleft" });

    await flushPromises();
    expect(wrapper.vm.leafletObject?.options.position).to.equal("bottomleft");
  });

  it("removes itself from the map on unmount", async () => {
    const wrapper = await getWrapper();
    const removeSpy = vi.spyOn(wrapper.vm.leafletObject!, "remove");

    wrapper.unmount();

    expect(removeSpy).toBeCalledTimes(1);
  });
});
+2 −1
Original line number Diff line number Diff line
import { describe, it, expect } from "vitest";
import { describe, expect, it } from "vitest";

import { capitalizeFirstLetter } from "@src/utils";

describe("capitalizeFirstLetter", () => {
Loading