Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that onChange:prop works #540

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions tests/lib/Map.test.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import Map from '../../lib/Map.js';
import OLMap from 'ol/Map.js';
import React from 'react';
import {describe, expect, it} from 'vitest';
import {render, screen} from '@testing-library/react';
import {afterEach, describe, expect, it} from 'vitest';
import {cleanup, render, screen} from '@testing-library/react';

afterEach(() => {
cleanup();
});

// shared style for map target
const style = {width: 512, height: 256};

describe('<Map>', () => {
it('renders a map', async () => {
it('renders a map', () => {
render(
<div data-testid="test" style={style}>
<Map />
Expand All @@ -18,7 +22,7 @@ describe('<Map>', () => {
expect(screen.getByTestId('test').childNodes.length).toBe(1);
});

it('accepts a ref for accessing the map', async () => {
it('accepts a ref for accessing the map', () => {
let mapRef;
render(
<div style={style}>
Expand Down
40 changes: 36 additions & 4 deletions tests/lib/View.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import Map from '../../lib/Map.js';
import OLView from 'ol/View.js';
import React from 'react';
import View from '../../lib/View.js';
import {describe, expect, it} from 'vitest';
import {render} from '@testing-library/react';
import {afterEach, describe, expect, it} from 'vitest';
import {callback} from './util.js';
import {cleanup, render} from '@testing-library/react';

afterEach(() => {
cleanup();
});

// shared style for map target
const style = {width: 512, height: 256};

describe('<View>', () => {
it('adds a view to a map', async () => {
it('adds a view to a map', () => {
let map;
render(
<div style={style}>
Expand All @@ -25,7 +30,7 @@ describe('<View>', () => {
expect(view.getZoom()).toEqual(3);
});

it('accepts center, zoom, and rotation props', async () => {
it('accepts center, zoom, and rotation props', () => {
let map;
render(
<div style={style}>
Expand All @@ -41,4 +46,31 @@ describe('<View>', () => {
expect(view.getZoom()).toEqual(3);
expect(view.getRotation()).toEqual(4);
});

it('accepts a change listener', async () => {
const onChangeCenter = callback(event => {
const view = event.target;
return view.getCenter();
});

let map;
render(
<div style={style}>
<Map ref={r => (map = r)}>
<View
center={[1, 2]}
zoom={3}
rotation={4}
onChange:center={onChangeCenter.handler}
/>
</Map>
</div>,
);

const view = map.getView();
view.setCenter([10, 20]);

const center = await onChangeCenter.called;
expect(center).toEqual([10, 20]);
});
});
21 changes: 21 additions & 0 deletions tests/lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function callback(func) {
let onComplete;
let onError;

function handler(...args) {
let result;
try {
result = func(...args);
} catch (error) {
onError(error);
}
onComplete(result);
}

const called = new Promise((resolve, reject) => {
onComplete = resolve;
onError = reject;
});

return {handler, called};
}