Skip to content

Commit

Permalink
Resolved PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ronitjadhav committed Dec 17, 2024
1 parent 8df6af9 commit 01b2a89
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
15 changes: 4 additions & 11 deletions packages/legend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,10 @@ createLegendFromLayer(layer).then((legendDiv) => {
});
```

## API Documentation
## Documentation

### `createLegendFromLayer(layer: Layer): Promise<HTMLDivElement>`
For more detailed API documentation, see the [documentation website](https://camptocamp.github.io/geospatial-sdk/docs/).

Creates a legend from a layer.
## Examples

#### Parameters

- `layer: (MapContextLayer)`: The layer to create the legend from.
- `options: (LegendOptions, optional)`: The options to create the legend.

#### Returns

- `Promise<HTMLElement | false>`: A promise that resolves to the legend element or `false` if the legend could not be created.
For examples and demos, see the [examples website](https://camptocamp.github.io/geospatial-sdk/).
12 changes: 6 additions & 6 deletions packages/legend/lib/create-legend/from-layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,28 @@ describe("createLegendFromLayer", () => {
);
});

it("returns false for invalid layer type", async () => {
it("returns null for invalid layer type", async () => {
const invalidLayer = { ...baseWmsLayer, type: "invalid" as any };

const result = await createLegendFromLayer(invalidLayer);

expect(result).toBe(false);
expect(result).toBe(null);
});

it("returns false for layer without URL", async () => {
it("returns null for layer without URL", async () => {
const layerWithoutUrl = { ...baseWmsLayer, url: "" };

const result = await createLegendFromLayer(layerWithoutUrl);

expect(result).toBe(false);
expect(result).toBe(null);
});

it("returns false for layer without name", async () => {
it("returns null for layer without name", async () => {
const layerWithoutName = { ...baseWmsLayer, name: "" };

const result = await createLegendFromLayer(layerWithoutName);

expect(result).toBe(false);
expect(result).toBe(null);
});

it("handles image load error", async () => {
Expand Down
13 changes: 6 additions & 7 deletions packages/legend/lib/create-legend/from-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,23 @@ async function createWmtsLegendUrl(
}

/**
* Create a legend from a layer
* Creates a legend from a layer.
*
*
* @param layer - The MapContextLayer to create a legend from
* @param options - Optional configuration for legend generation
* @returns The legend as a DOM element or false if the legend could not be created
* @param {MapContextLayer} layer - The layer to create the legend from.
* @param {LegendOptions} [options] - The options to create the legend.
* @returns {Promise<HTMLElement | null>} A promise that resolves to the legend element or `null` if the legend could not be created.
*/
export async function createLegendFromLayer(
layer: MapContextLayer,
options: LegendOptions = {},
): Promise<HTMLElement | false> {
): Promise<HTMLElement | null> {
if (
(layer.type !== "wms" && layer.type !== "wmts") ||
!layer.url ||
!layer.name
) {
console.error("Invalid layer for legend creation");
return false;
return null;
}

// Create a container for the legend
Expand Down

0 comments on commit 01b2a89

Please sign in to comment.