Skip to content

Commit

Permalink
Merge pull request #107 from Nosto/client-script-loading
Browse files Browse the repository at this point in the history
Improve client script loading
  • Loading branch information
timowestnosto authored Aug 16, 2024
2 parents 4c7a82e + 7125c0a commit 34e931a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
5 changes: 5 additions & 0 deletions spec/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ global.navigator = window.navigator
global.window.nostoReactTest = true

afterEach(() => {
// clearing Nosto iframe window handle
window.nosto = undefined
// clearing nostojs stub
window.nostojs = undefined
// clearing Shopify specific Nosto namespace
window.Nosto = undefined
document.head.innerHTML = ""
document.body.innerHTML = ""
})
55 changes: 53 additions & 2 deletions spec/useLoadClientScript.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
import { describe, expect, it } from "vitest"
import { renderHook } from "@testing-library/react"
import { useLoadClientScript } from "../src/hooks"
import "@testing-library/jest-dom/vitest"

function loadClientScript(merchant: string) {
const script = document.createElement("script")
script.setAttribute("nosto-client-script", "")
script.src = `http://connect.nosto.com/include/${merchant}`
script.type = "text/javascript"
script.async = true
const promise = new Promise<void>(resolve => {
script.onload = () => {
window.nosto?.reload({ site: "localhost" })
resolve()
}
})
document.body.appendChild(script)
return promise
}

function getScriptSources() {
return Array.from(document.querySelectorAll("script")).map(script => script.src)
}

describe("useLoadClientScript", () => {
it("set loaded state to true when client is loaded externally", () => {

it("loads client script", async () => {
const hook = renderHook(() => useLoadClientScript({ account: "shopify-11368366139" }))
await new Promise(window.nostojs)

hook.rerender()
expect(hook.result.current.clientScriptLoaded).toBe(true)
expect(window.nosto).toBeDefined()
expect(getScriptSources()).toEqual(["http://connect.nosto.com/include/shopify-11368366139"])
})

it("set loaded state to true when client is loaded externally after", async () => {
const hook = renderHook(() => useLoadClientScript({ loadScript: false, account: "shopify-11368366139" }))
expect(hook.result.current.clientScriptLoaded).toBe(false)

await loadClientScript("shopify-11368366139")
expect(window.nosto).toBeDefined()

hook.rerender()
expect(hook.result.current.clientScriptLoaded).toBe(true)
expect(getScriptSources()).toEqual(["http://connect.nosto.com/include/shopify-11368366139"])
})

it("set loaded state to true when client is loaded externally before", async () => {
await loadClientScript("shopify-11368366139")
expect(window.nosto).toBeDefined()

const { result } = renderHook(() => useLoadClientScript({ loadScript: false, account: "shopify-11368366139" }))
expect(result.current.clientScriptLoaded).toBe(true)
expect(getScriptSources()).toEqual(["http://connect.nosto.com/include/shopify-11368366139"])
})

it("remove existing Shopify markets related scripts before loading new ones", () => {
Expand All @@ -17,9 +65,12 @@ describe("useLoadClientScript", () => {
nostoSandbox.id = "nosto-sandbox"
document.body.appendChild(nostoSandbox)

const { result } = renderHook(() => useLoadClientScript({ account: "shopify-11368366139", shopifyMarkets: { marketId: "123", language: "en" } }))
renderHook(() => useLoadClientScript({ account: "shopify-11368366139", shopifyMarkets: { marketId: "123", language: "en" } }))

expect(document.body.contains(existingScript)).toBe(false)
expect(document.body.contains(nostoSandbox)).toBe(false)
expect(getScriptSources()).toEqual([
"http://connect.nosto.com/script/shopify/market/nosto.js?merchant=shopify-11368366139&market=123&locale=en"
])
})
})
2 changes: 1 addition & 1 deletion src/hooks/useLoadClientScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function useLoadClientScript(props: NostoScriptProps) {
}

if (!loadScript) {
scriptOnload()
window.nosto ? scriptOnload() : window.nostojs(scriptOnload)
return
}

Expand Down
3 changes: 1 addition & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default defineConfig({
test: {
include: ["*.spec.*"],
dir: "spec",
setupFiles: ['./spec/setup.js'],
silent: true
setupFiles: ['./spec/setup.js']
},
})

0 comments on commit 34e931a

Please sign in to comment.