Skip to content

Commit b110955

Browse files
authored
Improve test coverage for attribution_control.ts (#7120)
* Improve test coverage for attribution_control.ts Added comprehensive edge case tests: - Filtering non-string values from customAttribution array - Filtering whitespace-only attributions from sources - Testing _updateCompactMinimize behavior during map drag events - Verifying onRemove properly cleans up event listeners and state - Handling empty customAttribution strings - Deduplication of identical attributions from multiple sources These tests improve robustness and ensure edge cases are handled correctly. * test: remove redundant comments and split compact-show test Signed-off-by: pierreeurope <pierre.europe@pm.me> * Address review: use DOM queries instead of internal fields, split compact test * Revert unrelated CSS changes --------- Signed-off-by: pierreeurope <pierre.europe@pm.me>
1 parent 889c064 commit b110955

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/ui/control/attribution_control.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,79 @@ describe('AttributionControl test regarding the HTML elements details and summar
566566
expect(map.getContainer().querySelectorAll('.maplibregl-ctrl-attrib')[0].getAttribute('open')).toBe('');
567567
});
568568
});
569+
570+
describe('edge cases', () => {
571+
test('whitespace-only source attribution is filtered out', async () => {
572+
const attribution = new AttributionControl({});
573+
map.addControl(attribution);
574+
await map.once('load');
575+
576+
map.addSource('whitespace', {
577+
type: 'geojson',
578+
data: {type: 'FeatureCollection', features: []},
579+
attribution: ' '
580+
});
581+
map.addSource('valid', {
582+
type: 'geojson',
583+
data: {type: 'FeatureCollection', features: []},
584+
attribution: 'Valid Attribution'
585+
});
586+
587+
map.addLayer({id: 'whitespace', type: 'fill', source: 'whitespace'});
588+
map.addLayer({id: 'valid', type: 'fill', source: 'valid'});
589+
590+
await sleep(100);
591+
592+
const innerContainer = map.getContainer().querySelector('.maplibregl-ctrl-attrib-inner');
593+
expect(innerContainer.innerHTML).toBe('Valid Attribution');
594+
});
595+
596+
test('empty customAttribution string results in empty attribution', () => {
597+
map.addControl(new AttributionControl({customAttribution: ''}));
598+
599+
const container = map.getContainer();
600+
const attrib = container.querySelector('.maplibregl-ctrl-attrib-inner');
601+
expect(attrib.innerHTML).toBe('');
602+
expect(container.querySelectorAll('.maplibregl-attrib-empty')).toHaveLength(1);
603+
});
604+
605+
test('compact attribution is initially expanded', () => {
606+
Object.defineProperty(map.getCanvasContainer(), 'offsetWidth', {value: 600, configurable: true});
607+
map.addControl(new AttributionControl({
608+
compact: true,
609+
customAttribution: 'Test Attribution'
610+
}));
611+
612+
expect(map.getContainer().querySelectorAll('.maplibregl-compact-show')).toHaveLength(1);
613+
});
614+
615+
test('drag minimizes expanded compact attribution', () => {
616+
Object.defineProperty(map.getCanvasContainer(), 'offsetWidth', {value: 600, configurable: true});
617+
map.addControl(new AttributionControl({
618+
compact: true,
619+
customAttribution: 'Test Attribution'
620+
}));
621+
622+
const container = map.getContainer();
623+
const toggle = container.querySelector('.maplibregl-ctrl-attrib-button');
624+
simulate.click(toggle);
625+
simulate.click(toggle);
626+
expect(container.querySelectorAll('.maplibregl-compact-show')).toHaveLength(1);
627+
628+
map.fire('drag');
629+
expect(container.querySelectorAll('.maplibregl-compact-show')).toHaveLength(0);
630+
});
631+
632+
test('onRemove cleans up DOM', () => {
633+
const attribution = new AttributionControl({customAttribution: 'Test', compact: true});
634+
map.addControl(attribution);
635+
636+
const container = map.getContainer();
637+
expect(container.querySelectorAll('.maplibregl-ctrl-attrib')).toHaveLength(1);
638+
639+
map.removeControl(attribution);
640+
641+
expect(container.querySelectorAll('.maplibregl-ctrl-attrib')).toHaveLength(0);
642+
});
643+
});
569644
});

0 commit comments

Comments
 (0)