Skip to content

Commit

Permalink
test: adding new folders for utils to test, found one issue (#537)
Browse files Browse the repository at this point in the history
* test: adding new folders for utils to test, found one issue

* test: updated test to just pass

* fix: prettier

* test: added new test for unused string
  • Loading branch information
kevkevinpal authored Nov 2, 2023
1 parent 72277d0 commit 4dd7a6d
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 39 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@
],
"coverageThreshold": {
"global": {
"lines": 0
"lines": 0
},
"./src/components/**/utils/**.ts": {
"lines": 92
},
"./src/utils/": {
"lines": 23,
Expand Down
14 changes: 14 additions & 0 deletions src/components/AddContentModal/BudgetStep/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { formatBudget } from '../utils'

describe('formatBudget', () => {
/**
* @jest-environment jsdom
* @jest-environment-options {"url": "https://jestjs.io/"}
* */
it('should assert we format the budeget properly', async () => {
expect(formatBudget(null)).toBe('?')
expect(formatBudget(25)).toBe('25')
expect(formatBudget(7000000)).toBe('7 000 000')
expect(formatBudget('700000')).toBe('700000')
})
})
13 changes: 1 addition & 12 deletions src/components/AddContentModal/BudgetStep/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { useUserStore } from '~/stores/useUserStore'
import { colors } from '~/utils/colors'
import { formatBudget } from './utils'

type Props = {
onClick: () => void
Expand All @@ -14,18 +15,6 @@ type Props = {
export const BudgetStep: FC<Props> = ({ onClick }) => {
const [budget] = useUserStore((s) => [s.budget])

function formatBudget(value: number | null) {
if (value === null) {
return '?'
}

const stringBudget = value.toLocaleString()

const splittedBudget = stringBudget.split(',')

return splittedBudget.join(' ')
}

return (
<Flex>
<Flex align="center" direction="row" justify="space-between" mb={20}>
Expand Down
11 changes: 11 additions & 0 deletions src/components/AddContentModal/BudgetStep/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function formatBudget(value: number | null) {
if (value === null) {
return '?'
}

const stringBudget = value.toLocaleString()

const splittedBudget = stringBudget.split(',')

return splittedBudget.join(' ')
}
36 changes: 36 additions & 0 deletions src/components/AddContentModal/LocationStep/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { validateLatitude, validateLongitude } from '../utils'

describe('validateLongitude', () => {
/**
* @jest-environment jsdom
* @jest-environment-options {"url": "https://jestjs.io/"}
* */
it('should assert we validateLongitude properly', async () => {
const outsideRangeErrorMsg = 'Longitude must be between -180 and 180.'
const needValueErrorMsg = 'Longitude is required.'

expect(validateLongitude(180)).toBe(true)
expect(validateLongitude(181)).toBe(outsideRangeErrorMsg)
expect(validateLongitude(179)).toBe(true)
expect(validateLongitude(0)).toBe(true)
expect(validateLongitude(null)).toBe(needValueErrorMsg)
})
})

describe('validateLatitude', () => {
/**
* @jest-environment jsdom
* @jest-environment-options {"url": "https://jestjs.io/"}
* */
it('should assert we validateLatitude properly', async () => {
const outsideRangeErrorMsg = 'Latitude must be between -90 and 90.'
const needValueErrorMsg = 'Latitude is required.'

expect(validateLatitude(90)).toBe(true)
expect(validateLatitude(91)).toBe(outsideRangeErrorMsg)
expect(validateLatitude(89)).toBe(true)
expect(validateLatitude(0)).toBe(true)
expect(validateLatitude(null)).toBe(true)
expect(validateLatitude('abc')).toBe(needValueErrorMsg)
})
})
27 changes: 1 addition & 26 deletions src/components/AddContentModal/LocationStep/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Text } from '~/components/common/Text'
import { TextInput } from '~/components/common/TextInput'
import { colors } from '~/utils/colors'
import { FormData } from '..'
import { validateLatitude, validateLongitude } from './utils'

const latitudeReg = /^(-?\d{1,2}(\.\d+)?|90(\.0+)?)$/
const longitudeReg = /^(-?\d{1,3}(\.\d+)?|180(\.0+)?)$/
Expand All @@ -19,20 +20,6 @@ type Props = {
}

export const LocationStep: FC<Props> = ({ latitude, longitude, onNextStep, form }) => {
const validateLatitude = (valueString: string) => {
const value = Number(valueString)

if (value < -90 || value > 90) {
return 'Latitude must be between -90 and 90.'
}

if (!value && value !== 0) {
return 'Latitude is required.'
}

return true
}

const handleNextStep = () => {
const { errors } = form.formState

Expand All @@ -47,18 +34,6 @@ export const LocationStep: FC<Props> = ({ latitude, longitude, onNextStep, form
onNextStep()
}

const validateLongitude = (value: number) => {
if (value < -180 || value > 180) {
return 'Longitude must be between -180 and 180.'
}

if (!value && value !== 0) {
return 'Longitude is required.'
}

return true
}

return (
<Flex>
<Flex align="center" direction="row" justify="space-between" mb={20}>
Expand Down
25 changes: 25 additions & 0 deletions src/components/AddContentModal/LocationStep/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const validateLatitude = (valueString: string) => {
const value = Number(valueString)

if (value < -90 || value > 90) {
return 'Latitude must be between -90 and 90.'
}

if (!value && value !== 0) {
return 'Latitude is required.'
}

return true
}

export const validateLongitude = (value: number) => {
if (value < -180 || value > 180) {
return 'Longitude must be between -180 and 180.'
}

if (!value && value !== 0) {
return 'Longitude is required.'
}

return true
}

0 comments on commit 4dd7a6d

Please sign in to comment.