test: update unit tests for SortDropdown and Uploading components to handle edge cases and clarify upload success criteria

This commit is contained in:
CodingOnStar 2025-12-29 16:55:30 +08:00
parent b3e60f9eda
commit 678a40d462
3 changed files with 24 additions and 12 deletions

View File

@ -172,7 +172,13 @@ describe('Uploading', () => {
})
})
it('should call onPackageUploaded when package upload succeeds (no error message)', async () => {
// NOTE: The uploadFile API has an unconventional contract where it always rejects.
// Success vs failure is determined by whether response.message exists:
// - If response.message exists → treated as failure (calls onFailed)
// - If response.message is absent → treated as success (calls onPackageUploaded/onBundleUploaded)
// This explains why we use mockRejectedValue for "success" scenarios below.
it('should call onPackageUploaded when upload rejects without error message (success case)', async () => {
const mockResult = {
unique_identifier: 'test-uid',
manifest: createMockManifest(),
@ -198,7 +204,7 @@ describe('Uploading', () => {
})
})
it('should call onBundleUploaded when bundle upload succeeds (no error message)', async () => {
it('should call onBundleUploaded when upload rejects without error message (success case)', async () => {
const mockDependencies = createMockDependencies()
mockUploadFile.mockRejectedValue({
response: mockDependencies,

View File

@ -452,25 +452,31 @@ describe('SortDropdown', () => {
// Edge Cases Tests
// ================================
describe('Edge Cases', () => {
it('should handle unknown sortBy value gracefully', () => {
// The component falls back to the first option (Most Popular) when sort values are invalid
it('should fallback to default option when sortBy is unknown', () => {
mockSort = { sortBy: 'unknown_field', sortOrder: 'DESC' }
// This may cause an error or undefined behavior
// Component uses find() which returns undefined for non-matching
expect(() => render(<SortDropdown />)).toThrow()
render(<SortDropdown />)
// Should fallback to first option "Most Popular"
expect(screen.getByText('Most Popular')).toBeInTheDocument()
})
it('should handle empty sortBy value', () => {
it('should fallback to default option when sortBy is empty', () => {
mockSort = { sortBy: '', sortOrder: 'DESC' }
expect(() => render(<SortDropdown />)).toThrow()
render(<SortDropdown />)
expect(screen.getByText('Most Popular')).toBeInTheDocument()
})
it('should handle unknown sortOrder value', () => {
it('should fallback to default option when sortOrder is unknown', () => {
mockSort = { sortBy: 'install_count', sortOrder: 'UNKNOWN' }
// No matching option, selectedOption will be undefined
expect(() => render(<SortDropdown />)).toThrow()
render(<SortDropdown />)
expect(screen.getByText('Most Popular')).toBeInTheDocument()
})
it('should render correctly when handleSortChange is a no-op', () => {

View File

@ -44,7 +44,7 @@ const SortDropdown = ({
const sort = useMarketplaceContext(v => v.sort)
const handleSortChange = useMarketplaceContext(v => v.handleSortChange)
const [open, setOpen] = useState(false)
const selectedOption = options.find(option => option.value === sort.sortBy && option.order === sort.sortOrder)!
const selectedOption = options.find(option => option.value === sort.sortBy && option.order === sort.sortOrder) ?? options[0]
return (
<PortalToFollowElem