Skip to content

Commit

Permalink
Add ssr test
Browse files Browse the repository at this point in the history
  • Loading branch information
timowestnosto committed Jan 7, 2025
1 parent 6afbbd0 commit 17bdf3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions spec/home.ssr.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { test, expect } from "vitest"
import { renderToString } from "react-dom/server"
import { NostoProvider, NostoHome, NostoPlacement } from "../src/index"
import RecommendationComponent from "./renderer"

test("Home page server-side render", async () => {
const origWindow = global.window
try {
// @ts-expect-error SSR emulation
global.window = undefined

const html = renderToString(
<NostoProvider account="dummy-account" recommendationComponent={<RecommendationComponent />} loadScript={true}>
<NostoPlacement id="frontpage-nosto-1" />
<NostoPlacement id="frontpage-nosto-2" />
<NostoHome />
</NostoProvider>
)

// Check if the rendered HTML contains the expected elements
expect(html).toContain('id="frontpage-nosto-1"')
expect(html).toContain('id="frontpage-nosto-2"')
} finally {
global.window = origWindow
}
})
2 changes: 1 addition & 1 deletion 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, isNostoLoaded, getNostoWindow } from "@nosto/nosto-js"
import { nostojs, isNostoLoaded } from "@nosto/nosto-js"

function loadClientScript(merchant: string) {
const script = document.createElement("script")
Expand Down

0 comments on commit 17bdf3c

Please sign in to comment.