Skip to content
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

chore: Add unit tests for utils #1166

Merged
merged 26 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ccab62a
Added few tests and removed duplicate util impl
vardanbansal-harness Feb 26, 2025
39a760f
cleanup
vardanbansal-harness Feb 27, 2025
b999946
remove .only
vardanbansal-harness Feb 27, 2025
3407915
removing duplicate util function
vardanbansal-harness Feb 27, 2025
f85fb3b
fix vitest setup for apps/gitness
vardanbansal-harness Feb 27, 2025
48d83c1
cleanup
vardanbansal-harness Feb 27, 2025
78417cc
fix vitest
vardanbansal-harness Feb 27, 2025
5c2fb7c
resolve vitest config
vardanbansal-harness Feb 27, 2025
3bcc9eb
fix vitest config
vardanbansal-harness Feb 27, 2025
e3b24e6
add failing test
vardanbansal-harness Feb 27, 2025
40cac08
feat: add more tests in utils folder- part 2
cjlee1 Feb 28, 2025
1b74c45
feat: add more tests in utils folder- part 3
cjlee1 Feb 28, 2025
9b05b8f
cleanup
vardanbansal-harness Feb 28, 2025
323d18f
cleanup - remove vitest imports
vardanbansal-harness Feb 28, 2025
1438ef2
cleanup - remove vitest imports
vardanbansal-harness Feb 28, 2025
f216e4f
cleanup - remove unused time mocks
vardanbansal-harness Feb 28, 2025
4a6ddfa
cleanup - remove time mock
vardanbansal-harness Feb 28, 2025
fdbbdb1
skipping file till fixed
vardanbansal-harness Feb 28, 2025
07d5394
fix test compilation issue on CI
vardanbansal-harness Feb 28, 2025
bbe5dad
cleanup
vardanbansal-harness Feb 28, 2025
d011eca
fix vitest config
vardanbansal-harness Feb 28, 2025
f9c2988
cleanup vitest configs
vardanbansal-harness Feb 28, 2025
3665e4c
add unoptimised build step before running tests
abhinavrastogi-harness Feb 28, 2025
84e9d0a
Merge branch 'main' into add-uts-for-utils
vardanbansal-harness Feb 28, 2025
3b7d378
cleanup
vardanbansal-harness Feb 28, 2025
b325b50
removing unrelated change
vardanbansal-harness Feb 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/gitness/config/vitest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { vi } from 'vitest'

Object.defineProperty(document, 'queryCommandSupported', {
value: vi.fn().mockImplementation((command: string) => {
return command === 'copy' || command === 'cut'
}),
writable: true
})
2 changes: 1 addition & 1 deletion apps/gitness/src/components/GitBlame.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect, useState } from 'react'

import { useGetBlameQuery } from '@harnessio/code-service-client'
import { getInitials } from '@harnessio/ui/utils'
import { BlameEditor, BlameEditorProps, ThemeDefinition } from '@harnessio/yaml-editor'
import { BlameItem } from '@harnessio/yaml-editor/dist/types/blame'

import { useGetRepoRef } from '../framework/hooks/useGetRepoPath'
import useCodePathDetails from '../hooks/useCodePathDetails'
import { timeAgoFromISOTime } from '../pages/pipeline-edit/utils/time-utils'
import { getInitials } from '../utils/common-utils'
import { normalizeGitRef } from '../utils/git-utils'

interface GitBlameProps {
Expand Down
144 changes: 144 additions & 0 deletions apps/gitness/src/utils/__tests__/common-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { SummaryItemType, type RepoFile } from '@harnessio/ui/views'

import { getLogsText, sortFilesByType } from '../common-utils'

// Mock data for testing
const mockLogs = [{ out: 'Log line 1\n' }, { out: 'Log line 2\n' }, { out: 'Log line 3\n' }]

const mockFiles: RepoFile[] = [
{
name: 'file1.txt',
type: SummaryItemType.File,
id: '1',
path: 'file1.txt',
lastCommitMessage: 'file1 commit',
timestamp: '2021-09-01T00:00:00Z'
},
{
name: 'folder1',
type: SummaryItemType.Folder,
id: '2',
path: 'folder1',
lastCommitMessage: 'folder1 commit',
timestamp: '2021-09-01T00:00:00Z'
},
{
name: 'file2.txt',
type: SummaryItemType.File,
id: '3',
path: 'file2.txt',
lastCommitMessage: 'file2 commit',
timestamp: '2021-09-01T00:00:00Z'
},
{
name: 'folder2',
type: SummaryItemType.Folder,
id: '4',
path: 'folder2',
lastCommitMessage: 'folder2 commit',
timestamp: '2021-09-01T00:00:00Z'
}
]

describe('getLogsText', () => {
it('should concatenate log lines into a single string', () => {
const result = getLogsText(mockLogs)
expect(result).toBe('Log line 1\nLog line 2\nLog line 3\n')
})

it('should return an empty string if logs array is empty', () => {
const result = getLogsText([])
expect(result).toBe('')
})
})

describe('sortFilesByType', () => {
it('should sort files by type, with folders first', () => {
const result = sortFilesByType(mockFiles)
expect(result).toEqual([
{
name: 'folder1',
type: SummaryItemType.Folder,
id: '2',
path: 'folder1',
lastCommitMessage: 'folder1 commit',
timestamp: '2021-09-01T00:00:00Z'
},
{
name: 'folder2',
type: SummaryItemType.Folder,
id: '4',
path: 'folder2',
lastCommitMessage: 'folder2 commit',
timestamp: '2021-09-01T00:00:00Z'
},
{
name: 'file1.txt',
type: SummaryItemType.File,
id: '1',
path: 'file1.txt',
lastCommitMessage: 'file1 commit',
timestamp: '2021-09-01T00:00:00Z'
},
{
name: 'file2.txt',
type: SummaryItemType.File,
id: '3',
path: 'file2.txt',
lastCommitMessage: 'file2 commit',
timestamp: '2021-09-01T00:00:00Z'
}
])
})

it('should handle an empty array', () => {
const result = sortFilesByType([])
expect(result).toEqual([])
})

it('should handle an array with only files', () => {
const filesOnly = [
{
name: 'file1.txt',
type: SummaryItemType.File,
id: '1',
path: 'file1.txt',
lastCommitMessage: 'file1 commit',
timestamp: '2021-09-01T00:00:00Z'
},
{
name: 'file2.txt',
type: SummaryItemType.File,
id: '2',
path: 'file2.txt',
lastCommitMessage: 'file2 commit',
timestamp: '2021-10-01T00:00:00Z'
}
]
const result = sortFilesByType(filesOnly)
expect(result).toEqual(filesOnly)
})

it('should handle an array with only folders', () => {
const foldersOnly = [
{
name: 'folder1',
type: SummaryItemType.Folder,
id: '2',
path: 'folder1',
lastCommitMessage: 'folder1 commit',
timestamp: '2021-09-01T00:00:00Z'
},
{
name: 'folder2',
type: SummaryItemType.Folder,
id: '2',
path: 'folder2',
lastCommitMessage: 'folder2 commit',
timestamp: '2021-10-01T00:00:00Z'
}
]
const result = sortFilesByType(foldersOnly)
expect(result).toEqual(foldersOnly)
})
})
40 changes: 40 additions & 0 deletions apps/gitness/src/utils/__tests__/error-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getErrorMessage } from '../error-utils'

// Define a custom error type for testing
interface CustomError {
data?: {
error?: string
message?: string
}
message?: string
}

describe('getErrorMessage', () => {
it('should handle undefined error', () => {
expect(getErrorMessage(undefined)).toBeUndefined()
})

it('should handle null error', () => {
expect(getErrorMessage(null)).toBeUndefined()
})

it('should extract error from data.error', () => {
const error: CustomError = { data: { error: 'Test error message' } }
expect(getErrorMessage(error)).toBe('Test error message')
})

it('should extract error from data.message', () => {
const error: CustomError = { data: { message: 'Test message' } }
expect(getErrorMessage(error)).toBe('Test message')
})

it('should extract error from message', () => {
const error: CustomError = { message: 'Direct message' }
expect(getErrorMessage(error)).toBe('Direct message')
})

it('should return error as string if no structured format is found', () => {
const error = 'Plain error string'
expect(getErrorMessage(error)).toBe('Plain error string')
})
})
121 changes: 121 additions & 0 deletions apps/gitness/src/utils/__tests__/execution-utils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { EnumCiStatus, TypesExecution } from '@harnessio/code-service-client'
import { MeterState } from '@harnessio/ui/components'
import { PipelineExecutionStatus } from '@harnessio/ui/views'

import { getExecutionStatus, getLabel, getMeterState } from '../execution-utils'

describe('getLabel', () => {
it('should return empty string for missing author_name or event', () => {
const baseExecution: TypesExecution = {
number: 1,
status: 'success' as EnumCiStatus,
created: 0,
updated: 0,
started: 0,
finished: 0
}

expect(getLabel({ ...baseExecution, author_name: '', event: undefined })).toBe('')
expect(getLabel({ ...baseExecution, author_name: 'test', event: undefined })).toBe('')
expect(getLabel({ ...baseExecution, author_name: '', event: 'manual' })).toBe('')
})

it('should handle manual event', () => {
const execution: TypesExecution = {
author_name: 'John Doe',
event: 'manual',
number: 1,
status: 'success' as EnumCiStatus,
created: 0,
updated: 0,
started: 0,
finished: 0
}
expect(getLabel(execution)).toBe('John Doe triggered manually')
})

it('should handle pull_request event with source and target', () => {
const execution: TypesExecution = {
author_name: 'John Doe',
event: 'pull_request',
source: 'feature',
target: 'main',
number: 1,
status: 'success' as EnumCiStatus,
created: 0,
updated: 0,
started: 0,
finished: 0
}
const result = getLabel(execution)
expect(result).toBeTruthy() // Since it returns a React element, we just verify it's not null
})
})
describe('getExecutionStatus', () => {
it('should map running status correctly', () => {
expect(getExecutionStatus('running')).toBe(PipelineExecutionStatus.RUNNING)
})

it('should map success status correctly', () => {
expect(getExecutionStatus('success')).toBe(PipelineExecutionStatus.SUCCESS)
})

it('should map failure status correctly', () => {
expect(getExecutionStatus('failure')).toBe(PipelineExecutionStatus.FAILURE)
})

it('should map error status correctly', () => {
expect(getExecutionStatus('error')).toBe(PipelineExecutionStatus.ERROR)
})

it('should map killed status correctly', () => {
expect(getExecutionStatus('killed')).toBe(PipelineExecutionStatus.KILLED)
})

it('should return UNKNOWN for undefined status', () => {
expect(getExecutionStatus(undefined)).toBe(PipelineExecutionStatus.UNKNOWN)
})

it('should return UNKNOWN for invalid status', () => {
const invalidStatus = 'invalid' as EnumCiStatus
expect(getExecutionStatus(invalidStatus)).toBe(PipelineExecutionStatus.UNKNOWN)
})
})

describe('getMeterState', () => {
it('should return Error state for failure status', () => {
expect(getMeterState(PipelineExecutionStatus.FAILURE)).toBe(MeterState.Error)
})

it('should return Error state for killed status', () => {
expect(getMeterState(PipelineExecutionStatus.KILLED)).toBe(MeterState.Error)
})

it('should return Error state for error status', () => {
expect(getMeterState(PipelineExecutionStatus.ERROR)).toBe(MeterState.Error)
})

it('should return Success state for success status', () => {
expect(getMeterState(PipelineExecutionStatus.SUCCESS)).toBe(MeterState.Success)
})

it('should return Warning state for skipped status', () => {
expect(getMeterState(PipelineExecutionStatus.SKIPPED)).toBe(MeterState.Warning)
})

it('should return Warning state for blocked status', () => {
expect(getMeterState(PipelineExecutionStatus.BLOCKED)).toBe(MeterState.Warning)
})

it('should return Empty state for pending status', () => {
expect(getMeterState(PipelineExecutionStatus.PENDING)).toBe(MeterState.Empty)
})

it('should return Empty state for waiting on dependencies status', () => {
expect(getMeterState(PipelineExecutionStatus.WAITING_ON_DEPENDENCIES)).toBe(MeterState.Empty)
})

it('should return Empty state for undefined status', () => {
expect(getMeterState(undefined)).toBe(MeterState.Empty)
})
})
Loading