diff --git a/package.json b/package.json index cbbbcc3d0..1b2efc44a 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,10 @@ "collectCoverageFrom": ["./src/**/*.js", "./src/**/*.ts"], "coverageThreshold": { "global": { - "lines": 0 + "lines": 0 + }, + "./src/components/**/utils/**.ts": { + "lines": 92 }, "./src/utils/": { "lines": 23, diff --git a/src/components/AddContentModal/BudgetStep/__tests__/index.ts b/src/components/AddContentModal/BudgetStep/__tests__/index.ts new file mode 100644 index 000000000..010b6f268 --- /dev/null +++ b/src/components/AddContentModal/BudgetStep/__tests__/index.ts @@ -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') + }) +}) diff --git a/src/components/AddContentModal/BudgetStep/index.tsx b/src/components/AddContentModal/BudgetStep/index.tsx index fadb10c47..f95e11b74 100644 --- a/src/components/AddContentModal/BudgetStep/index.tsx +++ b/src/components/AddContentModal/BudgetStep/index.tsx @@ -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 @@ -14,18 +15,6 @@ type Props = { export const BudgetStep: FC = ({ 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 ( diff --git a/src/components/AddContentModal/BudgetStep/utils/index.ts b/src/components/AddContentModal/BudgetStep/utils/index.ts new file mode 100644 index 000000000..677e63c13 --- /dev/null +++ b/src/components/AddContentModal/BudgetStep/utils/index.ts @@ -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(' ') +} diff --git a/src/components/AddContentModal/LocationStep/__tests__/index.ts b/src/components/AddContentModal/LocationStep/__tests__/index.ts new file mode 100644 index 000000000..42a7dcf0e --- /dev/null +++ b/src/components/AddContentModal/LocationStep/__tests__/index.ts @@ -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) + }) +}) diff --git a/src/components/AddContentModal/LocationStep/index.tsx b/src/components/AddContentModal/LocationStep/index.tsx index 631973b47..2a38d2ee7 100644 --- a/src/components/AddContentModal/LocationStep/index.tsx +++ b/src/components/AddContentModal/LocationStep/index.tsx @@ -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+)?)$/ @@ -19,20 +20,6 @@ type Props = { } export const LocationStep: FC = ({ 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 @@ -47,18 +34,6 @@ export const LocationStep: FC = ({ 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 ( diff --git a/src/components/AddContentModal/LocationStep/utils/index.ts b/src/components/AddContentModal/LocationStep/utils/index.ts new file mode 100644 index 000000000..fb4749670 --- /dev/null +++ b/src/components/AddContentModal/LocationStep/utils/index.ts @@ -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 +}