Skip to content

Commit

Permalink
Add tests for <map-link rel="stylesheet" media="..."> attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
prushforth committed Dec 19, 2024
1 parent af3aed6 commit cf1d61e
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 14 deletions.
22 changes: 16 additions & 6 deletions src/map-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ export class HTMLLinkElement extends HTMLElement {
case 'image':
case 'features':
case 'query':
// because we skip the attributeChangedCallback for initialization,
// respect the disabled attribute which can be set by the author prior
// to initialization
if (!this.disabled) {
this._initTemplateVars();
await this._createTemplatedLink();
Expand All @@ -296,7 +299,9 @@ export class HTMLLinkElement extends HTMLElement {
//this._createLegendLink();
break;
case 'stylesheet':
this._createStylesheetLink();
if (!this.disabled) {
this._createStylesheetLink();
}
break;
case 'alternate':
this._createAlternateLink(); // add media attribute
Expand All @@ -305,6 +310,9 @@ export class HTMLLinkElement extends HTMLElement {
// this._createLicenseLink();
break;
}
// the media attribute uses / overrides the disabled attribute to enable or
// disable the link, so at this point the #hasConnected must be true so
// that the disabled attributeChangedCallback can have its desired side effect
await this._registerMediaQuery(this.media);
// create the type of templated leaflet layer appropriate to the rel value
// image/map/features = templated(Image/Feature), tile=templatedTile,
Expand Down Expand Up @@ -361,11 +369,9 @@ export class HTMLLinkElement extends HTMLElement {
case 'image':
case 'features':
case 'query':
if (!this.disabled) {
this._initTemplateVars();
await this._createTemplatedLink();
this.getLayerEl()._validateDisabled();
}
this._initTemplateVars();
await this._createTemplatedLink();
this.getLayerEl()._validateDisabled();
break;
case 'stylesheet':
this._createStylesheetLink();
Expand All @@ -383,6 +389,8 @@ export class HTMLLinkElement extends HTMLElement {
if (mq) {
let map = this.getMapEl();
if (!map) return;
// have to wait until map has an extent i.e. is ready, because the
// matchMedia function below relies on it for map related queries
await map.whenReady();

// Remove listener from the old media query (if it exists)
Expand All @@ -398,6 +406,8 @@ export class HTMLLinkElement extends HTMLElement {
// Clean up the existing listener
this._mql.removeEventListener('change', this._changeHandler);
delete this._mql;
// unlike map-layer.disabled, map-link.disabled is an observed attribute
this.disabled = false;
}
}
_createAlternateLink(mapml) {
Expand Down
16 changes: 8 additions & 8 deletions test/e2e/data/restaurants/restaurants.mapml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<map-meta name="extent" content="top-left-easting=1509080.1964270622,top-left-northing=-169400.558801122,bottom-right-easting=1512094.3357886747,bottom-right-northing=-172702.5654051304" ></map-meta>
</map-head>
<map-body>
<map-feature id="restaurant_map-point.77" class="restaurant_map-point asian chinese">
<map-feature id="restaurant_map-point.77" class="restaurant restaurant_map-point asian chinese">
<map-featurecaption>Hung Sum Restaurant</map-featurecaption>
<map-geometry>
<map-point>
Expand Down Expand Up @@ -50,7 +50,7 @@
</table>
</map-properties>
</map-feature>
<map-feature id="restaurant_map-point.2" class="restaurant_map-point cajun">
<map-feature id="restaurant_map-point.2" class="restaurant restaurant_map-point cajun">
<map-featurecaption>Big Daddy's</map-featurecaption>
<map-geometry>
<map-point>
Expand Down Expand Up @@ -90,7 +90,7 @@
</table>
</map-properties>
</map-feature>
<map-feature id="restaurant_map-point.9" class="restaurant_map-point african">
<map-feature id="restaurant_map-point.9" class="restaurant restaurant_map-point african">
<map-featurecaption>Hareg Cafe &amp; Variety</map-featurecaption>
<map-geometry>
<map-point>
Expand Down Expand Up @@ -130,7 +130,7 @@
</table>
</map-properties>
</map-feature>
<map-feature id="restaurant_map-point.13" class="restaurant_map-point asian thai">
<map-feature id="restaurant_map-point.13" class="restaurant restaurant_map-point asian thai">
<map-featurecaption>Phucket Royal</map-featurecaption>
<map-geometry>
<map-point>
Expand Down Expand Up @@ -170,7 +170,7 @@
</table>
</map-properties>
</map-feature>
<map-feature id="restaurant_map-point.18" class="restaurant_map-point asian japanese">
<map-feature id="restaurant_map-point.18" class="restaurant restaurant_map-point asian japanese">
<map-featurecaption>Sushi 88</map-featurecaption>
<map-geometry>
<map-point>
Expand Down Expand Up @@ -242,7 +242,7 @@
</table>
</map-properties>
</map-feature>
<map-feature id="restaurant_polygon.2" class="restaurant_polygon mexican">
<map-feature id="restaurant_polygon.2" class="restaurant restaurant_polygon mexican">
<map-featurecaption>Banditos</map-featurecaption>
<map-geometry>
<map-point>
Expand Down Expand Up @@ -286,7 +286,7 @@
</table>
</map-properties>
</map-feature>
<map-feature id="restaurant_polygon.7" class="restaurant_polygon asian indian">
<map-feature id="restaurant_polygon.7" class="restaurant restaurant_polygon asian indian">
<map-featurecaption>India Palace</map-featurecaption>
<map-geometry>
<map-point>
Expand Down Expand Up @@ -338,7 +338,7 @@
</table>
</map-properties>
</map-feature>
<map-feature id="restaurant_polygon.20" class="restaurant_polygon italian">
<map-feature id="restaurant_polygon.20" class="restaurant restaurant_polygon italian">
<map-featurecaption>The Prescott</map-featurecaption>
<map-geometry>
<map-point>
Expand Down
49 changes: 49 additions & 0 deletions test/e2e/elements/map-link/map-link-stylesheet-media.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>map-link-stylesheet-media.html</title>
<script type="module" src="mapml.js"></script>
<style>
html,
body {
height: 100%;
}
* {
margin: 0;
padding: 0;
}

/* Ensure inline layer content is hidden if custom/built-in elements isn't
supported, or if javascript is disabled. This needs to be defined separately
from the above, because the `:not(:defined)` selector invalidates the entire
declaration in browsers that do not support it. */
map-layer {
display: none;
}
</style>
</head>
<body>

<mapml-viewer data-testid="viewer" projection="OSMTILE" zoom="14" lat="45.40954467619979" lon="-75.70168476290948" style="height: 500px;width:500px;">
<map-layer data-testid="restaurants" label="Restaurants" checked="">
<map-meta name="extent" content="top-left-easting=-8433179, top-left-northing=5689316, bottom-right-easting=-8420968, bottom-right-northing=5683139"></map-meta>
<map-extent units="OSMTILE" checked="">
<!-- markers should be red on load -->
<map-link rel="stylesheet" data-testid="stylesheet-link" href="red.css"></map-link>
<map-select id="restaurants" name="cusine">
<map-option value="restaurants" selected="selected">All cuisines</map-option>
<map-option value="african">African</map-option>
<map-option value="asian">Asian</map-option>
<map-option value="cajun">Cajun</map-option>
<map-option value="indian">Indian</map-option>
<map-option value="italian">Italian</map-option>
<map-option value="mexican">Mexican</map-option>
</map-select>
<map-link tref="restaurants/{cusine}.mapml" rel="features"></map-link>
</map-extent>
</map-layer>
</mapml-viewer>
</body>
</html>
69 changes: 69 additions & 0 deletions test/e2e/elements/map-link/map-link-stylesheet-media.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { test, expect, chromium } from '@playwright/test';

test.describe('map-layer media attribute', () => {
let page;
let context;
let viewer;
let stylesheetLink;
test.beforeAll(async function () {
context = await chromium.launchPersistentContext('');
page =
context.pages().find((page) => page.url() === 'about:blank') ||
(await context.newPage());
await page.goto('map-link-stylesheet-media.html');
await page.waitForTimeout(1000);
viewer = page.getByTestId('viewer');
stylesheetLink = page.locator('map-link[rel=stylesheet][href="red.css"]');
});
test(`when a map-link rel=stylesheet disables due to a media query, the styles\
should be removed`,async ()=>{
// map starts off at
await expect(viewer).toHaveScreenshot('red_styled_markers.png', {
maxDiffPixels: 20
});
await stylesheetLink.evaluate((l) => l.media = '(14 < map-zoom <= 18)');
await page.waitForTimeout(500);
await expect(viewer).toHaveScreenshot('default_styled_markers.png', {
maxDiffPixels: 20
});
});
test(`when a map-link rel=stylesheet enables due to a mq being removed, the \
styles should apply`, async ()=>{
await stylesheetLink.evaluate((l) => l.removeAttribute('media'));
await expect(viewer).toHaveScreenshot('red_styled_markers.png', {
maxDiffPixels: 20
});
});

test(`when a map-link rel=stylesheet enables due to disabled attribute removed\
ensure styles change`, async ()=>{
await stylesheetLink.evaluate((l) => l.disabled = true);
await page.waitForTimeout(500);
await expect(viewer).toHaveScreenshot('default_styled_markers.png', {
maxDiffPixels: 20
});
await stylesheetLink.evaluate((l) => l.disabled = false);
await page.waitForTimeout(500);
await expect(viewer).toHaveScreenshot('red_styled_markers.png', {
maxDiffPixels: 20
});
});
test(`when a map-link rel=stylesheet does not enable due to disabled attribute \
removed while a non-matching media query is present, ensure that style does not \
change`, async ()=>{
await stylesheetLink.evaluate((l) => l.disabled = true);
await page.waitForTimeout(500);
await expect(viewer).toHaveScreenshot('default_styled_markers.png', {
maxDiffPixels: 20
});
// non-matching query (map z=14)
await stylesheetLink.evaluate((l) => l.media = '(14 < map-zoom <= 18)');
await page.waitForTimeout(500);
// disabled overrides the media query because they compete to change it
await stylesheetLink.evaluate((l) => l.disabled = false);
await page.waitForTimeout(500);
await expect(viewer).toHaveScreenshot('red_styled_markers.png', {
maxDiffPixels: 20
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/e2e/elements/map-link/red.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.restaurant {
fill: red;
stroke: red;
}

0 comments on commit cf1d61e

Please sign in to comment.