test(core): add unit tests for customer-group promotion condition#4883
test(core): add unit tests for customer-group promotion condition#4883GabrielRoc wants to merge 1 commit into
Conversation
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
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
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. |
📝 WalkthroughWalkthroughAdds a Vitest test suite for the Possibly related issues
🚥 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.
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
📒 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'; |
There was a problem hiding this comment.
🎯 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.
There was a problem hiding this comment.
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.tswith 6 unit tests covering customer presence, group membership outcomes, and forwarding of(ctx, customerId)intoCustomerService.getCustomerGroups(). - Uses lightweight mocks for
CustomerService,CacheService, andEventBusto keep the suite deterministic and database-free.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const mockInjector = { | ||
| get: (token: unknown) => { | ||
| if (token === CustomerService) return mockCustomerService; | ||
| if (token === CacheService) return mockCacheService; | ||
| if (token === EventBus) return mockEventBus; | ||
| }, | ||
| }; |
| async function check(order: Order, args: ConfigArg[]) { | ||
| return customerGroup.check(ctx, order, args, undefined as any); | ||
| } |
Description
Adds a direct unit test suite for the
customer_grouppromotion condition (customer-group-condition.ts), which previously had 0% function coverage.The condition checks whether
order.customerbelongs to a givenCustomerGroupby callingCustomerService.getCustomerGroups()through aCacheService-backed cache and then comparing IDs withidsAreEqual. Three NestJS services (CustomerService,CacheService,EventBus) are injected into the condition's module-level variables viainit()— 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):
falsefalsefalsetruetruecustomerIdandctxtogetCustomerGroups()All tests are database-free.
Breaking changes
None.
Checklist
Relates to #4835
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.