Skip to content

Commit

Permalink
feat(core): Implement collection duplicator
Browse files Browse the repository at this point in the history
Relates to #627
  • Loading branch information
michaelbromley committed Feb 20, 2024
1 parent 6ac43d9 commit d457851
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 50 deletions.
17 changes: 1 addition & 16 deletions packages/core/e2e/collection.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
DELETE_PRODUCT,
DELETE_PRODUCT_VARIANT,
GET_ASSET_LIST,
GET_COLLECTION,
GET_COLLECTIONS,
UPDATE_COLLECTION,
UPDATE_PRODUCT,
Expand Down Expand Up @@ -2400,22 +2401,6 @@ describe('Collection resolver', () => {
}
});

export const GET_COLLECTION = gql`
query GetCollection($id: ID, $slug: String, $variantListOptions: ProductVariantListOptions) {
collection(id: $id, slug: $slug) {
...Collection
productVariants(options: $variantListOptions) {
items {
id
name
price
}
}
}
}
${COLLECTION_FRAGMENT}
`;

export const GET_COLLECTION_LIST = gql`
query GetCollectionListAdmin($options: CollectionListOptions) {
collections(options: $options) {
Expand Down
159 changes: 145 additions & 14 deletions packages/core/e2e/duplicate-entity.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { pick } from '@vendure/common/lib/pick';
import {
Collection,
CollectionService,
defaultEntityDuplicators,
EntityDuplicator,
variantIdCollectionFilter,
LanguageCode,
mergeConfig,
PermissionDefinition,
Expand All @@ -29,7 +31,9 @@ import {
} from './graphql/generated-e2e-admin-types';
import {
CREATE_ADMINISTRATOR,
CREATE_COLLECTION,
CREATE_ROLE,
GET_COLLECTION,
GET_COLLECTIONS,
GET_PRODUCT_WITH_VARIANTS,
UPDATE_PRODUCT_VARIANTS,
Expand Down Expand Up @@ -274,13 +278,13 @@ describe('Duplicating entities', () => {

it('duplicate gets created', async () => {
const { collection } = await adminClient.query<
Codegen.GetDuplicatedCollectionQuery,
Codegen.GetDuplicatedCollectionQueryVariables
>(GET_DUPLICATED_COLLECTION, {
Codegen.GetCollectionQuery,
Codegen.GetCollectionQueryVariables
>(GET_COLLECTION, {
id: 'T_3',
});

expect(collection).toEqual({
expect(pick(collection, ['id', 'name', 'slug'])).toEqual({
id: 'T_3',
name: 'Plants (copy)',
slug: 'plants-copy',
Expand Down Expand Up @@ -441,6 +445,17 @@ describe('Duplicating entities', () => {
);
});

it('product name is suffixed', async () => {
const { product } = await adminClient.query<
Codegen.GetProductWithVariantsQuery,
Codegen.GetProductWithVariantsQueryVariables
>(GET_PRODUCT_WITH_VARIANTS, {
id: newProduct2Id,
});

expect(product?.name).toBe('Laptop (copy)');
});

it('variant SKUs are suffixed', async () => {
const { product } = await adminClient.query<
Codegen.GetProductWithVariantsQuery,
Expand Down Expand Up @@ -504,6 +519,132 @@ describe('Duplicating entities', () => {
);
});
});

describe('Collection duplicator', () => {
let testCollection: Codegen.CreateCollectionMutation['createCollection'];
let duplicatedCollectionId: string;

beforeAll(async () => {
await adminClient.asSuperAdmin();

const { createCollection } = await adminClient.query<
Codegen.CreateCollectionMutation,
Codegen.CreateCollectionMutationVariables
>(CREATE_COLLECTION, {
input: {
parentId: 'T_2',
assetIds: ['T_1'],
featuredAssetId: 'T_1',
isPrivate: false,
inheritFilters: false,
translations: [
{
languageCode: LanguageCode.en,
name: 'Test Collection',
description: 'Test Collection description',
slug: 'test-collection',
},
],
filters: [
{
code: variantIdCollectionFilter.code,
arguments: [
{
name: 'variantIds',
value: '["T_1"]',
},
{
name: 'combineWithAnd',
value: 'true',
},
],
},
],
},
});
testCollection = createCollection;
});

it('duplicate collection', async () => {
const { duplicateEntity } = await adminClient.query<
Codegen.DuplicateEntityMutation,
Codegen.DuplicateEntityMutationVariables
>(DUPLICATE_ENTITY, {
input: {
entityName: 'Collection',
entityId: testCollection.id,
duplicatorInput: {
code: 'collection-duplicator',
arguments: [],
},
},
});

duplicateEntityGuard.assertSuccess(duplicateEntity);

expect(testCollection.id).toBe('T_4');
expect(duplicateEntity.newEntityId).toBe('T_5');

duplicatedCollectionId = duplicateEntity.newEntityId;
});

it('collection name is suffixed', async () => {
const { collection } = await adminClient.query<
Codegen.GetCollectionQuery,
Codegen.GetCollectionQueryVariables
>(GET_COLLECTION, {
id: duplicatedCollectionId,
});

expect(collection?.name).toBe('Test Collection (copy)');
});

it('is initially private', async () => {
const { collection } = await adminClient.query<
Codegen.GetCollectionQuery,
Codegen.GetCollectionQueryVariables
>(GET_COLLECTION, {
id: duplicatedCollectionId,
});

expect(collection?.isPrivate).toBe(true);
});

it('assets are duplicated', async () => {
const { collection } = await adminClient.query<
Codegen.GetCollectionQuery,
Codegen.GetCollectionQueryVariables
>(GET_COLLECTION, {
id: duplicatedCollectionId,
});

expect(collection?.featuredAsset).toEqual(testCollection.featuredAsset);
expect(collection?.assets.length).toBe(1);
expect(collection?.assets).toEqual(testCollection.assets);
});

it('parentId matches', async () => {
const { collection } = await adminClient.query<
Codegen.GetCollectionQuery,
Codegen.GetCollectionQueryVariables
>(GET_COLLECTION, {
id: duplicatedCollectionId,
});

expect(collection?.parent?.id).toBe(testCollection.parent?.id);
});

it('filters are duplicated', async () => {
const { collection } = await adminClient.query<
Codegen.GetCollectionQuery,
Codegen.GetCollectionQueryVariables
>(GET_COLLECTION, {
id: duplicatedCollectionId,
});

expect(collection?.filters).toEqual(testCollection.filters);
});
});
});
});

Expand Down Expand Up @@ -536,13 +677,3 @@ const DUPLICATE_ENTITY = gql`
}
}
`;

export const GET_DUPLICATED_COLLECTION = gql`
query GetDuplicatedCollection($id: ID) {
collection(id: $id) {
id
name
slug
}
}
`;
Loading

0 comments on commit d457851

Please sign in to comment.