ProjectBranchSelector.utils.test.ts85 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { getProjectBranchSelectorState } from './ProjectBranchSelector.utils' |
| 4 | |
| 5 | describe('getProjectBranchSelectorState', () => { |
| 6 | it('returns main branch when branching disabled', () => { |
| 7 | const result = getProjectBranchSelectorState({ |
| 8 | selectedBranch: undefined, |
| 9 | isBranchingEnabled: false, |
| 10 | selectedOrganization: undefined, |
| 11 | }) |
| 12 | expect(result.isMainBranch).toBe(true) |
| 13 | expect(result.branchDisplayName).toBe('main') |
| 14 | }) |
| 15 | |
| 16 | it('returns main when branching enabled and selected branch is default', () => { |
| 17 | const result = getProjectBranchSelectorState({ |
| 18 | selectedBranch: { |
| 19 | id: '1', |
| 20 | name: 'main', |
| 21 | project_ref: 'ref', |
| 22 | is_default: true, |
| 23 | created_at: '', |
| 24 | } as any, |
| 25 | isBranchingEnabled: true, |
| 26 | selectedOrganization: undefined, |
| 27 | }) |
| 28 | expect(result.isMainBranch).toBe(true) |
| 29 | expect(result.branchDisplayName).toBe('main') |
| 30 | }) |
| 31 | |
| 32 | it('returns non-main when branching enabled and selected branch is not default', () => { |
| 33 | const result = getProjectBranchSelectorState({ |
| 34 | selectedBranch: { |
| 35 | id: '2', |
| 36 | name: 'feature-x', |
| 37 | project_ref: 'ref-2', |
| 38 | is_default: false, |
| 39 | created_at: '', |
| 40 | } as any, |
| 41 | isBranchingEnabled: true, |
| 42 | selectedOrganization: undefined, |
| 43 | }) |
| 44 | expect(result.isMainBranch).toBe(false) |
| 45 | expect(result.branchDisplayName).toBe('feature-x') |
| 46 | }) |
| 47 | |
| 48 | it('returns main when selected branch undefined and branching enabled', () => { |
| 49 | const result = getProjectBranchSelectorState({ |
| 50 | selectedBranch: undefined, |
| 51 | isBranchingEnabled: true, |
| 52 | selectedOrganization: undefined, |
| 53 | }) |
| 54 | expect(result.branchDisplayName).toBe('main') |
| 55 | }) |
| 56 | |
| 57 | it('returns organization href when org has slug', () => { |
| 58 | const result = getProjectBranchSelectorState({ |
| 59 | selectedBranch: undefined, |
| 60 | isBranchingEnabled: false, |
| 61 | selectedOrganization: { slug: 'my-org', name: 'My Org' } as any, |
| 62 | }) |
| 63 | expect(result.organizationHref).toBe('/org/my-org') |
| 64 | expect(result.selectedOrgInitial).toBe('M') |
| 65 | }) |
| 66 | |
| 67 | it('returns organizations fallback when no org slug', () => { |
| 68 | const result = getProjectBranchSelectorState({ |
| 69 | selectedBranch: undefined, |
| 70 | isBranchingEnabled: false, |
| 71 | selectedOrganization: undefined, |
| 72 | }) |
| 73 | expect(result.organizationHref).toBe('/organizations') |
| 74 | expect(result.selectedOrgInitial).toBe('O') |
| 75 | }) |
| 76 | |
| 77 | it('handles org name with leading/trailing spaces for initial', () => { |
| 78 | const result = getProjectBranchSelectorState({ |
| 79 | selectedBranch: undefined, |
| 80 | isBranchingEnabled: false, |
| 81 | selectedOrganization: { slug: 'x', name: ' Acme ' } as any, |
| 82 | }) |
| 83 | expect(result.selectedOrgInitial).toBe('A') |
| 84 | }) |
| 85 | }) |