From 861346b701311cc736f5045e2fcc0fd168fee32f Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 5 Jun 2024 09:45:22 -0600 Subject: [PATCH] Test that onChange:prop works --- tests/lib/Map.test.jsx | 12 ++++++++---- tests/lib/View.test.jsx | 40 ++++++++++++++++++++++++++++++++++++---- tests/lib/util.js | 21 +++++++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 tests/lib/util.js diff --git a/tests/lib/Map.test.jsx b/tests/lib/Map.test.jsx index 02c08a94..756458e8 100644 --- a/tests/lib/Map.test.jsx +++ b/tests/lib/Map.test.jsx @@ -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('', () => { - it('renders a map', async () => { + it('renders a map', () => { render(
@@ -18,7 +22,7 @@ describe('', () => { 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(
diff --git a/tests/lib/View.test.jsx b/tests/lib/View.test.jsx index a7397ba8..8f302f1c 100644 --- a/tests/lib/View.test.jsx +++ b/tests/lib/View.test.jsx @@ -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('', () => { - it('adds a view to a map', async () => { + it('adds a view to a map', () => { let map; render(
@@ -25,7 +30,7 @@ describe('', () => { expect(view.getZoom()).toEqual(3); }); - it('accepts center, zoom, and rotation props', async () => { + it('accepts center, zoom, and rotation props', () => { let map; render(
@@ -41,4 +46,31 @@ describe('', () => { 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( +
+ (map = r)}> + + +
, + ); + + const view = map.getView(); + view.setCenter([10, 20]); + + const center = await onChangeCenter.called; + expect(center).toEqual([10, 20]); + }); }); diff --git a/tests/lib/util.js b/tests/lib/util.js new file mode 100644 index 00000000..b72e7a95 --- /dev/null +++ b/tests/lib/util.js @@ -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}; +}