Skip to content

test(core): add unit tests for customer-group promotion condition#4883

Open
GabrielRoc wants to merge 1 commit into
vendurehq:masterfrom
GabrielRoc:test/customer-group-condition
Open

test(core): add unit tests for customer-group promotion condition#4883
GabrielRoc wants to merge 1 commit into
vendurehq:masterfrom
GabrielRoc:test/customer-group-condition

Conversation

@GabrielRoc

@GabrielRoc GabrielRoc commented Jun 27, 2026

Copy link
Copy Markdown

Description

Adds a direct unit test suite for the customer_group promotion condition (customer-group-condition.ts), which previously had 0% function coverage.

The condition checks whether order.customer belongs to a given CustomerGroup by calling CustomerService.getCustomerGroups() through a CacheService-backed cache and then comparing IDs with idsAreEqual. Three NestJS services (CustomerService, CacheService, EventBus) are injected into the condition's module-level variables via init() — the test supplies lightweight mocks, with the cache configured as a pass-through so group lookups always hit the mock service directly.

6 tests covering (equivalence partitioning):

  • Order with no customer → false
  • Customer with no groups → false
  • Customer in other groups but not the target → false
  • Customer in exactly the target group → true
  • Customer in multiple groups including the target → true
  • Correct forwarding of customerId and ctx to getCustomerGroups()

All tests are database-free.

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 `customer_group` check() via equivalence
partitioning over customer presence and group membership. Dependencies
(CustomerService, CacheService, EventBus) are mocked via init() so no
database is needed. 6 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 test suite for the customer_group promotion condition. It sets up mocked CustomerService, CacheService, and EventBus instances, defines helpers for config args, customer groups, orders, and the check wrapper, initializes the condition, and verifies behavior for orders without customers and with various customer group memberships. It also checks that getCustomerGroups() receives the expected customer id.

Possibly related issues

  • vendurehq/vendure issue 4835 — adds isolated unit tests for the customer_group promotion condition, matching the issue’s test coverage goal.
🚥 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: unit tests for the customer-group promotion condition.
Description check ✅ Passed The description follows the template with a summary, breaking changes, checklist, and linked issue, and screenshots are optional.
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.

@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/customer-group-condition.spec.ts`:
- Line 4: Reset the shared `mockGetCustomerGroups` between examples in
`customer-group-condition.spec.ts` so stubbed values and call history do not
leak across cases, and tighten the interaction assertion in the `it` block
around `getCustomerGroups(ctx, 'c42')` to verify exactly one matching call
rather than any matching call. Update the suite setup/teardown around the
`describe`/`beforeAll`-driven test flow to isolate each example and make the
expectation in `mockGetCustomerGroups` precise.
🪄 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: e456b42c-e9a5-43ef-b0c7-25a9c11c45f1

📥 Commits

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

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

import { ConfigArg } from '@vendure/common/lib/generated-types';
import { ID } from '@vendure/common/lib/shared-types';
import { of } from 'rxjs';
import { beforeAll, describe, expect, it, vi } from 'vitest';

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 | 🟡 Minor | ⚡ Quick win

Reset the shared mock between examples and make the interaction test exact.

mockGetCustomerGroups keeps both its stubbed return value and call history for the whole suite, so later cases can inherit state from earlier ones. That also means Line 133 only proves there was at least one matching call, not that this example made exactly one getCustomerGroups(ctx, 'c42') lookup.

Suggested fix
-import { beforeAll, describe, expect, it, vi } from 'vitest';
+import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
@@
 describe('customerGroup', () => {
     beforeAll(async () => {
         await customerGroup.init(mockInjector as any);
     });
+
+    beforeEach(() => {
+        mockGetCustomerGroups.mockReset();
+    });
@@
         it('passes the customer id to getCustomerGroups', async () => {
             const order = orderWithCustomer('c42');
             const args = buildArgs('g1');
             mockGetCustomerGroups.mockResolvedValue(groups('g1'));
 
             await check(order, args);
 
+            expect(mockGetCustomerGroups).toHaveBeenCalledTimes(1);
             expect(mockGetCustomerGroups).toHaveBeenCalledWith(ctx, 'c42');
         });

Also applies to: 79-134

🤖 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/customer-group-condition.spec.ts`
at line 4, Reset the shared `mockGetCustomerGroups` between examples in
`customer-group-condition.spec.ts` so stubbed values and call history do not
leak across cases, and tighten the interaction assertion in the `it` block
around `getCustomerGroups(ctx, 'c42')` to verify exactly one matching call
rather than any matching call. Update the suite setup/teardown around the
`describe`/`beforeAll`-driven test flow to isolate each example and make the
expectation in `mockGetCustomerGroups` precise.

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 new Vitest unit test suite for the customer_group promotion condition in @vendure/core, aiming to directly cover previously-uncovered business logic without requiring any DB/TestServer setup.

Changes:

  • Introduces customer-group-condition.spec.ts with 6 unit tests covering customer presence, group membership outcomes, and forwarding of (ctx, customerId) into CustomerService.getCustomerGroups().
  • Uses lightweight mocks for CustomerService, CacheService, and EventBus to keep the suite deterministic and database-free.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +53 to +59
const mockInjector = {
get: (token: unknown) => {
if (token === CustomerService) return mockCustomerService;
if (token === CacheService) return mockCacheService;
if (token === EventBus) return mockEventBus;
},
};
Comment on lines +75 to +77
async function check(order: Order, args: ConfigArg[]) {
return customerGroup.check(ctx, order, args, undefined as any);
}
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