Skip to content

Commit

Permalink
Remove test references from main code
Browse files Browse the repository at this point in the history
  • Loading branch information
timowestnosto committed Dec 30, 2024
1 parent 7a67372 commit e7de095
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
11 changes: 8 additions & 3 deletions spec/setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSDOM } from "jsdom"
import { afterEach, vi } from "vitest"
import { beforeEach, afterEach, vi } from "vitest"
import { clearNostoGlobals } from "@nosto/nosto-js/testing"

const { window } = new JSDOM("<html></html>", {
Expand All @@ -16,8 +16,13 @@ global.document = window.document
global.localStorage = window.localStorage
global.navigator = window.navigator

// test mode flag
global.window.nostoReactTest = true
beforeEach(() => {
window.nostoab = {
settings: {
site: "localhost"
}
}
})

afterEach(() => {
clearNostoGlobals()
Expand Down
13 changes: 1 addition & 12 deletions spec/useLoadClientScript.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { renderHook } from "@testing-library/react"
import { useLoadClientScript } from "../src/hooks/useLoadClientScript"
import scriptLoader from "../src/hooks/scriptLoader"
import "@testing-library/jest-dom/vitest"
import { nostojs, getNostoWindow, isNostoLoaded } from "@nosto/nosto-js"
import { nostojs, isNostoLoaded } from "@nosto/nosto-js"
import { reloadNosto } from "@nosto/nosto-js/testing"

function loadClientScript(merchant: string) {
Expand Down Expand Up @@ -71,17 +71,6 @@ describe("useLoadClientScript", () => {
expect(getScriptSources()).toEqual([`http://connect.nosto.com/include/${testAccount}`])
})

it("reloads client script once with loadScript=false", async () => {
await loadClientScript(testAccount)
const reloadSpy = vi.spyOn(getNostoWindow()!, "reload")

const hook = renderHook(() => useLoadClientScript({ loadScript: false, account: testAccount }))
expect(reloadSpy).toHaveBeenCalledTimes(1)

hook.rerender()
expect(reloadSpy).toHaveBeenCalledTimes(1)
})

it("remove existing Shopify markets related scripts before loading new ones", () => {
const props = { account: testAccount, shopifyMarkets: { marketId: "123", language: "en" } }
const hook = renderHook(props => useLoadClientScript(props), { initialProps: props })
Expand Down
13 changes: 4 additions & 9 deletions src/hooks/useLoadClientScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { useState, useEffect } from "react"
import type { NostoProviderProps } from "../components/NostoProvider"
import scriptLoaderFn from "./scriptLoader"
import { init, initNostoStub, isNostoLoaded, nostojs } from "@nosto/nosto-js"
import { reloadNosto } from "@nosto/nosto-js/testing"

type NostoScriptProps = Pick<NostoProviderProps, "account" | "host" | "shopifyMarkets" | "loadScript" | "scriptLoader">

const defaultAttributes = { "nosto-client-script": "" }

export function useLoadClientScript(props: NostoScriptProps) {
const {
host = "connect.nosto.com",
Expand All @@ -18,19 +19,13 @@ export function useLoadClientScript(props: NostoScriptProps) {

useEffect(() => {
function scriptOnload() {
// Override for production scripts to work in unit tests
if ("nostoReactTest" in window) {
reloadNosto({
site: "localhost"
})
}
setClientScriptLoaded(true)
}

// Create and append script element
async function injectScriptElement(urlPartial: string, extraAttributes: Record<string, string> = {}) {
const scriptSrc = `//${host}${urlPartial}`
const attributes = { "nosto-client-script": "", ...extraAttributes }
const attributes = { ...defaultAttributes, ...extraAttributes }
await scriptLoader(scriptSrc, { attributes })
scriptOnload()
}
Expand Down Expand Up @@ -71,7 +66,7 @@ export function useLoadClientScript(props: NostoScriptProps) {
await init({
merchantId: account,
options: {
attributes: { "nosto-client-script": ""}
attributes: defaultAttributes
},
scriptLoader
})
Expand Down

0 comments on commit e7de095

Please sign in to comment.