From 978ec481cc728643b5a8a98be17535b16e004a19 Mon Sep 17 00:00:00 2001
From: Briley Sandlin <bsandlin@gitlab.com>
Date: Wed, 31 Jan 2024 15:02:26 -0600
Subject: [PATCH] Sort resources by released_at

Adding released_at to our catalog resources sort options

Changelog: added
---
 .../components/list/catalog_search.vue        |  9 ++++--
 .../list/ci_resources_list_item.vue           | 20 ++++++------
 .../javascripts/ci/catalog/constants.js       |  1 +
 locale/gitlab.pot                             |  3 ++
 spec/features/explore/catalog/catalog_spec.rb | 31 ------------------
 .../components/list/catalog_search_spec.js    | 32 +++++++++++++++----
 6 files changed, 46 insertions(+), 50 deletions(-)

diff --git a/app/assets/javascripts/ci/catalog/components/list/catalog_search.vue b/app/assets/javascripts/ci/catalog/components/list/catalog_search.vue
index 1319f204573c7..ec8d6c917d951 100644
--- a/app/assets/javascripts/ci/catalog/components/list/catalog_search.vue
+++ b/app/assets/javascripts/ci/catalog/components/list/catalog_search.vue
@@ -1,7 +1,7 @@
 <script>
 import { GlSearchBoxByClick, GlSorting } from '@gitlab/ui';
 import { __ } from '~/locale';
-import { SORT_ASC, SORT_DESC, SORT_OPTION_CREATED } from '../../constants';
+import { SORT_ASC, SORT_DESC, SORT_OPTION_CREATED, SORT_OPTION_RELEASED } from '../../constants';
 
 export default {
   components: {
@@ -10,7 +10,7 @@ export default {
   },
   data() {
     return {
-      currentSortOption: SORT_OPTION_CREATED,
+      currentSortOption: SORT_OPTION_RELEASED,
       isAscending: false,
       searchTerm: '',
     };
@@ -48,7 +48,10 @@ export default {
       this.currentSortOption = sortingItem;
     },
   },
-  sortOptions: [{ value: SORT_OPTION_CREATED, text: __('Created at') }],
+  sortOptions: [
+    { value: SORT_OPTION_RELEASED, text: __('Released at') },
+    { value: SORT_OPTION_CREATED, text: __('Created at') },
+  ],
 };
 </script>
 <template>
diff --git a/app/assets/javascripts/ci/catalog/components/list/ci_resources_list_item.vue b/app/assets/javascripts/ci/catalog/components/list/ci_resources_list_item.vue
index a6218a4c5952e..6fcae4041d5e1 100644
--- a/app/assets/javascripts/ci/catalog/components/list/ci_resources_list_item.vue
+++ b/app/assets/javascripts/ci/catalog/components/list/ci_resources_list_item.vue
@@ -142,7 +142,17 @@ export default {
       <div
         class="gl-display-flex gl-flex-direction-column gl-md-flex-direction-row gl-justify-content-space-between gl-font-sm"
       >
-        <span class="gl-display-flex gl-flex-basis-two-thirds">{{ resource.description }}</span>
+        <div>
+          <span class="gl-display-flex gl-flex-basis-two-thirds">{{ resource.description }}</span>
+          <div
+            v-if="hasComponents"
+            data-testid="ci-resource-component-names"
+            class="gl-font-sm gl-mt-1"
+          >
+            <span class="gl-font-weight-bold"> &#8226; {{ $options.i18n.components }} </span>
+            <span class="gl-text-gray-900">{{ componentNames }}</span>
+          </div>
+        </div>
         <div class="gl-display-flex gl-justify-content-end">
           <span v-if="hasReleasedVersion">
             <gl-sprintf :message="$options.i18n.releasedMessage">
@@ -160,14 +170,6 @@ export default {
           </span>
         </div>
       </div>
-      <div
-        v-if="hasComponents"
-        data-testid="ci-resource-component-names"
-        class="gl-font-sm gl-mt-1"
-      >
-        <span class="gl-font-weight-bold"> &#8226; {{ $options.i18n.components }} </span>
-        <span class="gl-text-gray-900">{{ componentNames }}</span>
-      </div>
     </div>
   </li>
 </template>
diff --git a/app/assets/javascripts/ci/catalog/constants.js b/app/assets/javascripts/ci/catalog/constants.js
index a180aa8434452..2767c9d91ece9 100644
--- a/app/assets/javascripts/ci/catalog/constants.js
+++ b/app/assets/javascripts/ci/catalog/constants.js
@@ -8,6 +8,7 @@ export const SCOPE = {
 };
 
 export const SORT_OPTION_CREATED = 'CREATED';
+export const SORT_OPTION_RELEASED = 'LATEST_RELEASED_AT';
 export const SORT_ASC = 'ASC';
 export const SORT_DESC = 'DESC';
 export const DEFAULT_SORT_VALUE = `${SORT_OPTION_CREATED}_${SORT_DESC}`;
diff --git a/locale/gitlab.pot b/locale/gitlab.pot
index cc5c4eff54a25..f38adbdaae825 100644
--- a/locale/gitlab.pot
+++ b/locale/gitlab.pot
@@ -40832,6 +40832,9 @@ msgstr ""
 msgid "Released"
 msgstr ""
 
+msgid "Released at"
+msgstr ""
+
 msgid "Released date"
 msgstr ""
 
diff --git a/spec/features/explore/catalog/catalog_spec.rb b/spec/features/explore/catalog/catalog_spec.rb
index 1ad0e9679b887..7f5655e316927 100644
--- a/spec/features/explore/catalog/catalog_spec.rb
+++ b/spec/features/explore/catalog/catalog_spec.rb
@@ -136,36 +136,5 @@
         end
       end
     end
-
-    context 'when sorting' do
-      context 'with the creation date option' do
-        it 'sorts resources from last to first by default' do
-          expect(find_all('[data-testid="catalog-resource-item"]').length).to be(3)
-          expect(find_all('[data-testid="catalog-resource-item"]')[0]).to have_content(
-            public_projects_with_components[2].name
-          )
-          expect(find_all('[data-testid="catalog-resource-item"]')[2]).to have_content(
-            public_projects_with_components[0].name
-          )
-        end
-
-        context 'when changing the sort direction' do
-          before do
-            find('.sorting-direction-button').click
-            wait_for_requests
-          end
-
-          it 'sorts resources from first to last' do
-            expect(find_all('[data-testid="catalog-resource-item"]').length).to be(3)
-            expect(find_all('[data-testid="catalog-resource-item"]')[0]).to have_content(
-              public_projects_with_components[0].name
-            )
-            expect(find_all('[data-testid="catalog-resource-item"]')[2]).to have_content(
-              public_projects_with_components[2].name
-            )
-          end
-        end
-      end
-    end
   end
 end
diff --git a/spec/frontend/ci/catalog/components/list/catalog_search_spec.js b/spec/frontend/ci/catalog/components/list/catalog_search_spec.js
index 803deeb0d45f2..0242240e2e6b5 100644
--- a/spec/frontend/ci/catalog/components/list/catalog_search_spec.js
+++ b/spec/frontend/ci/catalog/components/list/catalog_search_spec.js
@@ -1,7 +1,12 @@
 import { GlSearchBoxByClick, GlSorting } from '@gitlab/ui';
 import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
 import CatalogSearch from '~/ci/catalog/components/list/catalog_search.vue';
-import { SORT_ASC, SORT_DESC, SORT_OPTION_CREATED } from '~/ci/catalog/constants';
+import {
+  SORT_ASC,
+  SORT_DESC,
+  SORT_OPTION_CREATED,
+  SORT_OPTION_RELEASED,
+} from '~/ci/catalog/constants';
 
 describe('CatalogSearch', () => {
   let wrapper;
@@ -25,12 +30,12 @@ describe('CatalogSearch', () => {
 
     it('sets sorting options', () => {
       const sortOptionsProp = findAllSortingItems();
-      expect(sortOptionsProp).toHaveLength(1);
-      expect(sortOptionsProp[0].text).toBe('Created at');
+      expect(sortOptionsProp).toHaveLength(2);
+      expect(sortOptionsProp[0].text).toBe('Released at');
     });
 
-    it('renders the `Created at` option as the default', () => {
-      expect(findSorting().props('text')).toBe('Created at');
+    it('renders the `Released at` option as the default', () => {
+      expect(findSorting().props('text')).toBe('Released at');
     });
   });
 
@@ -86,6 +91,10 @@ describe('CatalogSearch', () => {
         await findSorting().vm.$emit('sortDirectionChange');
 
         expect(findSorting().props().isAscending).toBe(true);
+
+        await findSorting().vm.$emit('sortDirectionChange');
+
+        expect(findSorting().props().isAscending).toBe(false);
       });
 
       it('emits an `update-sorting` event with the new direction', async () => {
@@ -95,10 +104,19 @@ describe('CatalogSearch', () => {
         await findSorting().vm.$emit('sortDirectionChange');
 
         expect(wrapper.emitted('update-sorting')).toEqual([
-          [`${SORT_OPTION_CREATED}_${SORT_ASC}`],
-          [`${SORT_OPTION_CREATED}_${SORT_DESC}`],
+          [`${SORT_OPTION_RELEASED}_${SORT_ASC}`],
+          [`${SORT_OPTION_RELEASED}_${SORT_DESC}`],
         ]);
       });
     });
+
+    describe('when changing sort option', () => {
+      it('changes the sort option to `Created at`', async () => {
+        await findSorting().vm.$emit('sortByChange', SORT_OPTION_CREATED);
+
+        expect(findSorting().props().sortBy).toBe(SORT_OPTION_CREATED);
+        expect(findSorting().props().text).toBe('Created at');
+      });
+    });
   });
 });
-- 
GitLab