Skip to content

Commit

Permalink
test: Add unit test for createMap (aws-amplify#162)
Browse files Browse the repository at this point in the history
* Add unit test for createMap

* Remove unnecessary jest env header

* Polyfill Worker for jsdom env
  • Loading branch information
slaymance committed Jul 6, 2022
1 parent 9bc43e7 commit fb4dfd0
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 13 deletions.
45 changes: 45 additions & 0 deletions __tests__/createMap.test.dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Amplify } from "@aws-amplify/core";
import { Geo } from "@aws-amplify/geo";
import type { AmazonLocationServiceMapStyle } from "@aws-amplify/geo";

import { createMap } from "../src/AmplifyMapLibreRequest";

jest.mock("@aws-amplify/geo");

describe('createMap', () => {
Amplify.Auth = {};
Amplify.Auth.currentCredentials = jest.fn().mockImplementation(() => {
return {
accessKeyId: "accessKeyId",
sessionToken: "sessionTokenId",
secretAccessKey: "secretAccessKey",
identityId: "identityId",
authenticated: true,
expiration: new Date(),
};
});

beforeEach(() => {
(Geo.getDefaultMap as jest.Mock).mockClear();
});

test("createMap returns a map object", async () => {
(Geo.getDefaultMap as jest.Mock).mockReturnValueOnce({
mapName: "map1ff111f1-staging",
region: "us-east-1",
style: "VectorEsriStreets"
} as AmazonLocationServiceMapStyle);

const el = document.createElement("div");
el.setAttribute("id", "map");
document.body.appendChild(el);

const map = await createMap({
container: "map",
center: [-123.1187, 49.2819],
zoom: 11,
});

expect(map).toBeDefined();
});
});
36 changes: 23 additions & 13 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["<rootDir>/__tests__/**/*.ts"],
transform: {
"/node_modules/mapbox-gl-draw-circle.+\\.js$": "babel-jest", // mapbox-gl-draw-circle needs to be transpiled to cjs
".+\\.(svg|css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
"jest-transform-stub",
},
moduleNameMapper: {
"^.+.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
"jest-transform-stub",
},
transformIgnorePatterns: ["/node_modules/(?!mapbox-gl-draw-circle).+\\.js$"],
projects: [
{
moduleNameMapper: {
"^.+.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
"jest-transform-stub",
},
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["<rootDir>/__tests__/**/*.test.ts"],
transform: {
"/node_modules/mapbox-gl-draw-circle.+\\.js$": "babel-jest", // mapbox-gl-draw-circle needs to be transpiled to cjs
".+\\.(svg|css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
"jest-transform-stub",
},
transformIgnorePatterns: ["/node_modules/(?!mapbox-gl-draw-circle).+\\.js$"],
},
{
preset: "ts-jest",
setupFiles: ["./jest.setup.dom.ts", "jest-webgl-canvas-mock", "jsdom-worker-fix"], // workarounds for jsdom and node env conflicts
testEnvironment: "jsdom",
testMatch: ["<rootDir>/__tests__/**/*.test.dom.ts"],
},
],
};
7 changes: 7 additions & 0 deletions jest.setup.dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This is a workaround to the problem of the jsdom library not supporting
* URL.createObjectURL. See https://github.com/jsdom/jsdom/issues/1721.
*/
if (typeof window.URL.createObjectURL === 'undefined') {
window.URL.createObjectURL = jest.fn();
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
"husky": "^7.0.4",
"jest": "^27.0.3",
"jest-transform-stub": "^2.0.0",
"jest-webgl-canvas-mock": "^0.2.3",
"jsdom-worker-fix": "^0.1.8",
"maplibre-gl": "^1.14.0",
"prettier": "^2.3.0",
"rollup": "^2.55.1",
Expand Down
43 changes: 43 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4147,6 +4147,11 @@ color-convert@^2.0.1:
dependencies:
color-name "~1.1.4"

color-convert@~0.5.0:
version "0.5.3"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-0.5.3.tgz#bdb6c69ce660fadffe0b0007cc447e1b9f7282bd"
integrity sha512-RwBeO/B/vZR3dfKL1ye/vx8MHZ40ugzpyfeVG5GsiuGnrlMWe2o8wxBbLCpw9CsxV+wHuzYlCiWnybrIA0ling==

color-name@1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
Expand Down Expand Up @@ -4335,6 +4340,11 @@ csscolorparser@~1.0.3:
resolved "https://registry.yarnpkg.com/csscolorparser/-/csscolorparser-1.0.3.tgz#b34f391eea4da8f3e98231e2ccd8df9c041f171b"
integrity sha1-s085HupNqPPpgjHizNjfnAQfFxs=

cssfontparser@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/cssfontparser/-/cssfontparser-1.2.1.tgz#f4022fc8f9700c68029d542084afbaf425a3f3e3"
integrity sha512-6tun4LoZnj7VN6YeegOVb67KBX/7JJsqvj+pv3ZA7F878/eN33AbGa5b/S/wXxS/tcp8nc40xRUrsPlxIyNUPg==

cssom@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.4.4.tgz#5a66cf93d2d0b661d80bf6a44fb65f5c2e4e0a10"
Expand Down Expand Up @@ -6289,6 +6299,14 @@ jest-watcher@^27.5.1:
jest-util "^27.5.1"
string-length "^4.0.1"

jest-webgl-canvas-mock@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/jest-webgl-canvas-mock/-/jest-webgl-canvas-mock-0.2.3.tgz#70f978b724d382e716b620096476293fca71ca26"
integrity sha512-82QePnztfps/CZMJP60gKeWEaMPz/ZOpcX+ayhkg9uKJ4+YyHmrWu0FTD+Z1FVcKeP5dqAY2Rv5ZG3msIKpSEA==
dependencies:
cssfontparser "^1.2.1"
parse-color "^1.0.0"

jest-worker@^27.5.1:
version "27.5.1"
resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-27.5.1.tgz#8d146f0900e8973b106b6f73cc1e9a8cb86f8db0"
Expand Down Expand Up @@ -6320,6 +6338,14 @@ js-yaml@^3.13.1:
argparse "^1.0.7"
esprima "^4.0.0"

jsdom-worker-fix@^0.1.8:
version "0.1.8"
resolved "https://registry.yarnpkg.com/jsdom-worker-fix/-/jsdom-worker-fix-0.1.8.tgz#2cd92b5e1b3e3f9e9b419b30f96f507525733949"
integrity sha512-hkbmlBkj66MkSIx3NywyO8lOOpzdleSBz+BaKaL4O4b9feIt1mSYLRl5D1RlbfvQ32OqXuukDNucrftMmkdSfw==
dependencies:
mitt "^1.1.3"
uuid-v4 "^0.1.0"

jsdom@^16.6.0:
version "16.7.0"
resolved "https://registry.yarnpkg.com/jsdom/-/jsdom-16.7.0.tgz#918ae71965424b197c819f8183a754e18977b710"
Expand Down Expand Up @@ -6998,6 +7024,11 @@ minizlib@^2.1.1, minizlib@^2.1.2:
minipass "^3.0.0"
yallist "^4.0.0"

mitt@^1.1.3:
version "1.2.0"
resolved "https://registry.yarnpkg.com/mitt/-/mitt-1.2.0.tgz#cb24e6569c806e31bd4e3995787fe38a04fdf90d"
integrity sha512-r6lj77KlwqLhIUku9UWYes7KJtsczvolZkzp8hbaDPPaE24OmWl5s539Mytlj22siEQKosZ26qCBgda2PKwoJw==

mkdirp-infer-owner@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/mkdirp-infer-owner/-/mkdirp-infer-owner-2.0.0.tgz#55d3b368e7d89065c38f32fd38e638f0ab61d316"
Expand Down Expand Up @@ -7536,6 +7567,13 @@ parent-module@^1.0.0:
dependencies:
callsites "^3.0.0"

parse-color@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/parse-color/-/parse-color-1.0.0.tgz#7b748b95a83f03f16a94f535e52d7f3d94658619"
integrity sha512-fuDHYgFHJGbpGMgw9skY/bj3HL/Jrn4l/5rSspy00DoT4RyLnDcRvPxdZ+r6OFwIsgAuhDh4I09tAId4mI12bw==
dependencies:
color-convert "~0.5.0"

parse-conflict-json@^2.0.1, parse-conflict-json@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/parse-conflict-json/-/parse-conflict-json-2.0.2.tgz#3d05bc8ffe07d39600dc6436c6aefe382033d323"
Expand Down Expand Up @@ -8968,6 +9006,11 @@ util-deprecate@^1.0.1, util-deprecate@~1.0.1:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=

uuid-v4@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/uuid-v4/-/uuid-v4-0.1.0.tgz#62d7b310406f6cecfea1528c69f1e8e0bcec5a3a"
integrity sha512-m11RYDtowtAIihBXMoGajOEKpAXrKbpKlpmxqyztMYQNGSY5nZAZ/oYch/w2HNS1RMA4WLGcZvuD8/wFMuCEzA==

uuid@^3.0.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
Expand Down

0 comments on commit fb4dfd0

Please sign in to comment.