Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Replace `lodash.sortby` and fix sources table ordering by url (#1389)

Krystle Salazar531667474254
diff --git a/package.json b/package.json
index 871846e5a..3964c4ada 100644
--- a/package.json
+++ b/package.json
@@ -83,8 +83,6 @@
"focus-trap-vue": "1.1.1",
"focus-visible": "^5.2.0",
"glob": "^7.1.6",
- "lodash.isempty": "^4.4.0",
- "lodash.sortby": "^4.7.0",
"node-html-parser": "^5.3.3",
"nuxt": "^2.15.4",
"pinia": "^2.0.13",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 377c364cb..deedf5704 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -66,8 +66,6 @@ specifiers:
jest: ^26.6.3
jest-transform-stub: ^2.0.0
lint-staged: ^12.3.7
- lodash.isempty: ^4.4.0
- lodash.sortby: ^4.7.0
module-alias: ^2.2.2
node-html-parser: ^5.3.3
npm-run-all: ^4.1.5
@@ -121,8 +119,6 @@ dependencies:
focus-trap-vue: [email protected]
focus-visible: 5.2.0
glob: 7.2.0
- lodash.isempty: 4.4.0
- lodash.sortby: 4.7.0
node-html-parser: 5.3.3
@@ -12715,10 +12711,6 @@ packages:
/lodash.debounce/4.0.8:
resolution: {integrity: sha1-gteb/zCmfEAF/9XiUVMArZyk168=}
- /lodash.isempty/4.4.0:
- resolution: {integrity: sha1-b4bL7di+TsmHvpqvM8loTbGzHn4=}
- dev: false
-
/lodash.isplainobject/4.0.6:
resolution: {integrity: sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=}
dev: false
@@ -12738,10 +12730,6 @@ packages:
resolution: {integrity: sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==}
dev: false
- /lodash.sortby/4.7.0:
- resolution: {integrity: sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=}
- dev: false
-
/lodash.template/4.5.0:
resolution: {integrity: sha512-84vYFxIkmidUiFxidA/KjjH9pAycqW+h980j7Fuz5qxRtO9pgB7MDFTdys1N7A5mcucRiDyEq4fusljItR1T/A==}
dependencies:
diff --git a/src/components/VSourcesTable.vue b/src/components/VSourcesTable.vue
index 6962657d9..0ade2fe9c 100644
--- a/src/components/VSourcesTable.vue
+++ b/src/components/VSourcesTable.vue
@@ -57,7 +57,6 @@
</template>
<script lang="ts">
-import sortBy from 'lodash.sortby'
import {
computed,
defineComponent,
@@ -69,6 +68,7 @@ import { useProviderStore } from '~/stores/provider'
import { useGetLocaleFormattedNumber } from '~/composables/use-get-locale-formatted-number'
import type { SupportedMediaType } from '~/constants/media'
+import type { MediaProvider } from '~/models/media-provider'
import TableSortIcon from '~/components/TableSortIcon.vue'
import VLink from '~/components/VLink.vue'
@@ -90,10 +90,10 @@ export default defineComponent({
setup(props) {
const sorting = reactive({
direction: 'asc',
- field: 'display_name',
+ field: 'display_name' as keyof Omit<MediaProvider, 'logo_url'>,
})
- function sortTable(field: string) {
+ function sortTable(field: keyof Omit<MediaProvider, 'logo_url'>) {
let direction = 'asc'
if (field === sorting.field) {
direction = sorting.direction === 'asc' ? 'desc' : 'asc'
@@ -102,6 +102,7 @@ export default defineComponent({
sorting.direction = direction
sorting.field = field
}
+
function cleanSourceUrlForPresentation(url: string) {
const stripProtocol = (s: string) => s.replace(/https?:\/\//, '')
const stripLeadingWww = (s: string) =>
@@ -110,15 +111,28 @@ export default defineComponent({
return removeAfterSlash(stripLeadingWww(stripProtocol(url)))
}
+
const getLocaleFormattedNumber = useGetLocaleFormattedNumber()
const providerStore = useProviderStore()
+ function compareProviders(prov1: MediaProvider, prov2: MediaProvider) {
+ let field1 = prov1[sorting.field]
+ let field2 = prov2[sorting.field]
+ if (sorting.field === 'source_url') {
+ field1 = cleanSourceUrlForPresentation(field1 as string)
+ field2 = cleanSourceUrlForPresentation(field2 as string)
+ }
+ if (field1 > field2) return 1
+ if (field1 < field2) return -1
+ return 0
+ }
+
const sortedProviders = computed(() => {
- const sorted = sortBy(providerStore.providers[props.media], [
- sorting.field,
- ])
- return sorting.direction === 'asc' ? sorted : sorted.reverse()
+ const providers = providerStore.providers[props.media]
+ providers.sort(compareProviders)
+ return sorting.direction === 'asc' ? providers : providers.reverse()
})
+
return {
getLocaleFormattedNumber,
externalLinkIcon,
diff --git a/test/unit/specs/components/v-modal.spec.js b/test/unit/specs/components/v-modal.spec.js
index fdfd162ef..9952f0d43 100644
--- a/test/unit/specs/components/v-modal.spec.js
+++ b/test/unit/specs/components/v-modal.spec.js
@@ -51,6 +51,7 @@ const doOpen = async () => {
// Eventually we'll want to abstract those tests out but for now it's not worth
// the work.
describe('VModal', () => {
+ let options
let _scrollTo
beforeAll(() => {
_scrollTo = window.scrollTo
@@ -61,11 +62,15 @@ describe('VModal', () => {
})
beforeEach(() => {
+ options = {
+ props: { useCustomInitialFocus: false },
+ stubs: ['NuxtLink'],
+ }
jest.resetAllMocks()
})
it('should render a close button that hides the modal', async () => {
- render(TestWrapper)
+ render(TestWrapper, options)
await doOpen()
expect(getDialog()).toBeVisible()
@@ -77,7 +82,7 @@ describe('VModal', () => {
})
it('should scroll lock the body when modal opens and unlock when modal closes', async () => {
- const { container } = render(TestWrapper)
+ const { container } = render(TestWrapper, options)
const scrollY = 10
window.scrollY = scrollY
await doOpen()
@@ -94,7 +99,7 @@ describe('VModal', () => {
})
it('should focus the close button by default', async () => {
- render(TestWrapper)
+ render(TestWrapper, options)
await doOpen()
expect(getDialog()).toBeVisible()
@@ -102,7 +107,8 @@ describe('VModal', () => {
})
it('should focus the explicit initial focus element when specified', async () => {
- render(TestWrapper, { props: { useCustomInitialFocus: true } })
+ options.props = { useCustomInitialFocus: true }
+ render(TestWrapper, options)
await doOpen()
expect(getDialog()).toBeVisible()
diff --git a/test/unit/specs/components/v-sources-table.spec.js b/test/unit/specs/components/v-sources-table.spec.js
new file mode 100644
index 000000000..7fbba4cfb
--- /dev/null
+++ b/test/unit/specs/components/v-sources-table.spec.js
@@ -0,0 +1,123 @@
+import { render, screen } from '@testing-library/vue'
+import userEvent from '@testing-library/user-event'
+import { PiniaVuePlugin } from 'pinia'
+import { createTestingPinia } from '@pinia/testing'
+import VueI18n from 'vue-i18n'
+
+import VSourcesTableVue from '~/components/VSourcesTable.vue'
+
+const enMessages = require('~/locales/en.json')
+
+const i18n = new VueI18n({
+ locale: 'en',
+ fallbackLocale: 'en',
+ messages: { en: enMessages },
+})
+
+const useMockStore = (vue) => {
+ vue.use(PiniaVuePlugin)
+ const pinia = createTestingPinia({
+ initialState: {
+ provider: {
+ providers: {
+ image: [
+ {
+ source_name: 'Provider_B',
+ display_name: 'Provider B',
+ source_url: 'http://yyy.com',
+ media_count: 1111,
+ },
+ {
+ source_name: 'Provider_C',
+ display_name: 'Provider C',
+ source_url: 'www.xxx.com',
+ media_count: 2222,
+ },
+ {
+ source_name: 'Provider_A',
+ display_name: 'Provider A',
+ source_url: 'https://zzz.com',
+ media_count: 3333,
+ },
+ ],
+ },
+ },
+ },
+ })
+ return { pinia }
+}
+
+const configureVue = (vue) => {
+ const { pinia } = useMockStore(vue)
+ vue.use(VueI18n)
+
+ return {
+ i18n,
+ pinia,
+ /** @todo Create a better mock that can be configured to test this behavior. */
+ }
+}
+
+const getTableData = (table) => {
+ const data = [...table.rows].map((t) =>
+ [...t.children].map((u) => u.textContent.trim())
+ )
+ // Remove the header row
+ data.shift()
+ return data
+}
+
+describe('VSourcesTable', () => {
+ let options
+
+ beforeEach(() => {
+ options = {
+ propsData: { media: 'image' },
+ stubs: ['TableSortIcon', 'VLink'],
+ mocks: {
+ $nuxt: { context: { i18n } },
+ },
+ }
+ })
+
+ it('should be sorted by display_name ("Source") by default', () => {
+ render(VSourcesTableVue, options, configureVue)
+ const table = getTableData(screen.getByLabelText('sources.aria.table'))
+ const expectedTable = [
+ ['Provider A', 'zzz.com', '3,333'],
+ ['Provider B', 'yyy.com', '1,111'],
+ ['Provider C', 'xxx.com', '2,222'],
+ ]
+ expect(table).toEqual(expectedTable)
+ })
+
+ it('should be sorted by clean url when click on "Domain" header', async () => {
+ render(VSourcesTableVue, options, configureVue)
+ const domainCell = screen.getByRole('columnheader', {
+ name: 'sources.providers.domain',
+ })
+ await userEvent.click(domainCell)
+ const table = getTableData(screen.getByLabelText('sources.aria.table'))
+ const expectedTable = [
+ ['Provider C', 'xxx.com', '2,222'],
+ ['Provider B', 'yyy.com', '1,111'],
+ ['Provider A', 'zzz.com', '3,333'],
+ ]
+ expect(table).toEqual(expectedTable)
+ })
+
+ it('should be sorted by media_count when click on "Total items" header', async () => {
+ render(VSourcesTableVue, options, configureVue)
+ const domainCell = screen.getByRole('columnheader', {
+ name: 'sources.providers.item',
+ })
+ await userEvent.click(domainCell)
+ const table = getTableData(screen.getByLabelText('sources.aria.table'))
+ const expectedTable = [
+ ['Provider B', 'yyy.com', '1,111'],
+ ['Provider C', 'xxx.com', '2,222'],
+ ['Provider A', 'zzz.com', '3,333'],
+ ]
+ expect(table).toEqual(expectedTable)
+ })
+})