Skip to content

Commit

Permalink
Merge pull request #111 from Nosto/shopify-market-tests
Browse files Browse the repository at this point in the history
Improve shopify market tests
  • Loading branch information
timowestnosto authored Aug 16, 2024
2 parents d418044 + ddd6f60 commit 91b2587
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
18 changes: 10 additions & 8 deletions spec/useLoadClientScript.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,22 @@ describe("useLoadClientScript", () => {
})

it("remove existing Shopify markets related scripts before loading new ones", () => {
const existingScript = document.createElement("script")
existingScript.setAttribute("nosto-client-script", "")
document.body.appendChild(existingScript)
const props = { account: testAccount, shopifyMarkets: { marketId: "123", language: "en" } }
const hook = renderHook(props => useLoadClientScript(props), { initialProps: props })
expect(getScriptSources()).toEqual([
`http://connect.nosto.com/script/shopify/market/nosto.js?merchant=${testAccount}&market=123&locale=en`
])

const nostoSandbox = document.createElement("div")
nostoSandbox.id = "nosto-sandbox"
document.body.appendChild(nostoSandbox)
const existingScript = document.querySelector("[nosto-client-script]")
const nostoSandbox = document.querySelector("#nosto-sandbox")

renderHook(() => useLoadClientScript({ account: testAccount, shopifyMarkets: { marketId: "123", language: "en" } }))
Object.assign(props.shopifyMarkets, { marketId: "234", language: "fr" })

hook.rerender(props)
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=${testAccount}&market=123&locale=en`
`http://connect.nosto.com/script/shopify/market/nosto.js?merchant=${testAccount}&market=234&locale=fr`
])
})
})
6 changes: 3 additions & 3 deletions src/hooks/useLoadClientScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export function useLoadClientScript(props: NostoScriptProps) {
const marketId = String(shopifyMarkets?.marketId || "")
const language = shopifyMarkets?.language || ""

const existingScriptAttributes =
const attributeMismatch =
existingScript?.getAttribute("nosto-language") !== language ||
existingScript?.getAttribute("nosto-market-id") !== marketId

if (!existingScript || existingScriptAttributes) {
if (!existingScript || attributeMismatch) {
if (clientScriptLoaded) {
setClientScriptLoaded(false)
}
Expand All @@ -80,7 +80,7 @@ export function useLoadClientScript(props: NostoScriptProps) {
document.body.appendChild(script)
}
}
}, [shopifyMarkets])
}, [shopifyMarkets?.marketId, shopifyMarkets?.language])

return { clientScriptLoaded }
}

0 comments on commit 91b2587

Please sign in to comment.