@eldrajs/theme-cli 0.0.0 → 0.1.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 +69 -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-C6jB8_Oc.js +510 -0
- package/package.json +50 -2
- package/template/.eldra/block-types.d.ts +429 -0
- package/template/.eldra/manifest.json +9377 -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 +52 -0
- package/template/.storybook/shims-vue.d.ts +10 -0
- package/template/.storybook/virtual-eldra.d.ts +27 -0
- package/template/README.md +288 -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 +247 -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 +1495 -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 +148 -0
- package/template/package.json +55 -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/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/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 +334 -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 +89 -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 +393 -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 +161 -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 +171 -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 +46 -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,1037 @@
|
|
|
1
|
+
import { nextTick, ref } from 'vue';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { createEldraClient, type EldraClient, type EldraHttpRequest } from '@eldrajs/sdk';
|
|
4
|
+
import {
|
|
5
|
+
createGatewayStorefront,
|
|
6
|
+
type StorefrontPrerenderHandle,
|
|
7
|
+
} from '../../app/storefront/gateway';
|
|
8
|
+
import { createVolatileRefresher, type VolatileRefresher } from '../../app/storefront/refresh';
|
|
9
|
+
import {
|
|
10
|
+
prerenderThroughAsyncData,
|
|
11
|
+
type KeyedAsyncData,
|
|
12
|
+
type KeyedAsyncDataOptions,
|
|
13
|
+
} from '../../app/storefront/prerender';
|
|
14
|
+
import type {
|
|
15
|
+
StorefrontProduct,
|
|
16
|
+
StorefrontRoute,
|
|
17
|
+
StorefrontSource,
|
|
18
|
+
VolatileSnapshot,
|
|
19
|
+
} from '../../app/storefront/types';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The prerender/refresh half of the gateway storefront: a result's first load runs under the
|
|
23
|
+
* framework's keyed async data — awaited while a prerender renders, read back out of the payload
|
|
24
|
+
* while the browser hydrates — and after hydration the page's results refresh *only* their
|
|
25
|
+
* volatile values, in one batched read.
|
|
26
|
+
*
|
|
27
|
+
* Nuxt is nowhere in here on purpose. `gateway.ts` takes both abilities as a `StorefrontRuntime`
|
|
28
|
+
* (`app/plugins/eldra-storefront.ts` implements them over `useAsyncData` and `app:mounted`), so
|
|
29
|
+
* what a prerender does can be stated in a stub and asserted — which a `nuxi generate` run, with
|
|
30
|
+
* no gateway credentials in this suite, cannot.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
function fakeRoute(): StorefrontRoute {
|
|
34
|
+
return {
|
|
35
|
+
productHandle: null,
|
|
36
|
+
collectionHandle: null,
|
|
37
|
+
orderToken: null,
|
|
38
|
+
query: null,
|
|
39
|
+
page: 1,
|
|
40
|
+
sort: null,
|
|
41
|
+
columns: null,
|
|
42
|
+
filters: {},
|
|
43
|
+
setQuery: () => {},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A product detail response in the gateway's own shape, with one variant. */
|
|
48
|
+
function detailRow(price: number): Record<string, unknown> {
|
|
49
|
+
return {
|
|
50
|
+
id: 'p-1',
|
|
51
|
+
slug: 'merino-crew-sweater',
|
|
52
|
+
title: 'Merino crew sweater',
|
|
53
|
+
status: 'ACTIVE',
|
|
54
|
+
variants: [{ id: 'v-1', sku: 'MCS-OAT-M', price, status: 'ACTIVE' }],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** One product list row, in the gateway's own shape. */
|
|
59
|
+
function listRow(
|
|
60
|
+
id: string,
|
|
61
|
+
price: number,
|
|
62
|
+
options: { status?: string } = {}
|
|
63
|
+
): Record<string, unknown> {
|
|
64
|
+
return {
|
|
65
|
+
id,
|
|
66
|
+
slug: id,
|
|
67
|
+
title: `Product ${id}`,
|
|
68
|
+
status: options.status ?? 'ACTIVE',
|
|
69
|
+
minPrice: price,
|
|
70
|
+
maxPrice: price,
|
|
71
|
+
totalVariants: 1,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const META = { page: 1, pageSize: 24, total: 2, totalPages: 1, rows: 2 };
|
|
76
|
+
|
|
77
|
+
interface FakeClient {
|
|
78
|
+
client: EldraClient;
|
|
79
|
+
detailReads: number;
|
|
80
|
+
/** The `filter` token lists `client.catalog.listProducts` was called with, in order. */
|
|
81
|
+
listFilters: string[][];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A client with no HTTP behind it, for the assertions that are about mapped values. */
|
|
85
|
+
function fakeClient(
|
|
86
|
+
options: {
|
|
87
|
+
detail?: () => Record<string, unknown>;
|
|
88
|
+
list?: (filter: string[]) => Promise<Array<Record<string, unknown>>>;
|
|
89
|
+
collectionProducts?: () => Array<Record<string, unknown>>;
|
|
90
|
+
/** The inventory answer for the detail read. Omitted, there is no inventory service at all —
|
|
91
|
+
* which is the fail-soft case every other test here runs in. */
|
|
92
|
+
stock?: () => Array<Record<string, unknown>>;
|
|
93
|
+
} = {}
|
|
94
|
+
): FakeClient {
|
|
95
|
+
const state: FakeClient = {
|
|
96
|
+
detailReads: 0,
|
|
97
|
+
listFilters: [],
|
|
98
|
+
client: {
|
|
99
|
+
inventory: {
|
|
100
|
+
availability: async () => {
|
|
101
|
+
if (options.stock === undefined) throw new Error('no inventory service');
|
|
102
|
+
return { items: options.stock() };
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
catalog: {
|
|
106
|
+
getProduct: async () => {
|
|
107
|
+
state.detailReads += 1;
|
|
108
|
+
return options.detail?.() ?? detailRow(96);
|
|
109
|
+
},
|
|
110
|
+
listProducts: async (query: Record<string, unknown>) => {
|
|
111
|
+
const filter = (query.filter ?? []) as string[];
|
|
112
|
+
state.listFilters.push(filter);
|
|
113
|
+
return { data: (await options.list?.(filter)) ?? [], meta: META };
|
|
114
|
+
},
|
|
115
|
+
listCollectionProducts: async () => ({
|
|
116
|
+
data: options.collectionProducts?.() ?? [listRow('p-1', 10), listRow('p-2', 20)],
|
|
117
|
+
meta: META,
|
|
118
|
+
}),
|
|
119
|
+
},
|
|
120
|
+
} as unknown as EldraClient,
|
|
121
|
+
};
|
|
122
|
+
return state;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The volatile reads among a fake client's product-list calls. */
|
|
126
|
+
function volatileReads(calls: FakeClient): string[][] {
|
|
127
|
+
return calls.listFilters.filter((filter) => filter[0]?.startsWith('id:in:') === true);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The stub async-data provider: keyed, awaited, payload-backed — `useAsyncData` reduced to the
|
|
132
|
+
* three things `gateway.ts` asks of it. A key already in `payload` is a hydrating browser, and its
|
|
133
|
+
* loader is never called; anything else is a prerender, whose load `settleAll()` awaits the way
|
|
134
|
+
* Nuxt awaits a page's async data before it writes the HTML.
|
|
135
|
+
*/
|
|
136
|
+
function asyncDataStub(payload: Record<string, unknown> = {}) {
|
|
137
|
+
const inFlight: Array<Promise<unknown>> = [];
|
|
138
|
+
const keys: string[] = [];
|
|
139
|
+
return {
|
|
140
|
+
keys,
|
|
141
|
+
payload,
|
|
142
|
+
async settleAll(): Promise<void> {
|
|
143
|
+
while (inFlight.length > 0) await inFlight.shift();
|
|
144
|
+
await nextTick();
|
|
145
|
+
},
|
|
146
|
+
prerender<T>(key: string, load: () => Promise<T | null>): StorefrontPrerenderHandle<T> {
|
|
147
|
+
keys.push(key);
|
|
148
|
+
if (key in payload) {
|
|
149
|
+
const value = payload[key] as T | null;
|
|
150
|
+
// Boxed: a payload that carries `null` is a read that answered "nothing", not one still
|
|
151
|
+
// running, and that is the whole difference the result's first render turns on.
|
|
152
|
+
return {
|
|
153
|
+
answered: { data: value },
|
|
154
|
+
settled: Promise.resolve({ data: value, error: null }),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const settled = load().then(
|
|
158
|
+
(data) => {
|
|
159
|
+
payload[key] = data;
|
|
160
|
+
return { data, error: null };
|
|
161
|
+
},
|
|
162
|
+
(caught: unknown) => ({ data: null, error: (caught as Error).message })
|
|
163
|
+
);
|
|
164
|
+
inFlight.push(settled);
|
|
165
|
+
return { answered: null, settled };
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The wiring `app/plugins/eldra-storefront.ts` does: one storefront, one page refresh. */
|
|
171
|
+
function wire(
|
|
172
|
+
client: EldraClient,
|
|
173
|
+
prerender?: <T>(key: string, load: () => Promise<T | null>) => StorefrontPrerenderHandle<T>,
|
|
174
|
+
locale?: () => string | undefined
|
|
175
|
+
): { storefront: StorefrontSource; refresher: VolatileRefresher } {
|
|
176
|
+
// The refresh runs when this spec says so, not on a schedule of its own. It reaches the
|
|
177
|
+
// storefront declared below through the closure, exactly as the plugin's does.
|
|
178
|
+
const refresher = createVolatileRefresher(
|
|
179
|
+
(ids) => storefront.catalog.volatileByIds(ids),
|
|
180
|
+
() => {}
|
|
181
|
+
);
|
|
182
|
+
const storefront = createGatewayStorefront(client, {
|
|
183
|
+
route: fakeRoute(),
|
|
184
|
+
runtime: { prerender, register: refresher.register },
|
|
185
|
+
locale,
|
|
186
|
+
});
|
|
187
|
+
return { storefront, refresher };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A few turns of the microtask/scheduler queue — long enough for a watcher to re-run a load.
|
|
192
|
+
*
|
|
193
|
+
* The count is slack, not a guard: it has to exceed the await depth of the deepest read these tests
|
|
194
|
+
* drive, which is `catalog.product` (the detail read, then the stock read and the category-tree read
|
|
195
|
+
* in parallel, each of which fails over a fake client that offers neither service — and a rejection
|
|
196
|
+
* caught and re-thrown costs turns of its own). Nothing here asserts on how many turns a read takes.
|
|
197
|
+
*/
|
|
198
|
+
async function settle(): Promise<void> {
|
|
199
|
+
for (let turn = 0; turn < 16; turn += 1) await nextTick();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function deferred<T>(): {
|
|
203
|
+
promise: Promise<T>;
|
|
204
|
+
resolve: (value: T) => void;
|
|
205
|
+
reject: (error: Error) => void;
|
|
206
|
+
} {
|
|
207
|
+
let resolve!: (value: T) => void;
|
|
208
|
+
let reject!: (error: Error) => void;
|
|
209
|
+
const promise = new Promise<T>((res, rej) => {
|
|
210
|
+
resolve = res;
|
|
211
|
+
reject = rej;
|
|
212
|
+
});
|
|
213
|
+
return { promise, resolve, reject };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
describe('gateway storefront — prerendered results', () => {
|
|
217
|
+
it('awaits its load under a keyed async data, and is not pending once it has one', async () => {
|
|
218
|
+
const ssr = asyncDataStub();
|
|
219
|
+
const calls = fakeClient();
|
|
220
|
+
const { storefront } = wire(calls.client, ssr.prerender);
|
|
221
|
+
|
|
222
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
223
|
+
// Nothing yet — this is the render the framework is about to wait on.
|
|
224
|
+
expect(result.pending.value).toBe(true);
|
|
225
|
+
expect(result.data.value).toBeNull();
|
|
226
|
+
|
|
227
|
+
await ssr.settleAll();
|
|
228
|
+
|
|
229
|
+
expect(result.data.value?.title).toBe('Merino crew sweater');
|
|
230
|
+
expect(result.data.value?.price.amount).toBe(96);
|
|
231
|
+
expect(result.pending.value).toBe(false);
|
|
232
|
+
expect(result.error.value).toBeNull();
|
|
233
|
+
// The key is the method plus its arguments, stable across renders of the same read.
|
|
234
|
+
expect(ssr.keys).toEqual(['storefront:catalog.product:["merino-crew-sweater"]']);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('keys a read by its arguments, sorting object keys so one read is one key', async () => {
|
|
238
|
+
const ssr = asyncDataStub();
|
|
239
|
+
const calls = fakeClient();
|
|
240
|
+
const { storefront } = wire(calls.client, ssr.prerender);
|
|
241
|
+
|
|
242
|
+
storefront.catalog.collectionProducts(
|
|
243
|
+
ref({ slug: 'winter-knitwear' }),
|
|
244
|
+
ref({ pageSize: 24, page: 1 })
|
|
245
|
+
);
|
|
246
|
+
storefront.catalog.related(ref('merino-crew-sweater'), 8);
|
|
247
|
+
await ssr.settleAll();
|
|
248
|
+
|
|
249
|
+
expect(ssr.keys).toEqual([
|
|
250
|
+
'storefront:catalog.collectionProducts:[{"slug":"winter-knitwear"},{"page":1,"pageSize":24}]',
|
|
251
|
+
'storefront:catalog.related:["merino-crew-sweater",8]',
|
|
252
|
+
]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* **One read of one product in two languages is two keys.** The key is what the value travels to
|
|
257
|
+
* the browser in, and Nuxt's payload plugin writes the destination's keys into
|
|
258
|
+
* `nuxtApp.static.data` on every navigation while `useAsyncData` reuses an entry for a key it
|
|
259
|
+
* has already seen — so a shared key would serve a shopper who switched language the data of the
|
|
260
|
+
* language they left, and for the tick both pages are mounted the two would fight over one
|
|
261
|
+
* entry. The unprefixed site keeps the key it always had, so no existing artifact's payload is
|
|
262
|
+
* invalidated.
|
|
263
|
+
*/
|
|
264
|
+
it('keys a read by its locale as well, so two languages are two payload entries', async () => {
|
|
265
|
+
const icelandic = asyncDataStub();
|
|
266
|
+
wire(fakeClient().client, icelandic.prerender, () => 'is-IS').storefront.catalog.product(
|
|
267
|
+
ref('merino-crew-sweater')
|
|
268
|
+
);
|
|
269
|
+
await icelandic.settleAll();
|
|
270
|
+
|
|
271
|
+
const english = asyncDataStub();
|
|
272
|
+
wire(fakeClient().client, english.prerender).storefront.catalog.product(
|
|
273
|
+
ref('merino-crew-sweater')
|
|
274
|
+
);
|
|
275
|
+
await english.settleAll();
|
|
276
|
+
|
|
277
|
+
expect(icelandic.keys).toEqual(['storefront:catalog.product:["merino-crew-sweater","is-IS"]']);
|
|
278
|
+
expect(english.keys).toEqual(['storefront:catalog.product:["merino-crew-sweater"]']);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('paints the payload’s own value while hydrating, without calling the gateway', async () => {
|
|
282
|
+
const prerendered: StorefrontProduct = {
|
|
283
|
+
handle: 'merino-crew-sweater',
|
|
284
|
+
title: 'Merino crew sweater',
|
|
285
|
+
url: '/products/merino-crew-sweater',
|
|
286
|
+
featuredImage: null,
|
|
287
|
+
images: [],
|
|
288
|
+
options: [],
|
|
289
|
+
categoryTrail: [],
|
|
290
|
+
description: '',
|
|
291
|
+
inventory: null,
|
|
292
|
+
price: { amount: 96, compareAt: null },
|
|
293
|
+
stock: 'in',
|
|
294
|
+
available: true,
|
|
295
|
+
productId: 'p-1',
|
|
296
|
+
variantId: 'v-1',
|
|
297
|
+
};
|
|
298
|
+
const hydrating = asyncDataStub({
|
|
299
|
+
'storefront:catalog.product:["merino-crew-sweater"]': prerendered,
|
|
300
|
+
});
|
|
301
|
+
const calls = fakeClient();
|
|
302
|
+
const { storefront } = wire(calls.client, hydrating.prerender);
|
|
303
|
+
|
|
304
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
305
|
+
// Synchronously — the browser's first render has to be the server's render.
|
|
306
|
+
expect(result.data.value).toEqual(prerendered);
|
|
307
|
+
expect(result.pending.value).toBe(false);
|
|
308
|
+
|
|
309
|
+
await hydrating.settleAll();
|
|
310
|
+
expect(calls.detailReads).toBe(0);
|
|
311
|
+
expect(result.data.value).toEqual(prerendered);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('loads the ordinary way for a result the runtime has nothing prerendered for', async () => {
|
|
315
|
+
const calls = fakeClient();
|
|
316
|
+
// A client navigation: the runtime answers `null`, and nothing is cached or awaited.
|
|
317
|
+
const { storefront } = wire(calls.client, undefined);
|
|
318
|
+
|
|
319
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
320
|
+
await settle();
|
|
321
|
+
|
|
322
|
+
expect(calls.detailReads).toBe(1);
|
|
323
|
+
expect(result.data.value?.title).toBe('Merino crew sweater');
|
|
324
|
+
expect(result.pending.value).toBe(false);
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
describe('gateway storefront — the volatile refresh after hydration', () => {
|
|
329
|
+
/**
|
|
330
|
+
* Asserted at the URL the SDK actually sends: `filter` is the gateway's one repeatable
|
|
331
|
+
* parameter, and the whole point of collecting the page's results is that they leave as one
|
|
332
|
+
* request rather than one per block.
|
|
333
|
+
*/
|
|
334
|
+
it('issues exactly one batched request for every result on the page', async () => {
|
|
335
|
+
const urls: string[] = [];
|
|
336
|
+
const client = createEldraClient({
|
|
337
|
+
apiBaseUrl: 'https://api.example.test/api',
|
|
338
|
+
orgId: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
|
|
339
|
+
httpClient: (async (request: EldraHttpRequest) => {
|
|
340
|
+
urls.push(request.url);
|
|
341
|
+
const filters = new URL(request.url).searchParams.getAll('filter');
|
|
342
|
+
if (filters.some((filter) => filter.startsWith('id:in:'))) return { data: [], meta: META };
|
|
343
|
+
if (filters.some((filter) => filter.startsWith('slug:in:'))) {
|
|
344
|
+
return { data: [listRow('p-3', 30), listRow('p-4', 40)], meta: META };
|
|
345
|
+
}
|
|
346
|
+
return { data: [listRow('p-1', 10), listRow('p-2', 20)], meta: META };
|
|
347
|
+
}) as never,
|
|
348
|
+
});
|
|
349
|
+
const ssr = asyncDataStub();
|
|
350
|
+
const { storefront, refresher } = wire(client, ssr.prerender);
|
|
351
|
+
|
|
352
|
+
storefront.catalog.collectionProducts(
|
|
353
|
+
ref({ slug: 'winter-knitwear' }),
|
|
354
|
+
ref({ page: 1, pageSize: 24 })
|
|
355
|
+
);
|
|
356
|
+
storefront.catalog.byHandles(ref(['p-3', 'p-4']));
|
|
357
|
+
await ssr.settleAll();
|
|
358
|
+
const prerenderRequests = urls.length;
|
|
359
|
+
|
|
360
|
+
await refresher.refresh();
|
|
361
|
+
|
|
362
|
+
const refreshUrls = urls.slice(prerenderRequests);
|
|
363
|
+
expect(refreshUrls).toHaveLength(1);
|
|
364
|
+
expect(new URL(refreshUrls[0]!).searchParams.getAll('filter')).toEqual([
|
|
365
|
+
'id:in:p-1,p-2,p-3,p-4',
|
|
366
|
+
]);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('swaps only the volatile fields, and clears `revalidating` when it has', async () => {
|
|
370
|
+
const answer = deferred<Array<Record<string, unknown>>>();
|
|
371
|
+
const calls = fakeClient({ list: () => answer.promise });
|
|
372
|
+
const ssr = asyncDataStub();
|
|
373
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
374
|
+
|
|
375
|
+
const grid = storefront.catalog.collectionProducts(
|
|
376
|
+
ref({ slug: 'winter-knitwear' }),
|
|
377
|
+
ref({ page: 1, pageSize: 24 })
|
|
378
|
+
);
|
|
379
|
+
await ssr.settleAll();
|
|
380
|
+
const before = grid.data.value!.items[0]!;
|
|
381
|
+
const untouched = grid.data.value!.items[1]!;
|
|
382
|
+
expect(before.price.amount).toBe(10);
|
|
383
|
+
expect(grid.revalidating.value.size).toBe(0);
|
|
384
|
+
|
|
385
|
+
const running = refresher.refresh();
|
|
386
|
+
// The prerendered value stays on screen while its replacement is in flight.
|
|
387
|
+
expect([...grid.revalidating.value].sort()).toEqual(['price', 'stock']);
|
|
388
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(10);
|
|
389
|
+
|
|
390
|
+
answer.resolve([listRow('p-1', 12, { status: 'DRAFT' })]);
|
|
391
|
+
await running;
|
|
392
|
+
|
|
393
|
+
const after = grid.data.value!.items[0]!;
|
|
394
|
+
expect(after.price.amount).toBe(12);
|
|
395
|
+
expect(after.available).toBe(false);
|
|
396
|
+
expect(after.stock).toBe('out');
|
|
397
|
+
// Everything that is not volatile is still the prerendered value…
|
|
398
|
+
expect(after.title).toBe(before.title);
|
|
399
|
+
expect(after.url).toBe(before.url);
|
|
400
|
+
expect(after.featuredImage).toBe(before.featuredImage);
|
|
401
|
+
// …and a product the answer said nothing about is still the very same object.
|
|
402
|
+
expect(grid.data.value?.items[1]).toBe(untouched);
|
|
403
|
+
expect(grid.revalidating.value.size).toBe(0);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* The detail page is the one result whose stock line is about a *variant*, so it needs the
|
|
408
|
+
* variant-level inventory the batched `id:in:` read (the products list) carries none of. It
|
|
409
|
+
* refreshes through its own product read instead, and stays out of the batch.
|
|
410
|
+
*/
|
|
411
|
+
it('refreshes the detail product through `catalog.product`, not through the batch', async () => {
|
|
412
|
+
let price = 96;
|
|
413
|
+
const calls = fakeClient({ detail: () => detailRow(price), list: async () => [] });
|
|
414
|
+
const ssr = asyncDataStub();
|
|
415
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
416
|
+
|
|
417
|
+
const detail = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
418
|
+
await ssr.settleAll();
|
|
419
|
+
expect(calls.detailReads).toBe(1);
|
|
420
|
+
|
|
421
|
+
price = 79;
|
|
422
|
+
await refresher.refresh();
|
|
423
|
+
|
|
424
|
+
expect(calls.detailReads).toBe(2);
|
|
425
|
+
// No batched read at all: the page's only product is the detail one.
|
|
426
|
+
expect(volatileReads(calls)).toEqual([]);
|
|
427
|
+
expect(detail.data.value?.price.amount).toBe(79);
|
|
428
|
+
expect(detail.data.value?.title).toBe('Merino crew sweater');
|
|
429
|
+
expect(detail.revalidating.value.size).toBe(0);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* The prerender half of the inventory read: the generated HTML carries the stock the product had
|
|
434
|
+
* at build, and the refresh after mount is what corrects it. A page prerendered while the last
|
|
435
|
+
* one was on the shelf has to stop saying "In stock, ready to ship" once it is gone.
|
|
436
|
+
*/
|
|
437
|
+
it('corrects a prerendered stock line from inventory on the refresh after mount', async () => {
|
|
438
|
+
let onHand = 4;
|
|
439
|
+
const calls = fakeClient({
|
|
440
|
+
detail: () => detailRow(96),
|
|
441
|
+
list: async () => [],
|
|
442
|
+
stock: () => [
|
|
443
|
+
{
|
|
444
|
+
variantId: 'v-1',
|
|
445
|
+
available: onHand > 0,
|
|
446
|
+
allowBackorder: false,
|
|
447
|
+
availableQuantity: onHand,
|
|
448
|
+
},
|
|
449
|
+
],
|
|
450
|
+
});
|
|
451
|
+
const ssr = asyncDataStub();
|
|
452
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
453
|
+
|
|
454
|
+
const detail = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
455
|
+
await ssr.settleAll();
|
|
456
|
+
expect(detail.data.value?.stock).toBe('in');
|
|
457
|
+
expect(detail.data.value?.inventory).toBe(4);
|
|
458
|
+
|
|
459
|
+
onHand = 0;
|
|
460
|
+
await refresher.refresh();
|
|
461
|
+
|
|
462
|
+
expect(detail.data.value?.stock).toBe('out');
|
|
463
|
+
expect(detail.data.value?.inventory).toBe(0);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it('drops an answer for data the result has already replaced', async () => {
|
|
467
|
+
const answer = deferred<Array<Record<string, unknown>>>();
|
|
468
|
+
const collection = { rows: () => [listRow('p-1', 10)] };
|
|
469
|
+
const calls = fakeClient({
|
|
470
|
+
list: () => answer.promise,
|
|
471
|
+
collectionProducts: () => collection.rows(),
|
|
472
|
+
});
|
|
473
|
+
const ssr = asyncDataStub();
|
|
474
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
475
|
+
|
|
476
|
+
const opts = ref({ page: 1, pageSize: 24 });
|
|
477
|
+
const grid = storefront.catalog.collectionProducts(ref({ slug: 'winter-knitwear' }), opts);
|
|
478
|
+
await ssr.settleAll();
|
|
479
|
+
|
|
480
|
+
const running = refresher.refresh();
|
|
481
|
+
// The shopper pages the grid: the result reloads and answers with the live price, 120.
|
|
482
|
+
collection.rows = () => [listRow('p-1', 120)];
|
|
483
|
+
opts.value = { page: 2, pageSize: 24 };
|
|
484
|
+
await settle();
|
|
485
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(120);
|
|
486
|
+
|
|
487
|
+
// …and only then does the refresh, issued against the data that is now gone, answer.
|
|
488
|
+
answer.resolve([listRow('p-1', 90)]);
|
|
489
|
+
await running;
|
|
490
|
+
|
|
491
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(120);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it('keeps the prerendered values when the refresh fails, and clears `revalidating`', async () => {
|
|
495
|
+
const answer = deferred<Array<Record<string, unknown>>>();
|
|
496
|
+
const calls = fakeClient({ list: () => answer.promise });
|
|
497
|
+
const ssr = asyncDataStub();
|
|
498
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
499
|
+
|
|
500
|
+
const grid = storefront.catalog.collectionProducts(
|
|
501
|
+
ref({ slug: 'winter-knitwear' }),
|
|
502
|
+
ref({ page: 1, pageSize: 24 })
|
|
503
|
+
);
|
|
504
|
+
await ssr.settleAll();
|
|
505
|
+
|
|
506
|
+
const running = refresher.refresh();
|
|
507
|
+
answer.reject(new Error('gateway unreachable'));
|
|
508
|
+
await running;
|
|
509
|
+
|
|
510
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(10);
|
|
511
|
+
expect(grid.data.value?.items[1]?.price.amount).toBe(20);
|
|
512
|
+
// A page never regresses to an error state for a value it can already show.
|
|
513
|
+
expect(grid.error.value).toBeNull();
|
|
514
|
+
expect(grid.revalidating.value.size).toBe(0);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it('refreshes each result once, and asks nothing when nothing is pending', async () => {
|
|
518
|
+
const calls = fakeClient({ list: async () => [listRow('p-1', 12)] });
|
|
519
|
+
const ssr = asyncDataStub();
|
|
520
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
521
|
+
|
|
522
|
+
storefront.catalog.collectionProducts(
|
|
523
|
+
ref({ slug: 'winter-knitwear' }),
|
|
524
|
+
ref({ page: 1, pageSize: 24 })
|
|
525
|
+
);
|
|
526
|
+
await ssr.settleAll();
|
|
527
|
+
|
|
528
|
+
await refresher.refresh();
|
|
529
|
+
await refresher.refresh();
|
|
530
|
+
await settle();
|
|
531
|
+
|
|
532
|
+
expect(volatileReads(calls)).toHaveLength(1);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* The defect a real `nuxi generate` + browser run found (`test/prerenderRefresh.browser.spec.ts`):
|
|
537
|
+
* a theme's blocks are lazily imported components, so the ones whose chunk lands after the app
|
|
538
|
+
* has mounted create their results after the first batch has already gone out. A refresher that
|
|
539
|
+
* ran once per page dropped them — the product page's carousel never refreshed a price and never
|
|
540
|
+
* drew the refresh treatment, because `product-detail` had opened and closed the page's only
|
|
541
|
+
* batch a tick earlier.
|
|
542
|
+
*/
|
|
543
|
+
it('opens another batch for a result that registered after the last one flushed', async () => {
|
|
544
|
+
// Rows by the ids asked for, so a late result can be about a different product than the first
|
|
545
|
+
// batch was — which is what makes a second batch the right answer rather than a duplicate one.
|
|
546
|
+
const calls = fakeClient({
|
|
547
|
+
list: async (filter) =>
|
|
548
|
+
(filter[0]?.replace(/^\w+:in:/, '').split(',') ?? []).map((id) => listRow(id, 12)),
|
|
549
|
+
});
|
|
550
|
+
const ssr = asyncDataStub();
|
|
551
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
552
|
+
|
|
553
|
+
storefront.catalog.byHandles(ref(['p-1']));
|
|
554
|
+
await ssr.settleAll();
|
|
555
|
+
await refresher.refresh();
|
|
556
|
+
expect(volatileReads(calls)).toEqual([['id:in:p-1']]);
|
|
557
|
+
|
|
558
|
+
const late = storefront.catalog.byHandles(ref(['p-2']));
|
|
559
|
+
await ssr.settleAll();
|
|
560
|
+
await refresher.refresh();
|
|
561
|
+
await settle();
|
|
562
|
+
|
|
563
|
+
expect(volatileReads(calls)).toEqual([['id:in:p-1'], ['id:in:p-2']]);
|
|
564
|
+
expect(late.data.value?.[0]?.price.amount).toBe(12);
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* The defect the deployed product page showed with a plain request log: the detail read and the
|
|
569
|
+
* `id:in` batch both went out twice, 35 ms apart, with identical ids — two bursts asking again
|
|
570
|
+
* about products the first burst had already re-read. A page load asks about a product once,
|
|
571
|
+
* whichever burst the result that shows it happens to arrive in.
|
|
572
|
+
*/
|
|
573
|
+
it('never asks twice about a product another result has already had refreshed', async () => {
|
|
574
|
+
const calls = fakeClient({
|
|
575
|
+
list: async (filter) =>
|
|
576
|
+
(filter[0]?.replace(/^\w+:in:/, '').split(',') ?? []).map((id) => listRow(id, 7)),
|
|
577
|
+
});
|
|
578
|
+
const ssr = asyncDataStub();
|
|
579
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
580
|
+
|
|
581
|
+
const grid = storefront.catalog.collectionProducts(
|
|
582
|
+
ref({ slug: 'winter-knitwear' }),
|
|
583
|
+
ref({ page: 1, pageSize: 24 })
|
|
584
|
+
);
|
|
585
|
+
await ssr.settleAll();
|
|
586
|
+
await refresher.refresh();
|
|
587
|
+
expect(volatileReads(calls)).toEqual([['id:in:p-1,p-2']]);
|
|
588
|
+
|
|
589
|
+
// A block whose chunk landed later, showing the same two products.
|
|
590
|
+
const late = storefront.catalog.byHandles(ref(['p-1', 'p-2']));
|
|
591
|
+
await ssr.settleAll();
|
|
592
|
+
await refresher.refresh();
|
|
593
|
+
await settle();
|
|
594
|
+
|
|
595
|
+
expect(volatileReads(calls)).toEqual([['id:in:p-1,p-2']]);
|
|
596
|
+
// …and it is not left showing the stale values either: the answer that already arrived for
|
|
597
|
+
// those ids is folded into it, so the two blocks cannot paint two different prices.
|
|
598
|
+
expect(late.data.value?.map((item) => item.price.amount)).toEqual([7, 7]);
|
|
599
|
+
expect(grid.data.value?.items.map((item) => item.price.amount)).toEqual([7, 7]);
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
it('refreshes a result once even when it registers twice', async () => {
|
|
603
|
+
// Straight at the collector: a block that creates its results twice (a remount, a second
|
|
604
|
+
// render pass) hands the same entry over twice, and that is one thing to ask about, not two.
|
|
605
|
+
const reads: string[][] = [];
|
|
606
|
+
const refresher = createVolatileRefresher(
|
|
607
|
+
async (ids) => {
|
|
608
|
+
reads.push(ids);
|
|
609
|
+
return ids.map((id) => ({
|
|
610
|
+
id,
|
|
611
|
+
price: { amount: 5, compareAt: null },
|
|
612
|
+
available: true,
|
|
613
|
+
stock: 'in' as const,
|
|
614
|
+
}));
|
|
615
|
+
},
|
|
616
|
+
() => {}
|
|
617
|
+
);
|
|
618
|
+
let written = 0;
|
|
619
|
+
const entry = {
|
|
620
|
+
read: () => [{ handle: 'p-1', productId: 'p-1', price: { amount: 4, compareAt: null } }],
|
|
621
|
+
write: () => {
|
|
622
|
+
written += 1;
|
|
623
|
+
},
|
|
624
|
+
setRevalidating: () => {},
|
|
625
|
+
token: () => 1,
|
|
626
|
+
};
|
|
627
|
+
refresher.register(entry);
|
|
628
|
+
refresher.register(entry);
|
|
629
|
+
await refresher.refresh();
|
|
630
|
+
await refresher.refresh();
|
|
631
|
+
|
|
632
|
+
expect(reads).toEqual([['p-1']]);
|
|
633
|
+
expect(written).toBe(1);
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* The product page and a grid on the same page are about the same product id, and the batched read
|
|
638
|
+
* cannot answer what the product page needs: the products list carries no inventory, so an answer
|
|
639
|
+
* folded in from it would leave the page's stock line on the value it was built with. Before the
|
|
640
|
+
* ids were named for what they are this fell out by accident — a detail product's volatile id was a
|
|
641
|
+
* *variant's*, which no list read matched — and the first page to hit it was a client-side
|
|
642
|
+
* navigation from a collection page, where the grid had already claimed that product.
|
|
643
|
+
*/
|
|
644
|
+
it('still reads the detail product for itself when a card list already asked about it', async () => {
|
|
645
|
+
const calls = fakeClient({
|
|
646
|
+
detail: () => detailRow(96),
|
|
647
|
+
list: async () => [listRow('p-1', 96)],
|
|
648
|
+
stock: () => [
|
|
649
|
+
{ variantId: 'v-1', available: false, allowBackorder: false, availableQuantity: 0 },
|
|
650
|
+
],
|
|
651
|
+
});
|
|
652
|
+
const ssr = asyncDataStub();
|
|
653
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
654
|
+
|
|
655
|
+
// The grid registers first, exactly as a collection page's own refresh does.
|
|
656
|
+
const grid = storefront.catalog.byHandles(ref(['p-1']));
|
|
657
|
+
const detail = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
658
|
+
await ssr.settleAll();
|
|
659
|
+
const before = calls.detailReads;
|
|
660
|
+
|
|
661
|
+
await refresher.refresh();
|
|
662
|
+
|
|
663
|
+
expect(calls.detailReads - before).toBe(1);
|
|
664
|
+
expect(detail.data.value?.stock).toBe('out');
|
|
665
|
+
expect(detail.data.value?.inventory).toBe(0);
|
|
666
|
+
// …and the grid still got its own batched answer, unaffected.
|
|
667
|
+
expect(volatileReads(calls)).toEqual([['id:in:p-1']]);
|
|
668
|
+
expect(grid.data.value?.[0]?.price.amount).toBe(96);
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
it('lets two results that need the same detail read share one request', async () => {
|
|
672
|
+
const calls = fakeClient();
|
|
673
|
+
const ssr = asyncDataStub();
|
|
674
|
+
const { storefront, refresher } = wire(calls.client, ssr.prerender);
|
|
675
|
+
|
|
676
|
+
const handle = ref('merino-crew-sweater');
|
|
677
|
+
const first = storefront.catalog.product(handle);
|
|
678
|
+
const second = storefront.catalog.product(handle);
|
|
679
|
+
await ssr.settleAll();
|
|
680
|
+
const beforeRefresh = calls.detailReads;
|
|
681
|
+
|
|
682
|
+
await refresher.refresh();
|
|
683
|
+
await settle();
|
|
684
|
+
|
|
685
|
+
expect(calls.detailReads - beforeRefresh).toBe(1);
|
|
686
|
+
expect(first.data.value?.price.amount).toBe(96);
|
|
687
|
+
expect(second.data.value?.price.amount).toBe(96);
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* The other half of that rule: batching by burst is only safe because a result created *after*
|
|
692
|
+
* hydration never joins one. It has just read the live values itself, and the runtime tells it
|
|
693
|
+
* so by prerendering nothing for it (`app/plugins/eldra-storefront.ts` returns `null` off the
|
|
694
|
+
* hydrating render).
|
|
695
|
+
*/
|
|
696
|
+
it('never batches a result the runtime prerendered nothing for', async () => {
|
|
697
|
+
const calls = fakeClient({ list: async () => [listRow('p-1', 12)] });
|
|
698
|
+
const { storefront, refresher } = wire(calls.client);
|
|
699
|
+
|
|
700
|
+
const later = storefront.catalog.byHandles(ref(['p-1']));
|
|
701
|
+
await settle();
|
|
702
|
+
await refresher.refresh();
|
|
703
|
+
await settle();
|
|
704
|
+
|
|
705
|
+
expect(later.data.value?.[0]?.price.amount).toBe(12);
|
|
706
|
+
expect(volatileReads(calls)).toHaveLength(0);
|
|
707
|
+
expect(later.revalidating.value.size).toBe(0);
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
it('hands one batched answer to every result that took part in it', async () => {
|
|
711
|
+
const seen: VolatileSnapshot[] = [];
|
|
712
|
+
const calls = fakeClient({
|
|
713
|
+
list: async (filter) =>
|
|
714
|
+
filter[0]?.startsWith('slug:in:') === true
|
|
715
|
+
? [listRow('p-1', 10)]
|
|
716
|
+
: [listRow('p-1', 11), listRow('p-2', 22)],
|
|
717
|
+
});
|
|
718
|
+
const ssr = asyncDataStub();
|
|
719
|
+
const refresher = createVolatileRefresher(
|
|
720
|
+
async (ids) => {
|
|
721
|
+
const answer = await storefront.catalog.volatileByIds(ids);
|
|
722
|
+
seen.push(...answer);
|
|
723
|
+
return answer;
|
|
724
|
+
},
|
|
725
|
+
() => {}
|
|
726
|
+
);
|
|
727
|
+
const storefront = createGatewayStorefront(calls.client, {
|
|
728
|
+
route: fakeRoute(),
|
|
729
|
+
runtime: { prerender: ssr.prerender, register: refresher.register },
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
const grid = storefront.catalog.collectionProducts(
|
|
733
|
+
ref({ slug: 'winter-knitwear' }),
|
|
734
|
+
ref({ page: 1, pageSize: 24 })
|
|
735
|
+
);
|
|
736
|
+
const row = storefront.catalog.byHandles(ref(['p-1']));
|
|
737
|
+
await ssr.settleAll();
|
|
738
|
+
expect(row.data.value?.[0]?.price.amount).toBe(10);
|
|
739
|
+
|
|
740
|
+
await refresher.refresh();
|
|
741
|
+
|
|
742
|
+
expect(volatileReads(calls)).toEqual([['id:in:p-1,p-2']]);
|
|
743
|
+
expect(seen.map((snapshot) => snapshot.id)).toEqual(['p-1', 'p-2']);
|
|
744
|
+
// Both results took the same answer: the grid's `p-1` and the row's `p-1` cannot disagree.
|
|
745
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(11);
|
|
746
|
+
expect(row.data.value?.[0]?.price.amount).toBe(11);
|
|
747
|
+
});
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* A stand-in for Nuxt 4.5's `useAsyncData` on a **hydrating client**, modelling the three
|
|
752
|
+
* behaviours `app/storefront/prerender.ts` exists to survive
|
|
753
|
+
* (`nuxt/dist/app/composables/asyncData.js`):
|
|
754
|
+
*
|
|
755
|
+
* * hydration **short-circuits** — status goes straight to `success` and the handler never runs
|
|
756
|
+
* — as soon as `data.value !== undefined`, which is exactly what an `options.default` makes
|
|
757
|
+
* true before anything has loaded. The stub honours a `default` it is passed for that reason:
|
|
758
|
+
* the adapter must not pass one, and this is what says so.
|
|
759
|
+
* * on a payload miss the first fetch is **deferred to the component's `onBeforeMount`**
|
|
760
|
+
* (`flushBeforeMount()` here — Vue runs it in the same synchronous mount pass, before any
|
|
761
|
+
* microtask, so it always precedes the adapter's own recovery), and the handle's promise
|
|
762
|
+
* resolves with the load not yet run and the status still `idle`.
|
|
763
|
+
* * a second registration of the same key runs the handler **again** under Nuxt's default
|
|
764
|
+
* `dedupe: 'cancel'`, and joins the request already in flight under `dedupe: 'defer'`.
|
|
765
|
+
*/
|
|
766
|
+
function nuxtAsyncDataStub(payload: Record<string, unknown> = {}) {
|
|
767
|
+
interface Entry {
|
|
768
|
+
data: { value: unknown };
|
|
769
|
+
error: { value: unknown };
|
|
770
|
+
status: { value: string };
|
|
771
|
+
inFlight: Promise<unknown> | null;
|
|
772
|
+
}
|
|
773
|
+
const entries = new Map<string, Entry>();
|
|
774
|
+
const beforeMount: Array<() => void> = [];
|
|
775
|
+
let handlerCalls = 0;
|
|
776
|
+
|
|
777
|
+
function run(entry: Entry, load: () => Promise<unknown>): Promise<unknown> {
|
|
778
|
+
handlerCalls += 1;
|
|
779
|
+
entry.status.value = 'pending';
|
|
780
|
+
const request = load().then(
|
|
781
|
+
(value) => {
|
|
782
|
+
entry.data.value = value;
|
|
783
|
+
entry.status.value = 'success';
|
|
784
|
+
entry.inFlight = null;
|
|
785
|
+
},
|
|
786
|
+
(caught: unknown) => {
|
|
787
|
+
entry.error.value = caught;
|
|
788
|
+
entry.status.value = 'error';
|
|
789
|
+
entry.inFlight = null;
|
|
790
|
+
}
|
|
791
|
+
);
|
|
792
|
+
entry.inFlight = request;
|
|
793
|
+
return request;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
return {
|
|
797
|
+
handlerCalls: () => handlerCalls,
|
|
798
|
+
/** Vue's `onBeforeMount`, where Nuxt put the deferred first fetch. Synchronous, as it is. */
|
|
799
|
+
flushBeforeMount(): void {
|
|
800
|
+
for (const deferred of beforeMount.splice(0)) deferred();
|
|
801
|
+
},
|
|
802
|
+
asyncData<T>(
|
|
803
|
+
key: string,
|
|
804
|
+
load: () => Promise<T | null>,
|
|
805
|
+
options: KeyedAsyncDataOptions
|
|
806
|
+
): KeyedAsyncData<T> {
|
|
807
|
+
const fallback = (options as { default?: () => unknown }).default;
|
|
808
|
+
let entry = entries.get(key);
|
|
809
|
+
if (entry === undefined) {
|
|
810
|
+
entry = {
|
|
811
|
+
data: { value: key in payload ? payload[key] : fallback?.() },
|
|
812
|
+
error: { value: null },
|
|
813
|
+
status: { value: 'idle' },
|
|
814
|
+
inFlight: null,
|
|
815
|
+
};
|
|
816
|
+
entries.set(key, entry);
|
|
817
|
+
}
|
|
818
|
+
const current = entry;
|
|
819
|
+
const execute = (): Promise<unknown> =>
|
|
820
|
+
current.inFlight !== null && options.dedupe === 'defer'
|
|
821
|
+
? current.inFlight
|
|
822
|
+
: run(current, load);
|
|
823
|
+
let settled: Promise<unknown>;
|
|
824
|
+
if (current.data.value !== undefined) {
|
|
825
|
+
// Hydrating with a value in hand: nothing runs, ever.
|
|
826
|
+
current.status.value = 'success';
|
|
827
|
+
settled = Promise.resolve();
|
|
828
|
+
} else if (current.inFlight !== null) {
|
|
829
|
+
settled = execute();
|
|
830
|
+
} else {
|
|
831
|
+
beforeMount.push(() => void execute());
|
|
832
|
+
settled = Promise.resolve();
|
|
833
|
+
}
|
|
834
|
+
return {
|
|
835
|
+
data: current.data as { value: T | null | undefined },
|
|
836
|
+
error: current.error,
|
|
837
|
+
status: current.status,
|
|
838
|
+
execute,
|
|
839
|
+
settled,
|
|
840
|
+
};
|
|
841
|
+
},
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
describe('gateway storefront — prerendering through Nuxt’s keyed async data', () => {
|
|
846
|
+
it('fetches on the client when the payload has no value for the key', async () => {
|
|
847
|
+
const stub = nuxtAsyncDataStub();
|
|
848
|
+
const calls = fakeClient();
|
|
849
|
+
const { storefront } = wire(calls.client, (key, load) =>
|
|
850
|
+
prerenderThroughAsyncData(stub.asyncData, key, load)
|
|
851
|
+
);
|
|
852
|
+
|
|
853
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
854
|
+
stub.flushBeforeMount();
|
|
855
|
+
await settle();
|
|
856
|
+
|
|
857
|
+
// A `default` here would have made Nuxt call this a hydration hit before anything ran, and the
|
|
858
|
+
// page would sit at `pending` with no data and no request for the rest of its life.
|
|
859
|
+
expect(stub.handlerCalls()).toBe(1);
|
|
860
|
+
expect(calls.detailReads).toBe(1);
|
|
861
|
+
expect(result.data.value?.title).toBe('Merino crew sweater');
|
|
862
|
+
expect(result.pending.value).toBe(false);
|
|
863
|
+
expect(result.loading.value).toBe(false);
|
|
864
|
+
});
|
|
865
|
+
|
|
866
|
+
it('runs the deferred load itself when nothing else has', async () => {
|
|
867
|
+
const stub = nuxtAsyncDataStub();
|
|
868
|
+
const calls = fakeClient();
|
|
869
|
+
const { storefront } = wire(calls.client, (key, load) =>
|
|
870
|
+
prerenderThroughAsyncData(stub.asyncData, key, load)
|
|
871
|
+
);
|
|
872
|
+
|
|
873
|
+
// No `flushBeforeMount()`: the handle settles `idle`, with the load still waiting.
|
|
874
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
875
|
+
await settle();
|
|
876
|
+
|
|
877
|
+
expect(calls.detailReads).toBe(1);
|
|
878
|
+
expect(result.data.value?.title).toBe('Merino crew sweater');
|
|
879
|
+
expect(result.pending.value).toBe(false);
|
|
880
|
+
});
|
|
881
|
+
|
|
882
|
+
it('costs one request when two results read the same key', async () => {
|
|
883
|
+
const stub = nuxtAsyncDataStub();
|
|
884
|
+
const calls = fakeClient();
|
|
885
|
+
const { storefront } = wire(calls.client, (key, load) =>
|
|
886
|
+
prerenderThroughAsyncData(stub.asyncData, key, load)
|
|
887
|
+
);
|
|
888
|
+
|
|
889
|
+
const first = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
890
|
+
const second = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
891
|
+
stub.flushBeforeMount();
|
|
892
|
+
await settle();
|
|
893
|
+
|
|
894
|
+
expect(stub.handlerCalls()).toBe(1);
|
|
895
|
+
expect(calls.detailReads).toBe(1);
|
|
896
|
+
expect(first.data.value?.title).toBe('Merino crew sweater');
|
|
897
|
+
expect(second.data.value?.title).toBe('Merino crew sweater');
|
|
898
|
+
});
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* The state a prerendered page for a discontinued product hydrates into. The server's read
|
|
902
|
+
* answered `null` and the page says so; the browser must start from that same answer, not from
|
|
903
|
+
* "still loading" — the block's not-found line and its loading line are different markup, and Vue
|
|
904
|
+
* would report the mismatch and repaint.
|
|
905
|
+
*/
|
|
906
|
+
it('hydrates a payload’s settled empty answer as answered, not as pending', async () => {
|
|
907
|
+
const stub = nuxtAsyncDataStub({ 'storefront:catalog.product:["gone-for-good"]': null });
|
|
908
|
+
const calls = fakeClient();
|
|
909
|
+
const { storefront } = wire(calls.client, (key, load) =>
|
|
910
|
+
prerenderThroughAsyncData(stub.asyncData, key, load)
|
|
911
|
+
);
|
|
912
|
+
|
|
913
|
+
const result = storefront.catalog.product(ref('gone-for-good'));
|
|
914
|
+
|
|
915
|
+
// Synchronously, before any tick: settled, empty, and nothing in flight.
|
|
916
|
+
expect(result.data.value).toBeNull();
|
|
917
|
+
expect(result.pending.value).toBe(false);
|
|
918
|
+
expect(result.loading.value).toBe(false);
|
|
919
|
+
|
|
920
|
+
stub.flushBeforeMount();
|
|
921
|
+
await settle();
|
|
922
|
+
|
|
923
|
+
expect(stub.handlerCalls()).toBe(0);
|
|
924
|
+
expect(calls.detailReads).toBe(0);
|
|
925
|
+
expect(result.data.value).toBeNull();
|
|
926
|
+
expect(result.pending.value).toBe(false);
|
|
927
|
+
expect(result.loading.value).toBe(false);
|
|
928
|
+
expect(result.error.value).toBeNull();
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
it('takes the payload’s value and runs nothing at all', async () => {
|
|
932
|
+
const stub = nuxtAsyncDataStub({
|
|
933
|
+
'storefront:catalog.product:["merino-crew-sweater"]': { title: 'From the payload' },
|
|
934
|
+
});
|
|
935
|
+
const calls = fakeClient();
|
|
936
|
+
const { storefront } = wire(calls.client, (key, load) =>
|
|
937
|
+
prerenderThroughAsyncData(stub.asyncData, key, load)
|
|
938
|
+
);
|
|
939
|
+
|
|
940
|
+
const result = storefront.catalog.product(ref('merino-crew-sweater'));
|
|
941
|
+
stub.flushBeforeMount();
|
|
942
|
+
await settle();
|
|
943
|
+
|
|
944
|
+
expect(stub.handlerCalls()).toBe(0);
|
|
945
|
+
expect(calls.detailReads).toBe(0);
|
|
946
|
+
expect(result.data.value?.title).toBe('From the payload');
|
|
947
|
+
});
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
describe('gateway storefront — `loading`, and who is allowed to write', () => {
|
|
951
|
+
it('is true for a reload over data the page already has, while `pending` stays false', async () => {
|
|
952
|
+
const answers = [[listRow('p-1', 10)], [listRow('p-1', 20)]];
|
|
953
|
+
const calls = fakeClient({ collectionProducts: () => answers.shift() ?? [] });
|
|
954
|
+
const ssr = asyncDataStub();
|
|
955
|
+
const { storefront } = wire(calls.client, ssr.prerender);
|
|
956
|
+
|
|
957
|
+
const opts = ref({ page: 1, pageSize: 24 });
|
|
958
|
+
const grid = storefront.catalog.collectionProducts(ref({ slug: 'winter-knitwear' }), opts);
|
|
959
|
+
expect(grid.loading.value).toBe(true);
|
|
960
|
+
await ssr.settleAll();
|
|
961
|
+
expect(grid.loading.value).toBe(false);
|
|
962
|
+
expect(grid.pending.value).toBe(false);
|
|
963
|
+
|
|
964
|
+
opts.value = { page: 2, pageSize: 24 };
|
|
965
|
+
await nextTick();
|
|
966
|
+
// A read is in flight over a value the page is already showing: a spinner, never a skeleton.
|
|
967
|
+
expect(grid.loading.value).toBe(true);
|
|
968
|
+
expect(grid.pending.value).toBe(false);
|
|
969
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(10);
|
|
970
|
+
|
|
971
|
+
await settle();
|
|
972
|
+
expect(grid.loading.value).toBe(false);
|
|
973
|
+
expect(grid.pending.value).toBe(false);
|
|
974
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(20);
|
|
975
|
+
});
|
|
976
|
+
|
|
977
|
+
it('stops being pending when the read answers nothing at all', async () => {
|
|
978
|
+
const ssr = asyncDataStub();
|
|
979
|
+
const calls = fakeClient();
|
|
980
|
+
const { storefront } = wire(calls.client, ssr.prerender);
|
|
981
|
+
|
|
982
|
+
// No handle: the read answers `null`, which is an answer — "no such product" — and a block
|
|
983
|
+
// that kept drawing a skeleton over it would never show its empty state.
|
|
984
|
+
const result = storefront.catalog.product(ref(null));
|
|
985
|
+
await ssr.settleAll();
|
|
986
|
+
|
|
987
|
+
expect(result.data.value).toBeNull();
|
|
988
|
+
expect(result.pending.value).toBe(false);
|
|
989
|
+
expect(result.loading.value).toBe(false);
|
|
990
|
+
expect(result.error.value).toBeNull();
|
|
991
|
+
});
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* `refresh()` is called from an event handler, outside any watcher, so nothing ever aborts its
|
|
995
|
+
* controller: without the generation guard its answer lands whenever the network returns it and
|
|
996
|
+
* overwrites the newer read the shopper actually asked for.
|
|
997
|
+
*/
|
|
998
|
+
it('lets a slow manual refresh lose to the reload that overtook it', async () => {
|
|
999
|
+
const slow = deferred<Array<Record<string, unknown>>>();
|
|
1000
|
+
// Answered in call order — the prerender, then the manual refresh, then the reload — so the
|
|
1001
|
+
// slow one is pinned to the manual refresh whatever the two of them do afterwards.
|
|
1002
|
+
const answers: Array<() => Promise<Array<Record<string, unknown>>>> = [
|
|
1003
|
+
async () => [listRow('p-1', 10)],
|
|
1004
|
+
() => slow.promise,
|
|
1005
|
+
async () => [listRow('p-1', 120)],
|
|
1006
|
+
];
|
|
1007
|
+
const client = {
|
|
1008
|
+
catalog: {
|
|
1009
|
+
listCollectionProducts: async () => ({
|
|
1010
|
+
data: await (answers.shift() ?? (async () => []))(),
|
|
1011
|
+
meta: META,
|
|
1012
|
+
}),
|
|
1013
|
+
},
|
|
1014
|
+
} as unknown as EldraClient;
|
|
1015
|
+
const ssr = asyncDataStub();
|
|
1016
|
+
const { storefront } = wire(client, ssr.prerender);
|
|
1017
|
+
|
|
1018
|
+
const opts = ref({ page: 1, pageSize: 24 });
|
|
1019
|
+
const grid = storefront.catalog.collectionProducts(ref({ slug: 'winter-knitwear' }), opts);
|
|
1020
|
+
await ssr.settleAll();
|
|
1021
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(10);
|
|
1022
|
+
|
|
1023
|
+
// A manual refresh that will take its time…
|
|
1024
|
+
const manual = grid.refresh();
|
|
1025
|
+
// …and, while it is out, the shopper pages the grid, which answers first.
|
|
1026
|
+
opts.value = { page: 2, pageSize: 24 };
|
|
1027
|
+
await settle();
|
|
1028
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(120);
|
|
1029
|
+
|
|
1030
|
+
slow.resolve([listRow('p-1', 90)]);
|
|
1031
|
+
await manual;
|
|
1032
|
+
await settle();
|
|
1033
|
+
|
|
1034
|
+
expect(grid.data.value?.items[0]?.price.amount).toBe(120);
|
|
1035
|
+
expect(grid.loading.value).toBe(false);
|
|
1036
|
+
});
|
|
1037
|
+
});
|