| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- import {
- ConfigArgType,
- CreatePromotion,
- DeletionResult,
- GetAdjustmentOperations,
- GetPromotion,
- GetPromotionList,
- Promotion,
- UpdatePromotion,
- } from '@vendure/common/lib/generated-types';
- import { pick } from '@vendure/common/lib/pick';
- import gql from 'graphql-tag';
- import path from 'path';
- import {
- CREATE_PROMOTION,
- GET_ADJUSTMENT_OPERATIONS,
- GET_PROMOTION,
- GET_PROMOTION_LIST,
- UPDATE_PROMOTION,
- } from '../../../admin-ui/src/app/data/definitions/promotion-definitions';
- import { PromotionAction, PromotionOrderAction } from '../src/config/promotion/promotion-action';
- import { PromotionCondition } from '../src/config/promotion/promotion-condition';
- import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
- import { TestAdminClient } from './test-client';
- import { TestServer } from './test-server';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- // tslint:disable:no-non-null-assertion
- describe('Promotion resolver', () => {
- const client = new TestAdminClient();
- const server = new TestServer();
- const promoCondition = generateTestCondition('promo_condition');
- const promoCondition2 = generateTestCondition('promo_condition2');
- const promoAction = generateTestAction('promo_action');
- const snapshotProps = ['name', 'actions', 'conditions', 'enabled'] as Array<
- 'name' | 'actions' | 'conditions' | 'enabled'
- >;
- let promotion: Promotion.Fragment;
- beforeAll(async () => {
- await server.init(
- {
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
- customerCount: 1,
- },
- {
- promotionOptions: {
- promotionConditions: [promoCondition, promoCondition2],
- promotionActions: [promoAction],
- },
- },
- );
- await client.init();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('createPromotion', async () => {
- const result = await client.query<CreatePromotion.Mutation, CreatePromotion.Variables>(
- CREATE_PROMOTION,
- {
- input: {
- name: 'test promotion',
- enabled: true,
- conditions: [
- {
- code: promoCondition.code,
- arguments: [{ name: 'arg', value: '500', type: ConfigArgType.MONEY }],
- },
- ],
- actions: [
- {
- code: promoAction.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: '["T_1"]',
- type: ConfigArgType.FACET_VALUE_IDS,
- },
- ],
- },
- ],
- },
- },
- );
- promotion = result.createPromotion;
- expect(pick(promotion, snapshotProps)).toMatchSnapshot();
- });
- it('updatePromotion', async () => {
- const result = await client.query<UpdatePromotion.Mutation, UpdatePromotion.Variables>(
- UPDATE_PROMOTION,
- {
- input: {
- id: promotion.id,
- conditions: [
- {
- code: promoCondition.code,
- arguments: [{ name: 'arg', value: '90', type: ConfigArgType.MONEY }],
- },
- {
- code: promoCondition2.code,
- arguments: [{ name: 'arg', value: '10', type: ConfigArgType.MONEY }],
- },
- ],
- },
- },
- );
- expect(pick(result.updatePromotion, snapshotProps)).toMatchSnapshot();
- });
- it('promotion', async () => {
- const result = await client.query<GetPromotion.Query, GetPromotion.Variables>(GET_PROMOTION, {
- id: promotion.id,
- });
- expect(result.promotion!.name).toBe(promotion.name);
- });
- it('promotions', async () => {
- const result = await client.query<GetPromotionList.Query, GetPromotionList.Variables>(
- GET_PROMOTION_LIST,
- {},
- );
- expect(result.promotions.totalItems).toBe(1);
- expect(result.promotions.items[0].name).toBe('test promotion');
- });
- it('adjustmentOperations', async () => {
- const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
- GET_ADJUSTMENT_OPERATIONS,
- );
- expect(result.adjustmentOperations).toMatchSnapshot();
- });
- describe('deletion', () => {
- let allPromotions: GetPromotionList.Items[];
- let promotionToDelete: GetPromotionList.Items;
- beforeAll(async () => {
- const result = await client.query<GetPromotionList.Query>(GET_PROMOTION_LIST);
- allPromotions = result.promotions.items;
- });
- it('deletes a promotion', async () => {
- promotionToDelete = allPromotions[0];
- const result = await client.query(DELETE_PROMOTION, { id: promotionToDelete.id });
- expect(result.deletePromotion).toEqual({ result: DeletionResult.DELETED });
- });
- it('cannot get a deleted promotion', async () => {
- const result = await client.query<GetPromotion.Query, GetPromotion.Variables>(GET_PROMOTION, {
- id: promotionToDelete.id,
- });
- expect(result.promotion).toBe(null);
- });
- it('deleted promotion omitted from list', async () => {
- const result = await client.query<GetPromotionList.Query>(GET_PROMOTION_LIST);
- 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(
- () =>
- client.query<UpdatePromotion.Mutation, UpdatePromotion.Variables>(UPDATE_PROMOTION, {
- input: {
- id: promotionToDelete.id,
- enabled: false,
- },
- }),
- `No Promotion with the id '1' could be found`,
- ),
- );
- });
- });
- function generateTestCondition(code: string): PromotionCondition<any> {
- return new PromotionCondition({
- code,
- description: `description for ${code}`,
- args: { arg: ConfigArgType.MONEY },
- check: (order, args) => true,
- });
- }
- function generateTestAction(code: string): PromotionAction<any> {
- return new PromotionOrderAction({
- code,
- description: `description for ${code}`,
- args: { facetValueIds: ConfigArgType.FACET_VALUE_IDS },
- execute: (order, args) => {
- return 42;
- },
- });
- }
- const DELETE_PROMOTION = gql`
- mutation DeletePromotion($id: ID!) {
- deletePromotion(id: $id) {
- result
- }
- }
- `;
|