| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- /* 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 { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
- import { manualFulfillmentHandler } from '../src/config/fulfillment/manual-fulfillment-handler';
- import { SHIPPING_METHOD_FRAGMENT } from './graphql/fragments';
- import * as Codegen from './graphql/generated-e2e-admin-types';
- import { DeletionResult, LanguageCode } from './graphql/generated-e2e-admin-types';
- import {
- CREATE_SHIPPING_METHOD,
- DELETE_SHIPPING_METHOD,
- GET_SHIPPING_METHOD_LIST,
- UPDATE_SHIPPING_METHOD,
- } from './graphql/shared-definitions';
- 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: () => {
- return {
- price: 100,
- priceIncludesTax: true,
- taxRate: 0,
- metadata: TEST_METADATA,
- };
- },
- });
- describe('ShippingMethod resolver', () => {
- const { server, adminClient, shopClient } = createTestEnvironment({
- ...testConfig(),
- shippingOptions: {
- shippingEligibilityCheckers: [defaultShippingEligibilityChecker],
- shippingCalculators: [defaultShippingCalculator, calculatorWithMetadata],
- },
- });
- beforeAll(async () => {
- await server.init({
- 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<Codegen.GetEligibilityCheckersQuery>(
- GET_ELIGIBILITY_CHECKERS,
- );
- expect(shippingEligibilityCheckers).toEqual([
- {
- args: [
- {
- description: 'Order is eligible only if its total is greater or equal to this value',
- label: 'Minimum order value',
- name: 'orderMinimum',
- type: 'int',
- ui: {
- component: 'currency-form-input',
- },
- },
- ],
- code: 'default-shipping-eligibility-checker',
- description: 'Default Shipping Eligibility Checker',
- },
- ]);
- });
- it('shippingCalculators', async () => {
- const { shippingCalculators } = await adminClient.query<Codegen.GetCalculatorsQuery>(GET_CALCULATORS);
- expect(shippingCalculators).toEqual([
- {
- args: [
- {
- ui: {
- component: 'currency-form-input',
- },
- description: null,
- label: 'Shipping price',
- name: 'rate',
- type: 'int',
- },
- {
- label: 'Price includes tax',
- name: 'includesTax',
- type: 'string',
- description: null,
- ui: {
- component: 'select-form-input',
- options: [
- {
- label: [{ languageCode: LanguageCode.en, value: 'Includes tax' }],
- value: 'include',
- },
- {
- label: [{ languageCode: LanguageCode.en, value: 'Excludes tax' }],
- value: 'exclude',
- },
- {
- label: [
- { languageCode: LanguageCode.en, value: 'Auto (based on Channel)' },
- ],
- value: 'auto',
- },
- ],
- },
- },
- {
- ui: {
- component: 'number-form-input',
- suffix: '%',
- },
- 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<Codegen.GetShippingMethodListQuery>(
- 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<
- Codegen.GetShippingMethodQuery,
- Codegen.GetShippingMethodQueryVariables
- >(GET_SHIPPING_METHOD, {
- id: 'T_1',
- });
- expect(shippingMethod!.code).toBe('standard-shipping');
- });
- it('createShippingMethod', async () => {
- const { createShippingMethod } = await adminClient.query<
- Codegen.CreateShippingMethodMutation,
- Codegen.CreateShippingMethodMutationVariables
- >(CREATE_SHIPPING_METHOD, {
- input: {
- code: 'new-method',
- fulfillmentHandler: manualFulfillmentHandler.code,
- checker: {
- code: defaultShippingEligibilityChecker.code,
- arguments: [
- {
- name: 'orderMinimum',
- value: '0',
- },
- ],
- },
- calculator: {
- code: calculatorWithMetadata.code,
- arguments: [],
- },
- translations: [{ languageCode: LanguageCode.en, name: 'new method', description: '' }],
- },
- });
- expect(createShippingMethod).toEqual({
- id: 'T_3',
- code: 'new-method',
- name: 'new method',
- description: '',
- calculator: {
- code: 'calculator-with-metadata',
- args: [],
- },
- checker: {
- code: 'default-shipping-eligibility-checker',
- args: [
- {
- name: 'orderMinimum',
- value: '0',
- },
- ],
- },
- });
- });
- it('testShippingMethod', async () => {
- const { testShippingMethod } = await adminClient.query<
- Codegen.TestShippingMethodQuery,
- Codegen.TestShippingMethodQueryVariables
- >(TEST_SHIPPING_METHOD, {
- input: {
- calculator: {
- code: calculatorWithMetadata.code,
- arguments: [],
- },
- checker: {
- code: defaultShippingEligibilityChecker.code,
- arguments: [
- {
- name: 'orderMinimum',
- 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<
- Codegen.TestEligibleMethodsQuery,
- Codegen.TestEligibleMethodsQueryVariables
- >(TEST_ELIGIBLE_SHIPPING_METHODS, {
- input: {
- lines: [{ productVariantId: 'T_1', quantity: 1 }],
- shippingAddress: {
- streetLine1: '',
- countryCode: 'GB',
- },
- },
- });
- expect(testEligibleShippingMethods).toEqual([
- {
- id: 'T_3',
- name: 'new method',
- description: '',
- price: 100,
- priceWithTax: 100,
- metadata: TEST_METADATA,
- },
- {
- id: 'T_1',
- name: 'Standard Shipping',
- description: '',
- price: 500,
- priceWithTax: 500,
- metadata: null,
- },
- {
- id: 'T_2',
- name: 'Express Shipping',
- description: '',
- price: 1000,
- priceWithTax: 1000,
- metadata: null,
- },
- ]);
- });
- it('updateShippingMethod', async () => {
- const { updateShippingMethod } = await adminClient.query<
- Codegen.UpdateShippingMethodMutation,
- Codegen.UpdateShippingMethodMutationVariables
- >(UPDATE_SHIPPING_METHOD, {
- input: {
- id: 'T_3',
- translations: [{ languageCode: LanguageCode.en, name: 'changed method', description: '' }],
- },
- });
- expect(updateShippingMethod.name).toBe('changed method');
- });
- it('deleteShippingMethod', async () => {
- const listResult1 = await adminClient.query<Codegen.GetShippingMethodListQuery>(
- GET_SHIPPING_METHOD_LIST,
- );
- expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
- const { deleteShippingMethod } = await adminClient.query<
- Codegen.DeleteShippingMethodMutation,
- Codegen.DeleteShippingMethodMutationVariables
- >(DELETE_SHIPPING_METHOD, {
- id: 'T_3',
- });
- expect(deleteShippingMethod).toEqual({
- result: DeletionResult.DELETED,
- message: null,
- });
- const listResult2 = await adminClient.query<Codegen.GetShippingMethodListQuery>(
- GET_SHIPPING_METHOD_LIST,
- );
- expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2']);
- });
- describe('argument ordering', () => {
- it('createShippingMethod corrects order of arguments', async () => {
- const { createShippingMethod } = await adminClient.query<
- Codegen.CreateShippingMethodMutation,
- Codegen.CreateShippingMethodMutationVariables
- >(CREATE_SHIPPING_METHOD, {
- input: {
- code: 'new-method',
- fulfillmentHandler: manualFulfillmentHandler.code,
- checker: {
- code: defaultShippingEligibilityChecker.code,
- arguments: [
- {
- name: 'orderMinimum',
- value: '0',
- },
- ],
- },
- calculator: {
- code: defaultShippingCalculator.code,
- arguments: [
- { name: 'rate', value: '500' },
- { name: 'taxRate', value: '20' },
- { name: 'includesTax', value: 'include' },
- ],
- },
- translations: [{ languageCode: LanguageCode.en, name: 'new method', description: '' }],
- },
- });
- expect(createShippingMethod.calculator).toEqual({
- code: defaultShippingCalculator.code,
- args: [
- { name: 'rate', value: '500' },
- { name: 'includesTax', value: 'include' },
- { name: 'taxRate', value: '20' },
- ],
- });
- });
- it('updateShippingMethod corrects order of arguments', async () => {
- const { updateShippingMethod } = await adminClient.query<
- Codegen.UpdateShippingMethodMutation,
- Codegen.UpdateShippingMethodMutationVariables
- >(UPDATE_SHIPPING_METHOD, {
- input: {
- id: 'T_4',
- translations: [],
- calculator: {
- code: defaultShippingCalculator.code,
- arguments: [
- { name: 'rate', value: '500' },
- { name: 'taxRate', value: '20' },
- { name: 'includesTax', value: 'include' },
- ],
- },
- },
- });
- expect(updateShippingMethod.calculator).toEqual({
- code: defaultShippingCalculator.code,
- args: [
- { name: 'rate', value: '500' },
- { name: 'includesTax', value: 'include' },
- { name: 'taxRate', value: '20' },
- ],
- });
- });
- it('get shippingMethod preserves correct ordering', async () => {
- const { shippingMethod } = await adminClient.query<
- Codegen.GetShippingMethodQuery,
- Codegen.GetShippingMethodQueryVariables
- >(GET_SHIPPING_METHOD, {
- id: 'T_4',
- });
- expect(shippingMethod?.calculator.args).toEqual([
- { name: 'rate', value: '500' },
- { name: 'includesTax', value: 'include' },
- { name: 'taxRate', value: '20' },
- ]);
- });
- it('testShippingMethod corrects order of arguments', async () => {
- const { testShippingMethod } = await adminClient.query<
- Codegen.TestShippingMethodQuery,
- Codegen.TestShippingMethodQueryVariables
- >(TEST_SHIPPING_METHOD, {
- input: {
- calculator: {
- code: defaultShippingCalculator.code,
- arguments: [
- { name: 'rate', value: '500' },
- { name: 'taxRate', value: '0' },
- { name: 'includesTax', value: 'include' },
- ],
- },
- checker: {
- code: defaultShippingEligibilityChecker.code,
- arguments: [
- {
- name: 'orderMinimum',
- value: '0',
- },
- ],
- },
- lines: [{ productVariantId: 'T_1', quantity: 1 }],
- shippingAddress: {
- streetLine1: '',
- countryCode: 'GB',
- },
- },
- });
- expect(testShippingMethod).toEqual({
- eligible: true,
- quote: {
- metadata: null,
- price: 500,
- priceWithTax: 500,
- },
- });
- });
- });
- });
- const GET_SHIPPING_METHOD = gql`
- query GetShippingMethod($id: ID!) {
- shippingMethod(id: $id) {
- ...ShippingMethod
- }
- }
- ${SHIPPING_METHOD_FRAGMENT}
- `;
- const GET_ELIGIBILITY_CHECKERS = gql`
- query GetEligibilityCheckers {
- shippingEligibilityCheckers {
- code
- description
- args {
- name
- type
- description
- label
- ui
- }
- }
- }
- `;
- const GET_CALCULATORS = gql`
- query GetCalculators {
- shippingCalculators {
- code
- description
- args {
- name
- type
- description
- label
- ui
- }
- }
- }
- `;
- 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
- name
- description
- price
- priceWithTax
- metadata
- }
- }
- `;
|