Skip to content

test(core): add unit tests for has-facet-values promotion condition#4882

Open
GabrielRoc wants to merge 1 commit into
vendurehq:masterfrom
GabrielRoc:test/has-facet-values-condition
Open

test(core): add unit tests for has-facet-values promotion condition#4882
GabrielRoc wants to merge 1 commit into
vendurehq:masterfrom
GabrielRoc:test/has-facet-values-condition

Conversation

@GabrielRoc

@GabrielRoc GabrielRoc commented Jun 27, 2026

Copy link
Copy Markdown

Description

Adds a direct unit test suite for the at_least_n_with_facets promotion condition (has-facet-values-condition.ts), which previously had 0% function coverage.

The condition delegates per-line facet matching to FacetValueChecker and sums the qualifying quantities against minimum. Because FacetValueChecker is a NestJS-injected service, the test injects a vi.fn() mock via the condition's init() hook — no database or TestServer is required.

7 tests covering:

  • Boundary-value analysis on total matching quantity vs minimum (below / at / above)
  • Correct aggregation across multiple lines
  • Correct exclusion of non-matching lines
  • Verification that ctx and the facet ID list are forwarded as-is to hasFacetValues()

Breaking changes

None.

Checklist

  • I have set a clear title
  • My PR is small and contains a single feature
  • I have added or updated test cases

Relates to #4835


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

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
Copilot AI review requested due to automatic review settings June 27, 2026 04:51
@vercel

vercel Bot commented Jun 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
vendure-storybook Ready Ready Preview, Comment Jun 27, 2026 4:53am

Request Review

@vendure-ci-automation-bot

Copy link
Copy Markdown
Contributor


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@coderabbitai

coderabbitai Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a Vitest spec for hasFacetValues that initializes the condition with a mocked FacetValueChecker.hasFacetValues, builds test order data with a request context, and verifies minimum-threshold behavior, accumulation across matching lines, exclusion of non-matching lines, and the arguments passed to the checker.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding unit tests for the has-facet-values promotion condition.
Description check ✅ Passed The description covers the summary, related issue, breaking changes, and checklist, with only the optional screenshots section omitted.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 minimum vs 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.

Comment on lines +58 to +62
const ctx = createRequestContext({ pricesIncludeTax: false });

async function check(order: Order, args: ConfigArg[]) {
return hasFacetValues.check(ctx, order, args, undefined as any);
}
Comment on lines +126 to +134
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);
});

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between c5a2785 and ba13e06.

📒 Files selected for processing (1)
  • packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts

Comment on lines +25 to +28
const mockHasFacetValues = vi.fn<
Parameters<FacetValueChecker['hasFacetValues']>,
ReturnType<FacetValueChecker['hasFacetValues']>
>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate Vitest version and the spec file
git ls-files 'packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts' 'package.json' 'pnpm-lock.yaml' 'package-lock.json' 'yarn.lock' '**/package.json' | sed -n '1,120p'

echo '--- package.json candidates with vitest ---'
rg -n '"vitest"|vi\.fn<|FacetValueChecker\[' -S . --glob '!**/node_modules/**' --glob '!**/dist/**' --glob '!**/build/**'

echo '--- file excerpt ---'
sed -n '1,120p' packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts

Repository: vendurehq/vendure

Length of output: 10308


🌐 Web query:

Vitest 3.2.4 vi.fn generic signature Parameters ReturnType two generics single function type docs

💡 Result:

In Vitest, including version 3.2.4, the vi.fn function accepts a single optional generic type parameter that represents the function signature [1][2]. This generic is used to strictly type the mock's arguments and return value [2][3]. To specify the signature, you pass a function type to the generic slot [2]. For example, if you want a mock that accepts a string and a boolean and returns a number, you would define it as follows [2]: const myMock = vi.fn<(arg1: string, arg2: boolean) => number>; When you provide this signature: 1. TypeScript uses the Parameters utility type to infer the argument types for the mock's implementation and call tracking methods [4][3]. 2. TypeScript uses the ReturnType utility type to enforce the return type of the mock's implementation and return-value methods (such as.mockReturnValue) [5][4][3]. If no generic type is provided, Vitest defaults to a signature of (...args: any[]) => any [2]. It is also possible to pass an implementation directly to vi.fn, in which case Vitest will attempt to infer the types automatically [1][6]. If the inferred type is insufficient or you are creating a mock without an initial implementation, explicitly providing the generic signature as shown above is the recommended approach [2].

Citations:


Use the current vi.fn generic signature

vi.fn in Vitest 3.x takes a single function-type generic, so this two-parameter form should be rewritten to vi.fn<FacetValueChecker['hasFacetValues']>() to avoid a type error.

Suggested fix
-const mockHasFacetValues = vi.fn<
-    Parameters<FacetValueChecker['hasFacetValues']>,
-    ReturnType<FacetValueChecker['hasFacetValues']>
->();
+const mockHasFacetValues = vi.fn<FacetValueChecker['hasFacetValues']>();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const mockHasFacetValues = vi.fn<
Parameters<FacetValueChecker['hasFacetValues']>,
ReturnType<FacetValueChecker['hasFacetValues']>
>();
const mockHasFacetValues = vi.fn<FacetValueChecker['hasFacetValues']>();
🤖 Prompt for 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.

In
`@packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts`
around lines 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants