test(core): add unit tests for has-facet-values promotion condition#4882
test(core): add unit tests for has-facet-values promotion condition#4882GabrielRoc wants to merge 3 commits into
Conversation
Adds a spec file covering the `at_least_n_with_facets` check() via equivalence partitioning and boundary-value analysis. Uses a lightweight mock injected via init() to avoid database access. 7 tests. Relates to vendurehq#4835
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
All contributors have signed the CLA ✍️ ✅ |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughAdds a Vitest spec for 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Adds a focused Vitest unit test suite for the at_least_n_with_facets (hasFacetValues) promotion condition in @vendure/core, improving direct function coverage of this business-logic condition without requiring DB/TestServer setup.
Changes:
- Introduces a new spec file covering threshold/boundary behavior for
minimumvs summed matching quantities. - Adds multi-line aggregation and non-matching exclusion scenarios.
- Verifies
FacetValueChecker.hasFacetValues()is invoked with the expected(line, facets, ctx)arguments via an injected mock.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const ctx = createRequestContext({ pricesIncludeTax: false }); | ||
|
|
||
| async function check(order: Order, args: ConfigArg[]) { | ||
| return hasFacetValues.check(ctx, order, args, undefined as any); | ||
| } |
There was a problem hiding this comment.
Dismissed. hasFacetValues.check() does not use the promotion argument. Passing undefined as any is the intentional, consistent pattern across all promotion-condition specs in this PR series.
| it('passes the ctx and facet ID list to hasFacetValues', async () => { | ||
| const order = orderWith([line('v1', 1)]); | ||
| const args = buildArgs(1, ['f1', 'f2']); | ||
| mockHasFacetValues.mockResolvedValue(true); | ||
|
|
||
| await check(order, args); | ||
|
|
||
| expect(mockHasFacetValues).toHaveBeenCalledWith(order.lines[0], ['f1', 'f2'], ctx); | ||
| }); |
There was a problem hiding this comment.
Valid point. Fixed in commit 8da9b635c: added mockHasFacetValues.mockClear() at the start of the test to reset the call history, and added expect(mockHasFacetValues).toHaveBeenCalledTimes(1) to pin the assertion to exactly one invocation from this test. The toHaveBeenCalledWith assertion is now unambiguous even if more tests are added later.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts`:
- Around line 25-28: The mock in has-facet-values-condition.spec.ts is using the
old two-parameter vi.fn generic form, which is incompatible with Vitest 3.x.
Update the mock declaration for mockHasFacetValues to use the current single
function-type generic signature based on FacetValueChecker['hasFacetValues'], so
the test type-checks correctly with the newer vi.fn API.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: b11cadd5-a9db-40ab-b6ce-43959280bbec
📒 Files selected for processing (1)
packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts
|
I have read the CLA Document and I hereby sign the CLA |
Description
Adds a direct unit test suite for the
at_least_n_with_facetspromotion condition (has-facet-values-condition.ts), which previously had 0% function coverage.The condition delegates per-line facet matching to
FacetValueCheckerand sums the qualifying quantities againstminimum. BecauseFacetValueCheckeris a NestJS-injected service, the test injects avi.fn()mock via the condition'sinit()hook — no database or TestServer is required.7 tests covering:
minimum(below / at / above)ctxand the facet ID list are forwarded as-is tohasFacetValues()Breaking changes
None.
Checklist
Relates to #4835
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.