From a17d9476888576cc18baeda34bb77b959d9698a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Wed, 14 Aug 2024 09:32:24 +0300 Subject: [PATCH 1/2] Use JSDOM directly --- package-lock.json | 4 ++-- package.json | 8 +++----- spec/setup.js | 14 ++++++++++++++ src/components/NostoProvider.tsx | 4 ++-- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 spec/setup.js diff --git a/package-lock.json b/package-lock.json index 2e60466..af8ed32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@nosto/nosto-react", - "version": "1.0.0", + "version": "2.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@nosto/nosto-react", - "version": "1.0.0", + "version": "2.0.0", "license": "ISC", "devDependencies": { "@testing-library/jest-dom": "^5.16.5", diff --git a/package.json b/package.json index 3aaab1f..96bd695 100644 --- a/package.json +++ b/package.json @@ -73,10 +73,8 @@ "homepage": "https://github.com/Nosto/nosto-react#readme", "jest": { "preset": "ts-jest", - "testEnvironment": "jsdom", - "testEnvironmentOptions": { - "resources": "usable", - "runScripts": "dangerously" - } + "setupFiles": [ + "./spec/setup.js" + ] } } diff --git a/spec/setup.js b/spec/setup.js new file mode 100644 index 0000000..fe6a129 --- /dev/null +++ b/spec/setup.js @@ -0,0 +1,14 @@ +const { JSDOM } = require("jsdom") + +const { window } = new JSDOM("", { + url: "http://localhost", + resources: "usable", + runScripts: "dangerously" }) +global.window = window +global.location = window.location +global.document = window.document +global.localStorage = window.localStorage +global.navigator = window.navigator + +// test mode flag +global.nostoReactTest = true \ No newline at end of file diff --git a/src/components/NostoProvider.tsx b/src/components/NostoProvider.tsx index e9a2347..19ebc27 100644 --- a/src/components/NostoProvider.tsx +++ b/src/components/NostoProvider.tsx @@ -95,7 +95,7 @@ export default function NostoProvider(props: NostoProviderProps) { script.setAttribute("nosto-client-script", "") script.onload = () => { - if (typeof jest !== "undefined") { + if ("nostoReactTest" in window) { window.nosto?.reload({ site: "localhost", }) @@ -136,7 +136,7 @@ export default function NostoProvider(props: NostoProviderProps) { script.setAttribute("nosto-market-id", String(shopifyMarkets?.marketId)) script.onload = () => { - if (typeof jest !== "undefined") { + if ("nostoReactTest" in window) { window.nosto?.reload({ site: "localhost", }) From 2d3f1ac4dce466545d82f52487889c20f2a40851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Wed, 14 Aug 2024 09:59:17 +0300 Subject: [PATCH 2/2] Fix nostoReactTest issue --- spec/setup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/setup.js b/spec/setup.js index fe6a129..9ef4acb 100644 --- a/spec/setup.js +++ b/spec/setup.js @@ -11,4 +11,4 @@ global.localStorage = window.localStorage global.navigator = window.navigator // test mode flag -global.nostoReactTest = true \ No newline at end of file +global.window.nostoReactTest = true \ No newline at end of file