Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Sep 12, 2023
1 parent 451b4fc commit fc05216
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .ocularrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export default {
'react-map-gl/test': resolve('./test'),
'react-map-gl': resolve('./src')
},
nodeAliases: {
'react-dom': resolve('./test/src/utils/react-dom-mock.js')
},

browserTest: {
server: {wait: 5000}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"README.md"
],
"scripts": {
"typecheck": "tsc -p tsconfig.esm.json --noEmit",
"typecheck": "tsc -p tsconfig.build.json --noEmit",
"bootstrap": "PUPPETEER_SKIP_DOWNLOAD=true yarn && ocular-bootstrap",
"build": "ocular-clean && ocular-build",
"lint": "ocular-lint",
Expand Down
4 changes: 2 additions & 2 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {JSDOM} = require('jsdom');
import {JSDOM} from 'jsdom';

const dom = new JSDOM(`<!DOCTYPE html><div id="map"></div>`);
/* global global */
global.document = dom.window.document;

require('./src');
import './src';
24 changes: 9 additions & 15 deletions test/src/components/map.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,13 @@ test('Map#controlled#no-update', async t => {
});
});

test('Map#controlled#mirrow-back', async t => {
t.plan(5);
test('Map#controlled#mirror-back', async t => {
t.plan(6);

const mapRef = {current: null};
let lastLat;
function onRender(e) {
function onRender(viewState) {
const {lat} = mapRef.current.getCenter();
t.is(lat, lastLat, `latitude should match state: ${lat}`);
t.is(lat, viewState.latitude, `latitude should match state: ${lat}`);
}

function App() {
Expand All @@ -124,8 +123,6 @@ test('Map#controlled#mirrow-back', async t => {
zoom: 4
});

lastLat = viewState.latitude;

return (
<Map
ref={mapRef}
Expand All @@ -135,7 +132,7 @@ test('Map#controlled#mirrow-back', async t => {
e.target.easeTo({center: [-122, 38], zoom: 14});
}}
onMove={e => setViewState(e.viewState)}
onRender={onRender}
onRender={onRender.bind(null, viewState)}
/>
);
}
Expand All @@ -146,13 +143,12 @@ test('Map#controlled#mirrow-back', async t => {
});

test('Map#controlled#delayed-update', async t => {
t.plan(6);
t.plan(7);

const mapRef = {current: null};
let lastLat;
function onRender(e) {
function onRender(viewState) {
const {lat} = mapRef.current.getCenter();
t.is(lat, lastLat, `latitude should match state: ${lat}`);
t.is(lat, viewState.latitude, `latitude should match state: ${lat}`);
}

function App() {
Expand All @@ -162,8 +158,6 @@ test('Map#controlled#delayed-update', async t => {
zoom: 4
});

lastLat = viewState.latitude;

return (
<Map
ref={mapRef}
Expand All @@ -173,7 +167,7 @@ test('Map#controlled#delayed-update', async t => {
e.target.easeTo({center: [-122, 38], zoom: 14});
}}
onMove={e => setTimeout(() => setViewState(e.viewState))}
onRender={onRender}
onRender={onRender.bind(null, viewState)}
/>
);
}
Expand Down
5 changes: 1 addition & 4 deletions test/src/components/marker.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import * as React from 'react';
import {create, act} from 'react-test-renderer';
import test from 'tape-promise/tape';

import {createPortalMock, waitForMapLoad} from '../utils/test-utils';
import {waitForMapLoad} from '../utils/test-utils';
import mapboxMock from '../utils/mapbox-gl-mock';

test('Marker', async t => {
const restoreMock = createPortalMock();
const mapRef = {current: null};

let map;
Expand Down Expand Up @@ -93,7 +92,5 @@ test('Marker', async t => {

map.unmount();

restoreMock();

t.end();
});
5 changes: 1 addition & 4 deletions test/src/components/popup.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import * as React from 'react';
import {create, act} from 'react-test-renderer';
import test from 'tape-promise/tape';

import {createPortalMock, waitForMapLoad} from '../utils/test-utils';
import {waitForMapLoad} from '../utils/test-utils';
import mapboxMock from '../utils/mapbox-gl-mock';

test('Popup', async t => {
const restoreMock = createPortalMock();
const mapRef = {current: null};

let map;
Expand Down Expand Up @@ -72,7 +71,5 @@ test('Popup', async t => {

t.is(popup.options.className, 'classA', 'className is updated');

restoreMock();

t.end();
});
5 changes: 5 additions & 0 deletions test/src/utils/react-dom-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from 'react';

export function createPortal(element, container) {
return <div>{element}</div>;
}
13 changes: 0 additions & 13 deletions test/src/utils/test-utils.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as React from 'react';

/* global setTimeout */
export function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
Expand All @@ -17,14 +15,3 @@ export function waitForMapLoad(mapRef) {
check();
});
}

export function createPortalMock() {
const reactDom = require('react-dom');
const createPortal = reactDom.createPortal;
reactDom.createPortal = function mockCreatePortal(content, container) {
return <div>{content}</div>;
};
return () => {
reactDom.createPortal = createPortal;
};
}
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2018",
"target": "es2020",
"jsx": "react",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"target": "es2020",
"module": "es2020",
"jsx": "react",
"allowJs": true,
"allowSyntheticDefaultImports": true,
Expand Down

0 comments on commit fc05216

Please sign in to comment.