Skip to content

Commit

Permalink
Merge pull request #89 from Nosto/use-render-campaigns
Browse files Browse the repository at this point in the history
Simplify useRenderCampaigns
  • Loading branch information
timowestnosto authored Jul 22, 2024
2 parents 44b892c + 630e404 commit bd65e2b
Show file tree
Hide file tree
Showing 25 changed files with 356 additions and 182 deletions.
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
"prettier": "^2.0.5",
"react": "*",
"react-dom": "*",
"react-router": "^6.25.1",
"react-router-dom": "^6.25.1",
"rimraf": "^3.0.2",
"ts-jest": "^29.1.0",
"typedoc": "^0.24.1",
Expand Down
8 changes: 3 additions & 5 deletions spec/category.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ test("Category page render", async () => {
account="shopify-11368366139"
recommendationComponent={<RecommendationComponent />}
>
<>
<NostoPlacement id="categorypage-nosto-1" />
<NostoPlacement id="categorypage-nosto-2" />
<NostoCategory category="Rings" />
</>
<NostoPlacement id="categorypage-nosto-1" />
<NostoPlacement id="categorypage-nosto-2" />
<NostoCategory category="Rings" />
</NostoProvider>
)

Expand Down
10 changes: 4 additions & 6 deletions spec/checkout.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ test("Checkout page render", async () => {
account="shopify-11368366139"
recommendationComponent={<RecommendationComponent />}
>
<>
<NostoPlacement id="cartpage-nosto-1" />
<NostoPlacement id="cartpage-nosto-2" />
<NostoPlacement id="cartpage-nosto-3" />
<NostoCheckout />
</>
<NostoPlacement id="cartpage-nosto-1" />
<NostoPlacement id="cartpage-nosto-2" />
<NostoPlacement id="cartpage-nosto-3" />
<NostoCheckout />
</NostoProvider>
)

Expand Down
58 changes: 58 additions & 0 deletions spec/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
interface Event {
cart_popup?: boolean
elements?: string[]
events?: [string, string][]
response_mode?: string
url?: string
categories?: string[]
page_type?: string
}

function createEvent(event: Event): Event {
return {
cart_popup: false,
elements: [],
events: [],
response_mode: "JSON_ORIGINAL",
url: "http://localhost/",
...event
}
}

export function frontEvent() {
return createEvent({
elements: [
"frontpage-nosto-1",
"frontpage-nosto-3",
"frontpage-nosto-4",
],
page_type: "front"
})
}

export function categoryEvent(category: string) {
return createEvent({
elements: [
"categorypage-nosto-1",
"categorypage-nosto-2",
],
page_type: "category",
categories: [category],
url: `http://localhost/collections/${category}`
})
}

export function productEvent(product: string) {
return createEvent({
elements: [
"productpage-nosto-1",
"productpage-nosto-2",
"productpage-nosto-3",
],
events: [
["vp", product],
],
page_type: "product",
url: `http://localhost/products/${product}`
})
}
8 changes: 3 additions & 5 deletions spec/fohofo.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ test("404 page render", async () => {
account="shopify-11368366139"
recommendationComponent={<RecommendationComponent />}
>
<>
<NostoPlacement id="notfound-nosto-1" />
<NostoPlacement id="notfound-nosto-2" />
<Nosto404 />
</>
<NostoPlacement id="notfound-nosto-1" />
<NostoPlacement id="notfound-nosto-2" />
<Nosto404 />
</NostoProvider>
)

Expand Down
10 changes: 4 additions & 6 deletions spec/home.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ test("Home page render", async () => {
account="shopify-11368366139"
recommendationComponent={<RecommendationComponent />}
>
<>
<NostoPlacement id="frontpage-nosto-1" />
<NostoPlacement id="frontpage-nosto-3" />
<NostoPlacement id="frontpage-nosto-4" />
<NostoHome />
</>
<NostoPlacement id="frontpage-nosto-1" />
<NostoPlacement id="frontpage-nosto-3" />
<NostoPlacement id="frontpage-nosto-4" />
<NostoHome />
</NostoProvider>
)

Expand Down
19 changes: 19 additions & 0 deletions spec/module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as imports from "../src/index"

test("module structure is stable", () => {
expect(Object.keys(imports)).toEqual([
"NostoContext",
"useNostoContext",
"Nosto404",
"NostoOther",
"NostoCheckout",
"NostoProduct",
"NostoCategory",
"NostoSearch",
"NostoOrder",
"NostoHome",
"NostoPlacement",
"NostoProvider",
"NostoSession"
])
})
92 changes: 92 additions & 0 deletions spec/navigation.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React from "react"
import { NostoCategory, NostoHome, NostoPlacement, NostoProduct, NostoProvider } from "../src"
import RecommendationComponent from "./renderer"
import { Link, BrowserRouter, Route, Routes, useParams } from "react-router-dom"
import { fireEvent, render, screen, waitFor } from "@testing-library/react"
import { WAIT_FOR_TIMEOUT } from "./utils"
import { categoryEvent, frontEvent, productEvent } from "./events"

function HomePage() {
return <>
<NostoPlacement id="frontpage-nosto-1" />
<NostoPlacement id="frontpage-nosto-3" />
<NostoPlacement id="frontpage-nosto-4" />
<NostoHome />
<Link to="/collections/hoodies">Hoodies</Link>
</>
}

function CategoryPage() {
const { category } = useParams()
return <>
<NostoPlacement id="categorypage-nosto-1" />
<NostoPlacement id="categorypage-nosto-2" />
<NostoCategory category={category!} />
<Link to="/products/123">Product 123</Link>
<Link to="/products/234">Product 234</Link>
<Link to="/">Home</Link>
</>
}

function ProductPage() {
const { product } = useParams()
return <>
<NostoPlacement id="productpage-nosto-1" />
<NostoPlacement id="productpage-nosto-2" />
<NostoPlacement id="productpage-nosto-3" />
<NostoProduct product={product!} />
<Link to="/products/234">Product 234</Link>
<Link to="/collections/hoodies">Hoodies</Link>
<Link to="/">Home</Link>
</>
}

function Main() {
return <NostoProvider
account="shopify-11368366139"
recommendationComponent={<RecommendationComponent />}>
<BrowserRouter>
<Routes>
<Route path="/collections/:category" element={<CategoryPage />} />
<Route path="/products/:product" element={<ProductPage />} />
<Route path="/" element={<HomePage />} />
</Routes>
</BrowserRouter>
</NostoProvider>
}

test("navigation events", async () => {
render(<Main />)

await waitFor(() => {
expect(screen.getAllByTestId("recommendation")).toHaveLength(3)
}, { timeout: WAIT_FOR_TIMEOUT })

const requests: unknown[] = []
window.nostojs(api => api.listen("prerequest", req => requests.push(req)))

function verifyEvents(given: unknown[]) {
expect(requests).toEqual(given)
requests.length = 0
}

// home -> category
fireEvent.click(screen.getByText("Hoodies"))
verifyEvents([ frontEvent(), categoryEvent("hoodies")])

// category -> product
fireEvent.click(screen.getByText("Product 123"))
verifyEvents([ productEvent("123") ])

// product -> product
fireEvent.click(screen.getByText("Product 234"))
verifyEvents([ productEvent("234") ])

// product -> category
fireEvent.click(screen.getByText("Hoodies"))
verifyEvents([ categoryEvent("hoodies") ])

// category -> home
fireEvent.click(screen.getByText("Home"))
verifyEvents([ frontEvent() ])
})
12 changes: 5 additions & 7 deletions spec/other.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ test("Other page render", async () => {
account="shopify-11368366139"
recommendationComponent={<RecommendationComponent />}
>
<>
<NostoPlacement id="cartpage-nosto-1" />
<NostoPlacement id="categorypage-nosto-1" />
<NostoPlacement id="productpage-nosto-1" />
<NostoPlacement id="productpage-nosto-2" />
<NostoOther />
</>
<NostoPlacement id="cartpage-nosto-1" />
<NostoPlacement id="categorypage-nosto-1" />
<NostoPlacement id="productpage-nosto-1" />
<NostoPlacement id="productpage-nosto-2" />
<NostoOther />
</NostoProvider>
)

Expand Down
10 changes: 4 additions & 6 deletions spec/product.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ test("Product page render", async () => {
account="shopify-11368366139"
recommendationComponent={<RecommendationComponent />}
>
<>
<NostoPlacement id="productpage-nosto-1" />
<NostoPlacement id="productpage-nosto-2" />
<NostoPlacement id="productpage-nosto-3" />
<NostoProduct product="7078777258043" />
</>
<NostoPlacement id="productpage-nosto-1" />
<NostoPlacement id="productpage-nosto-2" />
<NostoPlacement id="productpage-nosto-3" />
<NostoProduct product="7078777258043" />
</NostoProvider>
)

Expand Down
8 changes: 3 additions & 5 deletions spec/search.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ test("Search page render", async () => {
account="shopify-11368366139"
recommendationComponent={<RecommendationComponent />}
>
<>
<NostoPlacement id="searchpage-nosto-1" />
<NostoPlacement id="searchpage-nosto-2" />
<NostoSearch query="" />
</>
<NostoPlacement id="searchpage-nosto-1" />
<NostoPlacement id="searchpage-nosto-2" />
<NostoSearch query="" />
</NostoProvider>
)

Expand Down
12 changes: 4 additions & 8 deletions src/components/Nosto404.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useNostoContext, useNostoApi } from "../hooks"
import { useNostoApi, useRenderCampaigns } from "../hooks"

/**
* You can personalise your cart and checkout pages by using the `Nosto404` component.
Expand All @@ -21,19 +21,15 @@ import { useNostoContext, useNostoApi } from "../hooks"
* @group Components
*/
export default function Nosto404(props: { placements?: string[] }) {
const { recommendationComponent, useRenderCampaigns } = useNostoContext()

const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("404")
const { renderCampaigns } = useRenderCampaigns()

useNostoApi(
async (api) => {
const data = await api.defaultSession()
.viewNotFound()
.setPlacements(props.placements || api.placements.getPlacements())
.load()
renderCampaigns(data, api)
},
[recommendationComponent, pageTypeUpdated]
)
renderCampaigns(data)
})
return null
}
Loading

0 comments on commit bd65e2b

Please sign in to comment.