| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- /* tslint:disable:no-non-null-assertion */
- import {
- defaultShippingCalculator,
- defaultShippingEligibilityChecker,
- ShippingCalculator,
- } from '@vendure/core';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import {
- CreateShippingMethod,
- DeleteShippingMethod,
- DeletionResult,
- GetCalculators,
- GetEligibilityCheckers,
- GetShippingMethod,
- GetShippingMethodList,
- LanguageCode,
- TestEligibleMethods,
- TestShippingMethod,
- UpdateShippingMethod,
- } from './graphql/generated-e2e-admin-types';
- const TEST_METADATA = {
- foo: 'bar',
- baz: [1, 2, 3],
- };
- const calculatorWithMetadata = new ShippingCalculator({
- code: 'calculator-with-metadata',
- description: [{ languageCode: LanguageCode.en, value: 'Has metadata' }],
- args: {},
- calculate: order => {
- return {
- price: 100,
- priceWithTax: 100,
- metadata: TEST_METADATA,
- };
- },
- });
- describe('ShippingMethod resolver', () => {
- const { server, adminClient, shopClient } = createTestEnvironment({
- ...testConfig,
- shippingOptions: {
- shippingEligibilityCheckers: [defaultShippingEligibilityChecker],
- shippingCalculators: [defaultShippingCalculator, calculatorWithMetadata],
- },
- });
- beforeAll(async () => {
- await server.init({
- dataDir: path.join(__dirname, '__data__'),
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- customerCount: 1,
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('shippingEligibilityCheckers', async () => {
- const { shippingEligibilityCheckers } = await adminClient.query<GetEligibilityCheckers.Query>(
- GET_ELIGIBILITY_CHECKERS,
- );
- expect(shippingEligibilityCheckers).toEqual([
- {
- args: [
- {
- config: {
- inputType: 'money',
- },
- description: 'Order is eligible only if its total is greater or equal to this value',
- label: 'Minimum order value',
- name: 'orderMinimum',
- type: 'int',
- },
- ],
- code: 'default-shipping-eligibility-checker',
- description: 'Default Shipping Eligibility Checker',
- },
- ]);
- });
- it('shippingCalculators', async () => {
- const { shippingCalculators } = await adminClient.query<GetCalculators.Query>(GET_CALCULATORS);
- expect(shippingCalculators).toEqual([
- {
- args: [
- {
- config: {
- inputType: 'money',
- },
- description: null,
- label: 'Shipping price',
- name: 'rate',
- type: 'int',
- },
- {
- config: {
- inputType: 'percentage',
- },
- description: null,
- label: 'Tax rate',
- name: 'taxRate',
- type: 'int',
- },
- ],
- code: 'default-shipping-calculator',
- description: 'Default Flat-Rate Shipping Calculator',
- },
- {
- args: [],
- code: 'calculator-with-metadata',
- description: 'Has metadata',
- },
- ]);
- });
- it('shippingMethods', async () => {
- const { shippingMethods } = await adminClient.query<GetShippingMethodList.Query>(
- GET_SHIPPING_METHOD_LIST,
- );
- expect(shippingMethods.totalItems).toEqual(2);
- expect(shippingMethods.items[0].code).toBe('standard-shipping');
- expect(shippingMethods.items[1].code).toBe('express-shipping');
- });
- it('shippingMethod', async () => {
- const { shippingMethod } = await adminClient.query<
- GetShippingMethod.Query,
- GetShippingMethod.Variables
- >(GET_SHIPPING_METHOD, {
- id: 'T_1',
- });
- expect(shippingMethod!.code).toBe('standard-shipping');
- });
- it('createShippingMethod', async () => {
- const { createShippingMethod } = await adminClient.query<
- CreateShippingMethod.Mutation,
- CreateShippingMethod.Variables
- >(CREATE_SHIPPING_METHOD, {
- input: {
- code: 'new-method',
- description: 'new method',
- checker: {
- code: defaultShippingEligibilityChecker.code,
- arguments: [
- {
- name: 'orderMinimum',
- type: 'int',
- value: '0',
- },
- ],
- },
- calculator: {
- code: calculatorWithMetadata.code,
- arguments: [],
- },
- },
- });
- expect(createShippingMethod).toEqual({
- id: 'T_3',
- code: 'new-method',
- description: 'new method',
- calculator: {
- code: 'calculator-with-metadata',
- },
- checker: {
- code: 'default-shipping-eligibility-checker',
- },
- });
- });
- it('testShippingMethod', async () => {
- const { testShippingMethod } = await adminClient.query<
- TestShippingMethod.Query,
- TestShippingMethod.Variables
- >(TEST_SHIPPING_METHOD, {
- input: {
- calculator: {
- code: calculatorWithMetadata.code,
- arguments: [],
- },
- checker: {
- code: defaultShippingEligibilityChecker.code,
- arguments: [
- {
- name: 'orderMinimum',
- type: 'int',
- value: '0',
- },
- ],
- },
- lines: [{ productVariantId: 'T_1', quantity: 1 }],
- shippingAddress: {
- streetLine1: '',
- countryCode: 'GB',
- },
- },
- });
- expect(testShippingMethod).toEqual({
- eligible: true,
- quote: {
- price: 100,
- priceWithTax: 100,
- metadata: TEST_METADATA,
- },
- });
- });
- it('testEligibleShippingMethods', async () => {
- const { testEligibleShippingMethods } = await adminClient.query<
- TestEligibleMethods.Query,
- TestEligibleMethods.Variables
- >(TEST_ELIGIBLE_SHIPPING_METHODS, {
- input: {
- lines: [{ productVariantId: 'T_1', quantity: 1 }],
- shippingAddress: {
- streetLine1: '',
- countryCode: 'GB',
- },
- },
- });
- expect(testEligibleShippingMethods).toEqual([
- {
- id: 'T_3',
- description: 'new method',
- price: 100,
- priceWithTax: 100,
- metadata: TEST_METADATA,
- },
- {
- id: 'T_1',
- description: 'Standard Shipping',
- price: 500,
- priceWithTax: 500,
- metadata: null,
- },
- {
- id: 'T_2',
- description: 'Express Shipping',
- price: 1000,
- priceWithTax: 1000,
- metadata: null,
- },
- ]);
- });
- it('updateShippingMethod', async () => {
- const { updateShippingMethod } = await adminClient.query<
- UpdateShippingMethod.Mutation,
- UpdateShippingMethod.Variables
- >(UPDATE_SHIPPING_METHOD, {
- input: {
- id: 'T_3',
- description: 'changed method',
- },
- });
- expect(updateShippingMethod.description).toBe('changed method');
- });
- it('deleteShippingMethod', async () => {
- const listResult1 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
- expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
- const { deleteShippingMethod } = await adminClient.query<
- DeleteShippingMethod.Mutation,
- DeleteShippingMethod.Variables
- >(DELETE_SHIPPING_METHOD, {
- id: 'T_3',
- });
- expect(deleteShippingMethod).toEqual({
- result: DeletionResult.DELETED,
- message: null,
- });
- const listResult2 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
- expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2']);
- });
- });
- const SHIPPING_METHOD_FRAGMENT = gql`
- fragment ShippingMethod on ShippingMethod {
- id
- code
- description
- calculator {
- code
- }
- checker {
- code
- }
- }
- `;
- const GET_SHIPPING_METHOD_LIST = gql`
- query GetShippingMethodList {
- shippingMethods {
- items {
- ...ShippingMethod
- }
- totalItems
- }
- }
- ${SHIPPING_METHOD_FRAGMENT}
- `;
- const GET_SHIPPING_METHOD = gql`
- query GetShippingMethod($id: ID!) {
- shippingMethod(id: $id) {
- ...ShippingMethod
- }
- }
- ${SHIPPING_METHOD_FRAGMENT}
- `;
- const CREATE_SHIPPING_METHOD = gql`
- mutation CreateShippingMethod($input: CreateShippingMethodInput!) {
- createShippingMethod(input: $input) {
- ...ShippingMethod
- }
- }
- ${SHIPPING_METHOD_FRAGMENT}
- `;
- const UPDATE_SHIPPING_METHOD = gql`
- mutation UpdateShippingMethod($input: UpdateShippingMethodInput!) {
- updateShippingMethod(input: $input) {
- ...ShippingMethod
- }
- }
- ${SHIPPING_METHOD_FRAGMENT}
- `;
- const DELETE_SHIPPING_METHOD = gql`
- mutation DeleteShippingMethod($id: ID!) {
- deleteShippingMethod(id: $id) {
- result
- message
- }
- }
- `;
- const GET_ELIGIBILITY_CHECKERS = gql`
- query GetEligibilityCheckers {
- shippingEligibilityCheckers {
- code
- description
- args {
- name
- type
- description
- label
- config
- }
- }
- }
- `;
- const GET_CALCULATORS = gql`
- query GetCalculators {
- shippingCalculators {
- code
- description
- args {
- name
- type
- description
- label
- config
- }
- }
- }
- `;
- const TEST_SHIPPING_METHOD = gql`
- query TestShippingMethod($input: TestShippingMethodInput!) {
- testShippingMethod(input: $input) {
- eligible
- quote {
- price
- priceWithTax
- metadata
- }
- }
- }
- `;
- export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
- query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
- testEligibleShippingMethods(input: $input) {
- id
- description
- price
- priceWithTax
- metadata
- }
- }
- `;
|