Skip to content

Commit

Permalink
chore: Shared JS/TS Conversions Part 3 (#3500)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-sentry authored Nov 14, 2024
1 parent fe14916 commit f461fa7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 138 deletions.
1 change: 0 additions & 1 deletion src/services/pull/usePull.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ describe('usePull', () => {
await waitFor(() =>
expect(result.current.data).toEqual({
defaultBranch: 'main',
hasAccess: true,
pull: {
behindBy: 82367894,
behindByCommit: '1798hvs8ofhn',
Expand Down
5 changes: 0 additions & 5 deletions src/services/pull/usePull.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
import Api from 'shared/api'
import { type NetworkErrorObject } from 'shared/api/helpers'
import { UploadTypeEnum } from 'shared/utils/commit'
import { userHasAccess } from 'shared/utils/user'
import A from 'ui/A'

import { PullCompareWithBaseFragment } from './fragments'
Expand Down Expand Up @@ -372,10 +371,6 @@ export function usePull({
pull: {
...pull,
},
hasAccess: userHasAccess({
privateRepo: data?.owner?.repository?.private,
isCurrentUserPartOfOrg: data?.owner?.isCurrentUserPartOfOrg,
}),
defaultBranch: data?.owner?.repository?.defaultBranch,
}
}),
Expand Down
92 changes: 0 additions & 92 deletions src/shared/utils/dates.test.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/shared/utils/dates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { formatTimeFromSeconds, formatTimeToNow } from './dates'

describe('formatTimeToNow', () => {
it('returns null when date is null', () => {
expect(formatTimeToNow(undefined)).toBe(null)
})

it('returns the correct time format when date is a unix timestamp', () => {
vi.useFakeTimers().setSystemTime(new Date('2025-01-01'))
expect(formatTimeToNow(1715731200)).toBe('8 months ago')
vi.useRealTimers()
})

it('returns the correct time format when date is a iso string', () => {
vi.useFakeTimers().setSystemTime(new Date('2025-01-01'))
expect(formatTimeToNow('2024-09-01')).toBe('4 months ago')
vi.useRealTimers()
})
})

describe('formatTimeFromSeconds', () => {
it('returns "N/A" when totalSeconds is null', () => {
expect(formatTimeFromSeconds(null)).toBe('N/A')
})

it('returns "N/A" when totalSeconds is undefined', () => {
expect(formatTimeFromSeconds(undefined)).toBe('N/A')
})

it('returns "0s" when totalSeconds is 0', () => {
expect(formatTimeFromSeconds(0)).toBe('0s')
})

it('returns the correct time format when totalSeconds is greater than 0', () => {
expect(formatTimeFromSeconds(3661)).toBe('1h 1m 1s')
})
})
14 changes: 2 additions & 12 deletions src/shared/utils/dates.js → src/shared/utils/dates.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import {
format,
formatDistanceToNow,
fromUnixTime,
intervalToDuration,
parseISO,
} from 'date-fns'
import { useMemo } from 'react'

export function useDateFormatted(date, formatDescription = 'MMMM do yyyy') {
return useMemo(() => {
if (!date) return null
const parser = typeof date === 'string' ? parseISO : fromUnixTime
return format(parser(date), formatDescription)
}, [date, formatDescription])
}

export function formatTimeToNow(date) {
export function formatTimeToNow(date?: string | number | null) {
if (!date) return null

const parsedDate =
Expand All @@ -25,7 +15,7 @@ export function formatTimeToNow(date) {
})
}

export const formatTimeFromSeconds = (totalSeconds) => {
export const formatTimeFromSeconds = (totalSeconds?: number | null) => {
if (totalSeconds === 0) return '0s'
if (!totalSeconds) return 'N/A'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ describe('snakeifyKeys', () => {

it('else passes through', () => {
expect(snakeifyKeys([1, 2, 3])).toStrictEqual([1, 2, 3])
// @ts-expect-error
expect(snakeifyKeys(1)).toStrictEqual(1)
// @ts-expect-error
expect(snakeifyKeys('test')).toStrictEqual('test')
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import snakeCase from 'lodash/snakeCase'

export function snakeifyKeys(obj = {}) {
export function snakeifyKeys(
obj: Record<string, any> = {}
): Record<string, any> {
if (obj !== null && obj.constructor === Object) {
return Object.keys(obj).reduce(
(result, key) => ({
Expand Down
10 changes: 0 additions & 10 deletions src/shared/utils/user.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/shared/utils/user.test.js

This file was deleted.

0 comments on commit f461fa7

Please sign in to comment.