Skip to content

Commit

Permalink
Merge pull request #105 from Nosto/recommendationComponent-validation
Browse files Browse the repository at this point in the history
Add validation for recommendationComponent
  • Loading branch information
timowestnosto authored Aug 15, 2024
2 parents 6d06fc9 + bf74b24 commit 84f05ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
20 changes: 20 additions & 0 deletions spec/nosto.load.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ describe("Nosto client script loading", () => {
expect(document.querySelector("[nosto-client-script]")).toBeInTheDocument()
})

it("throws error on invalid recommendationComponent", () => {
expect(() => {
render(
<NostoProvider account="shopify-11368366139" recommendationComponent={true as unknown as React.ReactElement}>
<NostoHome />
</NostoProvider>
)
}).toThrowError()
})

it("access valid React elements in recommendationComponent", () => {
render(
<NostoProvider account="shopify-11368366139" recommendationComponent={<NostoHome />}>
<NostoHome />
</NostoProvider>
)

expect(document.querySelector("[nosto-client-script]")).toBeInTheDocument()
})

it("Shopify markets script", () => {
render(
<NostoProvider account="shopify-11368366139" shopifyMarkets={{ language: "en", marketId: "123" }}>
Expand Down
8 changes: 7 additions & 1 deletion src/components/NostoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ export default function NostoProvider(props: NostoProviderProps) {
// Pass currentVariation as empty string if multiCurrency is disabled
const currentVariation = multiCurrency ? props.currentVariation : ""

if (recommendationComponent && !isValidElement(recommendationComponent)) {
throw new Error(
"The recommendationComponent prop must be a valid React element. Please provide a valid React element."
)
}

// Set responseMode for loading campaigns:
const responseMode = isValidElement(recommendationComponent) ? "JSON_ORIGINAL" : "HTML"
const responseMode = recommendationComponent ? "JSON_ORIGINAL" : "HTML"

const { clientScriptLoaded } = useLoadClientScript(props)

Expand Down

0 comments on commit 84f05ed

Please sign in to comment.