| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- import { CurrencyCode, DeletionResult, ErrorCode, LanguageCode } from '@vendure/common/lib/generated-types';
- import { pick } from '@vendure/common/lib/pick';
- import { type PromotionAction, PromotionCondition, PromotionOrderAction } from '@vendure/core';
- import {
- createErrorResultGuard,
- createTestEnvironment,
- E2E_DEFAULT_CHANNEL_TOKEN,
- type ErrorResultGuard,
- } from '@vendure/testing';
- import path from 'node:path';
- import { afterAll, beforeAll, describe, expect, it } from 'vitest';
- import type { channelFragment, promotionFragment } from './graphql/fragments-admin';
- import type { FragmentOf, ResultOf } from './graphql/graphql-admin';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import {
- assignPromotionsToChannelDocument,
- createChannelDocument,
- createPromotionDocument,
- deletePromotionDocument,
- getAdjustmentOperationsDocument,
- getPromotionDocument,
- getPromotionListDocument,
- removePromotionsFromChannelDocument,
- updatePromotionDocument,
- } from './graphql/shared-definitions';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- type PromotionFragment = FragmentOf<typeof promotionFragment>;
- type PromotionListItem = ResultOf<typeof getPromotionListDocument>['promotions']['items'][number];
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
- describe('Promotion resolver', () => {
- const promoCondition = generateTestCondition('promo_condition');
- const promoCondition2 = generateTestCondition('promo_condition2');
- const promoAction = generateTestAction('promo_action');
- const { server, adminClient } = createTestEnvironment({
- ...testConfig(),
- promotionOptions: {
- promotionConditions: [promoCondition, promoCondition2],
- promotionActions: [promoAction],
- },
- });
- const snapshotProps: Array<keyof PromotionFragment> = [
- 'name',
- 'actions',
- 'conditions',
- 'enabled',
- 'couponCode',
- 'startsAt',
- 'endsAt',
- ];
- let promotion: PromotionFragment;
- const promotionGuard: ErrorResultGuard<PromotionFragment> = createErrorResultGuard(
- input => !!input.couponCode,
- );
- const promotionQueryGuard: ErrorResultGuard<
- NonNullable<ResultOf<typeof getPromotionDocument>['promotion']>
- > = createErrorResultGuard(input => !!input.id);
- const channelGuard: ErrorResultGuard<FragmentOf<typeof channelFragment>> = createErrorResultGuard(
- input => !!input.token,
- );
- beforeAll(async () => {
- await server.init({
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
- customerCount: 1,
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('createPromotion', async () => {
- const { createPromotion } = await adminClient.query(createPromotionDocument, {
- input: {
- enabled: true,
- couponCode: 'TEST123',
- startsAt: new Date('2019-10-30T00:00:00.000Z').toISOString(),
- endsAt: new Date('2019-12-01T00:00:00.000Z').toISOString(),
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'test promotion',
- description: 'a test promotion',
- },
- ],
- conditions: [
- {
- code: promoCondition.code,
- arguments: [{ name: 'arg', value: '500' }],
- },
- ],
- actions: [
- {
- code: promoAction.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: '["T_1"]',
- },
- ],
- },
- ],
- },
- });
- promotionGuard.assertSuccess(createPromotion);
- promotion = createPromotion;
- expect(pick(promotion, snapshotProps)).toMatchSnapshot();
- });
- it('createPromotion with no description', async () => {
- const { createPromotion } = await adminClient.query(createPromotionDocument, {
- input: {
- enabled: true,
- couponCode: 'TEST567',
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'test promotion no description',
- customFields: {},
- },
- ],
- conditions: [],
- actions: [
- {
- code: promoAction.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: '["T_1"]',
- },
- ],
- },
- ],
- },
- });
- promotionGuard.assertSuccess(createPromotion);
- expect(createPromotion.name).toBe('test promotion no description');
- expect(createPromotion.description).toBe('');
- expect(createPromotion.translations[0].description).toBe('');
- });
- it('createPromotion return error result with empty conditions and no couponCode', async () => {
- const { createPromotion } = await adminClient.query(createPromotionDocument, {
- input: {
- enabled: true,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'bad promotion',
- },
- ],
- conditions: [],
- actions: [
- {
- code: promoAction.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: '["T_1"]',
- },
- ],
- },
- ],
- },
- });
- promotionGuard.assertErrorResult(createPromotion);
- expect(createPromotion.message).toBe(
- 'A Promotion must have either at least one condition or a coupon code set',
- );
- expect(createPromotion.errorCode).toBe(ErrorCode.MISSING_CONDITIONS_ERROR);
- });
- it('updatePromotion', async () => {
- const { updatePromotion } = await adminClient.query(updatePromotionDocument, {
- input: {
- id: promotion.id,
- couponCode: 'TEST1235',
- startsAt: new Date('2019-05-30T22:00:00.000Z').toISOString(),
- endsAt: new Date('2019-06-01T22:00:00.000Z').toISOString(),
- conditions: [
- {
- code: promoCondition.code,
- arguments: [{ name: 'arg', value: '90' }],
- },
- {
- code: promoCondition2.code,
- arguments: [{ name: 'arg', value: '10' }],
- },
- ],
- },
- });
- promotionGuard.assertSuccess(updatePromotion);
- expect(pick(updatePromotion, snapshotProps)).toMatchSnapshot();
- });
- it('updatePromotion return error result with empty conditions and no couponCode', async () => {
- const { updatePromotion } = await adminClient.query(updatePromotionDocument, {
- input: {
- id: promotion.id,
- couponCode: '',
- conditions: [],
- },
- });
- promotionGuard.assertErrorResult(updatePromotion);
- expect(updatePromotion.message).toBe(
- 'A Promotion must have either at least one condition or a coupon code set',
- );
- expect(updatePromotion.errorCode).toBe(ErrorCode.MISSING_CONDITIONS_ERROR);
- });
- it('promotion', async () => {
- const result = await adminClient.query(getPromotionDocument, {
- id: promotion.id,
- });
- promotionQueryGuard.assertSuccess(result.promotion);
- expect(result.promotion.name).toBe(promotion.name);
- });
- it('promotions', async () => {
- const result = await adminClient.query(getPromotionListDocument, {});
- expect(result.promotions.totalItems).toBe(2);
- expect(result.promotions.items[0].name).toBe('test promotion');
- });
- it('adjustmentOperations', async () => {
- const result = await adminClient.query(getAdjustmentOperationsDocument);
- expect(result.promotionActions).toMatchSnapshot();
- expect(result.promotionConditions).toMatchSnapshot();
- });
- describe('channels', () => {
- const SECOND_CHANNEL_TOKEN = 'SECOND_CHANNEL_TOKEN';
- let secondChannel: FragmentOf<typeof channelFragment>;
- beforeAll(async () => {
- const { createChannel } = await adminClient.query(createChannelDocument, {
- input: {
- code: 'second-channel',
- token: SECOND_CHANNEL_TOKEN,
- defaultLanguageCode: LanguageCode.en,
- pricesIncludeTax: true,
- currencyCode: CurrencyCode.EUR,
- defaultTaxZoneId: 'T_1',
- defaultShippingZoneId: 'T_2',
- },
- });
- channelGuard.assertSuccess(createChannel);
- secondChannel = createChannel;
- });
- it('does not list Promotions not in active channel', async () => {
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { promotions } = await adminClient.query(getPromotionListDocument);
- expect(promotions.totalItems).toBe(0);
- expect(promotions.items).toEqual([]);
- });
- it('does not return Promotion not in active channel', async () => {
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { promotion: result } = await adminClient.query(getPromotionDocument, {
- id: promotion.id,
- });
- expect(result).toBeNull();
- });
- it('assignPromotionsToChannel', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { assignPromotionsToChannel } = await adminClient.query(assignPromotionsToChannelDocument, {
- input: {
- channelId: secondChannel.id,
- promotionIds: [promotion.id],
- },
- });
- expect(assignPromotionsToChannel).toEqual([{ id: promotion.id, name: promotion.name }]);
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { promotion: result } = await adminClient.query(getPromotionDocument, {
- id: promotion.id,
- });
- expect(result?.id).toBe(promotion.id);
- const { promotions } = await adminClient.query(getPromotionListDocument);
- expect(promotions.totalItems).toBe(1);
- expect(promotions.items.map(pick(['id']))).toEqual([{ id: promotion.id }]);
- });
- it('removePromotionsFromChannel', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { removePromotionsFromChannel } = await adminClient.query(
- removePromotionsFromChannelDocument,
- {
- input: {
- channelId: secondChannel.id,
- promotionIds: [promotion.id],
- },
- },
- );
- expect(removePromotionsFromChannel).toEqual([{ id: promotion.id, name: promotion.name }]);
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { promotion: result } = await adminClient.query(getPromotionDocument, {
- id: promotion.id,
- });
- expect(result).toBeNull();
- const { promotions } = await adminClient.query(getPromotionListDocument);
- expect(promotions.totalItems).toBe(0);
- });
- });
- describe('deletion', () => {
- let allPromotions: PromotionListItem[];
- let promotionToDelete: PromotionListItem;
- beforeAll(async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const result = await adminClient.query(getPromotionListDocument);
- allPromotions = result.promotions.items;
- });
- it('deletes a promotion', async () => {
- promotionToDelete = allPromotions[0];
- const result = await adminClient.query(deletePromotionDocument, {
- id: promotionToDelete.id,
- });
- expect(result.deletePromotion).toEqual({
- result: DeletionResult.DELETED,
- });
- });
- it('cannot get a deleted promotion', async () => {
- const result = await adminClient.query(getPromotionDocument, {
- id: promotionToDelete.id,
- });
- expect(result.promotion).toBe(null);
- });
- it('deleted promotion omitted from list', async () => {
- const result = await adminClient.query(getPromotionListDocument);
- expect(result.promotions.items.length).toBe(allPromotions.length - 1);
- expect(result.promotions.items.map(c => c.id).includes(promotionToDelete.id)).toBe(false);
- });
- it(
- 'updatePromotion throws for deleted promotion',
- assertThrowsWithMessage(
- () =>
- adminClient.query(updatePromotionDocument, {
- input: {
- id: promotionToDelete.id,
- enabled: false,
- },
- }),
- 'No Promotion with the id "1" could be found',
- ),
- );
- });
- });
- function generateTestCondition(code: string): PromotionCondition {
- return new PromotionCondition({
- code,
- description: [{ languageCode: LanguageCode.en, value: `description for ${code}` }],
- args: { arg: { type: 'int' } },
- check: () => true,
- });
- }
- function generateTestAction(code: string): PromotionAction<any> {
- return new PromotionOrderAction({
- code,
- description: [{ languageCode: LanguageCode.en, value: `description for ${code}` }],
- args: { facetValueIds: { type: 'ID', list: true } },
- execute: () => {
- return 42;
- },
- });
- }
|