Skip to content

Mock rules JSON schema in tests and cancel state update on unmount - #1188

Draft
gciotola wants to merge 1 commit into
mainfrom
fix/1187-rule-engine-test
Draft

Mock rules JSON schema in tests and cancel state update on unmount#1188
gciotola wants to merge 1 commit into
mainfrom
fix/1187-rule-engine-test

Conversation

@gciotola

@gciotola gciotola commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Closes #1187

Problem

fetchJsonSchema was performing a real network request to
https://core.commercelayer.io/api/public/schemas/order_rules: no MSW handler
existed, and that URL was explicitly excluded from the onUnhandledRequest
warning in src/mocks/setup.ts. Depending on network latency the promise could
resolve after the test file had finished, running setOptionsConfig against
an already torn-down jsdom environment.

Changes

  • src/mocks/data/core_schemas.ts (new): MSW handlers serving order_rules
    and price_rules from the local JSON files already present in
    RuleEngine/json_schema/, registered in src/mocks/handlers.ts. No more
    network calls in tests.
  • src/mocks/setup.ts: removed the onUnhandledRequest bypass for that URL,
    so unmocked requests are reported again.
  • RuleEngineComponent.tsx: the schema-parsing effect now returns a cleanup
    that sets a cancelled flag, preventing the state update after unmount. This
    is a general fix, relevant in production too on fast navigations.

Extra

If we want to keep testing against real API data, we can add the following test

it.concurrent("local order_rules schema matches remote", async () => {
  const remote = await fetch("https://core.commercelayer.io/api/public/schemas/order_rules").then(r => r.json())
  expect(remote).toEqual(orderRulesJsonSchema)
})

@netlify

netlify Bot commented Aug 3, 2026

Copy link
Copy Markdown

Deploy Preview for commercelayer-app-elements ready!

Name Link
🔨 Latest commit 3587dbf
🔍 Latest deploy log https://app.netlify.com/projects/commercelayer-app-elements/deploys/6a705c77c0139500085eaab7
😎 Deploy Preview https://deploy-preview-1188--commercelayer-app-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@gciotola
gciotola requested a review from pfferrari August 3, 2026 09:18
@gciotola gciotola self-assigned this Aug 3, 2026
@gciotola gciotola added the bug Something isn't working label Aug 3, 2026
@gciotola gciotola changed the title fix: mock rules JSON schema in tests and cancel state update on unmount Mock rules JSON schema in tests and cancel state update on unmount Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky RuleEngine test on CI: ReferenceError: window is not defined

1 participant