mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(map): a GL map follows its box, not only the window, so closing the plan pane on a tablet leaves no black band (#3023)
Mapbox GL measures its container on window resizes only. In the app on a tablet the trip's plan pane closes beside the map without one: the box grew to the full width while the canvas kept its old size, and the rest stayed black. Every GL map built through buildGlMap now watches its container and resizes once per frame while it changes; the observer goes with the map. Closes #3023
This commit is contained in:
@@ -105,6 +105,88 @@ describe('buildGlMap', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('followContainerSize', () => {
|
||||
type Observed = { callback: ResizeObserverCallback; target: Element | null; disconnected: boolean };
|
||||
const observers: Observed[] = [];
|
||||
|
||||
function fakeMap() {
|
||||
let onRemove: (() => void) | undefined;
|
||||
const container = document.createElement('div');
|
||||
return {
|
||||
map: {
|
||||
getContainer: () => container,
|
||||
resize: vi.fn(),
|
||||
once: (_type: 'remove', listener: () => void) => {
|
||||
onRemove = listener;
|
||||
},
|
||||
},
|
||||
container,
|
||||
remove: () => onRemove?.(),
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
observers.length = 0;
|
||||
vi.useFakeTimers();
|
||||
vi.stubGlobal(
|
||||
'ResizeObserver',
|
||||
class {
|
||||
private readonly entry: Observed;
|
||||
constructor(callback: ResizeObserverCallback) {
|
||||
this.entry = { callback, target: null, disconnected: false };
|
||||
observers.push(this.entry);
|
||||
}
|
||||
observe(target: Element) {
|
||||
this.entry.target = target;
|
||||
}
|
||||
disconnect() {
|
||||
this.entry.disconnected = true;
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
const fire = () => observers[0].callback([], {} as ResizeObserver);
|
||||
|
||||
it('FE-MAP-GLSUP-020: resizes the canvas when its box changes size without a window resize (#3023)', () => {
|
||||
const { map, container } = fakeMap();
|
||||
expect(buildGlMap(() => map)).toBe(map);
|
||||
expect(observers[0].target).toBe(container);
|
||||
|
||||
fire();
|
||||
fire();
|
||||
fire();
|
||||
expect(map.resize).not.toHaveBeenCalled();
|
||||
vi.advanceTimersToNextFrame();
|
||||
expect(map.resize).toHaveBeenCalledTimes(1);
|
||||
|
||||
fire();
|
||||
vi.advanceTimersToNextFrame();
|
||||
expect(map.resize).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('FE-MAP-GLSUP-021: stops watching once the map is removed, a pending resize included', () => {
|
||||
const { map, remove } = fakeMap();
|
||||
buildGlMap(() => map);
|
||||
|
||||
fire();
|
||||
remove();
|
||||
vi.advanceTimersToNextFrame();
|
||||
expect(observers[0].disconnected).toBe(true);
|
||||
expect(map.resize).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-MAP-GLSUP-022: leaves a map without a container alone', () => {
|
||||
expect(buildGlMap(() => ({ remove: vi.fn() }))).toEqual({ remove: expect.any(Function) });
|
||||
expect(observers).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('useGlRenderer', () => {
|
||||
it('FE-MAP-GLSUP-010: asks the canvas for webgl2 only', () => {
|
||||
const getContext = browserWithWebGL2(false);
|
||||
|
||||
Reference in New Issue
Block a user