Skip to content

Commit

Permalink
chore: move regex to pattern with test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Usmanfee committed Oct 10, 2024
1 parent 1862311 commit 7b9e5d8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/components/pages/AppOverview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { fetchImageWithToken } from 'services/ImageService'
import { setCurrentActiveStep } from 'features/appManagement/slice'
import { setAppId, setAppStatus } from 'features/appManagement/actions'
import NoItems from '../NoItems'
import { isValidAppOverviewSearch } from 'types/Patterns'

export default function AppOverview() {
const { t } = useTranslation()
Expand Down Expand Up @@ -222,9 +223,7 @@ export default function AppOverview() {

const doSearch = useCallback(
(expr: string) => {
const validateExpr =
/^[ A-Za-z0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]*$/.test(expr)
if (!validateExpr) {
if (!isValidAppOverviewSearch(expr)) {
return
}
setSearchExpr(expr)
Expand Down
13 changes: 13 additions & 0 deletions src/types/Patterns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
isCountryCode,
isClientID,
isPersonName,
isValidAppOverviewSearch,
} from './Patterns'

const TESTDATA = {
Expand Down Expand Up @@ -214,6 +215,10 @@ const TESTDATA = {
valid: ['sa-12', 'JSSS', 'Julia12'],
invalid: ['&^%#@', '!', 'hash &*^#$'],
},
appOverview: {
valid: ['sa-12', '1234', 'Test123!@#'],
invalid: ['🚀 Rocket!', 'Invalid\nNewLine'],
},
}

describe('Input Pattern Tests', () => {
Expand Down Expand Up @@ -304,4 +309,12 @@ describe('Input Pattern Tests', () => {
expect(isClientID(expr)).toBe(false)
})
})
it('validate appoverview search', () => {
TESTDATA.appOverview.valid.forEach((expr) => {
expect(isValidAppOverviewSearch(expr)).toBe(true)
})
TESTDATA.appOverview.invalid.forEach((expr) => {
expect(isValidAppOverviewSearch(expr)).toBe(false)
})
})
})
5 changes: 5 additions & 0 deletions src/types/Patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export const Patterns = {
POSTAL_CODE:
/^(?!.*\s$)(?=[a-zA-Z\d-]{0,10}[-\s]?[a-zA-Z\d-]{0,10}$)[a-zA-Z\d\s-]{2,10}$/,
},
appOverview: {
SEARCH: /^[ A-Za-z0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]*$/,
},
}

export const isEmpty = (expr: string) => !expr || expr.trim() === ''
Expand Down Expand Up @@ -208,5 +211,7 @@ export const isCompanyVies = (expr: string) =>
Patterns.companyData.VIES.test(expr)
export const isPostalCode = (expr: string) =>
Patterns.companyData.POSTAL_CODE.test(expr)
export const isValidAppOverviewSearch = (expr: string) =>
Patterns.appOverview.SEARCH.test(expr)

export default Patterns

0 comments on commit 7b9e5d8

Please sign in to comment.