@eldrajs/theme-cli 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/LICENSE +21 -0
- package/README.md +26 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +203 -0
- package/dist/index.d.ts +174 -0
- package/dist/index.js +3 -0
- package/dist/validate-DprFpjV7.js +535 -0
- package/package.json +50 -2
- package/template/.eldra/block-types.d.ts +429 -0
- package/template/.eldra/manifest.json +9378 -0
- package/template/.eldra/previews.json +36 -0
- package/template/.github/workflows/eldra-deploy.yml +24 -0
- package/template/.gitlab-ci.yml +16 -0
- package/template/.storybook/eldra.ts +123 -0
- package/template/.storybook/i18n.ts +23 -0
- package/template/.storybook/main.ts +59 -0
- package/template/.storybook/nuxt-link-stub.ts +23 -0
- package/template/.storybook/preview.ts +53 -0
- package/template/.storybook/shims-vue.d.ts +10 -0
- package/template/.storybook/virtual-eldra.d.ts +32 -0
- package/template/README.md +293 -0
- package/template/app/app.vue +126 -0
- package/template/app/assets/main.css +404 -0
- package/template/app/components/EldraIcon.vue +66 -0
- package/template/app/components/EldraRouterLink.vue +62 -0
- package/template/app/components/ui/UiImage.stories.ts +30 -0
- package/template/app/components/ui/UiImage.vue +224 -0
- package/template/app/components/ui/__tests__/UiImage.spec.ts +292 -0
- package/template/app/composables/iconComponent.ts +84 -0
- package/template/app/composables/iconFetcher.ts +14 -0
- package/template/app/composables/useBlockData.ts +30 -0
- package/template/app/composables/useEditing.ts +31 -0
- package/template/app/composables/useEldraIcon.ts +46 -0
- package/template/app/composables/useRevalidating.ts +69 -0
- package/template/app/composables/useRichTextScrollRegions.ts +80 -0
- package/template/app/composables/useStorefront.ts +18 -0
- package/template/app/composables/useStorefrontFeedback.ts +49 -0
- package/template/app/composables/useUiId.ts +18 -0
- package/template/app/composables/useWishlist.ts +42 -0
- package/template/app/i18n.d.ts +28 -0
- package/template/app/icons.ts +185 -0
- package/template/app/pages/[...slug].vue +134 -0
- package/template/app/plugins/eldra-i18n.ts +98 -0
- package/template/app/plugins/eldra-storefront.ts +288 -0
- package/template/app/plugins/eldra-ui-messages.ts +112 -0
- package/template/app/storefront/cart.ts +245 -0
- package/template/app/storefront/categories.ts +283 -0
- package/template/app/storefront/collectionSelector.ts +60 -0
- package/template/app/storefront/commerce.ts +58 -0
- package/template/app/storefront/demo.ts +1313 -0
- package/template/app/storefront/facets.ts +476 -0
- package/template/app/storefront/feedback.ts +122 -0
- package/template/app/storefront/gateway.ts +2256 -0
- package/template/app/storefront/history.ts +233 -0
- package/template/app/storefront/money.ts +248 -0
- package/template/app/storefront/options.ts +71 -0
- package/template/app/storefront/prerender.ts +77 -0
- package/template/app/storefront/refresh.ts +222 -0
- package/template/app/storefront/toProductCard.ts +154 -0
- package/template/app/storefront/types.ts +815 -0
- package/template/app/storefront/volatile.ts +211 -0
- package/template/app/templates.ts +544 -0
- package/template/app/utils/classes.ts +40 -0
- package/template/app/utils/links.ts +70 -0
- package/template/app/utils/pageStructure.ts +77 -0
- package/template/app/utils/pageTitle.ts +37 -0
- package/template/app/virtual-eldra.d.ts +12 -0
- package/template/bitbucket-pipelines.yml +19 -0
- package/template/blocks/announcement-bar/Block.vue +203 -0
- package/template/blocks/announcement-bar/__tests__/Block.spec.ts +227 -0
- package/template/blocks/announcement-bar/block.json +45 -0
- package/template/blocks/announcement-bar/mock.json +7 -0
- package/template/blocks/announcement-bar/preview.png +0 -0
- package/template/blocks/article/Block.vue +317 -0
- package/template/blocks/article/__tests__/Block.spec.ts +229 -0
- package/template/blocks/article/block.json +148 -0
- package/template/blocks/article/mock.json +512 -0
- package/template/blocks/article/preview.json +16 -0
- package/template/blocks/article/preview.png +0 -0
- package/template/blocks/article-list/Block.vue +343 -0
- package/template/blocks/article-list/__tests__/Block.spec.ts +279 -0
- package/template/blocks/article-list/block.json +181 -0
- package/template/blocks/article-list/mock.json +68 -0
- package/template/blocks/article-list/preview.json +84 -0
- package/template/blocks/article-list/preview.png +0 -0
- package/template/blocks/breadcrumbs/Block.vue +278 -0
- package/template/blocks/breadcrumbs/__tests__/Block.spec.ts +461 -0
- package/template/blocks/breadcrumbs/block.json +89 -0
- package/template/blocks/breadcrumbs/mock.json +11 -0
- package/template/blocks/breadcrumbs/preview.json +3 -0
- package/template/blocks/breadcrumbs/preview.png +0 -0
- package/template/blocks/cart/Block.vue +372 -0
- package/template/blocks/cart/__tests__/Block.spec.ts +561 -0
- package/template/blocks/cart/__tests__/ShippingBar.spec.ts +78 -0
- package/template/blocks/cart/__tests__/Summary.spec.ts +245 -0
- package/template/blocks/cart/block.json +74 -0
- package/template/blocks/cart/mock.json +11 -0
- package/template/blocks/cart/parts/CartLines.vue +257 -0
- package/template/blocks/cart/parts/ShippingBar.vue +105 -0
- package/template/blocks/cart/parts/Summary.vue +291 -0
- package/template/blocks/cart/preview.png +0 -0
- package/template/blocks/collection-grid/Block.vue +1832 -0
- package/template/blocks/collection-grid/__tests__/Block.spec.ts +3037 -0
- package/template/blocks/collection-grid/__tests__/groups.spec.ts +731 -0
- package/template/blocks/collection-grid/block.json +187 -0
- package/template/blocks/collection-grid/mock.json +23 -0
- package/template/blocks/collection-grid/parts/groups.ts +861 -0
- package/template/blocks/collection-grid/preview.json +7 -0
- package/template/blocks/collection-grid/preview.png +0 -0
- package/template/blocks/collection-header/Block.vue +533 -0
- package/template/blocks/collection-header/__tests__/Block.spec.ts +381 -0
- package/template/blocks/collection-header/block.json +140 -0
- package/template/blocks/collection-header/mock.json +28 -0
- package/template/blocks/collection-header/preview.json +14 -0
- package/template/blocks/collection-header/preview.png +0 -0
- package/template/blocks/contact/Block.vue +621 -0
- package/template/blocks/contact/__tests__/Block.spec.ts +347 -0
- package/template/blocks/contact/block.json +174 -0
- package/template/blocks/contact/mock.json +28 -0
- package/template/blocks/contact/preview.json +9 -0
- package/template/blocks/contact/preview.png +0 -0
- package/template/blocks/cta/Block.vue +383 -0
- package/template/blocks/cta/__tests__/Block.spec.ts +309 -0
- package/template/blocks/cta/block.json +86 -0
- package/template/blocks/cta/mock.json +10 -0
- package/template/blocks/cta/preview.json +9 -0
- package/template/blocks/cta/preview.png +0 -0
- package/template/blocks/faq/Block.vue +254 -0
- package/template/blocks/faq/__tests__/Block.spec.ts +312 -0
- package/template/blocks/faq/block.json +113 -0
- package/template/blocks/faq/mock.json +115 -0
- package/template/blocks/faq/preview.png +0 -0
- package/template/blocks/feature-grid/Block.vue +241 -0
- package/template/blocks/feature-grid/__tests__/Block.spec.ts +311 -0
- package/template/blocks/feature-grid/block.json +106 -0
- package/template/blocks/feature-grid/mock.json +29 -0
- package/template/blocks/feature-grid/preview.json +45 -0
- package/template/blocks/feature-grid/preview.png +0 -0
- package/template/blocks/footer/Block.vue +652 -0
- package/template/blocks/footer/__tests__/Block.spec.ts +700 -0
- package/template/blocks/footer/block.json +240 -0
- package/template/blocks/footer/mock.json +161 -0
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/gallery/Block.vue +416 -0
- package/template/blocks/gallery/__tests__/Block.spec.ts +289 -0
- package/template/blocks/gallery/block.json +87 -0
- package/template/blocks/gallery/mock.json +16 -0
- package/template/blocks/gallery/preview.json +64 -0
- package/template/blocks/gallery/preview.png +0 -0
- package/template/blocks/hero/Block.vue +378 -0
- package/template/blocks/hero/__tests__/Block.spec.ts +324 -0
- package/template/blocks/hero/block.json +146 -0
- package/template/blocks/hero/mock.json +11 -0
- package/template/blocks/hero/preview.json +55 -0
- package/template/blocks/hero/preview.png +0 -0
- package/template/blocks/image/Block.vue +232 -0
- package/template/blocks/image/__tests__/Block.spec.ts +224 -0
- package/template/blocks/image/block.json +67 -0
- package/template/blocks/image/mock.json +7 -0
- package/template/blocks/image/preview.json +9 -0
- package/template/blocks/image/preview.png +0 -0
- package/template/blocks/logo-cloud/Block.vue +206 -0
- package/template/blocks/logo-cloud/__tests__/Block.spec.ts +210 -0
- package/template/blocks/logo-cloud/block.json +79 -0
- package/template/blocks/logo-cloud/mock.json +15 -0
- package/template/blocks/logo-cloud/preview.json +92 -0
- package/template/blocks/logo-cloud/preview.png +0 -0
- package/template/blocks/navigation/Block.vue +1348 -0
- package/template/blocks/navigation/__tests__/Block.spec.ts +2030 -0
- package/template/blocks/navigation/block.json +167 -0
- package/template/blocks/navigation/mock.json +191 -0
- package/template/blocks/navigation/preview.png +0 -0
- package/template/blocks/navigation/useMegaMenuKeys.ts +276 -0
- package/template/blocks/newsletter/Block.vue +399 -0
- package/template/blocks/newsletter/__tests__/Block.spec.ts +365 -0
- package/template/blocks/newsletter/block.json +105 -0
- package/template/blocks/newsletter/mock.json +31 -0
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/blocks/order-status/Block.vue +754 -0
- package/template/blocks/order-status/__tests__/Block.spec.ts +515 -0
- package/template/blocks/order-status/block.json +138 -0
- package/template/blocks/order-status/mock.json +19 -0
- package/template/blocks/order-status/preview.png +0 -0
- package/template/blocks/pricing-table/Block.vue +297 -0
- package/template/blocks/pricing-table/__tests__/Block.spec.ts +357 -0
- package/template/blocks/pricing-table/block.json +155 -0
- package/template/blocks/pricing-table/mock.json +47 -0
- package/template/blocks/pricing-table/preview.png +0 -0
- package/template/blocks/product-carousel/Block.vue +459 -0
- package/template/blocks/product-carousel/__tests__/Block.spec.ts +957 -0
- package/template/blocks/product-carousel/block.json +71 -0
- package/template/blocks/product-carousel/mock.json +9 -0
- package/template/blocks/product-carousel/preview.json +7 -0
- package/template/blocks/product-carousel/preview.png +0 -0
- package/template/blocks/product-detail/Block.vue +886 -0
- package/template/blocks/product-detail/__tests__/Block.spec.ts +1492 -0
- package/template/blocks/product-detail/__tests__/stock.spec.ts +119 -0
- package/template/blocks/product-detail/block.json +155 -0
- package/template/blocks/product-detail/mock.json +113 -0
- package/template/blocks/product-detail/parts/Gallery.vue +181 -0
- package/template/blocks/product-detail/parts/StickyBuyBar.vue +99 -0
- package/template/blocks/product-detail/preview.png +0 -0
- package/template/blocks/product-detail/stock.ts +150 -0
- package/template/blocks/quote/Block.vue +206 -0
- package/template/blocks/quote/__tests__/Block.spec.ts +265 -0
- package/template/blocks/quote/block.json +76 -0
- package/template/blocks/quote/mock.json +9 -0
- package/template/blocks/quote/preview.json +16 -0
- package/template/blocks/quote/preview.png +0 -0
- package/template/blocks/rich-text/Block.vue +143 -0
- package/template/blocks/rich-text/__tests__/Block.spec.ts +191 -0
- package/template/blocks/rich-text/block.json +57 -0
- package/template/blocks/rich-text/mock.json +87 -0
- package/template/blocks/rich-text/preview.png +0 -0
- package/template/blocks/search/Block.vue +589 -0
- package/template/blocks/search/PopularChips.vue +40 -0
- package/template/blocks/search/TypeSection.vue +148 -0
- package/template/blocks/search/__tests__/Block.spec.ts +718 -0
- package/template/blocks/search/__tests__/results.spec.ts +166 -0
- package/template/blocks/search/block.json +102 -0
- package/template/blocks/search/mock.json +18 -0
- package/template/blocks/search/preview.png +0 -0
- package/template/blocks/search/results.ts +85 -0
- package/template/blocks/split-content/Block.vue +195 -0
- package/template/blocks/split-content/__tests__/Block.spec.ts +225 -0
- package/template/blocks/split-content/block.json +70 -0
- package/template/blocks/split-content/mock.json +45 -0
- package/template/blocks/split-content/preview.json +58 -0
- package/template/blocks/split-content/preview.png +0 -0
- package/template/blocks/stats/Block.vue +159 -0
- package/template/blocks/stats/__tests__/Block.spec.ts +194 -0
- package/template/blocks/stats/block.json +73 -0
- package/template/blocks/stats/mock.json +12 -0
- package/template/blocks/stats/preview.png +0 -0
- package/template/blocks/tabs/Block.vue +278 -0
- package/template/blocks/tabs/__tests__/Block.spec.ts +303 -0
- package/template/blocks/tabs/block.json +97 -0
- package/template/blocks/tabs/mock.json +129 -0
- package/template/blocks/tabs/preview.json +153 -0
- package/template/blocks/tabs/preview.png +0 -0
- package/template/blocks/team/Block.vue +288 -0
- package/template/blocks/team/__tests__/Block.spec.ts +221 -0
- package/template/blocks/team/block.json +125 -0
- package/template/blocks/team/mock.json +39 -0
- package/template/blocks/team/preview.json +62 -0
- package/template/blocks/team/preview.png +0 -0
- package/template/blocks/testimonials/Block.vue +408 -0
- package/template/blocks/testimonials/__tests__/Block.spec.ts +319 -0
- package/template/blocks/testimonials/block.json +121 -0
- package/template/blocks/testimonials/mock.json +46 -0
- package/template/blocks/testimonials/preview.json +68 -0
- package/template/blocks/testimonials/preview.png +0 -0
- package/template/blocks/timeline/Block.vue +274 -0
- package/template/blocks/timeline/__tests__/Block.spec.ts +310 -0
- package/template/blocks/timeline/block.json +98 -0
- package/template/blocks/timeline/mock.json +27 -0
- package/template/blocks/timeline/preview.png +0 -0
- package/template/blocks/trust-strip/Block.vue +330 -0
- package/template/blocks/trust-strip/__tests__/Block.spec.ts +290 -0
- package/template/blocks/trust-strip/block.json +117 -0
- package/template/blocks/trust-strip/mock.json +37 -0
- package/template/blocks/trust-strip/preview.png +0 -0
- package/template/blocks/video-embed/Block.vue +354 -0
- package/template/blocks/video-embed/__tests__/Block.spec.ts +219 -0
- package/template/blocks/video-embed/__tests__/embed.spec.ts +76 -0
- package/template/blocks/video-embed/block.json +94 -0
- package/template/blocks/video-embed/embed.ts +84 -0
- package/template/blocks/video-embed/mock.json +13 -0
- package/template/blocks/video-embed/preview.json +9 -0
- package/template/blocks/video-embed/preview.png +0 -0
- package/template/blocks/wishlist/Block.vue +367 -0
- package/template/blocks/wishlist/__tests__/Block.spec.ts +508 -0
- package/template/blocks/wishlist/block.json +42 -0
- package/template/blocks/wishlist/mock.json +9 -0
- package/template/blocks/wishlist/preview.png +0 -0
- package/template/i18n/en-US.json +602 -0
- package/template/i18n/is-IS.json +602 -0
- package/template/nuxt.config.ts +156 -0
- package/template/package.json +56 -0
- package/template/pages/article.page.json +731 -0
- package/template/pages/cart.page.json +85 -0
- package/template/pages/category.page.json +133 -0
- package/template/pages/collection.page.json +194 -0
- package/template/pages/home.page.json +353 -0
- package/template/pages/product.page.json +323 -0
- package/template/pages/products.page.json +144 -0
- package/template/pages/search.page.json +83 -0
- package/template/pages/wishlist.page.json +88 -0
- package/template/public/apple-touch-icon.png +0 -0
- package/template/public/demo/article-cover.svg +1 -0
- package/template/public/demo/article-figure.svg +1 -0
- package/template/public/demo/avatar-1.svg +3 -0
- package/template/public/demo/avatar-2.svg +3 -0
- package/template/public/demo/avatar-3.svg +3 -0
- package/template/public/demo/avatar-4.svg +3 -0
- package/template/public/demo/avatar-5.svg +3 -0
- package/template/public/demo/avatar-6.svg +3 -0
- package/template/public/demo/avatar-7.svg +3 -0
- package/template/public/demo/avatar-8.svg +3 -0
- package/template/public/demo/collection-1.svg +1 -0
- package/template/public/demo/collection-2.svg +1 -0
- package/template/public/demo/cta-split.svg +1 -0
- package/template/public/demo/feature-1.svg +1 -0
- package/template/public/demo/feature-2.svg +1 -0
- package/template/public/demo/feature-3.svg +1 -0
- package/template/public/demo/feature-4.svg +1 -0
- package/template/public/demo/gallery-1.svg +1 -0
- package/template/public/demo/gallery-2.svg +1 -0
- package/template/public/demo/gallery-3.svg +1 -0
- package/template/public/demo/gallery-4.svg +1 -0
- package/template/public/demo/gallery-5.svg +1 -0
- package/template/public/demo/gallery-6.svg +1 -0
- package/template/public/demo/gallery-7.svg +1 -0
- package/template/public/demo/gallery-8.svg +1 -0
- package/template/public/demo/hero-slide-1.svg +1 -0
- package/template/public/demo/hero-slide-2.svg +1 -0
- package/template/public/demo/hero-slide-3.svg +1 -0
- package/template/public/demo/hero-slide-4.svg +1 -0
- package/template/public/demo/hero-wide.svg +1 -0
- package/template/public/demo/hero.svg +1 -0
- package/template/public/demo/logo-1.svg +4 -0
- package/template/public/demo/logo-2.svg +4 -0
- package/template/public/demo/logo-3.svg +4 -0
- package/template/public/demo/logo-4.svg +4 -0
- package/template/public/demo/logo-5.svg +4 -0
- package/template/public/demo/logo-6.svg +4 -0
- package/template/public/demo/logo-7.svg +4 -0
- package/template/public/demo/logo-8.svg +4 -0
- package/template/public/demo/logo.svg +3 -0
- package/template/public/demo/map.svg +1 -0
- package/template/public/demo/poster.svg +1 -0
- package/template/public/demo/product-1.svg +4 -0
- package/template/public/demo/product-10.svg +4 -0
- package/template/public/demo/product-11.svg +4 -0
- package/template/public/demo/product-12.svg +4 -0
- package/template/public/demo/product-2.svg +4 -0
- package/template/public/demo/product-3.svg +4 -0
- package/template/public/demo/product-4.svg +4 -0
- package/template/public/demo/product-5.svg +4 -0
- package/template/public/demo/product-6.svg +4 -0
- package/template/public/demo/product-7.svg +4 -0
- package/template/public/demo/product-8.svg +4 -0
- package/template/public/demo/product-9.svg +4 -0
- package/template/public/demo/product-square-1.svg +4 -0
- package/template/public/demo/product-square-2.svg +4 -0
- package/template/public/demo/product-square-3.svg +4 -0
- package/template/public/demo/product-square-4.svg +4 -0
- package/template/public/demo/product-square-5.svg +4 -0
- package/template/public/demo/product-square-6.svg +4 -0
- package/template/public/demo/quote-portrait.svg +4 -0
- package/template/public/demo/split-1.svg +1 -0
- package/template/public/demo/split-2.svg +1 -0
- package/template/public/demo/split-3.svg +1 -0
- package/template/public/demo/split-4.svg +1 -0
- package/template/public/demo/team-1.svg +4 -0
- package/template/public/demo/team-2.svg +4 -0
- package/template/public/demo/team-3.svg +4 -0
- package/template/public/demo/team-4.svg +4 -0
- package/template/public/favicon.ico +0 -0
- package/template/scripts/demo-images.mjs +393 -0
- package/template/scripts/generate-stories.mjs +156 -0
- package/template/scripts/previewHash.mjs +125 -0
- package/template/scripts/previews.mjs +149 -0
- package/template/stories/generated/announcement-bar.stories.ts +44 -0
- package/template/stories/generated/article-list.stories.ts +87 -0
- package/template/stories/generated/article.stories.ts +27 -0
- package/template/stories/generated/breadcrumbs.stories.ts +39 -0
- package/template/stories/generated/cart.stories.ts +38 -0
- package/template/stories/generated/collection-grid.stories.ts +117 -0
- package/template/stories/generated/collection-header.stories.ts +51 -0
- package/template/stories/generated/contact.stories.ts +45 -0
- package/template/stories/generated/cta.stories.ts +51 -0
- package/template/stories/generated/faq.stories.ts +56 -0
- package/template/stories/generated/feature-grid.stories.ts +69 -0
- package/template/stories/generated/footer.stories.ts +50 -0
- package/template/stories/generated/gallery.stories.ts +81 -0
- package/template/stories/generated/hero.stories.ts +75 -0
- package/template/stories/generated/image.stories.ts +99 -0
- package/template/stories/generated/logo-cloud.stories.ts +57 -0
- package/template/stories/generated/navigation.stories.ts +62 -0
- package/template/stories/generated/newsletter.stories.ts +68 -0
- package/template/stories/generated/order-status.stories.ts +32 -0
- package/template/stories/generated/pricing-table.stories.ts +44 -0
- package/template/stories/generated/product-carousel.stories.ts +75 -0
- package/template/stories/generated/product-detail.stories.ts +62 -0
- package/template/stories/generated/quote.stories.ts +69 -0
- package/template/stories/generated/rich-text.stories.ts +50 -0
- package/template/stories/generated/search.stories.ts +56 -0
- package/template/stories/generated/split-content.stories.ts +39 -0
- package/template/stories/generated/stats.stories.ts +68 -0
- package/template/stories/generated/tabs.stories.ts +45 -0
- package/template/stories/generated/team.stories.ts +45 -0
- package/template/stories/generated/testimonials.stories.ts +69 -0
- package/template/stories/generated/timeline.stories.ts +80 -0
- package/template/stories/generated/trust-strip.stories.ts +62 -0
- package/template/stories/generated/video-embed.stories.ts +63 -0
- package/template/stories/generated/wishlist.stories.ts +26 -0
- package/template/stories/pages/article.stories.ts +51 -0
- package/template/stories/pages/cart.stories.ts +56 -0
- package/template/stories/pages/category.stories.ts +56 -0
- package/template/stories/pages/collection.stories.ts +51 -0
- package/template/stories/pages/home.stories.ts +51 -0
- package/template/stories/pages/product.stories.ts +47 -0
- package/template/stories/pages/products.stories.ts +58 -0
- package/template/stories/pages/search.stories.ts +54 -0
- package/template/stories/pages/wishlist.stories.ts +53 -0
- package/template/stories/support/pageBlocks.ts +115 -0
- package/template/test/blocksEmptyData.spec.ts +52 -0
- package/template/test/carouselKeyboard.spec.ts +272 -0
- package/template/test/carouselRing.browser.spec.ts +193 -0
- package/template/test/carouselSlideWidth.browser.spec.ts +102 -0
- package/template/test/cartDrawer.spec.ts +295 -0
- package/template/test/cartToast.spec.ts +86 -0
- package/template/test/countPill.browser.spec.ts +196 -0
- package/template/test/demoImages.spec.ts +140 -0
- package/template/test/deps.spec.ts +102 -0
- package/template/test/eldraIcon.spec.ts +58 -0
- package/template/test/framing.spec.ts +76 -0
- package/template/test/i18n.spec.ts +200 -0
- package/template/test/iconComponent.spec.ts +91 -0
- package/template/test/inlineEditable.spec.ts +335 -0
- package/template/test/localePrefix.spec.ts +342 -0
- package/template/test/mainCss.spec.ts +107 -0
- package/template/test/mockGatewayContract.spec.ts +122 -0
- package/template/test/mocks/blocks.ts +20 -0
- package/template/test/mocks/breakpoints.ts +7 -0
- package/template/test/mocks/manifest.ts +11 -0
- package/template/test/moneyFormatting.spec.ts +85 -0
- package/template/test/pageTitle.spec.ts +49 -0
- package/template/test/pages/article.spec.ts +231 -0
- package/template/test/pages/category.spec.ts +185 -0
- package/template/test/pages/collection.spec.ts +396 -0
- package/template/test/pages/home.spec.ts +295 -0
- package/template/test/pages/hydration.spec.ts +557 -0
- package/template/test/pages/product.spec.ts +396 -0
- package/template/test/pages/seededPages.spec.ts +190 -0
- package/template/test/pages/ssr.spec.ts +387 -0
- package/template/test/prerenderCommerce.spec.ts +134 -0
- package/template/test/prerenderFailure.spec.ts +130 -0
- package/template/test/prerenderRefresh.browser.spec.ts +1431 -0
- package/template/test/previewsFresh.spec.ts +56 -0
- package/template/test/richText.spec.ts +164 -0
- package/template/test/richTextTypography.spec.ts +182 -0
- package/template/test/setup.ts +21 -0
- package/template/test/slugPage.spec.ts +244 -0
- package/template/test/slugPageTemplate.spec.ts +194 -0
- package/template/test/starter.spec.ts +1416 -0
- package/template/test/storefront/cart.spec.ts +404 -0
- package/template/test/storefront/categories.spec.ts +235 -0
- package/template/test/storefront/collectionSelector.spec.ts +74 -0
- package/template/test/storefront/demo.spec.ts +555 -0
- package/template/test/storefront/facets.spec.ts +425 -0
- package/template/test/storefront/feedback.spec.ts +97 -0
- package/template/test/storefront/gateway.spec.ts +2673 -0
- package/template/test/storefront/gatewayCart.spec.ts +289 -0
- package/template/test/storefront/gatewayPrerender.spec.ts +1037 -0
- package/template/test/storefront/history.spec.ts +341 -0
- package/template/test/storefront/money.spec.ts +170 -0
- package/template/test/storefront/toProductCard.spec.ts +138 -0
- package/template/test/storefront/volatile.spec.ts +284 -0
- package/template/test/support/axe.ts +14 -0
- package/template/test/support/dialog.ts +51 -0
- package/template/test/support/hydrate.ts +118 -0
- package/template/test/support/i18nText.ts +11 -0
- package/template/test/support/mockGateway.ts +1017 -0
- package/template/test/support/mountBlock.ts +194 -0
- package/template/test/support/mountPage.spec.ts +72 -0
- package/template/test/support/mountPage.ts +254 -0
- package/template/test/support/mountProbe.client.ts +69 -0
- package/template/test/support/renderSsr.ts +97 -0
- package/template/test/support/staticServer.ts +96 -0
- package/template/test/themeIcons.spec.ts +140 -0
- package/template/test/toastAnnouncement.spec.ts +133 -0
- package/template/test/tokens.spec.ts +102 -0
- package/template/test/uiMessagesPlugin.spec.ts +104 -0
- package/template/test/useBlockData.spec.ts +49 -0
- package/template/test/useEditing.spec.ts +54 -0
- package/template/test/useEldraIcon.spec.ts +126 -0
- package/template/test/useUiId.spec.ts +40 -0
- package/template/tokens.json +149 -0
- package/template/tsconfig.json +3 -0
- package/template/tsconfig.storybook.json +17 -0
- package/template/vitest-axe.d.ts +13 -0
- package/template/vitest.config.ts +44 -0
|
@@ -0,0 +1,2673 @@
|
|
|
1
|
+
import { ref } from 'vue';
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import {
|
|
4
|
+
createEldraClient,
|
|
5
|
+
EldraHttpError,
|
|
6
|
+
type EldraClient,
|
|
7
|
+
type EldraHttpRequest,
|
|
8
|
+
} from '@eldrajs/sdk';
|
|
9
|
+
import { createGatewayStorefront } from '../../app/storefront/gateway';
|
|
10
|
+
import { safeHref } from '../../app/utils/links';
|
|
11
|
+
import type {
|
|
12
|
+
CatalogFacets,
|
|
13
|
+
StorefrontCollectionProducts,
|
|
14
|
+
StorefrontCollectionSelector,
|
|
15
|
+
StorefrontRoute,
|
|
16
|
+
} from '../../app/storefront/types';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `createGatewayStorefront`'s own spec: the route it is handed is exactly the route a block reads
|
|
20
|
+
* back (`storefront.route`, never a copy), `catalog.collectionProducts` maps the gateway's real
|
|
21
|
+
* response shape, and the shopper's facets go out as the catalog list's own query parameters —
|
|
22
|
+
* `minPrice`/`maxPrice`, `categoryId`, `availability`, `option`, `facets=true` (contract 3.7.0,
|
|
23
|
+
* `packages/sdk/src/__tests__/fixtures/{web-gateway.json,contract.ts}`) — with the response's
|
|
24
|
+
* `facets` read back as the view type the filter panel draws from.
|
|
25
|
+
*/
|
|
26
|
+
function fakeRoute(): StorefrontRoute {
|
|
27
|
+
return {
|
|
28
|
+
productHandle: null,
|
|
29
|
+
collectionHandle: null,
|
|
30
|
+
categorySlug: null,
|
|
31
|
+
categoryPath: null,
|
|
32
|
+
orderToken: null,
|
|
33
|
+
query: null,
|
|
34
|
+
page: 1,
|
|
35
|
+
sort: null,
|
|
36
|
+
columns: null,
|
|
37
|
+
filters: {},
|
|
38
|
+
setQuery: () => {},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** What a call recorder hands back: the client, plus what the gateway asked it. */
|
|
43
|
+
interface ClientCalls {
|
|
44
|
+
client: EldraClient;
|
|
45
|
+
productSlugs: string[];
|
|
46
|
+
collectionQueries: Array<Record<string, unknown>>;
|
|
47
|
+
collectionProductQueries: Array<Record<string, unknown>>;
|
|
48
|
+
productListQueries: Array<Record<string, unknown>>;
|
|
49
|
+
/** One entry per `GET /catalog/v1/categories` — the read a category slug becomes an id through. */
|
|
50
|
+
categoryReads: number[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A client whose collection list answers with `collections` — the by-id path's
|
|
54
|
+
* only source of a slug — and records every call, so a test can prove the
|
|
55
|
+
* products request was never made for an id nothing matched. */
|
|
56
|
+
function recordingClient(
|
|
57
|
+
collections: Array<{ id: string; slug: string }> = [],
|
|
58
|
+
options: {
|
|
59
|
+
product?: Record<string, unknown>;
|
|
60
|
+
products?: (query: Record<string, unknown>) => Array<Record<string, unknown>>;
|
|
61
|
+
/** The store's whole category list, which is how a `category` slug becomes a `categoryId`. */
|
|
62
|
+
categories?: Array<{ id: string; slug: string }>;
|
|
63
|
+
} = {}
|
|
64
|
+
): ClientCalls {
|
|
65
|
+
const productSlugs: string[] = [];
|
|
66
|
+
const collectionQueries: Array<Record<string, unknown>> = [];
|
|
67
|
+
const collectionProductQueries: Array<Record<string, unknown>> = [];
|
|
68
|
+
const productListQueries: Array<Record<string, unknown>> = [];
|
|
69
|
+
const categoryReads: number[] = [];
|
|
70
|
+
const meta = { page: 1, pageSize: 24, total: 0, totalPages: 0, rows: 0 };
|
|
71
|
+
const client = {
|
|
72
|
+
catalog: {
|
|
73
|
+
listCategories: async () => {
|
|
74
|
+
categoryReads.push(categoryReads.length + 1);
|
|
75
|
+
return (options.categories ?? []).map((row) => ({ ...row, title: row.slug }));
|
|
76
|
+
},
|
|
77
|
+
listCollections: async (query: Record<string, unknown>) => {
|
|
78
|
+
collectionQueries.push(query);
|
|
79
|
+
return {
|
|
80
|
+
data: collections.map((item) => ({ ...item, title: item.slug, productCount: 0 })),
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
listCollectionProducts: async (slug: string, query: Record<string, unknown>) => {
|
|
84
|
+
productSlugs.push(slug);
|
|
85
|
+
collectionProductQueries.push(query);
|
|
86
|
+
return { data: [], meta };
|
|
87
|
+
},
|
|
88
|
+
getProduct: async () => options.product ?? { id: 'p1', slug: 'merino-crew-sweater' },
|
|
89
|
+
listProducts: async (query: Record<string, unknown>) => {
|
|
90
|
+
productListQueries.push(query);
|
|
91
|
+
return { data: options.products?.(query) ?? [], meta };
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
} as unknown as EldraClient;
|
|
95
|
+
return {
|
|
96
|
+
client,
|
|
97
|
+
productSlugs,
|
|
98
|
+
collectionQueries,
|
|
99
|
+
collectionProductQueries,
|
|
100
|
+
productListQueries,
|
|
101
|
+
categoryReads,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** One product list row, in the gateway's own shape. */
|
|
106
|
+
function listRow(slug: string): Record<string, unknown> {
|
|
107
|
+
return {
|
|
108
|
+
id: slug,
|
|
109
|
+
slug,
|
|
110
|
+
title: slug,
|
|
111
|
+
status: 'ACTIVE',
|
|
112
|
+
minPrice: 1000,
|
|
113
|
+
maxPrice: 1000,
|
|
114
|
+
totalVariants: 1,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A store selling in krónur, which is the currency every assertion below that is not *about* the
|
|
120
|
+
* minor-unit conversion is written in: ISK has no minor unit, so a price parameter and the facets'
|
|
121
|
+
* own span read as the same number the shopper typed. `USD_COMMERCE` is the other half of that
|
|
122
|
+
* pair, where a hundred-fold mistake is visible.
|
|
123
|
+
*/
|
|
124
|
+
const ISK_COMMERCE = { currency: 'ISK', taxInclusivePricing: true, defaultTaxRate: 0.24 };
|
|
125
|
+
const USD_COMMERCE = { currency: 'USD', taxInclusivePricing: false, defaultTaxRate: 0 };
|
|
126
|
+
|
|
127
|
+
function fakeClient(): EldraClient {
|
|
128
|
+
return {
|
|
129
|
+
catalog: {
|
|
130
|
+
listCollectionProducts: async () => ({
|
|
131
|
+
facets: {
|
|
132
|
+
price: { min: 9600, max: 9600 },
|
|
133
|
+
categories: [{ id: 'cat-1', slug: 'knitwear', title: 'Knitwear', count: 1 }],
|
|
134
|
+
collections: [],
|
|
135
|
+
availability: { in_stock: 1, out_of_stock: 0 },
|
|
136
|
+
options: [
|
|
137
|
+
{
|
|
138
|
+
key: 'colour',
|
|
139
|
+
name: 'Colour',
|
|
140
|
+
kind: 'color',
|
|
141
|
+
values: [{ value: 'oat', label: 'Oat', count: 1, swatch: '#d8cbb0' }],
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
},
|
|
145
|
+
data: [
|
|
146
|
+
{
|
|
147
|
+
id: 'merino-crew-sweater::oat::m',
|
|
148
|
+
slug: 'merino-crew-sweater',
|
|
149
|
+
title: 'Merino crew sweater',
|
|
150
|
+
status: 'ACTIVE',
|
|
151
|
+
minPrice: 9600,
|
|
152
|
+
maxPrice: 9600,
|
|
153
|
+
totalVariants: 1,
|
|
154
|
+
},
|
|
155
|
+
],
|
|
156
|
+
meta: {
|
|
157
|
+
page: 1,
|
|
158
|
+
pageSize: 24,
|
|
159
|
+
total: 1,
|
|
160
|
+
totalPages: 1,
|
|
161
|
+
rows: 1,
|
|
162
|
+
hasNext: false,
|
|
163
|
+
hasPrev: false,
|
|
164
|
+
},
|
|
165
|
+
}),
|
|
166
|
+
},
|
|
167
|
+
} as unknown as EldraClient;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Drains the microtask queue far enough for a `StorefrontResult` to have
|
|
171
|
+
* resolved — including the by-id path, which awaits a collection lookup before
|
|
172
|
+
* the products request. */
|
|
173
|
+
async function settle(): Promise<void> {
|
|
174
|
+
for (let i = 0; i < 8; i += 1) await Promise.resolve();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
describe('createGatewayStorefront', () => {
|
|
178
|
+
it('hands back the caller-provided route unchanged — the same object a block reads', () => {
|
|
179
|
+
const route = fakeRoute();
|
|
180
|
+
const storefront = createGatewayStorefront(fakeClient(), { route });
|
|
181
|
+
expect(storefront.route).toBe(route);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('collectionProducts maps the gateway response, facets and all', async () => {
|
|
185
|
+
const storefront = createGatewayStorefront(fakeClient(), {
|
|
186
|
+
route: fakeRoute(),
|
|
187
|
+
commerce: ISK_COMMERCE,
|
|
188
|
+
});
|
|
189
|
+
const collection = ref<StorefrontCollectionSelector | null>({ slug: 'winter-knitwear' });
|
|
190
|
+
const opts = ref({ page: 1, pageSize: 24 });
|
|
191
|
+
const result = storefront.catalog.collectionProducts(collection, opts);
|
|
192
|
+
await settle();
|
|
193
|
+
|
|
194
|
+
expect(result.error.value).toBeNull();
|
|
195
|
+
expect(result.data.value).toEqual({
|
|
196
|
+
// The one source a collection's own product list cannot narrow by, declared rather than
|
|
197
|
+
// dropped so the panel can stop offering it (`COLLECTION_SCOPE_UNFILTERABLE`).
|
|
198
|
+
unfilterable: ['collection'],
|
|
199
|
+
// The one sort id neither scope reads sales data for, declared the same way
|
|
200
|
+
// (`GATEWAY_UNSORTABLE`).
|
|
201
|
+
unsortable: ['best-selling'],
|
|
202
|
+
items: [
|
|
203
|
+
expect.objectContaining({
|
|
204
|
+
handle: 'merino-crew-sweater',
|
|
205
|
+
title: 'Merino crew sweater',
|
|
206
|
+
url: '/products/merino-crew-sweater',
|
|
207
|
+
price: { amount: 9600, compareAt: null, from: false },
|
|
208
|
+
stock: 'in',
|
|
209
|
+
available: true,
|
|
210
|
+
}),
|
|
211
|
+
],
|
|
212
|
+
total: 1,
|
|
213
|
+
// The response's own `facets`, not a derivation: the terms carry the catalog ids a
|
|
214
|
+
// `categoryId` filter needs, the option values keep their labels and swatches, and the price
|
|
215
|
+
// span is the store's own (krónur here, so minor and major read alike — the conversion has
|
|
216
|
+
// its own test).
|
|
217
|
+
facets: {
|
|
218
|
+
price: { min: 9600, max: 9600 },
|
|
219
|
+
categories: [{ id: 'cat-1', slug: 'knitwear', title: 'Knitwear', count: 1 }],
|
|
220
|
+
collections: [],
|
|
221
|
+
availability: { in_stock: 1, out_of_stock: 0 },
|
|
222
|
+
options: [
|
|
223
|
+
{
|
|
224
|
+
key: 'colour',
|
|
225
|
+
name: 'Colour',
|
|
226
|
+
kind: 'color',
|
|
227
|
+
values: [{ value: 'oat', label: 'Oat', count: 1, swatch: '#d8cbb0' }],
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Every URL in a search response is gateway-supplied. `mapSearchResponse` used to write
|
|
236
|
+
* `result.targetUrl ?? '#'` into the product/journal/page rows, so a result with no destination
|
|
237
|
+
* became a card linking to nowhere (M10) and any URL the gateway returned reached the DOM
|
|
238
|
+
* unchecked while every CMS-authored href in the theme was gated by `safeHref` (I1). It now
|
|
239
|
+
* drops a result whose `targetUrl` does not survive `safeHref`.
|
|
240
|
+
*/
|
|
241
|
+
it('search drops results whose targetUrl is missing or unsafe, and never emits a "#" link', async () => {
|
|
242
|
+
const client = {
|
|
243
|
+
catalog: {
|
|
244
|
+
listProducts: async () => ({ data: [], meta: {} }),
|
|
245
|
+
search: async () => ({
|
|
246
|
+
total: 5,
|
|
247
|
+
results: [
|
|
248
|
+
{ id: 'p1', kind: 'PRODUCT', title: 'Good product', targetUrl: '/products/good' },
|
|
249
|
+
{ id: 'p2', kind: 'PRODUCT', title: 'No link product' },
|
|
250
|
+
{
|
|
251
|
+
id: 'p3',
|
|
252
|
+
kind: 'PRODUCT',
|
|
253
|
+
title: 'Unsafe product',
|
|
254
|
+
targetUrl: 'javascript:alert(1)',
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
id: 'e1',
|
|
258
|
+
kind: 'CMS_ENTRY',
|
|
259
|
+
title: 'Good story',
|
|
260
|
+
targetUrl: '/journal/good',
|
|
261
|
+
breadcrumb: 'Journal',
|
|
262
|
+
},
|
|
263
|
+
{ id: 'e2', kind: 'CMS_ENTRY', title: 'No link story' },
|
|
264
|
+
{ id: 's1', kind: 'CMS_SCHEMA', title: 'Good page', targetUrl: '/pages/good' },
|
|
265
|
+
{ id: 's2', kind: 'CMS_SCHEMA', title: 'Unsafe page', targetUrl: '//evil.example/x' },
|
|
266
|
+
],
|
|
267
|
+
}),
|
|
268
|
+
},
|
|
269
|
+
} as unknown as EldraClient;
|
|
270
|
+
|
|
271
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
272
|
+
const result = storefront.search.run(ref('linen'));
|
|
273
|
+
await settle();
|
|
274
|
+
|
|
275
|
+
const response = result.data.value!;
|
|
276
|
+
expect(response.products.map((product) => product.title)).toEqual(['Good product']);
|
|
277
|
+
expect(response.products[0]!.url).toBe('/products/good');
|
|
278
|
+
expect(response.articles.map((article) => article.title)).toEqual(['Good story']);
|
|
279
|
+
expect(response.pages.map((page) => page.title)).toEqual(['Good page']);
|
|
280
|
+
expect(response.pages[0]!.path).toBe('/pages/good');
|
|
281
|
+
|
|
282
|
+
const everyHref = [
|
|
283
|
+
...response.products.map((product) => product.url),
|
|
284
|
+
...response.articles.map((article) => article.href),
|
|
285
|
+
...response.pages.map((page) => page.href),
|
|
286
|
+
];
|
|
287
|
+
expect(everyHref).not.toContain('#');
|
|
288
|
+
// `safeHref` (`@eldrajs/theme-core/links`'s `safeLinkHref`) is the same allowlist
|
|
289
|
+
// (http/https/mailto/tel/relative) `mapSearchResponse` itself gates every targetUrl
|
|
290
|
+
// through — checking only `javascript:` here would miss `data:`, `vbscript:` and any
|
|
291
|
+
// other scheme the allowlist was never meant to let through.
|
|
292
|
+
expect(everyHref.every((href) => safeHref(href) === href)).toBe(true);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* The search index is built over the platform's own documents, and a document with no public
|
|
297
|
+
* route can only be named by its authoring URL — a `/cms/…` path. Those survive `safeHref` (they
|
|
298
|
+
* are ordinary site paths), so without a rule of their own they reached the modal and the results
|
|
299
|
+
* page as rows that take a shopper out of the shop and onto a path the site serves no file for.
|
|
300
|
+
* Core only returns routable results now; this is the theme refusing to depend on that.
|
|
301
|
+
*/
|
|
302
|
+
it('search drops a result whose destination is an authoring path', async () => {
|
|
303
|
+
const client = {
|
|
304
|
+
catalog: {
|
|
305
|
+
listProducts: async () => ({ data: [], meta: {} }),
|
|
306
|
+
search: async () => ({
|
|
307
|
+
total: 6,
|
|
308
|
+
results: [
|
|
309
|
+
{ id: 'p1', kind: 'PRODUCT', title: 'Routable product', targetUrl: '/products/good' },
|
|
310
|
+
{ id: 'p2', kind: 'PRODUCT', title: 'Authored product', targetUrl: '/cms/products/p2' },
|
|
311
|
+
{ id: 'e1', kind: 'CMS_ENTRY', title: 'Routable story', targetUrl: '/journal/good' },
|
|
312
|
+
{ id: 'e2', kind: 'CMS_ENTRY', title: 'Authored story', targetUrl: '/cms/entries/e2' },
|
|
313
|
+
{ id: 's1', kind: 'CMS_SCHEMA', title: 'Routable page', targetUrl: '/pages/good' },
|
|
314
|
+
// The prefix itself, with nothing under it: a path the theme cannot route either.
|
|
315
|
+
{ id: 's2', kind: 'CMS_SCHEMA', title: 'Authoring root', targetUrl: '/cms' },
|
|
316
|
+
],
|
|
317
|
+
}),
|
|
318
|
+
},
|
|
319
|
+
} as unknown as EldraClient;
|
|
320
|
+
|
|
321
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
322
|
+
const result = storefront.search.run(ref('linen'));
|
|
323
|
+
await settle();
|
|
324
|
+
|
|
325
|
+
const response = result.data.value!;
|
|
326
|
+
expect(response.products.map((product) => product.title)).toEqual(['Routable product']);
|
|
327
|
+
expect(response.articles.map((article) => article.title)).toEqual(['Routable story']);
|
|
328
|
+
expect(response.pages.map((page) => page.title)).toEqual(['Routable page']);
|
|
329
|
+
const everyHref = [
|
|
330
|
+
...response.products.map((product) => product.url),
|
|
331
|
+
...response.articles.map((article) => article.href),
|
|
332
|
+
...response.pages.map((page) => page.href),
|
|
333
|
+
];
|
|
334
|
+
expect(everyHref.some((href) => href.startsWith('/cms'))).toBe(false);
|
|
335
|
+
// A path that merely *contains* the word is a real storefront route and must survive.
|
|
336
|
+
expect(response.pages[0]!.href).toBe('/pages/good');
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* A search result carries no money at all, so the mapping has nothing to write but
|
|
341
|
+
* `{ amount: 0 }` — and a zero that reached a card rendered as the store's own zero amount, a
|
|
342
|
+
* real price formatted in the store's currency and wrong. The products a search found are priced
|
|
343
|
+
* from the catalogue by the same batched `id:in:` read the volatile refresh uses, before the
|
|
344
|
+
* response is published.
|
|
345
|
+
*/
|
|
346
|
+
it('search prices its products from a batched products-list read, in one request', async () => {
|
|
347
|
+
const productListQueries: Array<Record<string, unknown>> = [];
|
|
348
|
+
const client = {
|
|
349
|
+
catalog: {
|
|
350
|
+
listProducts: async (query: Record<string, unknown>) => {
|
|
351
|
+
productListQueries.push(query);
|
|
352
|
+
return {
|
|
353
|
+
data: [
|
|
354
|
+
{
|
|
355
|
+
id: 'p1',
|
|
356
|
+
slug: 'merino-crew-sweater',
|
|
357
|
+
title: 'Merino crew sweater',
|
|
358
|
+
status: 'ACTIVE',
|
|
359
|
+
minPrice: 9600,
|
|
360
|
+
maxPrice: 9600,
|
|
361
|
+
compareAtPrice: 12000,
|
|
362
|
+
totalVariants: 1,
|
|
363
|
+
thumbnail: { assetId: 'a-1', url: 'https://cdn.example/merino.jpg' },
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
id: 'p2',
|
|
367
|
+
slug: 'linen-tea-towels-pair',
|
|
368
|
+
title: 'Linen tea towels',
|
|
369
|
+
status: 'ARCHIVED',
|
|
370
|
+
minPrice: 2400,
|
|
371
|
+
// A price *range*: `mapProductListItem` marks it `from: true` ("From $24.00").
|
|
372
|
+
maxPrice: 3600,
|
|
373
|
+
totalVariants: 3,
|
|
374
|
+
},
|
|
375
|
+
],
|
|
376
|
+
meta: {},
|
|
377
|
+
};
|
|
378
|
+
},
|
|
379
|
+
search: async () => ({
|
|
380
|
+
total: 2,
|
|
381
|
+
results: [
|
|
382
|
+
// Two ids per row on purpose: `id` is the index row, `sourceId` the catalog product.
|
|
383
|
+
{
|
|
384
|
+
id: 'row-1',
|
|
385
|
+
sourceId: 'p1',
|
|
386
|
+
kind: 'PRODUCT',
|
|
387
|
+
title: 'Merino',
|
|
388
|
+
targetUrl: '/products/a',
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
id: 'row-2',
|
|
392
|
+
sourceId: 'p2',
|
|
393
|
+
kind: 'PRODUCT',
|
|
394
|
+
title: 'Towels',
|
|
395
|
+
targetUrl: '/products/b',
|
|
396
|
+
},
|
|
397
|
+
],
|
|
398
|
+
}),
|
|
399
|
+
},
|
|
400
|
+
} as unknown as EldraClient;
|
|
401
|
+
|
|
402
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
403
|
+
const result = storefront.search.run(ref('linen'));
|
|
404
|
+
await settle();
|
|
405
|
+
|
|
406
|
+
const products = result.data.value!.products;
|
|
407
|
+
// The whole snapshot, `from` included: on this path the merge is the product's *first* price,
|
|
408
|
+
// not a refresh of one already on screen, so dropping the range marker would turn a
|
|
409
|
+
// "From $24.00" product into an exact $24.00 one.
|
|
410
|
+
expect(products.map((product) => product.price)).toEqual([
|
|
411
|
+
{ amount: 9600, compareAt: 12000, from: false },
|
|
412
|
+
{ amount: 2400, compareAt: null, from: true },
|
|
413
|
+
]);
|
|
414
|
+
// Availability rides along, which is what lets the suggestion list rank sold-out items last.
|
|
415
|
+
expect(products.map((product) => product.available)).toEqual([true, false]);
|
|
416
|
+
expect(products.map((product) => product.stock)).toEqual(['in', 'out']);
|
|
417
|
+
|
|
418
|
+
// And the thumbnail, which a search result carries no more of than it carries a price: without
|
|
419
|
+
// it every product suggestion drew the "no image" placeholder beside its title.
|
|
420
|
+
expect(products[0]!.featuredImage).toEqual({
|
|
421
|
+
src: 'https://cdn.example/merino.jpg',
|
|
422
|
+
alt: 'Merino crew sweater',
|
|
423
|
+
});
|
|
424
|
+
expect(products[1]!.featuredImage).toBeNull();
|
|
425
|
+
|
|
426
|
+
// The title and the destination stay the index's: the title is what the query matched and what
|
|
427
|
+
// the panel highlights, and the href is the already-sanitised `targetUrl`.
|
|
428
|
+
expect(products.map((product) => [product.title, product.url])).toEqual([
|
|
429
|
+
['Merino', '/products/a'],
|
|
430
|
+
['Towels', '/products/b'],
|
|
431
|
+
]);
|
|
432
|
+
|
|
433
|
+
// One request for both ids, through the one repeatable `id:in:` token — never a read per row.
|
|
434
|
+
expect(productListQueries).toHaveLength(1);
|
|
435
|
+
expect(productListQueries[0]!.filter).toEqual(['id:in:p1,p2']);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* The contract this read lives or dies by, and the one that shipped wrong: a search result carries
|
|
440
|
+
* **two** ids, and only one of them is a catalog product.
|
|
441
|
+
*
|
|
442
|
+
* `id` names the search-index row; `sourceId` names the document the row is about. The enrichment
|
|
443
|
+
* sent `id`, so every deployed search issued `products/list?filter=id:in:<index row id>`, got
|
|
444
|
+
* `{"data":[],"total":0}` back, and rendered every suggestion and every search result card with no
|
|
445
|
+
* price and no thumbnail — a request that looked perfectly healthy in the network panel. Asserted
|
|
446
|
+
* on the filter token itself rather than on the rendered price, because that is the sentence that
|
|
447
|
+
* has to be right and an empty price renders as nothing at all.
|
|
448
|
+
*/
|
|
449
|
+
it('asks the catalogue by sourceId, never by the search row id', async () => {
|
|
450
|
+
const productListQueries: Array<Record<string, unknown>> = [];
|
|
451
|
+
const client = {
|
|
452
|
+
catalog: {
|
|
453
|
+
listProducts: async (query: Record<string, unknown>) => {
|
|
454
|
+
productListQueries.push(query);
|
|
455
|
+
return { data: [], meta: {} };
|
|
456
|
+
},
|
|
457
|
+
search: async () => ({
|
|
458
|
+
total: 2,
|
|
459
|
+
results: [
|
|
460
|
+
{
|
|
461
|
+
id: '784f3236-5419-4e0f-ba0f-9929a0a69e23',
|
|
462
|
+
sourceId: '43e660a0-4d43-4af1-8a31-591d7aaa1253',
|
|
463
|
+
kind: 'PRODUCT',
|
|
464
|
+
title: 'Ash glaze mug',
|
|
465
|
+
targetUrl: '/products/ash-glaze-mug',
|
|
466
|
+
},
|
|
467
|
+
// A row with no `sourceId` at all is asked about by nothing — never by its own id.
|
|
468
|
+
{ id: 'row-2', kind: 'PRODUCT', title: 'Orphan', targetUrl: '/products/orphan' },
|
|
469
|
+
],
|
|
470
|
+
}),
|
|
471
|
+
},
|
|
472
|
+
} as unknown as EldraClient;
|
|
473
|
+
|
|
474
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
475
|
+
const result = storefront.search.run(ref('mug'));
|
|
476
|
+
await settle();
|
|
477
|
+
|
|
478
|
+
expect(productListQueries).toHaveLength(1);
|
|
479
|
+
expect(productListQueries[0]!.filter).toEqual(['id:in:43e660a0-4d43-4af1-8a31-591d7aaa1253']);
|
|
480
|
+
// The row's own id is the row's key and nothing else; the catalogue has never heard of it.
|
|
481
|
+
expect(JSON.stringify(productListQueries)).not.toContain(
|
|
482
|
+
'784f3236-5419-4e0f-ba0f-9929a0a69e23'
|
|
483
|
+
);
|
|
484
|
+
expect(result.data.value!.products.map((product) => product.productId)).toEqual([
|
|
485
|
+
'43e660a0-4d43-4af1-8a31-591d7aaa1253',
|
|
486
|
+
'',
|
|
487
|
+
]);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
/** A gateway refusal, in the shape `@eldrajs/sdk` raises it. */
|
|
491
|
+
function httpError(status: number): EldraHttpError {
|
|
492
|
+
return new EldraHttpError({ status, statusText: 'Service Unavailable' } as Response, null);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** A search whose products the pricing read answers with `onPricing`. */
|
|
496
|
+
function searchWithPricing(onPricing: () => Promise<unknown>): EldraClient {
|
|
497
|
+
return {
|
|
498
|
+
catalog: {
|
|
499
|
+
listProducts: onPricing,
|
|
500
|
+
search: async () => ({
|
|
501
|
+
total: 1,
|
|
502
|
+
results: [
|
|
503
|
+
{
|
|
504
|
+
id: 'row-1',
|
|
505
|
+
sourceId: 'p1',
|
|
506
|
+
kind: 'PRODUCT',
|
|
507
|
+
title: 'Merino',
|
|
508
|
+
targetUrl: '/products/a',
|
|
509
|
+
},
|
|
510
|
+
],
|
|
511
|
+
}),
|
|
512
|
+
},
|
|
513
|
+
} as unknown as EldraClient;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* A pricing read that cannot answer must not cost the shopper the results themselves — and must
|
|
518
|
+
* not cost them a *wrong* price either. The product keeps its row with `price: null`, which the
|
|
519
|
+
* suggestion panel renders without a price; a zero would have been formatted as the store's own
|
|
520
|
+
* "$0.00", which is the defect this whole path exists to avoid.
|
|
521
|
+
*/
|
|
522
|
+
it.each([
|
|
523
|
+
['the gateway refuses', () => Promise.reject(httpError(503))],
|
|
524
|
+
['the network never completes', () => Promise.reject(new TypeError('Failed to fetch'))],
|
|
525
|
+
])('search keeps its results, unpriced, when %s', async (_name, onPricing) => {
|
|
526
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
527
|
+
try {
|
|
528
|
+
const storefront = createGatewayStorefront(searchWithPricing(onPricing), {
|
|
529
|
+
route: fakeRoute(),
|
|
530
|
+
});
|
|
531
|
+
const result = storefront.search.run(ref('linen'));
|
|
532
|
+
await settle();
|
|
533
|
+
|
|
534
|
+
expect(result.error.value).toBeNull();
|
|
535
|
+
const products = result.data.value!.products;
|
|
536
|
+
expect(products.map((product) => product.title)).toEqual(['Merino']);
|
|
537
|
+
expect(products.map((product) => product.price)).toEqual([null]);
|
|
538
|
+
// Not a silent degradation: a page where every row lost its price has to be diagnosable.
|
|
539
|
+
expect(warn).toHaveBeenCalledWith(
|
|
540
|
+
'[eldra] search results could not be priced:',
|
|
541
|
+
expect.any(String)
|
|
542
|
+
);
|
|
543
|
+
} finally {
|
|
544
|
+
warn.mockRestore();
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* A found id the catalogue read simply did not return — a stale index, a product deleted since it
|
|
550
|
+
* was written. `applyVolatileSnapshots` leaves an unmatched item exactly as it was, so the row has
|
|
551
|
+
* to arrive unpriced rather than carrying a placeholder the consumers would format.
|
|
552
|
+
*/
|
|
553
|
+
it('leaves a product the pricing read did not answer about unpriced', async () => {
|
|
554
|
+
const client = {
|
|
555
|
+
catalog: {
|
|
556
|
+
listProducts: async () => ({ data: [], meta: {} }),
|
|
557
|
+
search: async () => ({
|
|
558
|
+
total: 1,
|
|
559
|
+
results: [
|
|
560
|
+
{
|
|
561
|
+
id: 'row-1',
|
|
562
|
+
sourceId: 'gone',
|
|
563
|
+
kind: 'PRODUCT',
|
|
564
|
+
title: 'Discontinued mug',
|
|
565
|
+
targetUrl: '/products/a',
|
|
566
|
+
},
|
|
567
|
+
],
|
|
568
|
+
}),
|
|
569
|
+
},
|
|
570
|
+
} as unknown as EldraClient;
|
|
571
|
+
|
|
572
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
573
|
+
const result = storefront.search.run(ref('mug'));
|
|
574
|
+
await settle();
|
|
575
|
+
|
|
576
|
+
expect(result.data.value!.products.map((product) => product.price)).toEqual([null]);
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* The other half of the narrowed `catch`: a bug in this file must surface as an error rather than
|
|
581
|
+
* as a page that quietly lost every price. `RangeError` stands in for one — it is neither an
|
|
582
|
+
* `EldraHttpError`, an abort, nor the `TypeError` `fetch` rejects with.
|
|
583
|
+
*/
|
|
584
|
+
it('does not swallow a programming error from the pricing read', async () => {
|
|
585
|
+
const storefront = createGatewayStorefront(
|
|
586
|
+
searchWithPricing(() => Promise.reject(new RangeError('filter grammar is wrong'))),
|
|
587
|
+
{ route: fakeRoute() }
|
|
588
|
+
);
|
|
589
|
+
const result = storefront.search.run(ref('linen'));
|
|
590
|
+
await settle();
|
|
591
|
+
|
|
592
|
+
expect(result.error.value).toBe('filter grammar is wrong');
|
|
593
|
+
expect(result.data.value).toBeNull();
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
/** No products in the response: nothing to price, so nothing is asked for. */
|
|
597
|
+
it('search makes no pricing request when it found no products', async () => {
|
|
598
|
+
const productListQueries: Array<Record<string, unknown>> = [];
|
|
599
|
+
const client = {
|
|
600
|
+
catalog: {
|
|
601
|
+
listProducts: async (query: Record<string, unknown>) => {
|
|
602
|
+
productListQueries.push(query);
|
|
603
|
+
return { data: [], meta: {} };
|
|
604
|
+
},
|
|
605
|
+
search: async () => ({
|
|
606
|
+
total: 1,
|
|
607
|
+
results: [{ id: 's1', kind: 'CMS_SCHEMA', title: 'Shipping', targetUrl: '/pages/x' }],
|
|
608
|
+
}),
|
|
609
|
+
},
|
|
610
|
+
} as unknown as EldraClient;
|
|
611
|
+
|
|
612
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
613
|
+
const result = storefront.search.run(ref('shipping'));
|
|
614
|
+
await settle();
|
|
615
|
+
|
|
616
|
+
expect(result.data.value!.pages).toHaveLength(1);
|
|
617
|
+
expect(productListQueries).toEqual([]);
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
it('collectionProducts resolves null for no collection at all, without calling the client', async () => {
|
|
621
|
+
const storefront = createGatewayStorefront(fakeClient(), { route: fakeRoute() });
|
|
622
|
+
const collection = ref<StorefrontCollectionSelector | null>(null);
|
|
623
|
+
const opts = ref({ page: 1, pageSize: 24 });
|
|
624
|
+
const result = storefront.catalog.collectionProducts(collection, opts);
|
|
625
|
+
await settle();
|
|
626
|
+
expect(result.data.value).toBeNull();
|
|
627
|
+
expect(result.error.value).toBeNull();
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* A `reference` field stores the collection's id, and a depth-0 read or a page
|
|
632
|
+
* builder draft overlay carries nothing else — so the gateway has to find the
|
|
633
|
+
* slug itself. There is no by-id collection route, so it filters the list.
|
|
634
|
+
*/
|
|
635
|
+
it('collectionProducts resolves a collection id to its slug through the list filter', async () => {
|
|
636
|
+
const id = '2f1b8d54-0d3a-4a6f-9a0b-7f6c1d2e3a01';
|
|
637
|
+
const calls = recordingClient([{ id, slug: 'winter-knitwear' }]);
|
|
638
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
639
|
+
const collection = ref<StorefrontCollectionSelector | null>({ id });
|
|
640
|
+
const opts = ref({ page: 1, pageSize: 24 });
|
|
641
|
+
const result = storefront.catalog.collectionProducts(collection, opts);
|
|
642
|
+
await settle();
|
|
643
|
+
|
|
644
|
+
expect(calls.collectionQueries).toEqual([{ limit: 1, filter: [`id:eq:${id}`] }]);
|
|
645
|
+
expect(calls.productSlugs).toEqual(['winter-knitwear']);
|
|
646
|
+
expect(result.error.value).toBeNull();
|
|
647
|
+
// No `facets` on the answer — a gateway that cannot describe its scope — so none is invented:
|
|
648
|
+
// the panel then draws the groups it can fill without values (`StorefrontCollectionProducts`).
|
|
649
|
+
expect(result.data.value).toEqual({
|
|
650
|
+
items: [],
|
|
651
|
+
total: 0,
|
|
652
|
+
unfilterable: ['collection'],
|
|
653
|
+
unsortable: ['best-selling'],
|
|
654
|
+
});
|
|
655
|
+
expect(result.data.value).not.toHaveProperty('facets');
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Every token below is `[groupIndex:]field:op:value` — the shape the SDK's
|
|
660
|
+
* contract fixture documents — against a field the deployed gateway filters
|
|
661
|
+
* on (the fixture documents the shape only, not the field or operator set).
|
|
662
|
+
* The two the theme used to send (`slug:a,b` and `relatedTo:<handle>`) were
|
|
663
|
+
* neither, and the gateway answered 400.
|
|
664
|
+
*/
|
|
665
|
+
it('byHandles asks for the whole set in one `in` token, and only for active products', async () => {
|
|
666
|
+
const calls = recordingClient([], {
|
|
667
|
+
products: () => [listRow('merino-crew-sweater'), listRow('stoneware-mug')],
|
|
668
|
+
});
|
|
669
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
670
|
+
const result = storefront.catalog.byHandles(ref(['merino-crew-sweater', 'stoneware-mug']));
|
|
671
|
+
await settle();
|
|
672
|
+
|
|
673
|
+
expect(calls.productListQueries).toEqual([
|
|
674
|
+
{ pageSize: 2, filter: ['slug:in:merino-crew-sweater,stoneware-mug', 'status:eq:ACTIVE'] },
|
|
675
|
+
]);
|
|
676
|
+
expect(result.data.value?.map((item) => item.handle)).toEqual([
|
|
677
|
+
'merino-crew-sweater',
|
|
678
|
+
'stoneware-mug',
|
|
679
|
+
]);
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it('byHandles drops a handle that cannot survive the token grammar, and asks for nothing at all when none can', async () => {
|
|
683
|
+
const calls = recordingClient([], { products: () => [listRow('stoneware-mug')] });
|
|
684
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
685
|
+
storefront.catalog.byHandles(ref(['a,b', 'stoneware-mug']));
|
|
686
|
+
await settle();
|
|
687
|
+
expect(calls.productListQueries[0]?.filter).toEqual([
|
|
688
|
+
'slug:in:stoneware-mug',
|
|
689
|
+
'status:eq:ACTIVE',
|
|
690
|
+
]);
|
|
691
|
+
|
|
692
|
+
const empty = recordingClient();
|
|
693
|
+
const emptyStore = createGatewayStorefront(empty.client, { route: fakeRoute() });
|
|
694
|
+
const result = emptyStore.catalog.byHandles(ref(['x:y']));
|
|
695
|
+
await settle();
|
|
696
|
+
expect(empty.productListQueries).toEqual([]);
|
|
697
|
+
expect(result.data.value).toEqual([]);
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* The tests above assert the query object the theme builds; this one asserts
|
|
702
|
+
* the URL the SDK turns it into, because that is where the tokens were being
|
|
703
|
+
* lost: `filter` is the gateway's one repeatable parameter, and a
|
|
704
|
+
* comma-joined `filter=a,b` ran two tokens into one that the gateway then
|
|
705
|
+
* read as a single, malformed filter.
|
|
706
|
+
*/
|
|
707
|
+
it('reaches the gateway as one filter parameter per token', async () => {
|
|
708
|
+
const urls: string[] = [];
|
|
709
|
+
const client = createEldraClient({
|
|
710
|
+
apiBaseUrl: 'https://api.example.test/api',
|
|
711
|
+
orgId: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
|
|
712
|
+
httpClient: (async (request: EldraHttpRequest) => {
|
|
713
|
+
urls.push(request.url);
|
|
714
|
+
return { data: [], meta: { page: 1, pageSize: 24, total: 0, totalPages: 0, rows: 0 } };
|
|
715
|
+
}) as never,
|
|
716
|
+
});
|
|
717
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
718
|
+
storefront.catalog.byHandles(ref(['merino-crew-sweater', 'stoneware-mug']));
|
|
719
|
+
await settle();
|
|
720
|
+
|
|
721
|
+
expect(urls).toHaveLength(1);
|
|
722
|
+
expect(new URL(urls[0]!).searchParams.getAll('filter')).toEqual([
|
|
723
|
+
'slug:in:merino-crew-sweater,stoneware-mug',
|
|
724
|
+
'status:eq:ACTIVE',
|
|
725
|
+
]);
|
|
726
|
+
expect(urls[0]!.match(/filter=/g)).toHaveLength(2);
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
it('related asks for the current product’s category and leaves the product itself out', async () => {
|
|
730
|
+
const calls = recordingClient([], {
|
|
731
|
+
product: { id: 'p1', slug: 'merino-crew-sweater', categoryId: 'cat-knitwear' },
|
|
732
|
+
products: () => [listRow('merino-crew-sweater'), listRow('lambswool-scarf')],
|
|
733
|
+
});
|
|
734
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
735
|
+
const result = storefront.catalog.related(ref('merino-crew-sweater'), 4);
|
|
736
|
+
await settle();
|
|
737
|
+
|
|
738
|
+
expect(calls.productListQueries).toEqual([
|
|
739
|
+
{
|
|
740
|
+
pageSize: 5,
|
|
741
|
+
sort: ['-createdAt'],
|
|
742
|
+
filter: ['status:eq:ACTIVE'],
|
|
743
|
+
categoryId: 'cat-knitwear',
|
|
744
|
+
},
|
|
745
|
+
]);
|
|
746
|
+
expect(result.data.value?.map((item) => item.handle)).toEqual(['lambswool-scarf']);
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
it('related falls back to the newest active products when the product has no category', async () => {
|
|
750
|
+
const calls = recordingClient([], {
|
|
751
|
+
product: { id: 'p1', slug: 'merino-crew-sweater' },
|
|
752
|
+
products: () => [listRow('lambswool-scarf')],
|
|
753
|
+
});
|
|
754
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
755
|
+
const result = storefront.catalog.related(ref('merino-crew-sweater'), 4);
|
|
756
|
+
await settle();
|
|
757
|
+
|
|
758
|
+
expect(calls.productListQueries).toEqual([
|
|
759
|
+
{ pageSize: 5, sort: ['-createdAt'], filter: ['status:eq:ACTIVE'] },
|
|
760
|
+
]);
|
|
761
|
+
expect(calls.productListQueries[0]).not.toHaveProperty('categoryId');
|
|
762
|
+
expect(result.data.value?.map((item) => item.handle)).toEqual(['lambswool-scarf']);
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
it('related falls back again when the category has nothing else in it', async () => {
|
|
766
|
+
const calls = recordingClient([], {
|
|
767
|
+
product: { id: 'p1', slug: 'merino-crew-sweater', categoryId: 'cat-knitwear' },
|
|
768
|
+
products: (query) =>
|
|
769
|
+
query.categoryId === undefined
|
|
770
|
+
? [listRow('stoneware-mug')]
|
|
771
|
+
: [listRow('merino-crew-sweater')],
|
|
772
|
+
});
|
|
773
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
774
|
+
const result = storefront.catalog.related(ref('merino-crew-sweater'), 4);
|
|
775
|
+
await settle();
|
|
776
|
+
|
|
777
|
+
expect(calls.productListQueries.map((query) => query.categoryId)).toEqual([
|
|
778
|
+
'cat-knitwear',
|
|
779
|
+
undefined,
|
|
780
|
+
]);
|
|
781
|
+
expect(result.data.value?.map((item) => item.handle)).toEqual(['stoneware-mug']);
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
it('collectionProducts sends a sort field the endpoint knows, never a filter token', async () => {
|
|
785
|
+
const calls = recordingClient([], { categories: [{ id: 'cat-1', slug: 'knitwear' }] });
|
|
786
|
+
const storefront = createGatewayStorefront(calls.client, {
|
|
787
|
+
route: fakeRoute(),
|
|
788
|
+
commerce: ISK_COMMERCE,
|
|
789
|
+
});
|
|
790
|
+
const collection = ref<StorefrontCollectionSelector | null>({ slug: 'winter-knitwear' });
|
|
791
|
+
const opts = ref({
|
|
792
|
+
page: 1,
|
|
793
|
+
pageSize: 24,
|
|
794
|
+
sort: 'price-asc',
|
|
795
|
+
filters: { category: ['knitwear'], price: ['20-80'] },
|
|
796
|
+
});
|
|
797
|
+
storefront.catalog.collectionProducts(collection, opts);
|
|
798
|
+
await settle();
|
|
799
|
+
|
|
800
|
+
// The facets are their own parameters now; `filter` stays the `field:op:value` vocabulary and
|
|
801
|
+
// carries none of them.
|
|
802
|
+
expect(calls.collectionProductQueries).toEqual([
|
|
803
|
+
{
|
|
804
|
+
page: 1,
|
|
805
|
+
pageSize: 24,
|
|
806
|
+
sort: ['minPrice'],
|
|
807
|
+
minPrice: 20,
|
|
808
|
+
maxPrice: 80,
|
|
809
|
+
categoryId: ['cat-1'],
|
|
810
|
+
facets: true,
|
|
811
|
+
},
|
|
812
|
+
]);
|
|
813
|
+
expect(calls.collectionProductQueries[0]).not.toHaveProperty('filter');
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
it('collectionProducts sends no sort for the collection’s own order, or one it cannot express', async () => {
|
|
817
|
+
for (const sort of ['featured', 'best-selling']) {
|
|
818
|
+
const calls = recordingClient();
|
|
819
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
820
|
+
const collection = ref<StorefrontCollectionSelector | null>({ slug: 'winter-knitwear' });
|
|
821
|
+
storefront.catalog.collectionProducts(collection, ref({ page: 1, pageSize: 24, sort }));
|
|
822
|
+
await settle();
|
|
823
|
+
expect(calls.collectionProductQueries).toEqual([
|
|
824
|
+
{ page: 1, pageSize: 24, sort: undefined, facets: true },
|
|
825
|
+
]);
|
|
826
|
+
}
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
it('collectionProducts resolves nothing — never an error — for an id the gateway cannot match', async () => {
|
|
830
|
+
// A gateway that ignores the filter token answers with some other
|
|
831
|
+
// collection; matching the id back is what keeps that from loading the
|
|
832
|
+
// wrong collection's products.
|
|
833
|
+
const calls = recordingClient([
|
|
834
|
+
{ id: 'ffffffff-ffff-4fff-8fff-ffffffffffff', slug: 'something-else' },
|
|
835
|
+
]);
|
|
836
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
837
|
+
const collection = ref<StorefrontCollectionSelector | null>({
|
|
838
|
+
id: '2f1b8d54-0d3a-4a6f-9a0b-7f6c1d2e3a01',
|
|
839
|
+
});
|
|
840
|
+
const opts = ref({ page: 1, pageSize: 24 });
|
|
841
|
+
const result = storefront.catalog.collectionProducts(collection, opts);
|
|
842
|
+
await settle();
|
|
843
|
+
|
|
844
|
+
expect(calls.productSlugs).toEqual([]);
|
|
845
|
+
expect(result.data.value).toBeNull();
|
|
846
|
+
expect(result.error.value).toBeNull();
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
it('collectionProducts asks for a slug directly, with no collection lookup', async () => {
|
|
850
|
+
const calls = recordingClient();
|
|
851
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
852
|
+
const collection = ref<StorefrontCollectionSelector | null>({ slug: 'winter-knitwear' });
|
|
853
|
+
const opts = ref({ page: 1, pageSize: 24 });
|
|
854
|
+
storefront.catalog.collectionProducts(collection, opts);
|
|
855
|
+
await settle();
|
|
856
|
+
|
|
857
|
+
expect(calls.collectionQueries).toEqual([]);
|
|
858
|
+
expect(calls.productSlugs).toEqual(['winter-knitwear']);
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* **The shopper's facets, as the catalog list's own query parameters** (contract 3.7.0).
|
|
863
|
+
*
|
|
864
|
+
* Every one of them used to be dropped on the floor: the endpoint filtered on
|
|
865
|
+
* `id`/`slug`/`status`/`createdAt` only, so `?price=50-150` reached the gateway as a plain paged
|
|
866
|
+
* read, the $48 bowl stayed on screen, and the kit filtered a 200-product window client-side to
|
|
867
|
+
* cover for it. These are the parameters that replaced all of that, asserted at the URL the SDK
|
|
868
|
+
* actually sends rather than at the query object — `categoryId` and `option` are repeatable
|
|
869
|
+
* (`explode: true`), and a comma-joined pair is one value the catalogue has never heard of.
|
|
870
|
+
*/
|
|
871
|
+
describe('collectionProducts sends the shopper’s facets as query parameters', () => {
|
|
872
|
+
const ORG = '3fa85f64-5717-4562-b3fc-2c963f66afa6';
|
|
873
|
+
const BASE = 'https://api.example.test/api';
|
|
874
|
+
const CATEGORIES = [
|
|
875
|
+
{ id: 'cat-ceramics', slug: 'ceramics', title: 'Ceramics' },
|
|
876
|
+
{ id: 'cat-textiles', slug: 'textiles', title: 'Textiles' },
|
|
877
|
+
];
|
|
878
|
+
|
|
879
|
+
/** A real `@eldrajs/sdk` client over a recorded transport, so the query string is the one a
|
|
880
|
+
* deployed site would send — repeatable parameters, encoding and all. */
|
|
881
|
+
function httpRecorder(): { client: EldraClient; urls: string[] } {
|
|
882
|
+
const urls: string[] = [];
|
|
883
|
+
const client = createEldraClient({
|
|
884
|
+
apiBaseUrl: BASE,
|
|
885
|
+
orgId: ORG,
|
|
886
|
+
httpClient: (async (request: EldraHttpRequest) => {
|
|
887
|
+
urls.push(request.url);
|
|
888
|
+
if (request.url.includes('/catalog/v1/categories')) return CATEGORIES;
|
|
889
|
+
return { data: [], meta: { page: 1, pageSize: 24, total: 0, totalPages: 0, rows: 0 } };
|
|
890
|
+
}) as never,
|
|
891
|
+
});
|
|
892
|
+
return { client, urls };
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/** Long enough for the category lookup *and* the list read to have both resolved. */
|
|
896
|
+
async function drain(): Promise<void> {
|
|
897
|
+
for (let i = 0; i < 200; i += 1) await Promise.resolve();
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
async function urlsFor(
|
|
901
|
+
filters: Record<string, string[]>,
|
|
902
|
+
commerce = USD_COMMERCE
|
|
903
|
+
): Promise<string[]> {
|
|
904
|
+
const { client, urls } = httpRecorder();
|
|
905
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute(), commerce });
|
|
906
|
+
storefront.catalog.collectionProducts(
|
|
907
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
908
|
+
ref({ page: 1, pageSize: 24, filters })
|
|
909
|
+
);
|
|
910
|
+
await drain();
|
|
911
|
+
return urls;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/** The list read's query, by parameter — `getAll`, because three of them repeat. */
|
|
915
|
+
async function paramsFor(
|
|
916
|
+
filters: Record<string, string[]>,
|
|
917
|
+
commerce = USD_COMMERCE
|
|
918
|
+
): Promise<URLSearchParams> {
|
|
919
|
+
const urls = await urlsFor(filters, commerce);
|
|
920
|
+
return new URL(urls.at(-1)!).searchParams;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
it('sends every filter at once, in one request the gateway can answer', async () => {
|
|
924
|
+
const urls = await urlsFor({
|
|
925
|
+
price: ['50-150'],
|
|
926
|
+
category: ['ceramics', 'textiles'],
|
|
927
|
+
'option:colour': ['oat', 'clay'],
|
|
928
|
+
'option:size': ['m'],
|
|
929
|
+
availability: ['in_stock'],
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
// The category slugs are resolved first — the one read that buys every `categoryId` this
|
|
933
|
+
// storefront will ever need — and then the collection's products, filtered.
|
|
934
|
+
expect(urls).toEqual([
|
|
935
|
+
`${BASE}/catalog/v1/categories`,
|
|
936
|
+
`${BASE}/catalog/v1/collections/the-winter-edit/products` +
|
|
937
|
+
'?page=1&pageSize=24&minPrice=5000&maxPrice=15000' +
|
|
938
|
+
'&categoryId=cat-ceramics&categoryId=cat-textiles&availability=in_stock' +
|
|
939
|
+
'&option=colour%3Aoat&option=colour%3Aclay&option=size%3Am&facets=true',
|
|
940
|
+
]);
|
|
941
|
+
});
|
|
942
|
+
|
|
943
|
+
/**
|
|
944
|
+
* The URL carries whole major units, the parameters minor ones — the same units as the
|
|
945
|
+
* `facets.price` span they are counted over. Reading the shopper's 50 as 50 cents is a
|
|
946
|
+
* hundred-fold error on every two-decimal store, and skipping the conversion on a store with no
|
|
947
|
+
* minor unit is the same error the other way.
|
|
948
|
+
*/
|
|
949
|
+
it('converts the price range to the store currency’s minor units', async () => {
|
|
950
|
+
const usd = await paramsFor({ price: ['50-150'] });
|
|
951
|
+
expect(usd.get('minPrice')).toBe('5000');
|
|
952
|
+
expect(usd.get('maxPrice')).toBe('15000');
|
|
953
|
+
|
|
954
|
+
const isk = await paramsFor({ price: ['50-150'] }, ISK_COMMERCE);
|
|
955
|
+
expect(isk.get('minPrice')).toBe('50');
|
|
956
|
+
expect(isk.get('maxPrice')).toBe('150');
|
|
957
|
+
});
|
|
958
|
+
|
|
959
|
+
it('sends one bound when the shopper set one, and neither for a range that is not a number', async () => {
|
|
960
|
+
const floor = await paramsFor({ price: ['50-'] });
|
|
961
|
+
expect(floor.get('minPrice')).toBe('5000');
|
|
962
|
+
expect(floor.has('maxPrice')).toBe(false);
|
|
963
|
+
|
|
964
|
+
const ceiling = await paramsFor({ price: ['-150'] });
|
|
965
|
+
expect(ceiling.has('minPrice')).toBe(false);
|
|
966
|
+
expect(ceiling.get('maxPrice')).toBe('15000');
|
|
967
|
+
|
|
968
|
+
const nonsense = await paramsFor({ price: ['cheap-ish'] });
|
|
969
|
+
expect(nonsense.has('minPrice')).toBe(false);
|
|
970
|
+
expect(nonsense.has('maxPrice')).toBe(false);
|
|
971
|
+
});
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* Both boxes ticked is every product, so it is no filter at all: sending it would make the
|
|
975
|
+
* platform spend a cross-service stock read to exclude nothing, and would turn a page that
|
|
976
|
+
* cannot read stock into an error for no reason. One box is sent in the platform's own
|
|
977
|
+
* spelling, which the retired hyphenated one folds into.
|
|
978
|
+
*/
|
|
979
|
+
it('sends one availability value, never both, and never an unknown one', async () => {
|
|
980
|
+
expect((await paramsFor({ availability: ['in_stock'] })).get('availability')).toBe(
|
|
981
|
+
'in_stock'
|
|
982
|
+
);
|
|
983
|
+
expect((await paramsFor({ availability: ['out-of-stock'] })).get('availability')).toBe(
|
|
984
|
+
'out_of_stock'
|
|
985
|
+
);
|
|
986
|
+
expect(
|
|
987
|
+
(await paramsFor({ availability: ['in_stock', 'out_of_stock'] })).has('availability')
|
|
988
|
+
).toBe(false);
|
|
989
|
+
expect((await paramsFor({ availability: ['backorder'] })).has('availability')).toBe(false);
|
|
990
|
+
});
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* The lookup's answer belongs to every read, not to the one that asked first, so it is not tied
|
|
994
|
+
* to a read's abort signal: a shopper ticking a second category while the first read is still in
|
|
995
|
+
* flight aborts that read, and a lookup that aborted with it would reject in the hands of the
|
|
996
|
+
* read that replaced it — an error over a page whose filter was perfectly answerable.
|
|
997
|
+
*/
|
|
998
|
+
it('does not abort the category lookup with the read that started it', async () => {
|
|
999
|
+
const calls = recordingClient([], { categories: CATEGORIES });
|
|
1000
|
+
let aborted = 0;
|
|
1001
|
+
const client = {
|
|
1002
|
+
catalog: {
|
|
1003
|
+
...(calls.client as unknown as { catalog: Record<string, unknown> }).catalog,
|
|
1004
|
+
listCategories: async (_query: unknown, context?: { signal?: AbortSignal }) => {
|
|
1005
|
+
if (context?.signal !== undefined) aborted += 1;
|
|
1006
|
+
calls.categoryReads.push(calls.categoryReads.length + 1);
|
|
1007
|
+
return CATEGORIES.map((row) => ({ ...row, title: row.slug }));
|
|
1008
|
+
},
|
|
1009
|
+
},
|
|
1010
|
+
} as unknown as EldraClient;
|
|
1011
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
1012
|
+
const opts = ref({ page: 1, pageSize: 24, filters: { category: ['ceramics'] } });
|
|
1013
|
+
const result = storefront.catalog.collectionProducts(
|
|
1014
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
1015
|
+
opts
|
|
1016
|
+
);
|
|
1017
|
+
// A second gesture before the first read has answered, which is what aborts the first.
|
|
1018
|
+
opts.value = { page: 1, pageSize: 24, filters: { category: ['textiles'] } };
|
|
1019
|
+
await settle();
|
|
1020
|
+
|
|
1021
|
+
expect(aborted).toBe(0);
|
|
1022
|
+
expect(result.error.value).toBeNull();
|
|
1023
|
+
expect(calls.collectionProductQueries.at(-1)?.categoryId).toEqual(['cat-textiles']);
|
|
1024
|
+
});
|
|
1025
|
+
|
|
1026
|
+
it('reads the store’s categories once, however many slugs are ticked', async () => {
|
|
1027
|
+
const calls = recordingClient([], { categories: CATEGORIES });
|
|
1028
|
+
const storefront = createGatewayStorefront(calls.client, {
|
|
1029
|
+
route: fakeRoute(),
|
|
1030
|
+
commerce: USD_COMMERCE,
|
|
1031
|
+
});
|
|
1032
|
+
const opts = ref({ page: 1, pageSize: 24, filters: { category: ['ceramics'] } });
|
|
1033
|
+
storefront.catalog.collectionProducts(
|
|
1034
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
1035
|
+
opts
|
|
1036
|
+
);
|
|
1037
|
+
await settle();
|
|
1038
|
+
opts.value = { page: 1, pageSize: 24, filters: { category: ['ceramics', 'textiles'] } };
|
|
1039
|
+
await settle();
|
|
1040
|
+
|
|
1041
|
+
expect(calls.categoryReads).toHaveLength(1);
|
|
1042
|
+
expect(calls.collectionProductQueries.map((query) => query.categoryId)).toEqual([
|
|
1043
|
+
['cat-ceramics'],
|
|
1044
|
+
['cat-ceramics', 'cat-textiles'],
|
|
1045
|
+
]);
|
|
1046
|
+
});
|
|
1047
|
+
|
|
1048
|
+
it('never reads the categories for a request that filters on none', async () => {
|
|
1049
|
+
const calls = recordingClient([], { categories: CATEGORIES });
|
|
1050
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
1051
|
+
storefront.catalog.collectionProducts(
|
|
1052
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
1053
|
+
ref({ page: 1, pageSize: 24, filters: { 'option:colour': ['oat'] } })
|
|
1054
|
+
);
|
|
1055
|
+
await settle();
|
|
1056
|
+
|
|
1057
|
+
expect(calls.categoryReads).toEqual([]);
|
|
1058
|
+
expect(calls.collectionProductQueries[0]).toEqual({
|
|
1059
|
+
page: 1,
|
|
1060
|
+
pageSize: 24,
|
|
1061
|
+
sort: undefined,
|
|
1062
|
+
option: ['colour:oat'],
|
|
1063
|
+
facets: true,
|
|
1064
|
+
});
|
|
1065
|
+
});
|
|
1066
|
+
|
|
1067
|
+
/**
|
|
1068
|
+
* A slug this store has no category for — a stale shared link, a category since unpublished —
|
|
1069
|
+
* is left out rather than sent as something the catalogue would read as a different filter, and
|
|
1070
|
+
* a request left with nothing to filter on is the request an unfiltered page makes. That is the
|
|
1071
|
+
* storefront's standing "unknown, not unmatched" rule: a filter nothing can honour must not
|
|
1072
|
+
* empty a shopper's grid.
|
|
1073
|
+
*/
|
|
1074
|
+
it('drops a category slug the store does not have', async () => {
|
|
1075
|
+
const mixed = await paramsFor({ category: ['ceramics', 'knitwear'] });
|
|
1076
|
+
expect(mixed.getAll('categoryId')).toEqual(['cat-ceramics']);
|
|
1077
|
+
|
|
1078
|
+
const unknown = await paramsFor({ category: ['knitwear'] });
|
|
1079
|
+
expect(unknown.has('categoryId')).toBe(false);
|
|
1080
|
+
expect(unknown.get('facets')).toBe('true');
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
/**
|
|
1084
|
+
* There is no `collectionId` parameter on a collection's own product list: the scope already
|
|
1085
|
+
* *is* one collection, and the parameter is an OR, so a second id would widen the scope rather
|
|
1086
|
+
* than narrow it. The `collections` facet is still answered and still honest — it names the
|
|
1087
|
+
* other collections these products are in — but the platform does not read the intersection
|
|
1088
|
+
* yet, so nothing is sent for it (`docs/starter-kit.md`).
|
|
1089
|
+
*/
|
|
1090
|
+
it('sends nothing for a collection filter, and says the scope cannot narrow by it', async () => {
|
|
1091
|
+
const params = await paramsFor({ collection: ['the-autumn-edit'], 'option:colour': ['oat'] });
|
|
1092
|
+
expect(params.has('collectionId')).toBe(false);
|
|
1093
|
+
expect(params.getAll('option')).toEqual(['colour:oat']);
|
|
1094
|
+
|
|
1095
|
+
// Declared, not silently dropped: the panel hides the group rather than offering a filter
|
|
1096
|
+
// that moves the chips and the URL and leaves the grid as it was.
|
|
1097
|
+
const storefront = createGatewayStorefront(httpRecorder().client, { route: fakeRoute() });
|
|
1098
|
+
const result = storefront.catalog.collectionProducts(
|
|
1099
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
1100
|
+
ref({ page: 1, pageSize: 24 })
|
|
1101
|
+
);
|
|
1102
|
+
await drain();
|
|
1103
|
+
expect(result.data.value?.unfilterable).toEqual(['collection']);
|
|
1104
|
+
});
|
|
1105
|
+
|
|
1106
|
+
it('asks for facets on an unfiltered read too, so the panel has groups to draw', async () => {
|
|
1107
|
+
const urls = await urlsFor({});
|
|
1108
|
+
expect(urls).toEqual([
|
|
1109
|
+
`${BASE}/catalog/v1/collections/the-winter-edit/products?page=1&pageSize=24&facets=true`,
|
|
1110
|
+
]);
|
|
1111
|
+
});
|
|
1112
|
+
});
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* The response's `facets` object as the view type the panel reads (`CatalogFacets`).
|
|
1116
|
+
*
|
|
1117
|
+
* One conversion and one omission carry it: the platform counts its price span in the same minor
|
|
1118
|
+
* units as the parameters it is filtered by, while every money field in `types.ts` is major; and
|
|
1119
|
+
* an absent `availability` means stock could not be read at all, which is not two zeroes.
|
|
1120
|
+
*/
|
|
1121
|
+
describe('collectionProducts reads the response’s facets', () => {
|
|
1122
|
+
const FACETS = {
|
|
1123
|
+
price: { min: 2400, max: 16400 },
|
|
1124
|
+
categories: [{ id: 'cat-1', slug: 'ceramics', title: 'Ceramics', count: 12 }],
|
|
1125
|
+
collections: [{ id: 'col-1', slug: 'the-winter-edit', title: 'The winter edit', count: 48 }],
|
|
1126
|
+
availability: { in_stock: 44, out_of_stock: 4 },
|
|
1127
|
+
options: [
|
|
1128
|
+
{
|
|
1129
|
+
key: 'colour',
|
|
1130
|
+
name: 'Colour',
|
|
1131
|
+
kind: 'color',
|
|
1132
|
+
values: [
|
|
1133
|
+
{ value: 'oat', label: 'Oat', count: 9, swatch: '#d8cbb0' },
|
|
1134
|
+
{ value: 'clay', label: 'Clay', count: 0 },
|
|
1135
|
+
],
|
|
1136
|
+
},
|
|
1137
|
+
],
|
|
1138
|
+
};
|
|
1139
|
+
|
|
1140
|
+
function clientWithFacets(facets: unknown): EldraClient {
|
|
1141
|
+
return {
|
|
1142
|
+
catalog: {
|
|
1143
|
+
listCollectionProducts: async () => ({
|
|
1144
|
+
data: [],
|
|
1145
|
+
meta: { page: 1, pageSize: 24, total: 0, totalPages: 0, rows: 0 },
|
|
1146
|
+
...(facets === undefined ? {} : { facets }),
|
|
1147
|
+
}),
|
|
1148
|
+
},
|
|
1149
|
+
} as unknown as EldraClient;
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
async function facetsFor(
|
|
1153
|
+
facets: unknown,
|
|
1154
|
+
commerce = USD_COMMERCE
|
|
1155
|
+
): Promise<CatalogFacets | undefined> {
|
|
1156
|
+
const storefront = createGatewayStorefront(clientWithFacets(facets), {
|
|
1157
|
+
route: fakeRoute(),
|
|
1158
|
+
commerce,
|
|
1159
|
+
});
|
|
1160
|
+
const result = storefront.catalog.collectionProducts(
|
|
1161
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
1162
|
+
ref({ page: 1, pageSize: 24 })
|
|
1163
|
+
);
|
|
1164
|
+
await settle();
|
|
1165
|
+
expect(result.error.value).toBeNull();
|
|
1166
|
+
return result.data.value?.facets;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
it('converts the price span to major units and passes the counts through', async () => {
|
|
1170
|
+
expect(await facetsFor(FACETS)).toEqual({
|
|
1171
|
+
// $24–$164, from the platform's own 2400–16400 minor units.
|
|
1172
|
+
price: { min: 24, max: 164 },
|
|
1173
|
+
categories: [{ id: 'cat-1', slug: 'ceramics', title: 'Ceramics', count: 12 }],
|
|
1174
|
+
collections: [
|
|
1175
|
+
{ id: 'col-1', slug: 'the-winter-edit', title: 'The winter edit', count: 48 },
|
|
1176
|
+
],
|
|
1177
|
+
availability: { in_stock: 44, out_of_stock: 4 },
|
|
1178
|
+
options: [
|
|
1179
|
+
{
|
|
1180
|
+
key: 'colour',
|
|
1181
|
+
name: 'Colour',
|
|
1182
|
+
kind: 'color',
|
|
1183
|
+
values: [
|
|
1184
|
+
{ value: 'oat', label: 'Oat', count: 9, swatch: '#d8cbb0' },
|
|
1185
|
+
{ value: 'clay', label: 'Clay', count: 0 },
|
|
1186
|
+
],
|
|
1187
|
+
},
|
|
1188
|
+
],
|
|
1189
|
+
});
|
|
1190
|
+
|
|
1191
|
+
// The same span on a store with no minor unit is the same number, not a hundredth of it.
|
|
1192
|
+
expect((await facetsFor(FACETS, ISK_COMMERCE))?.price).toEqual({ min: 2400, max: 16400 });
|
|
1193
|
+
});
|
|
1194
|
+
|
|
1195
|
+
it('leaves availability out when the platform could not read stock', async () => {
|
|
1196
|
+
const { availability: _omitted, ...noStock } = FACETS;
|
|
1197
|
+
const facets = await facetsFor(noStock);
|
|
1198
|
+
|
|
1199
|
+
expect(facets).not.toHaveProperty('availability');
|
|
1200
|
+
expect(facets?.availability).toBeUndefined();
|
|
1201
|
+
// Every other family still arrives: only the one the platform could not count is missing.
|
|
1202
|
+
expect(facets?.categories).toHaveLength(1);
|
|
1203
|
+
expect(facets?.options[0]?.values).toHaveLength(2);
|
|
1204
|
+
});
|
|
1205
|
+
|
|
1206
|
+
it('carries the price histogram and the toggle facets through, counts untouched', async () => {
|
|
1207
|
+
const histogram = Array.from({ length: 24 }, (_, i) => (i % 5 === 0 ? 2 : 0));
|
|
1208
|
+
const facets = await facetsFor({
|
|
1209
|
+
price: { min: 2800, max: 15000, histogram },
|
|
1210
|
+
toggles: [{ key: 'on_sale', label: 'On sale', count: 3 }],
|
|
1211
|
+
});
|
|
1212
|
+
|
|
1213
|
+
// Counts are counts: no unit conversion, no reordering, one entry per bucket.
|
|
1214
|
+
expect(facets?.price?.histogram).toEqual(histogram);
|
|
1215
|
+
expect(facets?.toggles).toEqual([{ key: 'on_sale', label: 'On sale', count: 3 }]);
|
|
1216
|
+
});
|
|
1217
|
+
|
|
1218
|
+
it('offers no histogram and no toggles for a platform that sends none', async () => {
|
|
1219
|
+
const facets = await facetsFor({ price: { min: 2800, max: 15000 }, toggles: [] });
|
|
1220
|
+
|
|
1221
|
+
// The span itself still converts to major units like any other read in this fixture.
|
|
1222
|
+
expect(facets?.price).toEqual({ min: 28, max: 150 });
|
|
1223
|
+
expect(facets?.price).not.toHaveProperty('histogram');
|
|
1224
|
+
expect(facets).not.toHaveProperty('toggles');
|
|
1225
|
+
});
|
|
1226
|
+
|
|
1227
|
+
it('answers no facets at all for a gateway that sent none', async () => {
|
|
1228
|
+
expect(await facetsFor(undefined)).toBeUndefined();
|
|
1229
|
+
expect(await facetsFor(null)).toBeUndefined();
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
/**
|
|
1233
|
+
* The live shape when the scope minus the price filter holds nothing: the platform omits `price`
|
|
1234
|
+
* entirely (its own contract note says to read an absent key as absent, never as a zero), and so
|
|
1235
|
+
* does this mapping — a 0–0 span is a dead track labelled in the store's currency, and writing
|
|
1236
|
+
* one would also wipe the panel's "widest span seen for this collection" fallback, which exists
|
|
1237
|
+
* for exactly this answer.
|
|
1238
|
+
*/
|
|
1239
|
+
it('leaves the price span out when the platform could not span the scope', async () => {
|
|
1240
|
+
const facets = await facetsFor({ availability: { in_stock: 0, out_of_stock: 0 } });
|
|
1241
|
+
|
|
1242
|
+
expect(facets).not.toHaveProperty('price');
|
|
1243
|
+
expect(facets?.price).toBeUndefined();
|
|
1244
|
+
expect(facets?.availability).toEqual({ in_stock: 0, out_of_stock: 0 });
|
|
1245
|
+
// The arrays the platform also omits read as empty, which is what its contract says to do.
|
|
1246
|
+
expect(facets?.categories).toEqual([]);
|
|
1247
|
+
expect(facets?.collections).toEqual([]);
|
|
1248
|
+
expect(facets?.options).toEqual([]);
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
it('falls back to the stable key or slug for a field the response leaves out', async () => {
|
|
1252
|
+
const facets = await facetsFor({
|
|
1253
|
+
price: {},
|
|
1254
|
+
categories: [{ slug: 'ceramics', count: 2 }],
|
|
1255
|
+
options: [{ key: 'size', values: [{ value: 'm' }] }],
|
|
1256
|
+
});
|
|
1257
|
+
|
|
1258
|
+
expect(facets?.price).toEqual({ min: 0, max: 0 });
|
|
1259
|
+
expect(facets?.categories).toEqual([
|
|
1260
|
+
{ id: 'ceramics', slug: 'ceramics', title: 'ceramics', count: 2 },
|
|
1261
|
+
]);
|
|
1262
|
+
expect(facets?.collections).toEqual([]);
|
|
1263
|
+
// `kind` absent on the wire is `none`, the display every option had before the field
|
|
1264
|
+
// existed — not a guess from the key, and not a refusal.
|
|
1265
|
+
expect(facets?.options).toEqual([
|
|
1266
|
+
{
|
|
1267
|
+
key: 'size',
|
|
1268
|
+
name: 'size',
|
|
1269
|
+
kind: 'none',
|
|
1270
|
+
values: [{ value: 'm', label: 'm', count: 0 }],
|
|
1271
|
+
},
|
|
1272
|
+
]);
|
|
1273
|
+
});
|
|
1274
|
+
|
|
1275
|
+
/**
|
|
1276
|
+
* **A cleared colour is an absent one on the facet path too.** `swatch` is nullable on the wire
|
|
1277
|
+
* and `CatalogFacetOptionValue.swatch` is `string | undefined`, so a `!== undefined` test would
|
|
1278
|
+
* write `swatch: null` into it and the panel would bind `backgroundColor: null` on a dot. The
|
|
1279
|
+
* mapping uses the same truthiness test the product read does, on the same field — and with no
|
|
1280
|
+
* swatch left in the family the group draws pills rather than blank dots, which is
|
|
1281
|
+
* `facetTypeFor`'s own rule.
|
|
1282
|
+
*/
|
|
1283
|
+
it('drops a null or empty facet swatch rather than writing it through', async () => {
|
|
1284
|
+
const facets = await facetsFor({
|
|
1285
|
+
options: [
|
|
1286
|
+
{
|
|
1287
|
+
key: 'colour',
|
|
1288
|
+
name: 'Colour',
|
|
1289
|
+
kind: 'color',
|
|
1290
|
+
values: [
|
|
1291
|
+
{ value: 'oat', label: 'Oat', count: 3, swatch: null },
|
|
1292
|
+
{ value: 'clay', label: 'Clay', count: 1, swatch: '' },
|
|
1293
|
+
{ value: 'moss', label: 'Moss', count: 2, swatch: '#6b7a4f' },
|
|
1294
|
+
],
|
|
1295
|
+
},
|
|
1296
|
+
],
|
|
1297
|
+
});
|
|
1298
|
+
|
|
1299
|
+
expect(facets?.options[0]?.kind).toBe('color');
|
|
1300
|
+
expect(facets?.options[0]?.values).toEqual([
|
|
1301
|
+
{ value: 'oat', label: 'Oat', count: 3 },
|
|
1302
|
+
{ value: 'clay', label: 'Clay', count: 1 },
|
|
1303
|
+
{ value: 'moss', label: 'Moss', count: 2, swatch: '#6b7a4f' },
|
|
1304
|
+
]);
|
|
1305
|
+
for (const value of facets?.options[0]?.values ?? []) {
|
|
1306
|
+
expect(value.swatch).not.toBeNull();
|
|
1307
|
+
}
|
|
1308
|
+
});
|
|
1309
|
+
});
|
|
1310
|
+
|
|
1311
|
+
/**
|
|
1312
|
+
* **A filtered read that fails keeps the page the shopper is looking at.**
|
|
1313
|
+
*
|
|
1314
|
+
* The platform refuses an `availability` filter it cannot answer rather than quietly dropping it —
|
|
1315
|
+
* an unfiltered page would read to a shopper like a shop with nothing out of stock — so this is
|
|
1316
|
+
* the one filter that can turn a working grid into an error. The grid's own answer is already the
|
|
1317
|
+
* right one: the error is reported and the last good page stays on screen
|
|
1318
|
+
* (`blocks/collection-grid` draws the toast, never an empty grid).
|
|
1319
|
+
*/
|
|
1320
|
+
it('reports a failed filtered read and keeps the last good page', async () => {
|
|
1321
|
+
let reads = 0;
|
|
1322
|
+
const client = {
|
|
1323
|
+
catalog: {
|
|
1324
|
+
listCollectionProducts: async () => {
|
|
1325
|
+
reads += 1;
|
|
1326
|
+
if (reads > 1) {
|
|
1327
|
+
throw new EldraHttpError(
|
|
1328
|
+
{ status: 503, statusText: 'Service Unavailable' } as Response,
|
|
1329
|
+
{ detail: 'inventory is unreachable' }
|
|
1330
|
+
);
|
|
1331
|
+
}
|
|
1332
|
+
return {
|
|
1333
|
+
data: [
|
|
1334
|
+
{
|
|
1335
|
+
id: 'p1',
|
|
1336
|
+
slug: 'ash-glaze-mug',
|
|
1337
|
+
title: 'Ash glaze mug',
|
|
1338
|
+
status: 'ACTIVE',
|
|
1339
|
+
minPrice: 42,
|
|
1340
|
+
maxPrice: 42,
|
|
1341
|
+
totalVariants: 1,
|
|
1342
|
+
},
|
|
1343
|
+
],
|
|
1344
|
+
meta: { page: 1, pageSize: 24, total: 1, totalPages: 1, rows: 1 },
|
|
1345
|
+
};
|
|
1346
|
+
},
|
|
1347
|
+
},
|
|
1348
|
+
} as unknown as EldraClient;
|
|
1349
|
+
const storefront = createGatewayStorefront(client, {
|
|
1350
|
+
route: fakeRoute(),
|
|
1351
|
+
commerce: ISK_COMMERCE,
|
|
1352
|
+
});
|
|
1353
|
+
const opts = ref<{
|
|
1354
|
+
page: number;
|
|
1355
|
+
pageSize: number;
|
|
1356
|
+
filters?: Record<string, string[]>;
|
|
1357
|
+
}>({ page: 1, pageSize: 24 });
|
|
1358
|
+
const result = storefront.catalog.collectionProducts(
|
|
1359
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
1360
|
+
opts
|
|
1361
|
+
);
|
|
1362
|
+
await settle();
|
|
1363
|
+
expect(result.data.value?.items.map((item) => item.handle)).toEqual(['ash-glaze-mug']);
|
|
1364
|
+
|
|
1365
|
+
opts.value = { page: 1, pageSize: 24, filters: { availability: ['in_stock'] } };
|
|
1366
|
+
await settle();
|
|
1367
|
+
|
|
1368
|
+
expect(result.error.value).toBe('Web Studio request failed with 503 Service Unavailable');
|
|
1369
|
+
expect(result.data.value?.items.map((item) => item.handle)).toEqual(['ash-glaze-mug']);
|
|
1370
|
+
expect(result.pending.value).toBe(false);
|
|
1371
|
+
});
|
|
1372
|
+
|
|
1373
|
+
/** The category lookup is part of the filtered read, so its failure is the read's failure — not a
|
|
1374
|
+
* request that silently drops the category the chips say is applied. */
|
|
1375
|
+
it('reports a failed category lookup rather than dropping the filter', async () => {
|
|
1376
|
+
const client = {
|
|
1377
|
+
catalog: {
|
|
1378
|
+
listCategories: async () => {
|
|
1379
|
+
throw new Error('categories are unreachable');
|
|
1380
|
+
},
|
|
1381
|
+
listCollectionProducts: async () => ({
|
|
1382
|
+
data: [],
|
|
1383
|
+
meta: { page: 1, pageSize: 24, total: 0, totalPages: 0, rows: 0 },
|
|
1384
|
+
}),
|
|
1385
|
+
},
|
|
1386
|
+
} as unknown as EldraClient;
|
|
1387
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
1388
|
+
const result = storefront.catalog.collectionProducts(
|
|
1389
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
1390
|
+
ref({ page: 1, pageSize: 24, filters: { category: ['ceramics'] } })
|
|
1391
|
+
);
|
|
1392
|
+
await settle();
|
|
1393
|
+
|
|
1394
|
+
expect(result.error.value).toBe('categories are unreachable');
|
|
1395
|
+
expect(result.data.value).toBeNull();
|
|
1396
|
+
});
|
|
1397
|
+
/**
|
|
1398
|
+
* `volatileByIds` — the batched read the prerender/refresh contract is built on
|
|
1399
|
+
* (`app/storefront/volatile.ts`, `types.ts`'s `StorefrontResult` doc comment). It is asserted at
|
|
1400
|
+
* the URL the SDK actually sends, not just the query object, for the same reason `byHandles` is:
|
|
1401
|
+
* `filter` is the gateway's one repeatable parameter, and a comma-joined pair of tokens is a
|
|
1402
|
+
* single malformed filter rather than two.
|
|
1403
|
+
*/
|
|
1404
|
+
it('volatileByIds asks for one chunk per 50 ids, one `id:in:` token each', async () => {
|
|
1405
|
+
const urls: string[] = [];
|
|
1406
|
+
const client = createEldraClient({
|
|
1407
|
+
apiBaseUrl: 'https://api.example.test/api',
|
|
1408
|
+
orgId: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
|
|
1409
|
+
httpClient: (async (request: EldraHttpRequest) => {
|
|
1410
|
+
urls.push(request.url);
|
|
1411
|
+
return { data: [], meta: { page: 1, pageSize: 50, total: 0, totalPages: 0, rows: 0 } };
|
|
1412
|
+
}) as never,
|
|
1413
|
+
});
|
|
1414
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
1415
|
+
const ids = Array.from({ length: 51 }, (_unused, index) => `id-${index + 1}`);
|
|
1416
|
+
await storefront.catalog.volatileByIds(ids);
|
|
1417
|
+
|
|
1418
|
+
expect(urls).toHaveLength(2);
|
|
1419
|
+
const filters = urls.map((url) => new URL(url).searchParams.getAll('filter'));
|
|
1420
|
+
expect(filters[0]).toEqual([`id:in:${ids.slice(0, 50).join(',')}`]);
|
|
1421
|
+
expect(filters[1]).toEqual(['id:in:id-51']);
|
|
1422
|
+
// One `filter=` per chunk, and nothing else narrowing the read.
|
|
1423
|
+
for (const url of urls) expect(url.match(/filter=/g)).toHaveLength(1);
|
|
1424
|
+
});
|
|
1425
|
+
|
|
1426
|
+
/**
|
|
1427
|
+
* Availability is one of the values being refreshed, so the read must *not* filter the
|
|
1428
|
+
* catalogue down to what is buyable: a product that has just gone inactive has to come back
|
|
1429
|
+
* saying so, not vanish from the answer the way a product the request could not see would.
|
|
1430
|
+
*/
|
|
1431
|
+
it('volatileByIds does not filter by status, and maps price/availability like a card', async () => {
|
|
1432
|
+
const calls = recordingClient([], {
|
|
1433
|
+
products: () => [
|
|
1434
|
+
{
|
|
1435
|
+
...listRow('merino-crew-sweater'),
|
|
1436
|
+
id: 'p-1',
|
|
1437
|
+
minPrice: 79,
|
|
1438
|
+
maxPrice: 96,
|
|
1439
|
+
compareAtPrice: 128,
|
|
1440
|
+
},
|
|
1441
|
+
{ ...listRow('lambswool-scarf'), id: 'p-2', status: 'DRAFT' },
|
|
1442
|
+
],
|
|
1443
|
+
});
|
|
1444
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
1445
|
+
const snapshots = await storefront.catalog.volatileByIds(['p-1', 'p-2']);
|
|
1446
|
+
|
|
1447
|
+
expect(calls.productListQueries).toEqual([{ pageSize: 2, filter: ['id:in:p-1,p-2'] }]);
|
|
1448
|
+
expect(snapshots).toEqual([
|
|
1449
|
+
{
|
|
1450
|
+
id: 'p-1',
|
|
1451
|
+
price: { amount: 79, compareAt: 128, from: true },
|
|
1452
|
+
available: true,
|
|
1453
|
+
stock: 'in',
|
|
1454
|
+
},
|
|
1455
|
+
{
|
|
1456
|
+
id: 'p-2',
|
|
1457
|
+
price: { amount: 1000, compareAt: null, from: false },
|
|
1458
|
+
available: false,
|
|
1459
|
+
stock: 'out',
|
|
1460
|
+
},
|
|
1461
|
+
]);
|
|
1462
|
+
// No `inventory` key at all: the products list carries none, and absent means "unknown".
|
|
1463
|
+
for (const snapshot of snapshots) expect('inventory' in snapshot).toBe(false);
|
|
1464
|
+
});
|
|
1465
|
+
|
|
1466
|
+
it('volatileByIds makes no request for an empty page, or for ids the token grammar cannot carry', async () => {
|
|
1467
|
+
const calls = recordingClient();
|
|
1468
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
1469
|
+
|
|
1470
|
+
expect(await storefront.catalog.volatileByIds([])).toEqual([]);
|
|
1471
|
+
expect(calls.productListQueries).toEqual([]);
|
|
1472
|
+
|
|
1473
|
+
// Every id unusable: asking without the token would read the whole catalogue.
|
|
1474
|
+
expect(await storefront.catalog.volatileByIds(['a,b', 'x:y'])).toEqual([]);
|
|
1475
|
+
expect(calls.productListQueries).toEqual([]);
|
|
1476
|
+
});
|
|
1477
|
+
|
|
1478
|
+
/**
|
|
1479
|
+
* The product page's stock comes from inventory, not from the variant's publication status. A
|
|
1480
|
+
* published variant with nothing on the shelf used to read "In stock, ready to ship" next to an
|
|
1481
|
+
* Add to cart the cart service then refused for stock — the page and the cart disagreeing about
|
|
1482
|
+
* the same product.
|
|
1483
|
+
*/
|
|
1484
|
+
describe('the product page reads stock from inventory', () => {
|
|
1485
|
+
function productClient(
|
|
1486
|
+
availability: unknown,
|
|
1487
|
+
options: { variants?: Array<Record<string, unknown>> } = {}
|
|
1488
|
+
): { client: EldraClient; requests: unknown[] } {
|
|
1489
|
+
const requests: unknown[] = [];
|
|
1490
|
+
const client = {
|
|
1491
|
+
catalog: {
|
|
1492
|
+
getProduct: async () => ({
|
|
1493
|
+
id: 'prod-merino',
|
|
1494
|
+
slug: 'merino-crew-sweater',
|
|
1495
|
+
title: 'Merino crew sweater',
|
|
1496
|
+
status: 'ACTIVE',
|
|
1497
|
+
options: [
|
|
1498
|
+
{
|
|
1499
|
+
key: 'size',
|
|
1500
|
+
name: 'Size',
|
|
1501
|
+
values: [
|
|
1502
|
+
{ id: 'ov-m', key: 'm', name: 'M' },
|
|
1503
|
+
{ id: 'ov-l', key: 'l', name: 'L' },
|
|
1504
|
+
],
|
|
1505
|
+
},
|
|
1506
|
+
],
|
|
1507
|
+
variants: options.variants ?? [
|
|
1508
|
+
{
|
|
1509
|
+
id: 'var-m',
|
|
1510
|
+
status: 'ACTIVE',
|
|
1511
|
+
price: 96,
|
|
1512
|
+
optionValues: [{ id: 'x', name: 'M', optionId: 'size', optionValueId: 'ov-m' }],
|
|
1513
|
+
},
|
|
1514
|
+
{
|
|
1515
|
+
id: 'var-l',
|
|
1516
|
+
status: 'ACTIVE',
|
|
1517
|
+
price: 96,
|
|
1518
|
+
optionValues: [{ id: 'y', name: 'L', optionId: 'size', optionValueId: 'ov-l' }],
|
|
1519
|
+
},
|
|
1520
|
+
],
|
|
1521
|
+
}),
|
|
1522
|
+
},
|
|
1523
|
+
inventory: {
|
|
1524
|
+
availability: async (items: unknown) => {
|
|
1525
|
+
requests.push(items);
|
|
1526
|
+
if (availability instanceof Error) throw availability;
|
|
1527
|
+
return availability;
|
|
1528
|
+
},
|
|
1529
|
+
},
|
|
1530
|
+
} as unknown as EldraClient;
|
|
1531
|
+
return { client, requests };
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
async function read(client: EldraClient) {
|
|
1535
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
1536
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
1537
|
+
await settle();
|
|
1538
|
+
return result.data.value;
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
it('asks about every variant at once, with no location id', async () => {
|
|
1542
|
+
const { client, requests } = productClient({
|
|
1543
|
+
items: [
|
|
1544
|
+
{ variantId: 'var-m', available: true, allowBackorder: false, availableQuantity: 4 },
|
|
1545
|
+
{ variantId: 'var-l', available: true, allowBackorder: false, availableQuantity: 9 },
|
|
1546
|
+
],
|
|
1547
|
+
});
|
|
1548
|
+
await read(client);
|
|
1549
|
+
// One bulk call, and no `locationId`: an item without one resolves the org's default location.
|
|
1550
|
+
expect(requests).toEqual([[{ variantId: 'var-m' }, { variantId: 'var-l' }]]);
|
|
1551
|
+
});
|
|
1552
|
+
|
|
1553
|
+
it('keeps the first in-stock variant as the one to buy', async () => {
|
|
1554
|
+
const { client } = productClient({
|
|
1555
|
+
items: [
|
|
1556
|
+
{ variantId: 'var-m', available: true, allowBackorder: false, availableQuantity: 2 },
|
|
1557
|
+
{ variantId: 'var-l', available: true, allowBackorder: false, availableQuantity: 9 },
|
|
1558
|
+
],
|
|
1559
|
+
});
|
|
1560
|
+
const product = await read(client);
|
|
1561
|
+
expect(product?.stock).toBe('in');
|
|
1562
|
+
expect(product?.variantId).toBe('var-m');
|
|
1563
|
+
});
|
|
1564
|
+
|
|
1565
|
+
/**
|
|
1566
|
+
* `inventory` is the count of the variant the buy box is *selling*, and the mapping can only
|
|
1567
|
+
* speak for the one it chose — not for whichever the shopper picks in the picker. So a product
|
|
1568
|
+
* with options reports no count rather than labelling M's two units "only 2 left in L"; a product
|
|
1569
|
+
* with one thing to buy reports it, which is where the low-stock line comes from.
|
|
1570
|
+
*/
|
|
1571
|
+
it('reports a unit count only when there is one variant it can belong to', async () => {
|
|
1572
|
+
const withOptions = productClient({
|
|
1573
|
+
items: [
|
|
1574
|
+
{ variantId: 'var-m', available: true, allowBackorder: false, availableQuantity: 2 },
|
|
1575
|
+
{ variantId: 'var-l', available: true, allowBackorder: false, availableQuantity: 9 },
|
|
1576
|
+
],
|
|
1577
|
+
});
|
|
1578
|
+
expect((await read(withOptions.client))?.inventory).toBeNull();
|
|
1579
|
+
|
|
1580
|
+
const single = productClient(
|
|
1581
|
+
{
|
|
1582
|
+
items: [
|
|
1583
|
+
{ variantId: 'var-only', available: true, allowBackorder: false, availableQuantity: 2 },
|
|
1584
|
+
],
|
|
1585
|
+
},
|
|
1586
|
+
{ variants: [{ id: 'var-only', status: 'ACTIVE', price: 96 }] }
|
|
1587
|
+
);
|
|
1588
|
+
const product = await read(single.client);
|
|
1589
|
+
expect(product?.inventory).toBe(2);
|
|
1590
|
+
expect(product?.variantId).toBe('var-only');
|
|
1591
|
+
expect(product?.stock).toBe('in');
|
|
1592
|
+
});
|
|
1593
|
+
|
|
1594
|
+
it('reads sold out for a published variant with nothing on the shelf', async () => {
|
|
1595
|
+
const { client } = productClient({
|
|
1596
|
+
items: [
|
|
1597
|
+
{ variantId: 'var-m', available: false, allowBackorder: false, availableQuantity: 0 },
|
|
1598
|
+
{ variantId: 'var-l', available: false, allowBackorder: false, availableQuantity: 0 },
|
|
1599
|
+
],
|
|
1600
|
+
});
|
|
1601
|
+
const product = await read(client);
|
|
1602
|
+
expect(product?.stock).toBe('out');
|
|
1603
|
+
// Both option values are unbuyable, so the pickers say so too (spec: struck through, ", sold
|
|
1604
|
+
// out" in the accessible name) instead of offering a choice that cannot be fulfilled.
|
|
1605
|
+
expect(product?.options[0]?.values.map((value) => value.available)).toEqual([false, false]);
|
|
1606
|
+
});
|
|
1607
|
+
|
|
1608
|
+
it('opens on the first variant that can be bought, not the first published one', async () => {
|
|
1609
|
+
const { client } = productClient({
|
|
1610
|
+
items: [
|
|
1611
|
+
{ variantId: 'var-m', available: false, allowBackorder: false, availableQuantity: 0 },
|
|
1612
|
+
{ variantId: 'var-l', available: true, allowBackorder: false, availableQuantity: 5 },
|
|
1613
|
+
],
|
|
1614
|
+
});
|
|
1615
|
+
const product = await read(client);
|
|
1616
|
+
expect(product?.variantId).toBe('var-l');
|
|
1617
|
+
expect(product?.stock).toBe('in');
|
|
1618
|
+
expect(product?.options[0]?.values.map((value) => value.available)).toEqual([false, true]);
|
|
1619
|
+
});
|
|
1620
|
+
|
|
1621
|
+
it('reads back-order for a variant that is out but takes orders anyway', async () => {
|
|
1622
|
+
const { client } = productClient({
|
|
1623
|
+
items: [
|
|
1624
|
+
{ variantId: 'var-m', available: false, allowBackorder: true, availableQuantity: 0 },
|
|
1625
|
+
{ variantId: 'var-l', available: false, allowBackorder: true, availableQuantity: 0 },
|
|
1626
|
+
],
|
|
1627
|
+
});
|
|
1628
|
+
const product = await read(client);
|
|
1629
|
+
expect(product?.stock).toBe('preorder');
|
|
1630
|
+
});
|
|
1631
|
+
|
|
1632
|
+
/**
|
|
1633
|
+
* Fail-soft, three ways: the read threw, the read answered about nothing, and the read answered
|
|
1634
|
+
* about other variants. Each keeps the page exactly as it was before inventory was consulted —
|
|
1635
|
+
* a store that has not set stock up, or a service that is down, must not read sold out.
|
|
1636
|
+
*/
|
|
1637
|
+
it('keeps the status-derived stock when inventory cannot answer', async () => {
|
|
1638
|
+
for (const answer of [
|
|
1639
|
+
new Error('inventory unavailable'),
|
|
1640
|
+
{ items: [] },
|
|
1641
|
+
{ items: null },
|
|
1642
|
+
{
|
|
1643
|
+
items: [
|
|
1644
|
+
{
|
|
1645
|
+
variantId: 'var-other',
|
|
1646
|
+
available: false,
|
|
1647
|
+
allowBackorder: false,
|
|
1648
|
+
availableQuantity: 0,
|
|
1649
|
+
},
|
|
1650
|
+
],
|
|
1651
|
+
},
|
|
1652
|
+
]) {
|
|
1653
|
+
const { client } = productClient(answer);
|
|
1654
|
+
const product = await read(client);
|
|
1655
|
+
expect(product?.stock).toBe('in');
|
|
1656
|
+
expect(product?.inventory).toBeNull();
|
|
1657
|
+
expect(product?.variantId).toBe('var-m');
|
|
1658
|
+
expect(product?.options[0]?.values.map((value) => value.available)).toEqual([true, true]);
|
|
1659
|
+
}
|
|
1660
|
+
});
|
|
1661
|
+
|
|
1662
|
+
it('reads sold out for a product whose variants are all unpublished, inventory or not', async () => {
|
|
1663
|
+
const { client } = productClient(new Error('down'), {
|
|
1664
|
+
variants: [{ id: 'var-m', status: 'DRAFT', price: 96 }],
|
|
1665
|
+
});
|
|
1666
|
+
const product = await read(client);
|
|
1667
|
+
expect(product?.stock).toBe('out');
|
|
1668
|
+
});
|
|
1669
|
+
});
|
|
1670
|
+
|
|
1671
|
+
/**
|
|
1672
|
+
* **How a variant option is drawn is the merchant's choice, carried on the option itself.**
|
|
1673
|
+
*
|
|
1674
|
+
* Every option used to map to `pills`, so the only way a theme could draw the colour circles a
|
|
1675
|
+
* clothing store needs was to match the option's *name* against "Colour"/"Color" — a merchant's
|
|
1676
|
+
* word, theirs to translate and theirs to change, which is exactly the guess this field replaces.
|
|
1677
|
+
* The platform now stores `kind` (`none` | `color` | `custom`) on the option and a hex `swatch` on
|
|
1678
|
+
* each value, and `app/storefront/options.ts` is the one place a kind becomes a control.
|
|
1679
|
+
*/
|
|
1680
|
+
describe('variant options are drawn by their display kind', () => {
|
|
1681
|
+
function optionsClient(options: unknown): EldraClient {
|
|
1682
|
+
return {
|
|
1683
|
+
catalog: {
|
|
1684
|
+
getProduct: async () => ({
|
|
1685
|
+
id: 'prod-merino',
|
|
1686
|
+
slug: 'merino-crew-sweater',
|
|
1687
|
+
title: 'Merino crew sweater',
|
|
1688
|
+
status: 'ACTIVE',
|
|
1689
|
+
options,
|
|
1690
|
+
variants: [{ id: 'var-1', status: 'ACTIVE', price: 96 }],
|
|
1691
|
+
}),
|
|
1692
|
+
},
|
|
1693
|
+
} as unknown as EldraClient;
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
async function optionsOf(options: unknown) {
|
|
1697
|
+
const storefront = createGatewayStorefront(optionsClient(options), { route: fakeRoute() });
|
|
1698
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
1699
|
+
await settle();
|
|
1700
|
+
expect(result.error.value).toBeNull();
|
|
1701
|
+
return result.data.value?.options ?? [];
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
it('draws a color option as swatches and keeps every value’s colour', async () => {
|
|
1705
|
+
const mapped = await optionsOf([
|
|
1706
|
+
{
|
|
1707
|
+
id: 'opt-1',
|
|
1708
|
+
key: 'colour',
|
|
1709
|
+
name: 'Colour',
|
|
1710
|
+
kind: 'color',
|
|
1711
|
+
values: [
|
|
1712
|
+
{ id: 'ov-oat', key: 'oat', name: 'Oat', swatch: '#d8cbb0' },
|
|
1713
|
+
{ id: 'ov-moss', key: 'moss', name: 'Moss', swatch: '#6b7a4f' },
|
|
1714
|
+
],
|
|
1715
|
+
},
|
|
1716
|
+
]);
|
|
1717
|
+
|
|
1718
|
+
expect(mapped[0]).toMatchObject({ name: 'colour', label: 'Colour', kind: 'color' });
|
|
1719
|
+
expect(mapped[0]?.type).toBe('swatches');
|
|
1720
|
+
expect(mapped[0]?.values.map((value) => [value.value, value.swatch])).toEqual([
|
|
1721
|
+
['oat', '#d8cbb0'],
|
|
1722
|
+
['moss', '#6b7a4f'],
|
|
1723
|
+
]);
|
|
1724
|
+
});
|
|
1725
|
+
|
|
1726
|
+
/**
|
|
1727
|
+
* **The kind is permission to show colours, not a promise that there are any.** A merchant can
|
|
1728
|
+
* set "Display as: Color" and not pick the colours, or pick them and clear them again — the
|
|
1729
|
+
* platform leaves a value's swatch nullable and Studio's own control is clearable, so both are
|
|
1730
|
+
* ordinary states. A swatch picker handed no colours draws one blank disc per value with every
|
|
1731
|
+
* name in visually hidden text, so a sighted shopper is choosing between identical empty
|
|
1732
|
+
* circles; pills read their names out loud. The option therefore stays `kind: 'color'` — the
|
|
1733
|
+
* merchant's intent is still the merchant's — while `type` falls back to pills, which is the
|
|
1734
|
+
* same decision a filter group makes from the same evidence (`groups.ts`'s `facetTypeFor`).
|
|
1735
|
+
*/
|
|
1736
|
+
it('draws a color option whose colours are unset or cleared as pills, keeping the kind', async () => {
|
|
1737
|
+
for (const values of [
|
|
1738
|
+
[{ id: 'ov-oat', key: 'oat', name: 'Oat' }],
|
|
1739
|
+
// Cleared: the platform omits the key rather than writing an empty value, but a `null` or an
|
|
1740
|
+
// empty string from a source mid-migration is the same absence and must read the same way.
|
|
1741
|
+
[
|
|
1742
|
+
{ id: 'ov-oat', key: 'oat', name: 'Oat', swatch: null },
|
|
1743
|
+
{ id: 'ov-moss', key: 'moss', name: 'Moss', swatch: '' },
|
|
1744
|
+
],
|
|
1745
|
+
]) {
|
|
1746
|
+
const mapped = await optionsOf([
|
|
1747
|
+
{ id: 'opt-1', key: 'colour', name: 'Colour', kind: 'color', values },
|
|
1748
|
+
]);
|
|
1749
|
+
expect(mapped[0]?.kind).toBe('color');
|
|
1750
|
+
expect(mapped[0]?.type).toBe('pills');
|
|
1751
|
+
// No swatch written either, so nothing downstream can read one that is not a colour.
|
|
1752
|
+
expect(mapped[0]?.values.every((value) => value.swatch === undefined)).toBe(true);
|
|
1753
|
+
}
|
|
1754
|
+
});
|
|
1755
|
+
|
|
1756
|
+
/**
|
|
1757
|
+
* One colour is enough to earn the swatch control, and **the value that has none keeps its
|
|
1758
|
+
* place** — uncoloured, exactly as such a row survives in a filter panel's colour group rather
|
|
1759
|
+
* than being dropped. Dropping it would hide a variant the shopper can buy; the merchant's fix
|
|
1760
|
+
* is to fill the colour in, or to clear the rest and get pills.
|
|
1761
|
+
*/
|
|
1762
|
+
it('draws swatches when any value has a colour, and keeps a colourless value in place', async () => {
|
|
1763
|
+
const mapped = await optionsOf([
|
|
1764
|
+
{
|
|
1765
|
+
id: 'opt-1',
|
|
1766
|
+
key: 'colour',
|
|
1767
|
+
name: 'Colour',
|
|
1768
|
+
kind: 'color',
|
|
1769
|
+
values: [
|
|
1770
|
+
{ id: 'ov-oat', key: 'oat', name: 'Oat', swatch: '#d8cbb0' },
|
|
1771
|
+
{ id: 'ov-unset', key: 'unset', name: 'Unset' },
|
|
1772
|
+
],
|
|
1773
|
+
},
|
|
1774
|
+
]);
|
|
1775
|
+
|
|
1776
|
+
expect(mapped[0]?.type).toBe('swatches');
|
|
1777
|
+
expect(mapped[0]?.values.map((value) => [value.value, value.swatch])).toEqual([
|
|
1778
|
+
['oat', '#d8cbb0'],
|
|
1779
|
+
['unset', undefined],
|
|
1780
|
+
]);
|
|
1781
|
+
});
|
|
1782
|
+
|
|
1783
|
+
/** A kind with no colours to its name is still pills, whichever kind it is. */
|
|
1784
|
+
it('never draws swatches for a kind other than color, swatches or not', async () => {
|
|
1785
|
+
for (const kind of ['none', 'custom']) {
|
|
1786
|
+
const mapped = await optionsOf([
|
|
1787
|
+
{
|
|
1788
|
+
id: 'opt-1',
|
|
1789
|
+
key: 'colour',
|
|
1790
|
+
name: 'Colour',
|
|
1791
|
+
kind,
|
|
1792
|
+
...(kind === 'custom' ? { metadata: 'chip' } : {}),
|
|
1793
|
+
values: [{ id: 'ov-oat', key: 'oat', name: 'Oat', swatch: '#d8cbb0' }],
|
|
1794
|
+
},
|
|
1795
|
+
]);
|
|
1796
|
+
expect(mapped[0]?.type).toBe('pills');
|
|
1797
|
+
}
|
|
1798
|
+
});
|
|
1799
|
+
|
|
1800
|
+
/**
|
|
1801
|
+
* The defect this whole field exists to remove, stated as a test: an option a merchant named
|
|
1802
|
+
* "Colour" and left on None is a list of words, and nothing in the mapping may decide otherwise.
|
|
1803
|
+
* Both spellings and both cases are here because every one of them was a key a storefront used
|
|
1804
|
+
* to match on.
|
|
1805
|
+
*/
|
|
1806
|
+
it('never reads the control off the option’s name', async () => {
|
|
1807
|
+
for (const key of ['colour', 'color', 'Colour', 'Color']) {
|
|
1808
|
+
const mapped = await optionsOf([
|
|
1809
|
+
{
|
|
1810
|
+
id: 'opt-1',
|
|
1811
|
+
key,
|
|
1812
|
+
name: key,
|
|
1813
|
+
kind: 'none',
|
|
1814
|
+
values: [{ id: 'ov-oat', key: 'oat', name: 'Oat' }],
|
|
1815
|
+
},
|
|
1816
|
+
]);
|
|
1817
|
+
expect(mapped[0]?.kind).toBe('none');
|
|
1818
|
+
expect(mapped[0]?.type).toBe('pills');
|
|
1819
|
+
}
|
|
1820
|
+
});
|
|
1821
|
+
|
|
1822
|
+
/**
|
|
1823
|
+
* Two absences that read the same way, and must: a gateway answering a contract older than the
|
|
1824
|
+
* field sends no `kind` at all, and a kind this theme cannot draw is one it must not try to. Both
|
|
1825
|
+
* are `none` — the display every option had before any of this existed — rather than a throw
|
|
1826
|
+
* inside the product mapping, which would take a whole page down over a presentational field.
|
|
1827
|
+
*/
|
|
1828
|
+
it('reads an absent or unknown kind as none rather than failing the page', async () => {
|
|
1829
|
+
for (const kind of [undefined, null, '', 'swatches', 'image', 42]) {
|
|
1830
|
+
const mapped = await optionsOf([
|
|
1831
|
+
{
|
|
1832
|
+
id: 'opt-1',
|
|
1833
|
+
key: 'colour',
|
|
1834
|
+
name: 'Colour',
|
|
1835
|
+
...(kind === undefined ? {} : { kind }),
|
|
1836
|
+
values: [{ id: 'ov-oat', key: 'oat', name: 'Oat', swatch: '#d8cbb0' }],
|
|
1837
|
+
},
|
|
1838
|
+
]);
|
|
1839
|
+
expect(mapped[0]?.kind).toBe('none');
|
|
1840
|
+
expect(mapped[0]?.type).toBe('pills');
|
|
1841
|
+
// The colour still rides along — only what is *shown* depends on the kind, so a gateway
|
|
1842
|
+
// that populates the swatch before it populates the kind loses nothing.
|
|
1843
|
+
expect(mapped[0]?.values[0]?.swatch).toBe('#d8cbb0');
|
|
1844
|
+
}
|
|
1845
|
+
});
|
|
1846
|
+
|
|
1847
|
+
it('draws a custom option as pills and carries the merchant’s own name for it', async () => {
|
|
1848
|
+
const mapped = await optionsOf([
|
|
1849
|
+
{
|
|
1850
|
+
id: 'opt-1',
|
|
1851
|
+
key: 'fabric',
|
|
1852
|
+
name: 'Fabric',
|
|
1853
|
+
kind: 'custom',
|
|
1854
|
+
metadata: 'fabric-chip',
|
|
1855
|
+
values: [{ id: 'ov-merino', key: 'merino', name: 'Merino' }],
|
|
1856
|
+
},
|
|
1857
|
+
]);
|
|
1858
|
+
|
|
1859
|
+
expect(mapped[0]).toMatchObject({ kind: 'custom', metadata: 'fabric-chip' });
|
|
1860
|
+
expect(mapped[0]?.type).toBe('pills');
|
|
1861
|
+
});
|
|
1862
|
+
|
|
1863
|
+
/**
|
|
1864
|
+
* `metadata` is the name a merchant gives a `custom` option, and Studio offers the field under no
|
|
1865
|
+
* other kind. One arriving under `none` or `color` is therefore a name nobody can see or change,
|
|
1866
|
+
* so a theme branching on it would be branching on a ghost: the key is dropped rather than
|
|
1867
|
+
* carried.
|
|
1868
|
+
*/
|
|
1869
|
+
it('drops a metadata name sent under a kind that cannot have one', async () => {
|
|
1870
|
+
for (const kind of ['none', 'color']) {
|
|
1871
|
+
const mapped = await optionsOf([
|
|
1872
|
+
{
|
|
1873
|
+
id: 'opt-1',
|
|
1874
|
+
key: 'colour',
|
|
1875
|
+
name: 'Colour',
|
|
1876
|
+
kind,
|
|
1877
|
+
metadata: 'left-over',
|
|
1878
|
+
values: [{ id: 'ov-oat', key: 'oat', name: 'Oat' }],
|
|
1879
|
+
},
|
|
1880
|
+
]);
|
|
1881
|
+
expect(mapped[0]).not.toHaveProperty('metadata');
|
|
1882
|
+
}
|
|
1883
|
+
// An empty name is not a name either: a `custom` option the platform could not name is a
|
|
1884
|
+
// custom option with nothing to switch on, not one named "".
|
|
1885
|
+
const empty = await optionsOf([
|
|
1886
|
+
{
|
|
1887
|
+
id: 'opt-1',
|
|
1888
|
+
key: 'fabric',
|
|
1889
|
+
name: 'Fabric',
|
|
1890
|
+
kind: 'custom',
|
|
1891
|
+
metadata: '',
|
|
1892
|
+
values: [{ id: 'ov-merino', key: 'merino', name: 'Merino' }],
|
|
1893
|
+
},
|
|
1894
|
+
]);
|
|
1895
|
+
expect(empty[0]).not.toHaveProperty('metadata');
|
|
1896
|
+
expect(empty[0]?.kind).toBe('custom');
|
|
1897
|
+
});
|
|
1898
|
+
});
|
|
1899
|
+
|
|
1900
|
+
/**
|
|
1901
|
+
* **A product card's colour dots, from the same fact the product page's picker reads.**
|
|
1902
|
+
*
|
|
1903
|
+
* The catalogue's *list* read answers no options at all today, which is why the live site's cards
|
|
1904
|
+
* carry none; the mapping is written against the shape a list read would carry so a gateway that
|
|
1905
|
+
* grows the field needs no second change here. The distinction that matters is absent vs empty:
|
|
1906
|
+
* `undefined` is "this source cannot say what colours this product comes in" and `[]` is "none",
|
|
1907
|
+
* and `app/storefront/facets.ts` filters on the two differently (`optionValuesOf`).
|
|
1908
|
+
*/
|
|
1909
|
+
describe('a product card’s colour dots come from the colour-kind option', () => {
|
|
1910
|
+
function listClient(row: Record<string, unknown>): EldraClient {
|
|
1911
|
+
return {
|
|
1912
|
+
catalog: {
|
|
1913
|
+
listProducts: async () => ({
|
|
1914
|
+
data: [
|
|
1915
|
+
{
|
|
1916
|
+
id: 'prod-merino',
|
|
1917
|
+
slug: 'merino-crew-sweater',
|
|
1918
|
+
title: 'Merino crew sweater',
|
|
1919
|
+
status: 'ACTIVE',
|
|
1920
|
+
minPrice: 96,
|
|
1921
|
+
maxPrice: 96,
|
|
1922
|
+
totalVariants: 4,
|
|
1923
|
+
...row,
|
|
1924
|
+
},
|
|
1925
|
+
],
|
|
1926
|
+
meta: { page: 1, pageSize: 24, total: 1, totalPages: 1, rows: 1 },
|
|
1927
|
+
}),
|
|
1928
|
+
},
|
|
1929
|
+
} as unknown as EldraClient;
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
async function cardFor(row: Record<string, unknown>) {
|
|
1933
|
+
const storefront = createGatewayStorefront(listClient(row), { route: fakeRoute() });
|
|
1934
|
+
const result = storefront.catalog.products(ref({ page: 1, pageSize: 24 }));
|
|
1935
|
+
await settle();
|
|
1936
|
+
expect(result.error.value).toBeNull();
|
|
1937
|
+
return result.data.value?.items[0];
|
|
1938
|
+
}
|
|
1939
|
+
|
|
1940
|
+
it('names each dot after the value it belongs to', async () => {
|
|
1941
|
+
const card = await cardFor({
|
|
1942
|
+
options: [
|
|
1943
|
+
{
|
|
1944
|
+
id: 'opt-1',
|
|
1945
|
+
key: 'colour',
|
|
1946
|
+
name: 'Colour',
|
|
1947
|
+
kind: 'color',
|
|
1948
|
+
values: [
|
|
1949
|
+
{ id: 'ov-oat', key: 'oat', name: 'Oat', swatch: '#d8cbb0' },
|
|
1950
|
+
{ id: 'ov-moss', key: 'moss', name: 'Moss', swatch: '#6b7a4f' },
|
|
1951
|
+
],
|
|
1952
|
+
},
|
|
1953
|
+
],
|
|
1954
|
+
});
|
|
1955
|
+
|
|
1956
|
+
expect(card?.colours).toEqual([
|
|
1957
|
+
{ name: 'Oat', swatch: '#d8cbb0' },
|
|
1958
|
+
{ name: 'Moss', swatch: '#6b7a4f' },
|
|
1959
|
+
]);
|
|
1960
|
+
});
|
|
1961
|
+
|
|
1962
|
+
it('says nothing at all for a list read that carries no options', async () => {
|
|
1963
|
+
const card = await cardFor({});
|
|
1964
|
+
expect(card).not.toHaveProperty('colours');
|
|
1965
|
+
expect(card?.colours).toBeUndefined();
|
|
1966
|
+
});
|
|
1967
|
+
|
|
1968
|
+
/**
|
|
1969
|
+
* Known, and none — the answer that makes a colour filter exclude this product rather than
|
|
1970
|
+
* ignore it. The read carried options, so the source *can* say; it says there are no colours.
|
|
1971
|
+
*/
|
|
1972
|
+
it('answers an empty list for a product the read says has no colour option', async () => {
|
|
1973
|
+
const card = await cardFor({
|
|
1974
|
+
options: [
|
|
1975
|
+
{
|
|
1976
|
+
id: 'opt-1',
|
|
1977
|
+
key: 'size',
|
|
1978
|
+
name: 'Size',
|
|
1979
|
+
kind: 'none',
|
|
1980
|
+
values: [{ id: 'ov-m', key: 'm', name: 'M' }],
|
|
1981
|
+
},
|
|
1982
|
+
],
|
|
1983
|
+
});
|
|
1984
|
+
expect(card?.colours).toEqual([]);
|
|
1985
|
+
});
|
|
1986
|
+
|
|
1987
|
+
it('ignores an option named for a colour that the merchant left on none', async () => {
|
|
1988
|
+
const card = await cardFor({
|
|
1989
|
+
options: [
|
|
1990
|
+
{
|
|
1991
|
+
id: 'opt-1',
|
|
1992
|
+
key: 'colour',
|
|
1993
|
+
name: 'Colour',
|
|
1994
|
+
kind: 'none',
|
|
1995
|
+
values: [{ id: 'ov-oat', key: 'oat', name: 'Oat', swatch: '#d8cbb0' }],
|
|
1996
|
+
},
|
|
1997
|
+
],
|
|
1998
|
+
});
|
|
1999
|
+
expect(card?.colours).toEqual([]);
|
|
2000
|
+
});
|
|
2001
|
+
|
|
2002
|
+
/**
|
|
2003
|
+
* A dot's whole content is its colour, so a value without one is left out rather than drawn as a
|
|
2004
|
+
* blank circle beside the colours that do have one. The first colour option is the only one read:
|
|
2005
|
+
* a card has one row of dots, and concatenating two colour options would invent a list the store
|
|
2006
|
+
* never declared.
|
|
2007
|
+
*/
|
|
2008
|
+
it('leaves out a value with no colour, and reads the first colour option only', async () => {
|
|
2009
|
+
const card = await cardFor({
|
|
2010
|
+
options: [
|
|
2011
|
+
{
|
|
2012
|
+
id: 'opt-1',
|
|
2013
|
+
key: 'colour',
|
|
2014
|
+
name: 'Colour',
|
|
2015
|
+
kind: 'color',
|
|
2016
|
+
values: [
|
|
2017
|
+
{ id: 'ov-oat', key: 'oat', name: 'Oat', swatch: '#d8cbb0' },
|
|
2018
|
+
{ id: 'ov-unset', key: 'unset', name: 'Unset' },
|
|
2019
|
+
],
|
|
2020
|
+
},
|
|
2021
|
+
{
|
|
2022
|
+
id: 'opt-2',
|
|
2023
|
+
key: 'trim',
|
|
2024
|
+
name: 'Trim',
|
|
2025
|
+
kind: 'color',
|
|
2026
|
+
values: [{ id: 'ov-navy', key: 'navy', name: 'Navy', swatch: '#1f2a44' }],
|
|
2027
|
+
},
|
|
2028
|
+
],
|
|
2029
|
+
});
|
|
2030
|
+
expect(card?.colours).toEqual([{ name: 'Oat', swatch: '#d8cbb0' }]);
|
|
2031
|
+
});
|
|
2032
|
+
});
|
|
2033
|
+
|
|
2034
|
+
/**
|
|
2035
|
+
* The add payload. `productId` and `variantId` are two different ids, and the gateway's cart
|
|
2036
|
+
* service resolves them as a pair (one lookup of "this variant, of this product"): a pair that
|
|
2037
|
+
* does not exist answers 500, which is what sending the variant id as both did. The detail
|
|
2038
|
+
* mapping is the other half of the same fact — `productId` from the product, `variantId` from its
|
|
2039
|
+
* first buyable variant — so this asserts both, together, since either alone would pass while the
|
|
2040
|
+
* add stayed broken.
|
|
2041
|
+
*/
|
|
2042
|
+
it('cart.add sends the product id and the variant id as the two different ids they are', async () => {
|
|
2043
|
+
const addItemInputs: Array<Record<string, unknown>> = [];
|
|
2044
|
+
const client = {
|
|
2045
|
+
catalog: {
|
|
2046
|
+
getProduct: async () => ({
|
|
2047
|
+
id: 'prod-merino',
|
|
2048
|
+
slug: 'merino-crew-sweater',
|
|
2049
|
+
title: 'Merino crew sweater',
|
|
2050
|
+
status: 'ACTIVE',
|
|
2051
|
+
variants: [{ id: 'var-oat-m', status: 'ACTIVE', price: 96 }],
|
|
2052
|
+
}),
|
|
2053
|
+
},
|
|
2054
|
+
cart: {
|
|
2055
|
+
addItem: async (input: Record<string, unknown>) => {
|
|
2056
|
+
addItemInputs.push(input);
|
|
2057
|
+
return {
|
|
2058
|
+
id: 'cart-1',
|
|
2059
|
+
currency: 'USD',
|
|
2060
|
+
items: [
|
|
2061
|
+
{
|
|
2062
|
+
id: 'line-1',
|
|
2063
|
+
productId: 'prod-merino',
|
|
2064
|
+
variantId: 'var-oat-m',
|
|
2065
|
+
title: 'Merino crew sweater',
|
|
2066
|
+
price: 96,
|
|
2067
|
+
quantity: 2,
|
|
2068
|
+
},
|
|
2069
|
+
],
|
|
2070
|
+
totals: { subtotal: 192, discount: 0, taxAmount: 0, total: 192 },
|
|
2071
|
+
};
|
|
2072
|
+
},
|
|
2073
|
+
},
|
|
2074
|
+
checkout: { url: async () => 'https://checkout.example/cart-1' },
|
|
2075
|
+
} as unknown as EldraClient;
|
|
2076
|
+
|
|
2077
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
2078
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
2079
|
+
await settle();
|
|
2080
|
+
|
|
2081
|
+
const product = result.data.value;
|
|
2082
|
+
expect(product?.productId).toBe('prod-merino');
|
|
2083
|
+
expect(product?.variantId).toBe('var-oat-m');
|
|
2084
|
+
|
|
2085
|
+
await storefront.cart.add({
|
|
2086
|
+
productId: product!.productId,
|
|
2087
|
+
variantId: product!.variantId,
|
|
2088
|
+
quantity: 2,
|
|
2089
|
+
});
|
|
2090
|
+
|
|
2091
|
+
expect(addItemInputs).toEqual([
|
|
2092
|
+
{ cartId: undefined, productId: 'prod-merino', variantId: 'var-oat-m', quantity: 2 },
|
|
2093
|
+
]);
|
|
2094
|
+
// And the line that comes back carries both halves, so Undo can re-add exactly this pair.
|
|
2095
|
+
expect(storefront.cart.lines.value).toEqual([
|
|
2096
|
+
expect.objectContaining({ productId: 'prod-merino', variantId: 'var-oat-m' }),
|
|
2097
|
+
]);
|
|
2098
|
+
});
|
|
2099
|
+
|
|
2100
|
+
/**
|
|
2101
|
+
* **Every merchant-written answer is read in the page's own language.** A page served under a
|
|
2102
|
+
* locale prefix renders its CMS content in that locale; if the catalog, search and order reads
|
|
2103
|
+
* did not carry it too, a visitor on `/is-IS/products/x` would read Icelandic page copy around a
|
|
2104
|
+
* product whose own title, description and option names were the default language's. The rule is
|
|
2105
|
+
* applied by wrapping the client once (`withContentLocale`), not per call site, so a read added
|
|
2106
|
+
* later cannot forget it — which is exactly what this asserts: a *handful* of reads driven here,
|
|
2107
|
+
* and `locale` on all of them.
|
|
2108
|
+
*/
|
|
2109
|
+
it('sends the active content locale on every catalog, search and order read', async () => {
|
|
2110
|
+
const queries: Array<{ what: string; query: Record<string, unknown> | undefined }> = [];
|
|
2111
|
+
const record =
|
|
2112
|
+
(what: string) =>
|
|
2113
|
+
async (...args: unknown[]) => {
|
|
2114
|
+
queries.push({ what, query: args.at(-2) as Record<string, unknown> | undefined });
|
|
2115
|
+
return { data: [], meta: { page: 1, pageSize: 24, total: 0, totalPages: 0, rows: 0 } };
|
|
2116
|
+
};
|
|
2117
|
+
const client = {
|
|
2118
|
+
catalog: {
|
|
2119
|
+
getProduct: async (_slug: string, query?: Record<string, unknown>) => {
|
|
2120
|
+
queries.push({ what: 'getProduct', query });
|
|
2121
|
+
return { id: 'p1', slug: 'merino-crew-sweater', variants: [] };
|
|
2122
|
+
},
|
|
2123
|
+
getCollection: async (_slug: string, query?: Record<string, unknown>) => {
|
|
2124
|
+
queries.push({ what: 'getCollection', query });
|
|
2125
|
+
return { id: 'c1', slug: 'winter-knitwear', title: 'Winter', productCount: 0 };
|
|
2126
|
+
},
|
|
2127
|
+
listCollectionProducts: record('listCollectionProducts'),
|
|
2128
|
+
listProducts: record('listProducts'),
|
|
2129
|
+
search: async (_q: string, query?: Record<string, unknown>) => {
|
|
2130
|
+
queries.push({ what: 'search', query });
|
|
2131
|
+
return { query: 'mug', total: 0, results: [] };
|
|
2132
|
+
},
|
|
2133
|
+
},
|
|
2134
|
+
orders: {
|
|
2135
|
+
get: async (_token: string, query?: Record<string, unknown>) => {
|
|
2136
|
+
queries.push({ what: 'orders.get', query });
|
|
2137
|
+
return { id: 'o1', orderNumber: 1, status: 'PAID', items: [], totals: {} };
|
|
2138
|
+
},
|
|
2139
|
+
},
|
|
2140
|
+
inventory: {
|
|
2141
|
+
availability: async () => ({ items: [] }),
|
|
2142
|
+
},
|
|
2143
|
+
} as unknown as EldraClient;
|
|
2144
|
+
|
|
2145
|
+
const storefront = createGatewayStorefront(client, {
|
|
2146
|
+
route: fakeRoute(),
|
|
2147
|
+
locale: () => 'is-IS',
|
|
2148
|
+
});
|
|
2149
|
+
storefront.catalog.product(ref('merino-crew-sweater'));
|
|
2150
|
+
storefront.catalog.collection(ref('winter-knitwear'));
|
|
2151
|
+
storefront.catalog.collectionProducts(
|
|
2152
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'winter-knitwear' }),
|
|
2153
|
+
ref({ page: 1, pageSize: 24 })
|
|
2154
|
+
);
|
|
2155
|
+
storefront.catalog.byHandles(ref(['merino-crew-sweater']));
|
|
2156
|
+
storefront.search.run(ref('mug'));
|
|
2157
|
+
storefront.orders.current(ref('token-1'));
|
|
2158
|
+
await settle();
|
|
2159
|
+
|
|
2160
|
+
expect(queries.length).toBeGreaterThanOrEqual(6);
|
|
2161
|
+
for (const { what, query } of queries) {
|
|
2162
|
+
expect(query?.locale, `${what} sent ${JSON.stringify(query)}`).toBe('is-IS');
|
|
2163
|
+
}
|
|
2164
|
+
});
|
|
2165
|
+
|
|
2166
|
+
it('sends no locale at all on a single-locale store, exactly as it always did', async () => {
|
|
2167
|
+
// The overwhelming majority of stores, and every story and mapping spec. An empty `?locale=`
|
|
2168
|
+
// is a locale the gateway answers with 400, so "no locale" has to mean no key.
|
|
2169
|
+
const { client, collectionProductQueries } = recordingClient();
|
|
2170
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
2171
|
+
storefront.catalog.collectionProducts(
|
|
2172
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'winter-knitwear' }),
|
|
2173
|
+
ref({ page: 1, pageSize: 24 })
|
|
2174
|
+
);
|
|
2175
|
+
await settle();
|
|
2176
|
+
|
|
2177
|
+
expect(collectionProductQueries.length).toBeGreaterThan(0);
|
|
2178
|
+
for (const query of collectionProductQueries) expect(query).not.toHaveProperty('locale');
|
|
2179
|
+
});
|
|
2180
|
+
|
|
2181
|
+
/**
|
|
2182
|
+
* **The category tree**: one read of `GET /catalog/v1/categories` answers a product's breadcrumb
|
|
2183
|
+
* trail and completes the collection grid's category facet into something the panel can nest.
|
|
2184
|
+
*
|
|
2185
|
+
* The trail used to be `[]` on every product the gateway ever mapped — the only field it had to
|
|
2186
|
+
* fill it from was `result.breadcrumb`, which belongs to the *search* response and no product read
|
|
2187
|
+
* has ever carried.
|
|
2188
|
+
*/
|
|
2189
|
+
describe('the category tree', () => {
|
|
2190
|
+
const TREE = [
|
|
2191
|
+
{ id: 'cat-tableware', slug: 'tableware', title: 'Tableware', parentId: null },
|
|
2192
|
+
{ id: 'cat-cup', slug: 'cup', title: 'Cup', parentId: 'cat-tableware' },
|
|
2193
|
+
{ id: 'cat-bowl', slug: 'bowl', title: 'Bowl', parentId: 'cat-tableware' },
|
|
2194
|
+
];
|
|
2195
|
+
|
|
2196
|
+
function treeClient(
|
|
2197
|
+
options: {
|
|
2198
|
+
product?: Record<string, unknown>;
|
|
2199
|
+
categories?: unknown;
|
|
2200
|
+
facets?: Record<string, unknown> | null;
|
|
2201
|
+
} = {}
|
|
2202
|
+
): { client: EldraClient; categoryReads: () => number } {
|
|
2203
|
+
let reads = 0;
|
|
2204
|
+
const client = {
|
|
2205
|
+
catalog: {
|
|
2206
|
+
listCategories: async () => {
|
|
2207
|
+
reads += 1;
|
|
2208
|
+
if (options.categories instanceof Error) throw options.categories;
|
|
2209
|
+
return options.categories ?? TREE;
|
|
2210
|
+
},
|
|
2211
|
+
getProduct: async () =>
|
|
2212
|
+
options.product ?? {
|
|
2213
|
+
id: 'prod-mug',
|
|
2214
|
+
slug: 'ash-glaze-mug',
|
|
2215
|
+
title: 'Ash glaze mug',
|
|
2216
|
+
status: 'ACTIVE',
|
|
2217
|
+
primaryCategoryId: 'cat-cup',
|
|
2218
|
+
variants: [{ id: 'var-mug', status: 'ACTIVE', price: 4200 }],
|
|
2219
|
+
},
|
|
2220
|
+
listCollectionProducts: async () => ({
|
|
2221
|
+
data: [],
|
|
2222
|
+
meta: { page: 1, pageSize: 24, total: 0, totalPages: 0, rows: 0 },
|
|
2223
|
+
...(options.facets === null ? {} : { facets: options.facets ?? BARE_FACETS }),
|
|
2224
|
+
}),
|
|
2225
|
+
},
|
|
2226
|
+
} as unknown as EldraClient;
|
|
2227
|
+
return { client, categoryReads: () => reads };
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
/** What a platform that counts **assigned** categories answers: the leaves, and nothing above. */
|
|
2231
|
+
const BARE_FACETS = {
|
|
2232
|
+
price: { min: 4200, max: 4200 },
|
|
2233
|
+
categories: [
|
|
2234
|
+
{ id: 'cat-cup', slug: 'cup', title: 'Cup', count: 6 },
|
|
2235
|
+
{ id: 'cat-bowl', slug: 'bowl', title: 'Bowl', count: 4 },
|
|
2236
|
+
],
|
|
2237
|
+
collections: [],
|
|
2238
|
+
availability: { in_stock: 10, out_of_stock: 0 },
|
|
2239
|
+
options: [],
|
|
2240
|
+
};
|
|
2241
|
+
|
|
2242
|
+
async function readProduct(client: EldraClient) {
|
|
2243
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
2244
|
+
const result = storefront.catalog.product(ref('ash-glaze-mug'));
|
|
2245
|
+
await settle();
|
|
2246
|
+
return result.data.value;
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
async function readCollection(client: EldraClient) {
|
|
2250
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
2251
|
+
const result = storefront.catalog.collectionProducts(
|
|
2252
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
2253
|
+
ref({ page: 1, pageSize: 24 })
|
|
2254
|
+
);
|
|
2255
|
+
await settle();
|
|
2256
|
+
return result.data.value;
|
|
2257
|
+
}
|
|
2258
|
+
|
|
2259
|
+
it('walks a product’s primaryCategoryId up to its root', async () => {
|
|
2260
|
+
const { client } = treeClient();
|
|
2261
|
+
expect((await readProduct(client))?.categoryTrail).toEqual([
|
|
2262
|
+
{ label: 'Tableware', href: '/categories/tableware' },
|
|
2263
|
+
{ label: 'Cup', href: '/categories/tableware/cup' },
|
|
2264
|
+
]);
|
|
2265
|
+
});
|
|
2266
|
+
|
|
2267
|
+
/** `categoryId` is the field `related` has always read; `primaryCategoryId` is the one the public
|
|
2268
|
+
* contract names for this. A response carrying only the older one still gets a trail. */
|
|
2269
|
+
it('falls back to categoryId when the response carries no primaryCategoryId', async () => {
|
|
2270
|
+
const { client } = treeClient({
|
|
2271
|
+
product: {
|
|
2272
|
+
id: 'prod-mug',
|
|
2273
|
+
slug: 'ash-glaze-mug',
|
|
2274
|
+
title: 'Ash glaze mug',
|
|
2275
|
+
status: 'ACTIVE',
|
|
2276
|
+
categoryId: 'cat-bowl',
|
|
2277
|
+
variants: [],
|
|
2278
|
+
},
|
|
2279
|
+
});
|
|
2280
|
+
expect((await readProduct(client))?.categoryTrail.map((level) => level.label)).toEqual([
|
|
2281
|
+
'Tableware',
|
|
2282
|
+
'Bowl',
|
|
2283
|
+
]);
|
|
2284
|
+
});
|
|
2285
|
+
|
|
2286
|
+
it('leaves the trail empty for a product with no category', async () => {
|
|
2287
|
+
const { client } = treeClient({
|
|
2288
|
+
product: {
|
|
2289
|
+
id: 'prod-mug',
|
|
2290
|
+
slug: 'ash-glaze-mug',
|
|
2291
|
+
title: 'Ash glaze mug',
|
|
2292
|
+
status: 'ACTIVE',
|
|
2293
|
+
variants: [],
|
|
2294
|
+
},
|
|
2295
|
+
});
|
|
2296
|
+
expect((await readProduct(client))?.categoryTrail).toEqual([]);
|
|
2297
|
+
});
|
|
2298
|
+
|
|
2299
|
+
it('leaves the trail empty for a category the store’s list no longer holds', async () => {
|
|
2300
|
+
const { client } = treeClient({ categories: [] });
|
|
2301
|
+
expect((await readProduct(client))?.categoryTrail).toEqual([]);
|
|
2302
|
+
});
|
|
2303
|
+
|
|
2304
|
+
/**
|
|
2305
|
+
* A product page without a category crumb is a product page. One that fails because the category
|
|
2306
|
+
* list was unreachable is not — which is the opposite of what the `category` *filter* does with
|
|
2307
|
+
* the same read, and deliberately so: a dropped filter is a lie about what is on screen.
|
|
2308
|
+
*/
|
|
2309
|
+
it('serves the product with no trail when the category read fails', async () => {
|
|
2310
|
+
const { client } = treeClient({ categories: new Error('categories are down') });
|
|
2311
|
+
const product = await readProduct(client);
|
|
2312
|
+
expect(product?.title).toBe('Ash glaze mug');
|
|
2313
|
+
expect(product?.categoryTrail).toEqual([]);
|
|
2314
|
+
});
|
|
2315
|
+
|
|
2316
|
+
/**
|
|
2317
|
+
* **A family the platform did not place is passed through untouched, and costs no category read.**
|
|
2318
|
+
*
|
|
2319
|
+
* Completing it from the category list was the earlier behaviour and was wrong: the contract that
|
|
2320
|
+
* adds the ancestor counts adds the `categoryId` subtree match with them, so a parent row
|
|
2321
|
+
* synthesised here would be a filter *this* gateway answers by direct membership only — an empty
|
|
2322
|
+
* grid under a chip saying otherwise. The panel keeps such a family flat instead
|
|
2323
|
+
* (`parts/groups.ts`'s `rawValuesFor`), and nothing in the response pretends otherwise.
|
|
2324
|
+
*/
|
|
2325
|
+
it('passes a flat category family through untouched, reading no category list', async () => {
|
|
2326
|
+
const { client, categoryReads } = treeClient();
|
|
2327
|
+
const facets = (await readCollection(client))?.facets;
|
|
2328
|
+
expect(facets?.categories).toEqual([
|
|
2329
|
+
{ id: 'cat-cup', slug: 'cup', title: 'Cup', count: 6 },
|
|
2330
|
+
{ id: 'cat-bowl', slug: 'bowl', title: 'Bowl', count: 4 },
|
|
2331
|
+
]);
|
|
2332
|
+
// No `parentId` anywhere and no claim about the counts: the panel reads both as "flat".
|
|
2333
|
+
expect(facets?.categories.every((term) => term.parentId === undefined)).toBe(true);
|
|
2334
|
+
expect(facets?.categoryCounts).toBeUndefined();
|
|
2335
|
+
expect(categoryReads()).toBe(0);
|
|
2336
|
+
});
|
|
2337
|
+
|
|
2338
|
+
/**
|
|
2339
|
+
* **The platform's own tree wins, and buys back the request.** `parentId` on a facet term and the
|
|
2340
|
+
* ancestor roll-ups land together, so a response that places its own terms is left exactly as it
|
|
2341
|
+
* came — counts included, because the platform's number is deduplicated and a client-side sum over
|
|
2342
|
+
* siblings cannot be — and the category list is not read at all.
|
|
2343
|
+
*/
|
|
2344
|
+
it('keeps the platform’s own placement and counts, and reads no categories for them', async () => {
|
|
2345
|
+
const { client, categoryReads } = treeClient({
|
|
2346
|
+
// Contract 3.8.0's own shape, verbatim: depth-first by title, `parentId` **absent** on the
|
|
2347
|
+
// root, and a parent's count already the subtree's (9, which is deliberately not 6 — a
|
|
2348
|
+
// rolled-up count is not the sum of the children on screen).
|
|
2349
|
+
facets: {
|
|
2350
|
+
...BARE_FACETS,
|
|
2351
|
+
categories: [
|
|
2352
|
+
{ id: 'cat-tableware', slug: 'tableware', title: 'Tableware', count: 9 },
|
|
2353
|
+
{ id: 'cat-cup', slug: 'cup', title: 'Cup', count: 6, parentId: 'cat-tableware' },
|
|
2354
|
+
],
|
|
2355
|
+
},
|
|
2356
|
+
});
|
|
2357
|
+
const facets = (await readCollection(client))?.facets;
|
|
2358
|
+
expect(facets?.categoryCounts).toBe('rolled-up');
|
|
2359
|
+
// An absent `parentId` on a placed family is normalised to the explicit `null` this theme's
|
|
2360
|
+
// view type uses, so a root never looks like a term the source could not place.
|
|
2361
|
+
expect(facets?.categories).toEqual([
|
|
2362
|
+
{ id: 'cat-tableware', slug: 'tableware', title: 'Tableware', count: 9, parentId: null },
|
|
2363
|
+
{ id: 'cat-cup', slug: 'cup', title: 'Cup', count: 6, parentId: 'cat-tableware' },
|
|
2364
|
+
]);
|
|
2365
|
+
expect(categoryReads()).toBe(0);
|
|
2366
|
+
});
|
|
2367
|
+
|
|
2368
|
+
/**
|
|
2369
|
+
* A 3.8.0 scope whose categories are **all roots** carries no `parentId` anywhere, so it reads as
|
|
2370
|
+
* unplaced — and nothing is lost by that, which is what this pins: the counts come out exactly as
|
|
2371
|
+
* the platform sent them, and a family with no parent/child pair has nothing to nest anyway.
|
|
2372
|
+
*/
|
|
2373
|
+
it('is harmless on a 3.8.0 scope whose categories are all roots', async () => {
|
|
2374
|
+
const { client, categoryReads } = treeClient({
|
|
2375
|
+
facets: {
|
|
2376
|
+
...BARE_FACETS,
|
|
2377
|
+
categories: [
|
|
2378
|
+
{ id: 'cat-blankets', slug: 'blankets', title: 'Blankets', count: 3 },
|
|
2379
|
+
{ id: 'cat-tableware', slug: 'tableware', title: 'Tableware', count: 9 },
|
|
2380
|
+
],
|
|
2381
|
+
},
|
|
2382
|
+
});
|
|
2383
|
+
expect((await readCollection(client))?.facets?.categories).toEqual([
|
|
2384
|
+
{ id: 'cat-blankets', slug: 'blankets', title: 'Blankets', count: 3 },
|
|
2385
|
+
{ id: 'cat-tableware', slug: 'tableware', title: 'Tableware', count: 9 },
|
|
2386
|
+
]);
|
|
2387
|
+
expect(categoryReads()).toBe(0);
|
|
2388
|
+
});
|
|
2389
|
+
|
|
2390
|
+
/**
|
|
2391
|
+
* One read for the life of the storefront, however many readers want it — and the readers are now
|
|
2392
|
+
* exactly two: a product's trail, and a `category` filter's slug→id lookup. A *facet* never pays
|
|
2393
|
+
* for it (the three tests above), so an unfiltered collection page costs the request it always did.
|
|
2394
|
+
*/
|
|
2395
|
+
it('reads the category list once for a trail and a category filter together', async () => {
|
|
2396
|
+
const { client, categoryReads } = treeClient();
|
|
2397
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
2398
|
+
storefront.catalog.product(ref('ash-glaze-mug'));
|
|
2399
|
+
storefront.catalog.collectionProducts(
|
|
2400
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'the-winter-edit' }),
|
|
2401
|
+
ref({ page: 1, pageSize: 24, filters: { category: ['cup'] } })
|
|
2402
|
+
);
|
|
2403
|
+
await settle();
|
|
2404
|
+
expect(categoryReads()).toBe(1);
|
|
2405
|
+
});
|
|
2406
|
+
});
|
|
2407
|
+
|
|
2408
|
+
/**
|
|
2409
|
+
* **The catalogue scope** — `catalog.products`, the read behind the `/products` page. Same answer
|
|
2410
|
+
* shape as a collection's own products, same filters, same `facets=true`, over
|
|
2411
|
+
* `GET /catalog/v1/products/list` instead. Two differences, and both are the point: the
|
|
2412
|
+
* `collection` clause goes out as a `collectionId` here (that list takes one; a collection's own
|
|
2413
|
+
* does not), so nothing is `unfilterable`.
|
|
2414
|
+
*/
|
|
2415
|
+
describe('the catalogue-wide product list', () => {
|
|
2416
|
+
function catalogueClient(collections: Array<{ id: string; slug: string }> = []): {
|
|
2417
|
+
client: EldraClient;
|
|
2418
|
+
queries: Array<Record<string, unknown>>;
|
|
2419
|
+
collectionQueries: number;
|
|
2420
|
+
} {
|
|
2421
|
+
const queries: Array<Record<string, unknown>> = [];
|
|
2422
|
+
const state = { collectionQueries: 0 };
|
|
2423
|
+
const client = {
|
|
2424
|
+
catalog: {
|
|
2425
|
+
listCategories: async () => [{ id: 'cat-ceramics', slug: 'ceramics', title: 'Ceramics' }],
|
|
2426
|
+
listCollections: async (query: Record<string, unknown>) => {
|
|
2427
|
+
state.collectionQueries += 1;
|
|
2428
|
+
void query;
|
|
2429
|
+
return {
|
|
2430
|
+
data: collections.map((row) => ({ ...row, title: row.slug, productCount: 0 })),
|
|
2431
|
+
};
|
|
2432
|
+
},
|
|
2433
|
+
listProducts: async (query: Record<string, unknown>) => {
|
|
2434
|
+
queries.push(query);
|
|
2435
|
+
return {
|
|
2436
|
+
data: [listRow('merino-crew-sweater')],
|
|
2437
|
+
meta: { page: 1, pageSize: 24, total: 1, totalPages: 1, rows: 1 },
|
|
2438
|
+
facets: {
|
|
2439
|
+
price: { min: 1000, max: 1000 },
|
|
2440
|
+
categories: [],
|
|
2441
|
+
collections: [],
|
|
2442
|
+
availability: { in_stock: 1, out_of_stock: 0 },
|
|
2443
|
+
options: [],
|
|
2444
|
+
},
|
|
2445
|
+
};
|
|
2446
|
+
},
|
|
2447
|
+
},
|
|
2448
|
+
} as unknown as EldraClient;
|
|
2449
|
+
return {
|
|
2450
|
+
client,
|
|
2451
|
+
queries,
|
|
2452
|
+
get collectionQueries() {
|
|
2453
|
+
return state.collectionQueries;
|
|
2454
|
+
},
|
|
2455
|
+
};
|
|
2456
|
+
}
|
|
2457
|
+
|
|
2458
|
+
async function read(
|
|
2459
|
+
client: EldraClient,
|
|
2460
|
+
filters?: Record<string, string[]>
|
|
2461
|
+
): Promise<StorefrontCollectionProducts | null> {
|
|
2462
|
+
const storefront = createGatewayStorefront(client, {
|
|
2463
|
+
route: fakeRoute(),
|
|
2464
|
+
commerce: ISK_COMMERCE,
|
|
2465
|
+
});
|
|
2466
|
+
const result = storefront.catalog.products(
|
|
2467
|
+
ref({ page: 1, pageSize: 24, ...(filters === undefined ? {} : { filters }) })
|
|
2468
|
+
);
|
|
2469
|
+
await settle();
|
|
2470
|
+
return result.data.value;
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
it('asks the catalogue list for active products, with facets', async () => {
|
|
2474
|
+
const calls = catalogueClient();
|
|
2475
|
+
const answer = await read(calls.client);
|
|
2476
|
+
expect(answer?.items.map((item) => item.handle)).toEqual(['merino-crew-sweater']);
|
|
2477
|
+
expect(answer?.total).toBe(1);
|
|
2478
|
+
expect(calls.queries[0]).toMatchObject({
|
|
2479
|
+
page: 1,
|
|
2480
|
+
pageSize: 24,
|
|
2481
|
+
facets: true,
|
|
2482
|
+
filter: ['status:eq:ACTIVE'],
|
|
2483
|
+
});
|
|
2484
|
+
});
|
|
2485
|
+
|
|
2486
|
+
/** The whole reason this scope exists as a separate one: nothing is declared unfilterable, so the
|
|
2487
|
+
* Collection group is offered and really filters. */
|
|
2488
|
+
it('declares nothing unfilterable', async () => {
|
|
2489
|
+
const calls = catalogueClient();
|
|
2490
|
+
expect((await read(calls.client))?.unfilterable).toBeUndefined();
|
|
2491
|
+
});
|
|
2492
|
+
|
|
2493
|
+
/** Unlike `unfilterable`, `best-selling` is declared unsortable on *this* scope too — the
|
|
2494
|
+
* catalogue-wide list has no more sales data to order by than the collection-scoped one does
|
|
2495
|
+
* (`GATEWAY_UNSORTABLE`). */
|
|
2496
|
+
it('declares best-selling unsortable', async () => {
|
|
2497
|
+
const calls = catalogueClient();
|
|
2498
|
+
expect((await read(calls.client))?.unsortable).toEqual(['best-selling']);
|
|
2499
|
+
});
|
|
2500
|
+
|
|
2501
|
+
it('sends a collection clause as a collectionId, resolved by slug', async () => {
|
|
2502
|
+
const calls = catalogueClient([{ id: 'col-winter', slug: 'the-winter-edit' }]);
|
|
2503
|
+
await read(calls.client, { collection: ['the-winter-edit'] });
|
|
2504
|
+
expect(calls.queries.at(-1)?.collectionId).toEqual(['col-winter']);
|
|
2505
|
+
expect(calls.collectionQueries).toBe(1);
|
|
2506
|
+
});
|
|
2507
|
+
|
|
2508
|
+
/** "Unknown, not unmatched": a slug nothing matches is left out rather than sent as something
|
|
2509
|
+
* that would empty the grid. */
|
|
2510
|
+
it('drops a collection slug the store has no collection for', async () => {
|
|
2511
|
+
const calls = catalogueClient();
|
|
2512
|
+
await read(calls.client, { collection: ['gone'] });
|
|
2513
|
+
expect(calls.queries.at(-1)).not.toHaveProperty('collectionId');
|
|
2514
|
+
});
|
|
2515
|
+
|
|
2516
|
+
it('still sends every other filter the collection scope sends', async () => {
|
|
2517
|
+
const calls = catalogueClient();
|
|
2518
|
+
await read(calls.client, {
|
|
2519
|
+
category: ['ceramics'],
|
|
2520
|
+
availability: ['in_stock'],
|
|
2521
|
+
'option:colour': ['oat'],
|
|
2522
|
+
price: ['50-150'],
|
|
2523
|
+
});
|
|
2524
|
+
expect(calls.queries.at(-1)).toMatchObject({
|
|
2525
|
+
categoryId: ['cat-ceramics'],
|
|
2526
|
+
availability: 'in_stock',
|
|
2527
|
+
option: ['colour:oat'],
|
|
2528
|
+
// ISK has no minor unit, so the shopper's whole units pass straight through.
|
|
2529
|
+
minPrice: 50,
|
|
2530
|
+
maxPrice: 150,
|
|
2531
|
+
});
|
|
2532
|
+
});
|
|
2533
|
+
});
|
|
2534
|
+
|
|
2535
|
+
/**
|
|
2536
|
+
* **A category page**: the same catalogue-wide list, scoped by one category's canonical path.
|
|
2537
|
+
*
|
|
2538
|
+
* The platform's `categoryId` matches a whole **subtree**, so the page's own id is the scope — and
|
|
2539
|
+
* because the parameter is an OR over a list, the shopper's own `category` clause has to be
|
|
2540
|
+
* *intersected* with that subtree rather than sent beside it: a value outside it would widen the
|
|
2541
|
+
* page past its own category, which is the one thing a category page cannot do.
|
|
2542
|
+
*/
|
|
2543
|
+
describe('a category page’s own scope', () => {
|
|
2544
|
+
const TREE = [
|
|
2545
|
+
{ id: 'cat-home', slug: 'home', title: 'Homeware', parentId: null },
|
|
2546
|
+
{ id: 'cat-ceramics', slug: 'ceramics', title: 'Ceramics', parentId: 'cat-home' },
|
|
2547
|
+
{ id: 'cat-cups', slug: 'cups', title: 'Cups', parentId: 'cat-ceramics' },
|
|
2548
|
+
{ id: 'cat-knitwear', slug: 'knitwear', title: 'Knitwear', parentId: null },
|
|
2549
|
+
];
|
|
2550
|
+
|
|
2551
|
+
function categoryClient(): {
|
|
2552
|
+
client: EldraClient;
|
|
2553
|
+
queries: Array<Record<string, unknown>>;
|
|
2554
|
+
categoryReads: () => number;
|
|
2555
|
+
} {
|
|
2556
|
+
const queries: Array<Record<string, unknown>> = [];
|
|
2557
|
+
let reads = 0;
|
|
2558
|
+
const client = {
|
|
2559
|
+
catalog: {
|
|
2560
|
+
listCategories: async () => {
|
|
2561
|
+
reads += 1;
|
|
2562
|
+
return TREE;
|
|
2563
|
+
},
|
|
2564
|
+
listProducts: async (query: Record<string, unknown>) => {
|
|
2565
|
+
queries.push(query);
|
|
2566
|
+
return {
|
|
2567
|
+
data: [listRow('ash-glaze-mug')],
|
|
2568
|
+
meta: { page: 1, pageSize: 24, total: 1, totalPages: 1, rows: 1 },
|
|
2569
|
+
};
|
|
2570
|
+
},
|
|
2571
|
+
},
|
|
2572
|
+
} as unknown as EldraClient;
|
|
2573
|
+
return { client, queries, categoryReads: () => reads };
|
|
2574
|
+
}
|
|
2575
|
+
|
|
2576
|
+
async function read(
|
|
2577
|
+
client: EldraClient,
|
|
2578
|
+
categoryPath: string,
|
|
2579
|
+
filters?: Record<string, string[]>
|
|
2580
|
+
): Promise<StorefrontCollectionProducts | null> {
|
|
2581
|
+
const storefront = createGatewayStorefront(client, { route: fakeRoute() });
|
|
2582
|
+
const result = storefront.catalog.products(
|
|
2583
|
+
ref({
|
|
2584
|
+
page: 1,
|
|
2585
|
+
pageSize: 24,
|
|
2586
|
+
categoryPath,
|
|
2587
|
+
...(filters === undefined ? {} : { filters }),
|
|
2588
|
+
})
|
|
2589
|
+
);
|
|
2590
|
+
await settle();
|
|
2591
|
+
return result.data.value;
|
|
2592
|
+
}
|
|
2593
|
+
|
|
2594
|
+
it('sends the category’s own id, which the platform matches over its whole subtree', async () => {
|
|
2595
|
+
const calls = categoryClient();
|
|
2596
|
+
expect((await read(calls.client, 'home/ceramics'))?.total).toBe(1);
|
|
2597
|
+
expect(calls.queries.at(-1)?.categoryId).toEqual(['cat-ceramics']);
|
|
2598
|
+
});
|
|
2599
|
+
|
|
2600
|
+
/** A child the shopper ticked narrows **within** the subtree, so the scope's own id gives way to
|
|
2601
|
+
* it rather than being OR-ed beside it — which would put the whole subtree back. */
|
|
2602
|
+
it('replaces the scope with a ticked descendant rather than widening past it', async () => {
|
|
2603
|
+
const calls = categoryClient();
|
|
2604
|
+
await read(calls.client, 'home/ceramics', { category: ['cups'] });
|
|
2605
|
+
expect(calls.queries.at(-1)?.categoryId).toEqual(['cat-cups']);
|
|
2606
|
+
});
|
|
2607
|
+
|
|
2608
|
+
/** A value from outside the subtree — a shared link, an author's own URL — is dropped, and the
|
|
2609
|
+
* page falls back to its own category rather than listing something it does not contain. */
|
|
2610
|
+
it('drops a ticked category that is not in the subtree', async () => {
|
|
2611
|
+
const calls = categoryClient();
|
|
2612
|
+
await read(calls.client, 'home/ceramics', { category: ['knitwear'] });
|
|
2613
|
+
expect(calls.queries.at(-1)?.categoryId).toEqual(['cat-ceramics']);
|
|
2614
|
+
await read(calls.client, 'home/ceramics', { category: ['knitwear', 'cups'] });
|
|
2615
|
+
expect(calls.queries.at(-1)?.categoryId).toEqual(['cat-cups']);
|
|
2616
|
+
});
|
|
2617
|
+
|
|
2618
|
+
/**
|
|
2619
|
+
* **Canonical only**: a path that is not a root→descendant chain is no category, and the read
|
|
2620
|
+
* answers `null` — the grid's own empty state — rather than the unscoped catalogue, which would
|
|
2621
|
+
* answer a 404 page with every product in the store.
|
|
2622
|
+
*/
|
|
2623
|
+
it('answers nothing for a path no category occupies, and asks the list for nothing', async () => {
|
|
2624
|
+
for (const path of ['ceramics', 'knitwear/ceramics', 'home/ceramics/nope']) {
|
|
2625
|
+
const calls = categoryClient();
|
|
2626
|
+
expect(await read(calls.client, path)).toBeNull();
|
|
2627
|
+
expect(calls.queries).toEqual([]);
|
|
2628
|
+
}
|
|
2629
|
+
});
|
|
2630
|
+
|
|
2631
|
+
/** `catalog.category` is the page's other read, over the same memoised list: one request serves
|
|
2632
|
+
* the header's title and strip, the breadcrumb's ancestors and the grid's scope. */
|
|
2633
|
+
it('places the category for the page, from the same one category read', async () => {
|
|
2634
|
+
const calls = categoryClient();
|
|
2635
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
2636
|
+
const placed = storefront.catalog.category(ref('home/ceramics'));
|
|
2637
|
+
const products = storefront.catalog.products(
|
|
2638
|
+
ref({ page: 1, pageSize: 24, categoryPath: 'home/ceramics' })
|
|
2639
|
+
);
|
|
2640
|
+
await settle();
|
|
2641
|
+
expect(placed.data.value).toEqual({
|
|
2642
|
+
id: 'cat-ceramics',
|
|
2643
|
+
slug: 'ceramics',
|
|
2644
|
+
title: 'Ceramics',
|
|
2645
|
+
path: 'home/ceramics',
|
|
2646
|
+
ancestors: [{ slug: 'home', title: 'Homeware', path: 'home' }],
|
|
2647
|
+
children: [{ slug: 'cups', title: 'Cups', path: 'home/ceramics/cups' }],
|
|
2648
|
+
});
|
|
2649
|
+
expect(products.data.value?.total).toBe(1);
|
|
2650
|
+
expect(calls.categoryReads()).toBe(1);
|
|
2651
|
+
});
|
|
2652
|
+
|
|
2653
|
+
it('answers no category for a path that is not one, and none for a null source', async () => {
|
|
2654
|
+
const calls = categoryClient();
|
|
2655
|
+
const storefront = createGatewayStorefront(calls.client, { route: fakeRoute() });
|
|
2656
|
+
const missing = storefront.catalog.category(ref('ceramics'));
|
|
2657
|
+
const none = storefront.catalog.category(ref<string | null>(null));
|
|
2658
|
+
await settle();
|
|
2659
|
+
expect(missing.data.value).toBeNull();
|
|
2660
|
+
expect(none.data.value).toBeNull();
|
|
2661
|
+
});
|
|
2662
|
+
});
|
|
2663
|
+
|
|
2664
|
+
it('every result carries an empty `revalidating` set — the prerender contract’s resting state', async () => {
|
|
2665
|
+
const storefront = createGatewayStorefront(fakeClient(), { route: fakeRoute() });
|
|
2666
|
+
const result = storefront.catalog.collectionProducts(
|
|
2667
|
+
ref<StorefrontCollectionSelector | null>({ slug: 'winter-knitwear' }),
|
|
2668
|
+
ref({ page: 1, pageSize: 24 })
|
|
2669
|
+
);
|
|
2670
|
+
await settle();
|
|
2671
|
+
expect(result.revalidating.value.size).toBe(0);
|
|
2672
|
+
});
|
|
2673
|
+
});
|