Skip to content

Commit 1466e8b

Browse files
committed
test: encapsute similar filter lambda logic
1 parent 9a72c6b commit 1466e8b

1 file changed

Lines changed: 22 additions & 66 deletions

File tree

test/nuxt/components/PackageVersions.spec.ts

Lines changed: 22 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it, vi, beforeEach } from 'vitest'
22
import { mountSuspended } from '@nuxt/test-utils/runtime'
3+
import type { DOMWrapper } from '@vue/test-utils'
34
import PackageVersions from '~/components/Package/Versions.vue'
45

56
// Mock the fetchAllPackageVersions function
@@ -26,6 +27,18 @@ function createVersion(
2627
} as SlimVersion
2728
}
2829

30+
/**
31+
* Predicate for filtering anchor elements to version links only,
32+
* excluding anchor links, external links, and action buttons.
33+
*/
34+
function isVersionLink(a: DOMWrapper<Element>): boolean {
35+
return (
36+
!a.attributes('href')?.startsWith('#') &&
37+
a.attributes('target') !== '_blank' &&
38+
!a.attributes('data-testid')?.includes('view-all-versions')
39+
)
40+
}
41+
2942
describe('PackageVersions', () => {
3043
beforeEach(() => {
3144
mockFetchAllPackageVersions.mockReset()
@@ -73,14 +86,7 @@ describe('PackageVersions', () => {
7386
})
7487

7588
// Find version links (exclude anchor links, external links, and action buttons)
76-
const versionLinks = component
77-
.findAll('a')
78-
.filter(
79-
a =>
80-
!a.attributes('href')?.startsWith('#') &&
81-
a.attributes('target') !== '_blank' &&
82-
!a.attributes('data-testid')?.includes('view-all-versions'),
83-
)
89+
const versionLinks = component.findAll('a').filter(isVersionLink)
8490
expect(versionLinks.length).toBeGreaterThan(0)
8591
expect(versionLinks[0]?.text()).toBe('2.0.0')
8692
})
@@ -98,14 +104,7 @@ describe('PackageVersions', () => {
98104
})
99105

100106
// Find version links (exclude anchor links, external links, and action buttons)
101-
const versionLinks = component
102-
.findAll('a')
103-
.filter(
104-
a =>
105-
!a.attributes('href')?.startsWith('#') &&
106-
a.attributes('target') !== '_blank' &&
107-
!a.attributes('data-testid')?.includes('view-all-versions'),
108-
)
107+
const versionLinks = component.findAll('a').filter(isVersionLink)
109108
expect(versionLinks.length).toBeGreaterThan(0)
110109
expect(versionLinks[0]?.text()).toBe('1.0.0')
111110
})
@@ -242,14 +241,7 @@ describe('PackageVersions', () => {
242241
})
243242

244243
// Find version links (exclude anchor links that start with # and external links)
245-
const versionLinks = component
246-
.findAll('a')
247-
.filter(
248-
a =>
249-
!a.attributes('href')?.startsWith('#') &&
250-
a.attributes('target') !== '_blank' &&
251-
!a.attributes('data-testid')?.includes('view-all-versions'),
252-
)
244+
const versionLinks = component.findAll('a').filter(isVersionLink)
253245
const versions = versionLinks.map(l => l.text())
254246
// Should be sorted by version descending
255247
expect(versions[0]).toBe('2.0.0')
@@ -270,14 +262,7 @@ describe('PackageVersions', () => {
270262
})
271263

272264
// Find version links (exclude anchor links that start with # and external links)
273-
const versionLinks = component
274-
.findAll('a')
275-
.filter(
276-
a =>
277-
!a.attributes('href')?.startsWith('#') &&
278-
a.attributes('target') !== '_blank' &&
279-
!a.attributes('data-testid')?.includes('view-all-versions'),
280-
)
265+
const versionLinks = component.findAll('a').filter(isVersionLink)
281266
expect(versionLinks.length).toBeGreaterThan(0)
282267
expect(versionLinks[0]?.classes()).toContain('text-red-800')
283268
})
@@ -295,14 +280,7 @@ describe('PackageVersions', () => {
295280
})
296281

297282
// Find version links (exclude anchor links that start with # and external links)
298-
const versionLinks = component
299-
.findAll('a')
300-
.filter(
301-
a =>
302-
!a.attributes('href')?.startsWith('#') &&
303-
a.attributes('target') !== '_blank' &&
304-
!a.attributes('data-testid')?.includes('view-all-versions'),
305-
)
283+
const versionLinks = component.findAll('a').filter(isVersionLink)
306284
expect(versionLinks.length).toBeGreaterThan(0)
307285
expect(versionLinks[0]?.attributes('title')).toContain('deprecated')
308286
})
@@ -632,14 +610,7 @@ describe('PackageVersions', () => {
632610
})
633611

634612
// Count visible version links (excluding anchor links that start with # and external links)
635-
const visibleLinks = component
636-
.findAll('a')
637-
.filter(
638-
a =>
639-
!a.attributes('href')?.startsWith('#') &&
640-
a.attributes('target') !== '_blank' &&
641-
!a.attributes('data-testid')?.includes('view-all-versions'),
642-
)
613+
const visibleLinks = component.findAll('a').filter(isVersionLink)
643614
// Should have max 10 visible links in the main section
644615
expect(visibleLinks.length).toBeLessThanOrEqual(10)
645616
})
@@ -1043,14 +1014,7 @@ describe('PackageVersions', () => {
10431014
expect(text).toContain('2.1.0')
10441015
// 3.0.0 does NOT match ^2.0.0
10451016
// Find version links (exclude anchor and external links)
1046-
const versionLinks = component
1047-
.findAll('a')
1048-
.filter(
1049-
a =>
1050-
!a.attributes('href')?.startsWith('#') &&
1051-
a.attributes('target') !== '_blank' &&
1052-
!a.attributes('data-testid')?.includes('view-all-versions'),
1053-
)
1017+
const versionLinks = component.findAll('a').filter(isVersionLink)
10541018
const versions = versionLinks.map(l => l.text())
10551019
expect(versions).not.toContain('3.0.0')
10561020
})
@@ -1151,11 +1115,7 @@ describe('PackageVersions', () => {
11511115

11521116
// 2.0.0 should not appear in the expanded list
11531117
await vi.waitFor(() => {
1154-
const versionLinks = component
1155-
.findAll('a')
1156-
.filter(
1157-
a => !a.attributes('href')?.startsWith('#') && a.attributes('target') !== '_blank',
1158-
)
1118+
const versionLinks = component.findAll('a').filter(isVersionLink)
11591119
const versions = versionLinks.map(l => l.text())
11601120
expect(versions).not.toContain('2.0.0')
11611121
})
@@ -1193,11 +1153,7 @@ describe('PackageVersions', () => {
11931153

11941154
// After loading, 3.4.0 should appear as an auto-expanded child of the latest tag
11951155
await vi.waitFor(() => {
1196-
const versionLinks = component
1197-
.findAll('a')
1198-
.filter(
1199-
a => !a.attributes('href')?.startsWith('#') && a.attributes('target') !== '_blank',
1200-
)
1156+
const versionLinks = component.findAll('a').filter(isVersionLink)
12011157
const versions = versionLinks.map(l => l.text())
12021158
expect(versions).toContain('3.4.0')
12031159
})

0 commit comments

Comments
 (0)