diff --git a/package-lock.json b/package-lock.json index 055ce50..79263df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ }, "devDependencies": { "@types/jest": "^27.0.1", + "@types/jest-in-case": "^1.0.5", "@types/leaflet": "1.7.5", "@types/node": "^16.9.1", "@typescript-eslint/eslint-plugin": "^4.31", @@ -29,6 +30,7 @@ "eslint": "^7.32", "eslint-plugin-vue": "^7.17", "jest": "^26.6.3", + "jest-in-case": "^1.0.2", "patch-package": "^6.4", "rollup-plugin-analyzer": "^4.0", "sass": "^1.39", @@ -1262,6 +1264,16 @@ "pretty-format": "^27.0.0" } }, + "node_modules/@types/jest-in-case": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@types/jest-in-case/-/jest-in-case-1.0.5.tgz", + "integrity": "sha512-QcIc5IqSVfpl+9jFl4/LlZnAPDNYUfLD+VrvtrK/cOx2CbS7K+FoXGIajiaY3gpFAkBiHGV2PThuWrmaRFE/Tg==", + "dev": true, + "dependencies": { + "@types/jest": "*", + "@types/node": "*" + } + }, "node_modules/@types/json-schema": { "version": "7.0.9", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", @@ -4974,6 +4986,15 @@ "fsevents": "^2.1.2" } }, + "node_modules/jest-in-case": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/jest-in-case/-/jest-in-case-1.0.2.tgz", + "integrity": "sha1-VnRLWvMyIr0KurcM+Rnx0XCrdcw=", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/jest-jasmine2": { "version": "26.6.3", "resolved": "https://registry.npmjs.org/jest-jasmine2/-/jest-jasmine2-26.6.3.tgz", @@ -10564,6 +10585,16 @@ "pretty-format": "^27.0.0" } }, + "@types/jest-in-case": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@types/jest-in-case/-/jest-in-case-1.0.5.tgz", + "integrity": "sha512-QcIc5IqSVfpl+9jFl4/LlZnAPDNYUfLD+VrvtrK/cOx2CbS7K+FoXGIajiaY3gpFAkBiHGV2PThuWrmaRFE/Tg==", + "dev": true, + "requires": { + "@types/jest": "*", + "@types/node": "*" + } + }, "@types/json-schema": { "version": "7.0.9", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", @@ -13452,6 +13483,12 @@ "walker": "^1.0.7" } }, + "jest-in-case": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/jest-in-case/-/jest-in-case-1.0.2.tgz", + "integrity": "sha1-VnRLWvMyIr0KurcM+Rnx0XCrdcw=", + "dev": true + }, "jest-jasmine2": { "version": "26.6.3", "resolved": "https://registry.npmjs.org/jest-jasmine2/-/jest-jasmine2-26.6.3.tgz", diff --git a/package.json b/package.json index 3921934..4897838 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "devDependencies": { "@types/jest": "^27.0.1", + "@types/jest-in-case": "^1.0.5", "@types/leaflet": "1.7.5", "@types/node": "^16.9.1", "@typescript-eslint/eslint-plugin": "^4.31", @@ -32,6 +33,7 @@ "eslint": "^7.32", "eslint-plugin-vue": "^7.17", "jest": "^26.6.3", + "jest-in-case": "^1.0.2", "patch-package": "^6.4", "rollup-plugin-analyzer": "^4.0", "sass": "^1.39", diff --git a/tests/util.test.ts b/tests/util.test.ts index 157b2c3..7cf3950 100644 --- a/tests/util.test.ts +++ b/tests/util.test.ts @@ -15,269 +15,321 @@ */ import {parseUrl} from "@/util"; -import {LiveAtlasParsedUrl} from "@/index"; +import cases from 'jest-in-case'; describe("parseURL", () => { - const testUrls = (urls: Map) => { - urls.forEach((value, key) => { - // @ts-ignore - process.stdout.write(key.toString() + '\n'); - expect(parseUrl(key)).toEqual(value); - }); - }; + cases('valid Dynmap URLs', (options: any) => { + expect(parseUrl(options.url)).toEqual(options.result); + }, [ + { + name: 'Complete URL', + url: new URL('https://minecraft.rtgame.co.uk/build?worldname=world&mapname=surface&zoom=4&x=6885&y=64&z=24608'), + result: { + world: 'world', + map: 'surface', + location: {x: 6885, y: 64, z: 24608}, + zoom: 4, + legacy: true + } + }, + { + name: 'no location', + url: new URL('https://minecraft.rtgame.co.uk/build?worldname=world&mapname=surface&zoom=4'), + result: { + world: 'world', + map: 'surface', + location: undefined, + zoom: 4, + legacy: true + } + }, + { + name: 'no zoom', + url: new URL('https://minecraft.rtgame.co.uk/build?worldname=world&mapname=surface&x=6885&y=64&z=24608'), + result: { + world: 'world', + map: 'surface', + location: {x: 6885, y: 64, z: 24608}, + zoom: undefined, + legacy: true + } + }, + { + name: 'no zoom or location', + url: new URL('https://minecraft.rtgame.co.uk/build?worldname=world&mapname=surface'), + result: { + world: 'world', + map: 'surface', + location: undefined, + zoom: undefined, + legacy: true + } + }, + { + name: 'no map, zoom or location', + url: new URL('https://minecraft.rtgame.co.uk/build?worldname=world'), + result: { + world: 'world', + map: undefined, + location: undefined, + zoom: undefined, + legacy: true + } + } + ]); - test('parses valid Dynmap URLs', () => { - const tests = new Map(); + cases('valid Pl3xmap URLs', (options: any) => { + expect(parseUrl(options.url)).toEqual(options.result); + }, [ + { + name: 'Complete URL', + url: new URL('https://minecraft.rtgame.co.uk/build?world=world&zoom=4&x=6885&y=64&z=24608'), + result: { + world: 'world', + map: undefined, + location: {x: 6885, y: 64, z: 24608}, + zoom: 4, + legacy: true + } + }, + { + name: 'no location', + url: new URL('https://minecraft.rtgame.co.uk/build?world=world&zoom=4'), + result: { + world: 'world', + map: undefined, + location: undefined, + zoom: 4, + legacy: true + } + }, + { + name: 'no zoom', + url: new URL('https://minecraft.rtgame.co.uk/build?world=world&x=6885&y=64&z=24608'), + result: { + world: 'world', + map: undefined, + location: {x: 6885, y: 64, z: 24608}, + zoom: undefined, + legacy: true + } + }, + { + name: 'no zoom or location', + url: new URL('https://minecraft.rtgame.co.uk/build?world=world'), + result: { + world: 'world', + map: undefined, + location: undefined, + zoom: undefined, + legacy: true + } + }, + { + name: 'nothing', + url: new URL('https://minecraft.rtgame.co.uk/map/survival?'), + result: null + } + ]); - // Complete URL - tests.set(new URL('https://minecraft.rtgame.co.uk/build?worldname=world&mapname=surface&zoom=4&x=6885&y=64&z=24608'), { - world: 'world', - map: 'surface', - location: { x: 6885, y: 64, z: 24608 }, - zoom: 4, - legacy: true - }); + cases('valid LiveAtlas URLs', (options: any) => { + expect(parseUrl(options.url)).toEqual(options.result); + }, [ + { + name: 'Complete URL', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,1914;2'), + result: { + world: 'world', + map: 'flat', + location: {x: 168, y: 64, z: 1914}, + zoom: 2, + legacy: false + } + }, + { + name: 'no zoom', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,1914'), + result: { + world: 'world', + map: 'flat', + location: {x: 168, y: 64, z: 1914}, + zoom: undefined, + legacy: false + } + }, + { + name: 'no location or zoom', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat'), + result: { + world: 'world', + map: 'flat', + location: undefined, + zoom: undefined, + legacy: false + } + }, + { + name: 'no map, location or zoom', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world'), + result: { + world: 'world', + map: undefined, + location: undefined, + zoom: undefined, + legacy: false + } + }, + { + name: 'nothing', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#'), + result: null + } + ]); - // No location - tests.set(new URL('https://minecraft.rtgame.co.uk/build?worldname=world&mapname=surface&zoom=4'), { - world: 'world', - map: 'surface', - location: undefined, - zoom: 4, - legacy: true - }); + cases('incomplete or invalid LiveAtlas URLs', (options: any) => { + expect(parseUrl(options.url)).toEqual(options.result); + }, [ + { + name: 'negative zoom', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,1914;-2'), + result: { + world: 'world', + map: 'flat', + location: {x: 168, y: 64, z: 1914}, + zoom: undefined, + legacy: false + } + }, + { + name: 'NaN zoom', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,1914;ewsjtuiewshfoes'), + result: { + world: 'world', + map: 'flat', + location: {x: 168, y: 64, z: 1914}, + zoom: undefined, + legacy: false + } + }, + { + name: 'missing z coordinate', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64;2'), + result: { + world: 'world', + map: 'flat', + location: undefined, + zoom: 2, + legacy: false + } + }, + { + name: 'NaN z coordinate', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,j;2'), + result: { + world: 'world', + map: 'flat', + location: undefined, + zoom: 2, + legacy: false + } + }, + { + name: 'missing y and z coordinates', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168;2'), + result: { + world: 'world', + map: 'flat', + location: undefined, + zoom: 2, + legacy: false + } + }, + { + name: 'invalid location', + url: new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;23wteyuisezujsezr'), + result: { + world: 'world', + map: 'flat', + location: undefined, + zoom: undefined, + legacy: false + } + } + ]); - // No zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/build?worldname=world&mapname=surface&x=6885&y=64&z=24608'), { - world: 'world', - map: 'surface', - location: { x: 6885, y: 64, z: 24608 }, - zoom: undefined, - legacy: true - }); - - // No zoom or location - tests.set(new URL('https://minecraft.rtgame.co.uk/build?worldname=world&mapname=surface'), { - world: 'world', - map: 'surface', - location: undefined, - zoom: undefined, - legacy: true - }); - - // No zoom, location or map - tests.set(new URL('https://minecraft.rtgame.co.uk/build?worldname=world'), { - world: 'world', - map: undefined, - location: undefined, - zoom: undefined, - legacy: true - }); - - testUrls(tests); - }); - - test('parses valid Pl3xmap URLs', () => { - const tests = new Map(); - - // Complete URL - tests.set(new URL('https://minecraft.rtgame.co.uk/build?world=world&zoom=4&x=6885&z=24608'), { - world: 'world', - map: undefined, - location: { x: 6885, y: 64, z: 24608 }, - zoom: 4, - legacy: true - }); - - // No location - tests.set(new URL('https://minecraft.rtgame.co.uk/build?world=world&zoom=4'), { - world: 'world', - map: undefined, - location: undefined, - zoom: 4, - legacy: true - }); - - // No zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/build?world=world&x=6885&z=24608'), { - world: 'world', - map: undefined, - location: { x: 6885, y: 64, z: 24608 }, - zoom: undefined, - legacy: true - }); - - // No zoom or location - tests.set(new URL('https://minecraft.rtgame.co.uk/build?world=world'), { - world: 'world', - map: undefined, - location: undefined, - zoom: undefined, - legacy: true - }); - - testUrls(tests); - }); - - test('parses valid LiveAtlas URL', () => { - const tests = new Map(); - - // Complete URL - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,1914;2'), { - world: 'world', - map: 'flat', - location: { x: 168, y: 64, z: 1914 }, - zoom: 2, - legacy: false - }); - - // No zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,1914'), { - world: 'world', - map: 'flat', - location: { x: 168, y: 64, z: 1914 }, - zoom: undefined, - legacy: false - }); - - // No location or zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat'), { - world: 'world', - map: 'flat', - location: undefined, - zoom: undefined, - legacy: false - }); - - // No map, location or zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world'), { - world: 'world', - map: undefined, - location: undefined, - zoom: undefined, - legacy: false - }); - - testUrls(tests); - }); - - test('handles incomplete or invalid LiveAtlas URLs', () => { - const tests = new Map(); - - //Negative zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,1914;-2'), { - world: 'world', - map: 'flat', - location: { x: 168, y: 64, z: 1914 }, - zoom: undefined, - legacy: false - }); - - //NaN zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,1914;ewsjtuiewshfoes'), { - world: 'world', - map: 'flat', - location: { x: 168, y: 64, z: 1914 }, - zoom: undefined, - legacy: false - }); - - //Missing z coordinate - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64;2'), { - world: 'world', - map: 'flat', - location: undefined, - zoom: 2, - legacy: false - }); - - //NaN z coordinate - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168,64,j;2'), { - world: 'world', - map: 'flat', - location: undefined, - zoom: 2, - legacy: false - }); - - //Missing y and z coordinates - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;168;2'), { - world: 'world', - map: 'flat', - location: undefined, - zoom: 2, - legacy: false - }); - - //Invalid location - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival#world;flat;23wteyuisezujsezr'), { - world: 'world', - map: 'flat', - location: undefined, - zoom: undefined, - legacy: false - }); - - testUrls(tests); - }); - - test('handles incomplete or invalid Dynmap URLs', () => { - const tests = new Map(); - - //Negative zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=-2&x=6885&y=64&z=24608'), { - world: 'world', - map: 'surface', - location: { x: 6885, y: 64, z: 24608 }, - zoom: undefined, - legacy: true - }); - - //NaN zoom - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=fsghrtdrh&x=6885&y=64&z=24608'), { - world: 'world', - map: 'surface', - location: { x: 6885, y: 64, z: 24608 }, - zoom: undefined, - legacy: true - }); - - //Missing z coordinate - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=2&x=6885&y=64'), { - world: 'world', - map: 'surface', - location: undefined, - zoom: 2, - legacy: true - }); - - //NaN z coordinate - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=2&x=6885&y=64&z=j'), { - world: 'world', - map: 'surface', - location: undefined, - zoom: 2, - legacy: true - }); - - //Missing y and z coordinates - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=2&x=6885'), { - world: 'world', - map: 'surface', - location: undefined, - zoom: 2, - legacy: true - }); - - //Invalid/missing coordinates - tests.set(new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&x=3wresfsg'), { - world: 'world', - map: 'surface', - location: undefined, - zoom: undefined, - legacy: true - }); - - testUrls(tests); - }); + cases('incomplete or invalid Dynmap URLs', (options: any) => { + expect(parseUrl(options.url)).toEqual(options.result); + }, [ + { + name: 'Negative zoom', + url: new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=-2&x=6885&y=64&z=24608'), + result: { + world: 'world', + map: 'surface', + location: {x: 6885, y: 64, z: 24608}, + zoom: undefined, + legacy: true + } + }, + { + name: 'NaN zoom', + url: new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=fsghrtdrh&x=6885&y=64&z=24608'), + result: { + world: 'world', + map: 'surface', + location: {x: 6885, y: 64, z: 24608}, + zoom: undefined, + legacy: true + } + }, + { + name: 'Missing z coordinate', + url: new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=2&x=6885&y=64'), + result: { + world: 'world', + map: 'surface', + location: undefined, + zoom: 2, + legacy: true + } + }, + { + name: 'NaN z coordinate', + url: new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=2&x=6885&y=64&z=j'), + result: { + world: 'world', + map: 'surface', + location: undefined, + zoom: 2, + legacy: true + } + }, + { + name: 'Missing y and z coordinates', + url: new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&zoom=2&x=6885'), + result: { + world: 'world', + map: 'surface', + location: undefined, + zoom: 2, + legacy: true + } + }, + { + name: 'Invalid/missing coordinates', + url: new URL('https://minecraft.rtgame.co.uk/map/survival?worldname=world&mapname=surface&x=3wresfsg'), + result: { + world: 'world', + map: 'surface', + location: undefined, + zoom: undefined, + legacy: true + } + }, + { + name: 'Missing world', + url: new URL('https://minecraft.rtgame.co.uk/map/survival?mapname=surface&x=3wresfsg'), + result: null + } + ]); }); diff --git a/tsconfig.json b/tsconfig.json index e0a03b3..fbcd47a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,8 @@ "types": [ "vite/client", "vue", - "jest" + "jest", + "jest-in-case" ], "paths": { "@/*": [