Browse Source

fix(core): Correct ordering of Collection breadcrumbs

* #779 Fix collection.e2e-spec.ts tests to produce error

* #779 Fix collection.service.ts to always get breadcrumbs in parent to child order
Artem Danilov 4 years ago
parent
commit
92952fb556

+ 263 - 14
packages/core/e2e/collection.e2e-spec.ts

@@ -65,6 +65,9 @@ describe('Collection resolver', () => {
     let electronicsCollection: Collection.Fragment;
     let computersCollection: Collection.Fragment;
     let pearCollection: Collection.Fragment;
+    let electronicsBreadcrumbsCollection: Collection.Fragment;
+    let computersBreadcrumbsCollection: Collection.Fragment;
+    let pearBreadcrumbsCollection: Collection.Fragment;
 
     beforeAll(async () => {
         await server.init({
@@ -276,6 +279,115 @@ describe('Collection resolver', () => {
                 'zubehor-2',
             );
         });
+        it('creates a root collection to became a 1st level collection later #779', async () => {
+            const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
+                CREATE_COLLECTION,
+                {
+                    input: {
+                        assetIds: [assets[0].id, assets[1].id],
+                        featuredAssetId: assets[1].id,
+                        filters: [
+                            {
+                                code: facetValueCollectionFilter.code,
+                                arguments: [
+                                    {
+                                        name: 'facetValueIds',
+                                        value: `["${getFacetValueId('computers')}"]`,
+                                    },
+                                    {
+                                        name: 'containsAny',
+                                        value: `false`,
+                                    },
+                                ],
+                            },
+                        ],
+                        translations: [
+                            {
+                                languageCode: LanguageCode.en,
+                                name: 'Computers Breadcrumbs',
+                                description: '',
+                                slug: 'computers_breadcrumbs',
+                            },
+                        ],
+                    },
+                },
+            );
+
+            computersBreadcrumbsCollection = result.createCollection;
+            expect(computersBreadcrumbsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
+        });
+        it('creates a root collection to be a parent collection for 1st level collection with id greater than child collection #779', async () => {
+            const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
+                CREATE_COLLECTION,
+                {
+                    input: {
+                        assetIds: [assets[0].id, assets[1].id],
+                        featuredAssetId: assets[1].id,
+                        filters: [
+                            {
+                                code: facetValueCollectionFilter.code,
+                                arguments: [
+                                    {
+                                        name: 'facetValueIds',
+                                        value: `["${getFacetValueId('electronics')}"]`,
+                                    },
+                                    {
+                                        name: 'containsAny',
+                                        value: `false`,
+                                    },
+                                ],
+                            },
+                        ],
+                        translations: [
+                            {
+                                languageCode: LanguageCode.en,
+                                name: 'Electronics Breadcrumbs',
+                                description: '',
+                                slug: 'electronics_breadcrumbs',
+                            },
+                        ],
+                    },
+                },
+            );
+
+            electronicsBreadcrumbsCollection = result.createCollection;
+            expect(electronicsBreadcrumbsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
+        });
+        it('creates a 2nd level nested collection #779', async () => {
+            const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
+                CREATE_COLLECTION,
+                {
+                    input: {
+                        parentId: computersBreadcrumbsCollection.id,
+                        translations: [
+                            {
+                                languageCode: LanguageCode.en,
+                                name: 'Pear Breadcrumbs',
+                                description: '',
+                                slug: 'pear_breadcrumbs',
+                            },
+                        ],
+                        filters: [
+                            {
+                                code: facetValueCollectionFilter.code,
+                                arguments: [
+                                    {
+                                        name: 'facetValueIds',
+                                        value: `["${getFacetValueId('pear')}"]`,
+                                    },
+                                    {
+                                        name: 'containsAny',
+                                        value: `false`,
+                                    },
+                                ],
+                            },
+                        ],
+                    },
+                },
+            );
+            pearBreadcrumbsCollection = result.createCollection;
+            expect(pearBreadcrumbsCollection.parent!.name).toBe(computersBreadcrumbsCollection.name);
+        });
     });
 
     describe('updateCollection', () => {
@@ -416,8 +528,16 @@ describe('Collection resolver', () => {
                 id: 'T_1',
             });
 
-            expect(product?.collections.length).toBe(3);
+            expect(product?.collections.length).toBe(6);
             expect(product?.collections.sort(sortById)).toEqual([
+                {
+                    id: 'T_10',
+                    name: 'Pear Breadcrumbs',
+                    parent: {
+                        id: 'T_8',
+                        name: 'Computers Breadcrumbs',
+                    },
+                },
                 {
                     id: 'T_3',
                     name: 'Electronics',
@@ -442,6 +562,22 @@ describe('Collection resolver', () => {
                         name: 'Computers',
                     },
                 },
+                {
+                    id: 'T_8',
+                    name: 'Computers Breadcrumbs',
+                    parent: {
+                        id: 'T_1',
+                        name: '__root_collection__',
+                    },
+                },
+                {
+                    id: 'T_9',
+                    name: 'Electronics Breadcrumbs',
+                    parent: {
+                        id: 'T_1',
+                        name: '__root_collection__',
+                    },
+                },
             ]);
         });
 
@@ -589,6 +725,55 @@ describe('Collection resolver', () => {
             ]);
         });
 
+        it('moves a 1st level collection to a new parent to check breadcrumbs', async () => {
+            const result = await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(
+                MOVE_COLLECTION,
+                {
+                    input: {
+                        collectionId: computersBreadcrumbsCollection.id,
+                        parentId: electronicsBreadcrumbsCollection.id,
+                        index: 0,
+                    },
+                },
+            );
+
+            expect(result.moveCollection.parent!.id).toBe(electronicsBreadcrumbsCollection.id);
+
+            const positions = await getChildrenOf(electronicsBreadcrumbsCollection.id);
+            expect(positions.map(i => i.id)).toEqual([computersBreadcrumbsCollection.id]);
+        });
+
+        it('breadcrumbs for collection with ids out of order', async () => {
+            const result = await adminClient.query<
+                GetCollectionBreadcrumbs.Query,
+                GetCollectionBreadcrumbs.Variables
+            >(GET_COLLECTION_BREADCRUMBS, {
+                id: pearBreadcrumbsCollection.id,
+            });
+            if (!result.collection) {
+                fail(`did not return the collection`);
+                return;
+            }
+            expect(result.collection.breadcrumbs).toEqual([
+                { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
+                {
+                    id: electronicsBreadcrumbsCollection.id,
+                    name: electronicsBreadcrumbsCollection.name,
+                    slug: electronicsBreadcrumbsCollection.slug,
+                },
+                {
+                    id: computersBreadcrumbsCollection.id,
+                    name: computersBreadcrumbsCollection.name,
+                    slug: computersBreadcrumbsCollection.slug,
+                },
+                {
+                    id: pearBreadcrumbsCollection.id,
+                    name: pearBreadcrumbsCollection.name,
+                    slug: pearBreadcrumbsCollection.slug,
+                },
+            ]);
+        });
+
         it('alters the position in the current parent 1', async () => {
             await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
                 input: {
@@ -657,7 +842,7 @@ describe('Collection resolver', () => {
         );
 
         it(
-            'throws if attempting to move into a decendant of self',
+            'throws if attempting to move into a descendant of self',
             assertThrowsWithMessage(
                 () =>
                     adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
@@ -774,11 +959,38 @@ describe('Collection resolver', () => {
             });
 
             expect(product!.collections).toEqual([
-                { id: 'T_3', name: 'Electronics' },
-                { id: 'T_4', name: 'Computers' },
-                { id: 'T_5', name: 'Pear' },
-                { id: 'T_8', name: 'Delete Me Parent' },
-                { id: 'T_9', name: 'Delete Me Child' },
+                {
+                    id: 'T_3',
+                    name: 'Electronics',
+                },
+                {
+                    id: 'T_4',
+                    name: 'Computers',
+                },
+                {
+                    id: 'T_5',
+                    name: 'Pear',
+                },
+                {
+                    id: 'T_8',
+                    name: 'Computers Breadcrumbs',
+                },
+                {
+                    id: 'T_9',
+                    name: 'Electronics Breadcrumbs',
+                },
+                {
+                    id: 'T_10',
+                    name: 'Pear Breadcrumbs',
+                },
+                {
+                    id: 'T_11',
+                    name: 'Delete Me Parent',
+                },
+                {
+                    id: 'T_12',
+                    name: 'Delete Me Child',
+                },
             ]);
         });
 
@@ -825,6 +1037,18 @@ describe('Collection resolver', () => {
                 { id: 'T_3', name: 'Electronics' },
                 { id: 'T_4', name: 'Computers' },
                 { id: 'T_5', name: 'Pear' },
+                {
+                    id: 'T_8',
+                    name: 'Computers Breadcrumbs',
+                },
+                {
+                    id: 'T_9',
+                    name: 'Electronics Breadcrumbs',
+                },
+                {
+                    id: 'T_10',
+                    name: 'Pear Breadcrumbs',
+                },
             ]);
         });
     });
@@ -1353,13 +1577,38 @@ describe('Collection resolver', () => {
                 GetCollectionsForProducts.Variables
             >(GET_COLLECTIONS_FOR_PRODUCTS, { term: 'camera' });
             expect(result.products.items[0].collections).toEqual([
-                { id: 'T_3', name: 'Electronics' },
-                { id: 'T_5', name: 'Pear' },
-                { id: 'T_11', name: 'Photo AND Pear' },
-                { id: 'T_12', name: 'Photo OR Pear' },
-                { id: 'T_14', name: 'contains camera' },
-                { id: 'T_16', name: 'endsWith camera' },
-                { id: 'T_18', name: 'pear electronics' },
+                {
+                    id: 'T_3',
+                    name: 'Electronics',
+                },
+                {
+                    id: 'T_5',
+                    name: 'Pear',
+                },
+                {
+                    id: 'T_9',
+                    name: 'Electronics Breadcrumbs',
+                },
+                {
+                    id: 'T_14',
+                    name: 'Photo AND Pear',
+                },
+                {
+                    id: 'T_15',
+                    name: 'Photo OR Pear',
+                },
+                {
+                    id: 'T_17',
+                    name: 'contains camera',
+                },
+                {
+                    id: 'T_19',
+                    name: 'endsWith camera',
+                },
+                {
+                    id: 'T_21',
+                    name: 'pear electronics',
+                },
             ]);
         });
     });

+ 9 - 2
packages/core/src/service/services/collection.service.ts

@@ -203,7 +203,7 @@ export class CollectionService implements OnModuleInit {
         }
         const pickProps = pick(['id', 'name', 'slug']);
         const ancestors = await this.getAncestors(collection.id, ctx);
-        return [pickProps(rootCollection), ...ancestors.map(pickProps), pickProps(collection)];
+        return [pickProps(rootCollection), ...ancestors.map(pickProps).reverse(), pickProps(collection)];
     }
 
     async getCollectionsByProductId(
@@ -285,7 +285,14 @@ export class CollectionService implements OnModuleInit {
             .getRepository(Collection)
             .findByIds(ancestors.map(c => c.id))
             .then(categories => {
-                return ctx ? categories.map(c => translateDeep(c, ctx.languageCode)) : categories;
+                const resultCategories: Array<Collection | Translated<Collection>> = [];
+                ancestors.forEach(a => {
+                    const category = categories.find(c => c.id === a.id);
+                    if (category) {
+                        resultCategories.push(ctx ? translateDeep(category, ctx.languageCode) : category);
+                    }
+                });
+                return resultCategories;
             });
     }