Skip to content

Commit

Permalink
Use callback-style test, change compatibility to be a pure callback
Browse files Browse the repository at this point in the history
  • Loading branch information
keichan34 committed Oct 28, 2024
1 parent e6e66ce commit 6b88ae4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 124 deletions.
5 changes: 4 additions & 1 deletion docs/embed.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
/**
* MapLibre GL JS
* @license 3-Clause BSD. Full text of license: https://github.com/maplibre/maplibre-gl-js/blob/v4.4.1/LICENSE.txt
*/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"homepage": "https://github.com/geolonia/embed#readme",
"devDependencies": {
"@geolonia/eslint-config": "latest",
"@types/jsdom": "^21.1.7",
"@types/mocha": "^10.0.1",
"babel-loader": "^9.1.0",
"css-loader": "^6.7.2",
Expand All @@ -38,7 +39,6 @@
"node-fetch": "2",
"prettier-eslint": "^15.0.0",
"prettier-eslint-cli": "^7.1.0",
"sinon": "^19.0.2",
"style-loader": "^3.3.1",
"svg-inline-loader": "^0.8.2",
"ts-loader": "^9.4.3",
Expand Down
5 changes: 3 additions & 2 deletions src/lib/geolonia-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ export default class GeoloniaMap extends maplibregl.Map {
* @param url
* @param callback
*/
loadImage(url: string, callback?: GetImageCallback): Promise<GetResourceResponse<HTMLImageElement | ImageBitmap>> {
loadImage(url: string, callback: GetImageCallback): void;
loadImage(url: string): Promise<GetResourceResponse<HTMLImageElement | ImageBitmap>>;
loadImage(url: string, callback?: GetImageCallback): Promise<GetResourceResponse<HTMLImageElement | ImageBitmap>> | void {

const promise = super.loadImage(url);

Expand All @@ -336,5 +338,4 @@ export default class GeoloniaMap extends maplibregl.Map {
return promise;
}
}

}
2 changes: 2 additions & 0 deletions src/lib/parse-atts.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */

import parseAtts from './parse-atts';
import assert from 'assert';
import { JSDOM } from 'jsdom';
Expand Down
43 changes: 19 additions & 24 deletions src/lib/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
/* eslint-disable @typescript-eslint/ban-ts-comment */

import assert from 'assert';
import { JSDOM } from 'jsdom';
import sinon from 'sinon';
import { getContainer, getLang, getOptions, getStyle, handleMarkerOptions, isDomElement, isURL, parseControlOption, parseSimpleVector, sanitizeDescription, loadImageCompatibility } from './util';

const base = 'https://base.example.com/parent/';
Expand Down Expand Up @@ -63,6 +62,7 @@ describe('Tests for util.js', () => {
<div id="test-element"></div>
</body></html>`);

// @ts-ignore
global.window = dom.window;
global.document = dom.window.document;

Expand Down Expand Up @@ -90,6 +90,7 @@ describe('Tests for util.js', () => {
<div id="test-element"></div>
</body></html>`);

// @ts-ignore
global.window = dom.window;
global.document = dom.window.document;

Expand Down Expand Up @@ -239,8 +240,7 @@ describe('Tests for util.js', () => {


describe('loadImageCompatibility', () => {

it('should call the callback with response data when the promise resolves', async () => {
it('should call the callback with response data when the promise resolves', (done) => {
// モックされた成功した promise
const mockResponse = {
data: new Image(),
Expand All @@ -249,32 +249,27 @@ describe('loadImageCompatibility', () => {
};
const promise = Promise.resolve(mockResponse);

// コールバックのモック
const callback = sinon.spy();

await loadImageCompatibility(promise, callback);

// コールバックのデータが期待したものかを確認
assert.deepEqual(callback.firstCall.args[1], mockResponse.data);
assert.deepEqual(callback.firstCall.args[2], {
cacheControl: mockResponse.cacheControl,
expires: mockResponse.expires,
loadImageCompatibility(promise, (error, data, expiry) => {
assert.equal(error, null);
assert.deepEqual(data, mockResponse.data);
assert.deepEqual(expiry, {
cacheControl: mockResponse.cacheControl,
expires: mockResponse.expires,
});
done();
});
});

it('should call the callback with error when the promise rejects', async () => {
it('should call the callback with error when the promise rejects', (done) => {
// モックされた失敗した promise
const mockError = new Error('Failed to load image');
const promise = Promise.reject(mockError);

// コールバックのモック
const callback = sinon.spy();

await loadImageCompatibility(promise, callback);

// コールバックのエラーが期待したものかを確認
assert.deepEqual(callback.firstCall.args[0], mockError);
assert.deepEqual(callback.firstCall.args[1], undefined);
loadImageCompatibility(promise, (error, data, expiry) => {
assert.deepEqual(error, mockError);
assert.strictEqual(data, undefined);
assert.strictEqual(expiry, undefined);
done();
});
});

});
36 changes: 23 additions & 13 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import { keyring } from './keyring';
import type { GetResourceResponse, MapOptions, MarkerOptions, ExpiryData } from 'maplibre-gl';

// GetImageCallback extracted from maplibre-gl v3.6.2 ( https://github.com/maplibre/maplibre-gl-js/releases/tag/v3.6.2 )
export type GetImageCallback = (error?: Error | null, image?: HTMLImageElement | ImageBitmap | null, expiry?: ExpiryData | null) => void;

/**
*
* @param {string} str target URL string
* @return {string|false} Resolved URL or false if not resolved
*/
export function isURL(str) {
export function isURL(str: string): string | false {
if (str.match(/^https?:\/\//)) {
return str;
} else if (str.match(/^\//) || str.match(/^\.\.?/)) {
Expand Down Expand Up @@ -302,18 +303,27 @@ export const sanitizeDescription = async (description) => {

export const random = (max: number): number => Math.floor(Math.random() * max);

export const loadImageCompatibility = async (
// This function is used to provide backward compatibility with callback invocations,
// so we ignore ESLint rules for this function.
// Note: this cannot be a generic promiseToCallback function because the callback is not
// the traditional (error, result) style callback.
export function loadImageCompatibility(
promise: Promise<GetResourceResponse<HTMLImageElement | ImageBitmap>>,
callback: GetImageCallback,
): Promise<void> => {
try {
const response = await promise;
callback(null, response.data, {
cacheControl: response.cacheControl,
expires: response.expires,
): void {
promise
// eslint-disable-next-line promise/prefer-await-to-then
.then((response) => {
// eslint-disable-next-line promise/no-callback-in-promise
callback(null, response.data, {
cacheControl: response.cacheControl,
expires: response.expires,
});
return;
})
// eslint-disable-next-line promise/prefer-await-to-then
.catch((error) => {
// eslint-disable-next-line promise/no-callback-in-promise
callback(error);
});
} catch (error) {
callback(error);
}
};

}
106 changes: 23 additions & 83 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -311,34 +311,6 @@
typescript "^4.5.4"
vue-eslint-parser "^8.0.1"

"@sinonjs/commons@^3.0.1":
version "3.0.1"
resolved "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.1.tgz"
integrity sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==
dependencies:
type-detect "4.0.8"

"@sinonjs/fake-timers@^13.0.1", "@sinonjs/fake-timers@^13.0.2":
version "13.0.2"
resolved "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-13.0.2.tgz"
integrity sha512-4Bb+oqXZTSTZ1q27Izly9lv8B9dlV61CROxPiVtywwzv5SnytJqhvYe6FclHYuXml4cd1VHPo1zd5PmTeJozvA==
dependencies:
"@sinonjs/commons" "^3.0.1"

"@sinonjs/samsam@^8.0.1":
version "8.0.2"
resolved "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-8.0.2.tgz"
integrity sha512-v46t/fwnhejRSFTGqbpn9u+LQ9xJDse10gNnPgAcxgdoCDMXj/G2asWAC/8Qs+BAZDicX+MNZouXT1A7c83kVw==
dependencies:
"@sinonjs/commons" "^3.0.1"
lodash.get "^4.4.2"
type-detect "^4.1.0"

"@sinonjs/text-encoding@^0.7.3":
version "0.7.3"
resolved "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.3.tgz"
integrity sha512-DE427ROAphMQzU4ENbliGYrBSYPXF+TtLg9S8vzeA+OF4ZKzoDdzfL8sxuMUGS/lgRhM6j1URSk9ghf7Xo1tyA==

"@tootallnate/once@2":
version "2.0.0"
resolved "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz"
Expand Down Expand Up @@ -487,6 +459,15 @@
dependencies:
"@types/node" "*"

"@types/jsdom@^21.1.7":
version "21.1.7"
resolved "https://registry.yarnpkg.com/@types/jsdom/-/jsdom-21.1.7.tgz#9edcb09e0b07ce876e7833922d3274149c898cfa"
integrity sha512-yOriVnggzrnQ3a9OKOCxaVuSug3w3/SbOj5i7VwXWZEyUNl3bLF9V3MfxGbZKuwqJOQyRfqXyROBB1CoZLFWzA==
dependencies:
"@types/node" "*"
"@types/tough-cookie" "*"
parse5 "^7.0.0"

"@types/json-schema@*", "@types/json-schema@^7.0.8", "@types/json-schema@^7.0.9":
version "7.0.12"
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz"
Expand Down Expand Up @@ -611,6 +592,11 @@
dependencies:
"@types/geojson" "*"

"@types/tough-cookie@*":
version "4.0.5"
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.5.tgz#cb6e2a691b70cb177c6e3ae9c1d2e8b2ea8cd304"
integrity sha512-/Ad8+nIOV7Rl++6f1BdKxFSMgmoqEoYbHRpPcx3JEfv8VRsQe9Z4mCXeJBzxs7mbHY/XOZZuXlRNfhpVPbs6ZA==

"@types/ws@^8.5.5":
version "8.5.5"
resolved "https://registry.npmjs.org/@types/ws/-/ws-8.5.5.tgz"
Expand Down Expand Up @@ -1637,11 +1623,6 @@ diff@^4.0.1:
resolved "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

diff@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/diff/-/diff-7.0.0.tgz"
integrity sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==

dir-glob@^3.0.1:
version "3.0.1"
resolved "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz"
Expand Down Expand Up @@ -1775,7 +1756,7 @@ enhanced-resolve@^5.0.0, enhanced-resolve@^5.15.0:
graceful-fs "^4.2.4"
tapable "^2.2.0"

entities@^4.2.0, entities@^4.4.0:
entities@^4.2.0, entities@^4.4.0, entities@^4.5.0:
version "4.5.0"
resolved "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz"
integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==
Expand Down Expand Up @@ -3173,11 +3154,6 @@ json5@^1.0.1, json5@^1.0.2:
object.assign "^4.1.4"
object.values "^1.1.6"

just-extend@^6.2.0:
version "6.2.0"
resolved "https://registry.npmjs.org/just-extend/-/just-extend-6.2.0.tgz"
integrity sha512-cYofQu2Xpom82S6qD778jBDpwvvy39s1l/hrYij2u9AMdQcGRpaBu6kY4mVhuno5kJVi1DAz4aiphA2WI1/OAw==

kdbush@^4.0.2:
version "4.0.2"
resolved "https://registry.npmjs.org/kdbush/-/kdbush-4.0.2.tgz"
Expand Down Expand Up @@ -3252,11 +3228,6 @@ locate-path@^7.1.0:
dependencies:
p-locate "^6.0.0"

lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz"
integrity sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==

lodash.memoize@^4.1.2:
version "4.1.2"
resolved "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz"
Expand Down Expand Up @@ -3543,17 +3514,6 @@ neo-async@^2.6.2:
resolved "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==

nise@^6.1.1:
version "6.1.1"
resolved "https://registry.npmjs.org/nise/-/nise-6.1.1.tgz"
integrity sha512-aMSAzLVY7LyeM60gvBS423nBmIPP+Wy7St7hsb+8/fc1HmeoHJfLO8CKse4u3BtOZvQLJghYPI2i/1WZrEj5/g==
dependencies:
"@sinonjs/commons" "^3.0.1"
"@sinonjs/fake-timers" "^13.0.1"
"@sinonjs/text-encoding" "^0.7.3"
just-extend "^6.2.0"
path-to-regexp "^8.1.0"

node-fetch@2:
version "2.6.13"
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.13.tgz"
Expand Down Expand Up @@ -3789,6 +3749,13 @@ parse-srcset@^1.0.2:
resolved "https://registry.npmjs.org/parse-srcset/-/parse-srcset-1.0.2.tgz"
integrity sha512-/2qh0lav6CmI15FzA3i/2Bzk2zCgQhGMkvhOhKNcBVQ1ldgpbfiNTVslmooUmWJcADi1f1kIeynbDRVzNlfR6Q==

parse5@^7.0.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-7.2.0.tgz#8a0591ce9b7c5e2027173ab737d4d3fc3d826fab"
integrity sha512-ZkDsAOcxsUMZ4Lz5fVciOehNcJ+Gb8gTzcA4yl3wnc273BAybYWrQ+Ks/OjCjSEpjvQkDSeZbybK9qj2VHHdGA==
dependencies:
entities "^4.5.0"

parse5@^7.1.1:
version "7.1.2"
resolved "https://registry.npmjs.org/parse5/-/parse5-7.1.2.tgz"
Expand Down Expand Up @@ -3836,11 +3803,6 @@ [email protected]:
resolved "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz"
integrity sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==

path-to-regexp@^8.1.0:
version "8.2.0"
resolved "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.2.0.tgz"
integrity sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==

path-type@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz"
Expand Down Expand Up @@ -4484,18 +4446,6 @@ simple-html-tokenizer@^0.1.1:
resolved "https://registry.npmjs.org/simple-html-tokenizer/-/simple-html-tokenizer-0.1.1.tgz"
integrity sha512-Mc/gH3RvlKvB/gkp9XwgDKEWrSYyefIJPGG8Jk1suZms/rISdUuVEMx5O1WBnTWaScvxXDvGJrZQWblUmQHjkQ==

sinon@^19.0.2:
version "19.0.2"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-19.0.2.tgz#944cf771d22236aa84fc1ab70ce5bffc3a215dad"
integrity sha512-euuToqM+PjO4UgXeLETsfQiuoyPXlqFezr6YZDFwHR3t4qaX0fZUe1MfPMznTL5f8BWrVS89KduLdMUsxFCO6g==
dependencies:
"@sinonjs/commons" "^3.0.1"
"@sinonjs/fake-timers" "^13.0.2"
"@sinonjs/samsam" "^8.0.1"
diff "^7.0.0"
nise "^6.1.1"
supports-color "^7.2.0"

sirv@^1.0.7:
version "1.0.19"
resolved "https://registry.npmjs.org/sirv/-/sirv-1.0.19.tgz"
Expand Down Expand Up @@ -4732,7 +4682,7 @@ supports-color@^2.0.0:
resolved "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz"
integrity sha512-KKNVtd6pCYgPIKU4cp2733HWYCpplQhddZLBUryaAHou723x+FRzQ5Df824Fj+IyyuiQTRoub4SnIFfIcrp70g==

supports-color@^7.1.0, supports-color@^7.2.0:
supports-color@^7.1.0:
version "7.2.0"
resolved "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz"
integrity sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==
Expand Down Expand Up @@ -4911,16 +4861,6 @@ type-check@^0.4.0, type-check@~0.4.0:
dependencies:
prelude-ls "^1.2.1"

[email protected]:
version "4.0.8"
resolved "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz"
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==

type-detect@^4.1.0:
version "4.1.0"
resolved "https://registry.npmjs.org/type-detect/-/type-detect-4.1.0.tgz"
integrity sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==

type-fest@^0.20.2:
version "0.20.2"
resolved "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz"
Expand Down

0 comments on commit 6b88ae4

Please sign in to comment.