Skip to content

Commit

Permalink
Merge pull request #25 from andrewgryan/feature/missing-tests
Browse files Browse the repository at this point in the history
Add tests to cover l-control-layers
  • Loading branch information
andrewgryan authored Jul 15, 2024
2 parents fc238fa + 7c33a90 commit d73b606
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/l-control-layers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// @vitest-environment happy-dom
import { control } from "leaflet";
import { it, expect } from "vitest";
import { layerConnected } from "./events.js";
import "./index.js"


it("should raise a custom event when l-control-layers added to DOM", async () => {
// Create a <l-control-layers />
const el = document.createElement("l-control-layers")
const promise = new Promise((resolve) => {
el.addEventListener(layerConnected, (ev) => {
resolve(ev)
})
})

// Add to document object model
document.body.appendChild(el)

// Assert custom event raised
const event = await promise
expect(event.detail).toEqual({
layer: control.layers({}, {})
})
})

it("should append a base layer to l-control-layers", async () => {
// Create a <l-control-layers />
const el = document.createElement("l-control-layers")
const base = document.createElement("l-base-layers")
const marker = document.createElement("l-marker")
marker.setAttribute("lat-lng", "[0,0]")
base.appendChild(marker)
el.appendChild(base)
const promise = new Promise((resolve) => {
el.addEventListener(layerConnected, (ev) => {
resolve(ev.detail.layer)
})
})

// Add to document object model
document.body.appendChild(el)

// Assertions
const controlLayers = await promise
const expected = control.layers({}, {})
expected.addBaseLayer(marker.layer)
expect(controlLayers).toEqual(expected)
})

it("should append a overlay layer to l-control-layers", async () => {
// Create a <l-control-layers />
const el = document.createElement("l-control-layers")
const layers = document.createElement("l-overlay-layers")
const marker = document.createElement("l-marker")
marker.setAttribute("lat-lng", "[0,0]")
layers.appendChild(marker)
el.appendChild(layers)
const promise = new Promise((resolve) => {
el.addEventListener(layerConnected, (ev) => {
resolve(ev.detail.layer)
})
})

// Add to document object model
document.body.appendChild(el)

// Assertions
const controlLayers = await promise
const expected = control.layers({}, {})
expected.addOverlay(marker.layer)
expect(controlLayers).toEqual(expected)
})
3 changes: 3 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import { defineConfig } from "vite";
export default defineConfig({
test: {
includeSource: ["src/**/*.{js,ts}"],
coverage: {
exclude: ["docs"]
}
},
});

0 comments on commit d73b606

Please sign in to comment.