| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- 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 { FACET_VALUE_FRAGMENT, FACET_WITH_VALUES_FRAGMENT } from './graphql/fragments';
- import {
- CreateFacet,
- CreateFacetValues,
- DeleteFacet,
- DeleteFacetValues,
- DeletionResult,
- FacetWithValues,
- GetFacetList,
- GetFacetWithValues,
- GetProductListWithVariants,
- GetProductWithVariants,
- LanguageCode,
- UpdateFacet,
- UpdateFacetValues,
- UpdateProduct,
- UpdateProductVariants,
- } from './graphql/generated-e2e-admin-types';
- import {
- CREATE_FACET,
- GET_FACET_LIST,
- GET_PRODUCT_WITH_VARIANTS,
- UPDATE_FACET,
- UPDATE_PRODUCT,
- UPDATE_PRODUCT_VARIANTS,
- } from './graphql/shared-definitions';
- // tslint:disable:no-non-null-assertion
- describe('Facet resolver', () => {
- const { server, adminClient } = createTestEnvironment(testConfig);
- let brandFacet: FacetWithValues.Fragment;
- let speakerTypeFacet: FacetWithValues.Fragment;
- 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('createFacet', async () => {
- const result = await adminClient.query<CreateFacet.Mutation, CreateFacet.Variables>(CREATE_FACET, {
- input: {
- isPrivate: false,
- code: 'speaker-type',
- translations: [{ languageCode: LanguageCode.en, name: 'Speaker Type' }],
- values: [
- {
- code: 'portable',
- translations: [{ languageCode: LanguageCode.en, name: 'Portable' }],
- },
- ],
- },
- });
- speakerTypeFacet = result.createFacet;
- expect(speakerTypeFacet).toMatchSnapshot();
- });
- it('updateFacet', async () => {
- const result = await adminClient.query<UpdateFacet.Mutation, UpdateFacet.Variables>(UPDATE_FACET, {
- input: {
- id: speakerTypeFacet.id,
- translations: [{ languageCode: LanguageCode.en, name: 'Speaker Category' }],
- },
- });
- expect(result.updateFacet.name).toBe('Speaker Category');
- });
- it('createFacetValues', async () => {
- const result = await adminClient.query<CreateFacetValues.Mutation, CreateFacetValues.Variables>(
- CREATE_FACET_VALUES,
- {
- input: [
- {
- facetId: speakerTypeFacet.id,
- code: 'pc',
- translations: [{ languageCode: LanguageCode.en, name: 'PC Speakers' }],
- },
- {
- facetId: speakerTypeFacet.id,
- code: 'hi-fi',
- translations: [{ languageCode: LanguageCode.en, name: 'Hi Fi Speakers' }],
- },
- ],
- },
- );
- expect(result.createFacetValues).toMatchSnapshot();
- });
- it('updateFacetValues', async () => {
- const result = await adminClient.query<UpdateFacetValues.Mutation, UpdateFacetValues.Variables>(
- UPDATE_FACET_VALUES,
- {
- input: [
- {
- id: speakerTypeFacet.values[0].id,
- code: 'compact',
- },
- ],
- },
- );
- expect(result.updateFacetValues[0].code).toBe('compact');
- });
- it('facets', async () => {
- const result = await adminClient.query<GetFacetList.Query>(GET_FACET_LIST);
- const { items } = result.facets;
- expect(items.length).toBe(2);
- expect(items[0].name).toBe('category');
- expect(items[1].name).toBe('Speaker Category');
- brandFacet = items[0];
- speakerTypeFacet = items[1];
- });
- it('facet', async () => {
- const result = await adminClient.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
- GET_FACET_WITH_VALUES,
- {
- id: speakerTypeFacet.id,
- },
- );
- expect(result.facet!.name).toBe('Speaker Category');
- });
- describe('deletion', () => {
- let products: GetProductListWithVariants.Items[];
- beforeAll(async () => {
- // add the FacetValues to products and variants
- const result1 = await adminClient.query<GetProductListWithVariants.Query>(
- GET_PRODUCTS_LIST_WITH_VARIANTS,
- );
- products = result1.products.items;
- await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
- input: {
- id: products[0].id,
- facetValueIds: [speakerTypeFacet.values[0].id],
- },
- });
- await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
- UPDATE_PRODUCT_VARIANTS,
- {
- input: [
- {
- id: products[0].variants[0].id,
- facetValueIds: [speakerTypeFacet.values[0].id],
- },
- ],
- },
- );
- await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
- input: {
- id: products[1].id,
- facetValueIds: [speakerTypeFacet.values[1].id],
- },
- });
- });
- it('deleteFacetValues deletes unused facetValue', async () => {
- const facetValueToDelete = speakerTypeFacet.values[2];
- const result1 = await adminClient.query<DeleteFacetValues.Mutation, DeleteFacetValues.Variables>(
- DELETE_FACET_VALUES,
- {
- ids: [facetValueToDelete.id],
- force: false,
- },
- );
- const result2 = await adminClient.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
- GET_FACET_WITH_VALUES,
- {
- id: speakerTypeFacet.id,
- },
- );
- expect(result1.deleteFacetValues).toEqual([
- {
- result: DeletionResult.DELETED,
- message: ``,
- },
- ]);
- expect(result2.facet!.values[0]).not.toEqual(facetValueToDelete);
- });
- it('deleteFacetValues for FacetValue in use returns NOT_DELETED', async () => {
- const facetValueToDelete = speakerTypeFacet.values[0];
- const result1 = await adminClient.query<DeleteFacetValues.Mutation, DeleteFacetValues.Variables>(
- DELETE_FACET_VALUES,
- {
- ids: [facetValueToDelete.id],
- force: false,
- },
- );
- const result2 = await adminClient.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
- GET_FACET_WITH_VALUES,
- {
- id: speakerTypeFacet.id,
- },
- );
- expect(result1.deleteFacetValues).toEqual([
- {
- result: DeletionResult.NOT_DELETED,
- message: `The selected FacetValue is assigned to 1 Product, 1 ProductVariant`,
- },
- ]);
- expect(result2.facet!.values[0]).toEqual(facetValueToDelete);
- });
- it('deleteFacetValues for FacetValue in use can be force deleted', async () => {
- const facetValueToDelete = speakerTypeFacet.values[0];
- const result1 = await adminClient.query<DeleteFacetValues.Mutation, DeleteFacetValues.Variables>(
- DELETE_FACET_VALUES,
- {
- ids: [facetValueToDelete.id],
- force: true,
- },
- );
- expect(result1.deleteFacetValues).toEqual([
- {
- result: DeletionResult.DELETED,
- message: `The selected FacetValue was removed from 1 Product, 1 ProductVariant and deleted`,
- },
- ]);
- // FacetValue no longer in the Facet.values array
- const result2 = await adminClient.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
- GET_FACET_WITH_VALUES,
- {
- id: speakerTypeFacet.id,
- },
- );
- expect(result2.facet!.values[0]).not.toEqual(facetValueToDelete);
- // FacetValue no longer in the Product.facetValues array
- const result3 = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: products[0].id,
- });
- expect(result3.product!.facetValues).toEqual([]);
- });
- it('deleteFacet that is in use returns NOT_DELETED', async () => {
- const result1 = await adminClient.query<DeleteFacet.Mutation, DeleteFacet.Variables>(
- DELETE_FACET,
- {
- id: speakerTypeFacet.id,
- force: false,
- },
- );
- const result2 = await adminClient.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
- GET_FACET_WITH_VALUES,
- {
- id: speakerTypeFacet.id,
- },
- );
- expect(result1.deleteFacet).toEqual({
- result: DeletionResult.NOT_DELETED,
- message: `The selected Facet includes FacetValues which are assigned to 1 Product`,
- });
- expect(result2.facet).not.toBe(null);
- });
- it('deleteFacet that is in use can be force deleted', async () => {
- const result1 = await adminClient.query<DeleteFacet.Mutation, DeleteFacet.Variables>(
- DELETE_FACET,
- {
- id: speakerTypeFacet.id,
- force: true,
- },
- );
- expect(result1.deleteFacet).toEqual({
- result: DeletionResult.DELETED,
- message: `The Facet was deleted and its FacetValues were removed from 1 Product`,
- });
- // FacetValue no longer in the Facet.values array
- const result2 = await adminClient.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
- GET_FACET_WITH_VALUES,
- {
- id: speakerTypeFacet.id,
- },
- );
- expect(result2.facet).toBe(null);
- // FacetValue no longer in the Product.facetValues array
- const result3 = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: products[1].id,
- });
- expect(result3.product!.facetValues).toEqual([]);
- });
- it('deleteFacet with no FacetValues works', async () => {
- const { createFacet } = await adminClient.query<CreateFacet.Mutation, CreateFacet.Variables>(
- CREATE_FACET,
- {
- input: {
- code: 'test',
- isPrivate: false,
- translations: [{ languageCode: LanguageCode.en, name: 'Test' }],
- },
- },
- );
- const result = await adminClient.query<DeleteFacet.Mutation, DeleteFacet.Variables>(
- DELETE_FACET,
- {
- id: createFacet.id,
- force: false,
- },
- );
- expect(result.deleteFacet.result).toBe(DeletionResult.DELETED);
- });
- });
- });
- export const GET_FACET_WITH_VALUES = gql`
- query GetFacetWithValues($id: ID!) {
- facet(id: $id) {
- ...FacetWithValues
- }
- }
- ${FACET_WITH_VALUES_FRAGMENT}
- `;
- const DELETE_FACET_VALUES = gql`
- mutation DeleteFacetValues($ids: [ID!]!, $force: Boolean) {
- deleteFacetValues(ids: $ids, force: $force) {
- result
- message
- }
- }
- `;
- const DELETE_FACET = gql`
- mutation DeleteFacet($id: ID!, $force: Boolean) {
- deleteFacet(id: $id, force: $force) {
- result
- message
- }
- }
- `;
- const GET_PRODUCTS_LIST_WITH_VARIANTS = gql`
- query GetProductListWithVariants {
- products {
- items {
- id
- name
- variants {
- id
- name
- }
- }
- totalItems
- }
- }
- `;
- export const CREATE_FACET_VALUES = gql`
- mutation CreateFacetValues($input: [CreateFacetValueInput!]!) {
- createFacetValues(input: $input) {
- ...FacetValue
- }
- }
- ${FACET_VALUE_FRAGMENT}
- `;
- export const UPDATE_FACET_VALUES = gql`
- mutation UpdateFacetValues($input: [UpdateFacetValueInput!]!) {
- updateFacetValues(input: $input) {
- ...FacetValue
- }
- }
- ${FACET_VALUE_FRAGMENT}
- `;
|