From 9722336410c0c00af2397063dd8d297fb3cd77aa Mon Sep 17 00:00:00 2001 From: kevkevinpal Date: Tue, 31 Oct 2023 19:48:36 -0500 Subject: [PATCH 1/4] test: adding new folders for utils to test, found one issue --- package.json | 5 ++- .../BudgetStep/__tests__/index.ts | 15 ++++++++ .../AddContentModal/BudgetStep/index.tsx | 13 +------ .../AddContentModal/BudgetStep/utils/index.ts | 11 ++++++ .../LocationStep/__tests__/index.ts | 35 +++++++++++++++++++ .../AddContentModal/LocationStep/index.tsx | 27 +------------- .../LocationStep/utils/index.ts | 25 +++++++++++++ 7 files changed, 92 insertions(+), 39 deletions(-) create mode 100644 src/components/AddContentModal/BudgetStep/__tests__/index.ts create mode 100644 src/components/AddContentModal/BudgetStep/utils/index.ts create mode 100644 src/components/AddContentModal/LocationStep/__tests__/index.ts create mode 100644 src/components/AddContentModal/LocationStep/utils/index.ts diff --git a/package.json b/package.json index cbbbcc3d0..bc4500f19 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": 100 }, "./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..5d2ac1339 --- /dev/null +++ b/src/components/AddContentModal/BudgetStep/__tests__/index.ts @@ -0,0 +1,15 @@ +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..c1e1fa03d --- /dev/null +++ b/src/components/AddContentModal/LocationStep/__tests__/index.ts @@ -0,0 +1,35 @@ +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(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..3c4df2f6c --- /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 + } From 23ab3121eff271721a7ba15cf64e2a7f8b84ca4e Mon Sep 17 00:00:00 2001 From: kevkevinpal Date: Thu, 2 Nov 2023 13:28:01 -0500 Subject: [PATCH 2/4] test: updated test to just pass --- package.json | 2 +- src/components/AddContentModal/LocationStep/__tests__/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index bc4500f19..1b2efc44a 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,7 @@ "lines": 0 }, "./src/components/**/utils/**.ts": { - "lines": 100 + "lines": 92 }, "./src/utils/": { "lines": 23, diff --git a/src/components/AddContentModal/LocationStep/__tests__/index.ts b/src/components/AddContentModal/LocationStep/__tests__/index.ts index c1e1fa03d..40287a0e3 100644 --- a/src/components/AddContentModal/LocationStep/__tests__/index.ts +++ b/src/components/AddContentModal/LocationStep/__tests__/index.ts @@ -30,6 +30,6 @@ describe('validateLatitude', () => { expect(validateLatitude(91)).toBe(outsideRangeErrorMsg) expect(validateLatitude(89)).toBe(true) expect(validateLatitude(0)).toBe(true) - expect(validateLatitude(null)).toBe(needValueErrorMsg) + expect(validateLatitude(null)).toBe(true) }) }) From 230635a66f1049d86a745e366325324e02b8cc98 Mon Sep 17 00:00:00 2001 From: kevkevinpal Date: Thu, 2 Nov 2023 13:29:34 -0500 Subject: [PATCH 3/4] fix: prettier --- .../BudgetStep/__tests__/index.ts | 3 +-- .../AddContentModal/LocationStep/utils/index.ts | 16 ++++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/components/AddContentModal/BudgetStep/__tests__/index.ts b/src/components/AddContentModal/BudgetStep/__tests__/index.ts index 5d2ac1339..010b6f268 100644 --- a/src/components/AddContentModal/BudgetStep/__tests__/index.ts +++ b/src/components/AddContentModal/BudgetStep/__tests__/index.ts @@ -9,7 +9,6 @@ describe('formatBudget', () => { expect(formatBudget(null)).toBe('?') expect(formatBudget(25)).toBe('25') expect(formatBudget(7000000)).toBe('7 000 000') - expect(formatBudget("700000")).toBe('700000') + expect(formatBudget('700000')).toBe('700000') }) }) - diff --git a/src/components/AddContentModal/LocationStep/utils/index.ts b/src/components/AddContentModal/LocationStep/utils/index.ts index 3c4df2f6c..fb4749670 100644 --- a/src/components/AddContentModal/LocationStep/utils/index.ts +++ b/src/components/AddContentModal/LocationStep/utils/index.ts @@ -13,13 +13,13 @@ export const validateLatitude = (valueString: string) => { } 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.' - } + if (value < -180 || value > 180) { + return 'Longitude must be between -180 and 180.' + } - return true + if (!value && value !== 0) { + return 'Longitude is required.' } + + return true +} From 216e80e311b90cd6e19742615a94035c8ab98ec1 Mon Sep 17 00:00:00 2001 From: kevkevinpal Date: Thu, 2 Nov 2023 13:40:57 -0500 Subject: [PATCH 4/4] test: added new test for unused string --- src/components/AddContentModal/LocationStep/__tests__/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/AddContentModal/LocationStep/__tests__/index.ts b/src/components/AddContentModal/LocationStep/__tests__/index.ts index 40287a0e3..42a7dcf0e 100644 --- a/src/components/AddContentModal/LocationStep/__tests__/index.ts +++ b/src/components/AddContentModal/LocationStep/__tests__/index.ts @@ -31,5 +31,6 @@ describe('validateLatitude', () => { expect(validateLatitude(89)).toBe(true) expect(validateLatitude(0)).toBe(true) expect(validateLatitude(null)).toBe(true) + expect(validateLatitude('abc')).toBe(needValueErrorMsg) }) })