-
Notifications
You must be signed in to change notification settings - Fork 56
Create mocks for the entire point-of-sale extension API #3904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kumar303
merged 1 commit into
2026-04-rc
from
02-12-create_mocks_for_the_entire_point-of-sale_extension_api
Mar 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Environment Configuration | ||
| .env | ||
| .env.* | ||
|
|
||
| # Dependency directory | ||
| node_modules | ||
|
|
||
| # Test coverage directory | ||
| coverage | ||
|
|
||
| # Ignore Apple macOS Desktop Services Store | ||
| .DS_Store | ||
|
|
||
| # Logs | ||
| logs | ||
| *.log | ||
|
|
||
| # extensions build output | ||
| extensions/*/build | ||
| extensions/*/dist | ||
|
|
||
| # lock files | ||
|
|
||
|
|
||
|
|
||
| # Ignore shopify files created during app dev | ||
| .shopify/* | ||
| .shopify.lock |
30 changes: 30 additions & 0 deletions
30
examples/testing/point-of-sale-testing-example/.graphqlrc.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| const fs = require("node:fs"); | ||
|
|
||
| function getConfig() { | ||
| const config = { | ||
| projects: {}, | ||
| }; | ||
|
|
||
| let extensions = []; | ||
| try { | ||
| extensions = fs.readdirSync("./extensions"); | ||
| } catch { | ||
| // ignore if no extensions | ||
| } | ||
|
|
||
| for (const entry of extensions) { | ||
| const extensionPath = `./extensions/${entry}`; | ||
| const schema = `${extensionPath}/schema.graphql`; | ||
| if (!fs.existsSync(schema)) { | ||
| continue; | ||
| } | ||
| config.projects[entry] = { | ||
| schema, | ||
| documents: [`${extensionPath}/**/*.graphql`], | ||
| }; | ||
| } | ||
|
|
||
| return config; | ||
| } | ||
|
|
||
| module.exports = getConfig(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| This extension was created with: | ||
|
|
||
| ``` | ||
| shopify app init --name point-of-sale-testing-example | ||
| cd point-of-sale-testing-example | ||
| shopify app generate extension | ||
| # I chose 'POS block' | ||
| ``` | ||
|
|
||
| See it in action: | ||
|
|
||
| Build the root package: | ||
|
|
||
| ``` | ||
| yarn build | ||
| ``` | ||
|
|
||
| Change into this example package and run: | ||
|
|
||
| ``` | ||
| npm install | ||
| npm run typecheck | ||
| npm test | ||
| ``` |
Empty file.
32 changes: 32 additions & 0 deletions
32
.../point-of-sale-testing-example/extensions/point-of-sale-testing-example/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...sting/point-of-sale-testing-example/extensions/point-of-sale-testing-example/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "name": "point-of-sale-testing-example-ext", | ||
| "private": true, | ||
| "version": "1.0.0", | ||
| "license": "UNLICENSED", | ||
| "dependencies": { | ||
| "@shopify/ui-extensions": "file:../../../../packages/ui-extensions", | ||
| "preact": "^10.10.x" | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
...sting/point-of-sale-testing-example/extensions/point-of-sale-testing-example/shopify.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import '@shopify/ui-extensions'; | ||
|
|
||
| //@ts-ignore | ||
| declare module './src/Tile.jsx' { | ||
| const shopify: import('@shopify/ui-extensions/pos.home.tile.render').Api; | ||
| const globalThis: { shopify: typeof shopify }; | ||
| } | ||
|
|
||
| //@ts-ignore | ||
| declare module './src/Modal.jsx' { | ||
| const shopify: import('@shopify/ui-extensions/pos.home.modal.render').Api; | ||
| const globalThis: { shopify: typeof shopify }; | ||
| } |
21 changes: 21 additions & 0 deletions
21
...t-of-sale-testing-example/extensions/point-of-sale-testing-example/shopify.extension.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Learn more about configuring your POS UI extension: | ||
| # https://shopify.dev/docs/api/pos-ui-extensions | ||
|
|
||
| # The version of APIs your extension will receive. Learn more: | ||
| # https://shopify.dev/docs/api/usage/versioning | ||
| api_version = "2026-04" | ||
|
|
||
| [[extensions]] | ||
| uid = "80f617f3-cfaf-bcfc-f4bd-8bf4faabbc38dc0e7226" | ||
| type = "ui_extension" | ||
| name = "point-of-sale-testing-example" | ||
| handle = "point-of-sale-testing-example" | ||
| description = "A preact POS UI extension" | ||
|
|
||
| [[extensions.targeting]] | ||
| module = "./src/Tile.jsx" | ||
| target = "pos.home.tile.render" | ||
|
|
||
| [[extensions.targeting]] | ||
| module = "./src/Modal.jsx" | ||
| target = "pos.home.modal.render" |
27 changes: 27 additions & 0 deletions
27
...ting/point-of-sale-testing-example/extensions/point-of-sale-testing-example/src/Modal.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import {render} from 'preact'; | ||
|
|
||
| export default async () => { | ||
| render(<Extension />, document.body); | ||
| }; | ||
|
|
||
| function Extension() { | ||
| const cart = shopify.cart.current.value; | ||
| const lineItems = cart ? cart.lineItems : []; | ||
|
|
||
| return ( | ||
| <s-page heading="Cart details"> | ||
| <s-stack direction="block"> | ||
| {lineItems.length === 0 ? ( | ||
| <s-text>No items in cart</s-text> | ||
| ) : ( | ||
| lineItems.map((item) => ( | ||
| <s-stack key={item.uuid} direction="inline"> | ||
| <s-text>{item.title}</s-text> | ||
| <s-text>Qty: {item.quantity}</s-text> | ||
| </s-stack> | ||
| )) | ||
| )} | ||
| </s-stack> | ||
| </s-page> | ||
| ); | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
...sting/point-of-sale-testing-example/extensions/point-of-sale-testing-example/src/Tile.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import {render} from 'preact'; | ||
|
|
||
| export default async () => { | ||
| render(<Extension />, document.body); | ||
| }; | ||
|
|
||
| function Extension() { | ||
| const cart = shopify.cart.current.value; | ||
| const lineItems = cart ? cart.lineItems : []; | ||
| const itemCount = lineItems.length; | ||
|
|
||
| function handleClick() { | ||
| shopify.action.presentModal(); | ||
| } | ||
|
|
||
| function handleAddQuantity() { | ||
| shopify.cart.bulkCartUpdate({ | ||
| lineItems: lineItems.map((item) => ({ | ||
| ...item, | ||
| quantity: item.quantity + 1, | ||
| })), | ||
| cartDiscounts: cart ? cart.cartDiscounts : [], | ||
| properties: cart ? cart.properties : {}, | ||
| }); | ||
| } | ||
|
|
||
| return ( | ||
| <s-tile | ||
| heading="Cart summary" | ||
| subheading={`${itemCount} item(s)`} | ||
| onClick={handleClick} | ||
| > | ||
| <s-button onClick={handleAddQuantity}>Increment all quantities</s-button> | ||
| </s-tile> | ||
| ); | ||
| } |
41 changes: 41 additions & 0 deletions
41
...oint-of-sale-testing-example/extensions/point-of-sale-testing-example/tests/Modal.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /// <reference types="@shopify/ui-extensions/pos.home.modal.render" /> | ||
| // ^ This defines types for custom Shopify elements supported by the target. | ||
|
|
||
| import {expect, test, beforeEach, afterEach} from 'vitest'; | ||
| import {getExtension} from '@shopify/ui-extensions-tester'; | ||
| import {createCartLineItem} from '@shopify/ui-extensions-tester/point-of-sale'; | ||
|
|
||
| const extension = getExtension('pos.home.modal.render'); | ||
|
|
||
| beforeEach(() => { | ||
| extension.setUp(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| extension.tearDown(); | ||
| }); | ||
|
|
||
| test('shows "No items in cart" when the cart is empty', async () => { | ||
| await extension.render(); | ||
|
|
||
| const firstText = document.body.querySelector('s-text')!; | ||
| expect(firstText.textContent).toEqual('No items in cart'); | ||
| }); | ||
|
|
||
| test('shows each line item with its title and quantity', async () => { | ||
| extension.shopify.cart.current.value.lineItems = [ | ||
| createCartLineItem({title: 'Widget', quantity: 3}), | ||
| createCartLineItem({title: 'Gadget', quantity: 1}), | ||
| ]; | ||
|
|
||
| await extension.render(); | ||
|
|
||
| const textNodes = Array.from( | ||
| document.body.querySelectorAll('s-text'), | ||
| ); | ||
| const textContents = textNodes.map((t) => t.textContent); | ||
| expect(textContents).toContain('Widget'); | ||
| expect(textContents).toContain('Qty: 3'); | ||
| expect(textContents).toContain('Gadget'); | ||
| expect(textContents).toContain('Qty: 1'); | ||
| }); |
73 changes: 73 additions & 0 deletions
73
...point-of-sale-testing-example/extensions/point-of-sale-testing-example/tests/Tile.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /// <reference types="@shopify/ui-extensions/pos.home.tile.render" /> | ||
| // ^ This defines types for custom Shopify elements supported by the target. | ||
|
|
||
| import {expect, test, vi, beforeEach, afterEach} from 'vitest'; | ||
| import {fireEvent, waitFor} from '@testing-library/preact'; | ||
| import {getExtension} from '@shopify/ui-extensions-tester'; | ||
| import {createCartLineItem} from '@shopify/ui-extensions-tester/point-of-sale'; | ||
|
|
||
| const extension = getExtension('pos.home.tile.render'); | ||
|
|
||
| beforeEach(() => { | ||
| extension.setUp(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| extension.tearDown(); | ||
| }); | ||
|
|
||
| test('shows "0 item(s)" when the cart is empty', async () => { | ||
| await extension.render(); | ||
|
|
||
| const tile = document.body.querySelector('s-tile')!; | ||
| expect(tile.getAttribute('subheading')).toEqual('0 item(s)'); | ||
| }); | ||
|
|
||
| test('shows the line item count when the cart has items', async () => { | ||
| extension.shopify.cart.current.value.lineItems = [ | ||
| createCartLineItem(), | ||
| createCartLineItem(), | ||
| ]; | ||
|
|
||
| await extension.render(); | ||
|
|
||
| const tile = document.body.querySelector('s-tile')!; | ||
| expect(tile.getAttribute('subheading')).toEqual('2 item(s)'); | ||
| }); | ||
|
|
||
| test('calls action.presentModal when the tile is clicked', async () => { | ||
| const presentModal = vi.fn(); | ||
| extension.shopify.action.presentModal = presentModal; | ||
|
|
||
| await extension.render(); | ||
|
|
||
| const tile = document.body.querySelector('s-tile')!; | ||
| fireEvent.click(tile); | ||
|
|
||
| await waitFor(() => { | ||
| expect(presentModal).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| test('increments all line item quantities via bulkCartUpdate on button click', async () => { | ||
| const widget = createCartLineItem({uuid: 'widget', quantity: 2}); | ||
| const gadget = createCartLineItem({uuid: 'gadget', quantity: 5}); | ||
| extension.shopify.cart.current.value.lineItems = [widget, gadget]; | ||
| const bulkCartUpdate = vi.spyOn(extension.shopify.cart, 'bulkCartUpdate'); | ||
|
|
||
| await extension.render(); | ||
|
|
||
| const button = document.body.querySelector('s-button')!; | ||
| fireEvent.click(button); | ||
|
|
||
| await waitFor(() => { | ||
| expect(bulkCartUpdate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| lineItems: [ | ||
| expect.objectContaining({uuid: 'widget', quantity: 3}), | ||
| expect.objectContaining({uuid: 'gadget', quantity: 6}), | ||
| ], | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
21 changes: 21 additions & 0 deletions
21
...ting/point-of-sale-testing-example/extensions/point-of-sale-testing-example/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "jsx": "react-jsx", | ||
| "jsxImportSource": "preact", | ||
| "target": "ES2020", | ||
| "checkJs": true, | ||
| "allowJs": true, | ||
| "moduleResolution": "node", | ||
| "esModuleInterop": true, | ||
| "noEmit": true, | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| // Monorepo only: file: deps create symlinks that cause TS to | ||
| // resolve a different preact instance. Not needed with published packages. | ||
| "paths": { | ||
| "preact": ["../../node_modules/preact"], | ||
| "preact/*": ["../../node_modules/preact/*"] | ||
| } | ||
| }, | ||
| "include": ["./src", "./tests", "./shopify.d.ts"] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.