-
-
Notifications
You must be signed in to change notification settings - Fork 400
fix: only show stars/likes in package OG image when there are any #1215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f2ea817
test: unskip OG image tests
serhalp 3c57caa
fix: only show stars/likes in OG image when there are any
serhalp 56f3d5c
docs: add a todo about OG image e2e test
serhalp 79d0e4c
chore: set a sensible playwright snapshot path template
serhalp 25da402
test: add component tests for OgImagePackage
serhalp dd8cdff
docs: add a comment about the zero fallbacks for stars and such
serhalp 78d99c5
Merge branch 'main' into fix/1212
serhalp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.3 KB
test/e2e/og-image.spec.ts-snapshots/og-image-for--package-nuxt-v-3-20-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { mockNuxtImport, mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime' | ||
| import { describe, expect, it, vi, beforeEach } from 'vitest' | ||
|
|
||
| const { mockUseResolvedVersion, mockUsePackageDownloads, mockUsePackage, mockUseRepoMeta } = | ||
| vi.hoisted(() => ({ | ||
| mockUseResolvedVersion: vi.fn(), | ||
| mockUsePackageDownloads: vi.fn(), | ||
| mockUsePackage: vi.fn(), | ||
| mockUseRepoMeta: vi.fn(), | ||
| })) | ||
|
|
||
| mockNuxtImport('useResolvedVersion', () => mockUseResolvedVersion) | ||
| mockNuxtImport('usePackageDownloads', () => mockUsePackageDownloads) | ||
| mockNuxtImport('usePackage', () => mockUsePackage) | ||
| mockNuxtImport('useRepoMeta', () => mockUseRepoMeta) | ||
| mockNuxtImport('normalizeGitUrl', () => () => 'https://github.com/test/repo') | ||
|
|
||
| import OgImagePackage from '~/components/OgImage/Package.vue' | ||
|
|
||
| describe('OgImagePackage', () => { | ||
| const baseProps = { | ||
| name: 'test-package', | ||
| version: '1.0.0', | ||
| } | ||
|
|
||
| function setupMocks( | ||
| overrides: { | ||
| stars?: number | ||
| totalLikes?: number | ||
| downloads?: number | null | ||
| license?: string | null | ||
| packageName?: string | ||
| } = {}, | ||
| ) { | ||
| const { | ||
| stars = 0, | ||
| totalLikes = 0, | ||
| downloads = 1000, | ||
| license = 'MIT', | ||
| packageName = 'test-package', | ||
| } = overrides | ||
|
|
||
| mockUseResolvedVersion.mockReturnValue({ | ||
| data: ref('1.0.0'), | ||
| status: ref('success'), | ||
| error: ref(null), | ||
| }) | ||
|
|
||
| mockUsePackageDownloads.mockReturnValue({ | ||
| data: downloads != null ? ref({ downloads }) : ref(null), | ||
| refresh: vi.fn().mockResolvedValue(undefined), | ||
| }) | ||
|
|
||
| mockUsePackage.mockReturnValue({ | ||
| data: ref({ | ||
| name: packageName, | ||
| license, | ||
| requestedVersion: { | ||
| repository: { url: 'git+https://github.com/test/repo.git' }, | ||
| }, | ||
| }), | ||
| refresh: vi.fn().mockResolvedValue(undefined), | ||
| }) | ||
|
|
||
| mockUseRepoMeta.mockReturnValue({ | ||
| stars: computed(() => stars), | ||
| refresh: vi.fn().mockResolvedValue(undefined), | ||
| }) | ||
|
|
||
| // Mock the likes API endpoint used by useFetch | ||
| registerEndpoint(`/api/social/likes/${packageName}`, () => ({ | ||
| totalLikes, | ||
| userHasLiked: false, | ||
| })) | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| mockUseResolvedVersion.mockReset() | ||
| mockUsePackageDownloads.mockReset() | ||
| mockUsePackage.mockReset() | ||
| mockUseRepoMeta.mockReset() | ||
| }) | ||
|
|
||
| it('renders the package name and version', async () => { | ||
| setupMocks({ packageName: 'vue' }) | ||
|
|
||
| const component = await mountSuspended(OgImagePackage, { | ||
| props: { ...baseProps, name: 'vue' }, | ||
| }) | ||
|
|
||
| expect(component.text()).toContain('vue') | ||
| expect(component.text()).toContain('1.0.0') | ||
| }) | ||
|
|
||
| describe('license', () => { | ||
| it('renders the license when present', async () => { | ||
| setupMocks({ license: 'MIT' }) | ||
|
|
||
| const component = await mountSuspended(OgImagePackage, { | ||
| props: baseProps, | ||
| }) | ||
|
|
||
| expect(component.text()).toContain('MIT') | ||
| }) | ||
|
|
||
| it('hides the license section when license is missing', async () => { | ||
| setupMocks({ license: null }) | ||
|
|
||
| const component = await mountSuspended(OgImagePackage, { | ||
| props: baseProps, | ||
| }) | ||
|
|
||
| expect(component.find('[data-testid="license"]').exists()).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('stars', () => { | ||
| it('hides stars section when count is 0', async () => { | ||
| setupMocks({ stars: 0 }) | ||
|
|
||
| const component = await mountSuspended(OgImagePackage, { | ||
| props: baseProps, | ||
| }) | ||
|
|
||
| expect(component.find('[data-testid="stars"]').exists()).toBe(false) | ||
| }) | ||
|
|
||
| it('shows formatted stars when count is positive', async () => { | ||
| setupMocks({ stars: 45200 }) | ||
|
|
||
| const component = await mountSuspended(OgImagePackage, { | ||
| props: baseProps, | ||
| }) | ||
|
|
||
| expect(component.text()).toContain('45.2K') | ||
| }) | ||
| }) | ||
|
|
||
| describe('likes', () => { | ||
| it('hides likes section when totalLikes is 0', async () => { | ||
| setupMocks({ totalLikes: 0 }) | ||
|
|
||
| const component = await mountSuspended(OgImagePackage, { | ||
| props: baseProps, | ||
| }) | ||
|
|
||
| expect(component.find('[data-testid="likes"]').exists()).toBe(false) | ||
| }) | ||
|
|
||
| it('shows likes section when totalLikes is positive', async () => { | ||
| setupMocks({ totalLikes: 42 }) | ||
|
|
||
| const component = await mountSuspended(OgImagePackage, { | ||
| props: baseProps, | ||
| }) | ||
|
|
||
| expect(component.text()).toContain('42') | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.