-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from Nosto/use-render-campaigns
Simplify useRenderCampaigns
- Loading branch information
Showing
25 changed files
with
356 additions
and
182 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.