Skip to content

Commit

Permalink
test: moved some regex and made a new function that we test (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevkevinpal authored Nov 6, 2023
1 parent 23bc80f commit e6e1200
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
24 changes: 24 additions & 0 deletions src/components/AddContentModal/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getInputType } from '../utils'
import { LINK, TWITTER_SOURCE, WEB_PAGE } from '~/constants'

describe('youtubeRegex', () => {
it('should assert we can check for youtube clip regex', async () => {
expect(getInputType('https://www.youtube.com/watch?v=83eQ9flwVS0&ab_channel=EthanChlebowski')).toBe(LINK)
})

it('should assert we can check for twitter spaces regex', async () => {
expect(getInputType('https://twitter.com/i/spaces/1zqKVqwrVzlxB?s=20')).toBe(LINK)
})

it('should assert we can check for twitter tweet regex', async () => {
expect(getInputType('https://twitter.com/LarryRuane/status/1720496960489095668')).toBe(TWITTER_SOURCE)
})

it('should assert we can check for mp3 url regex', async () => {
expect(getInputType('https://hahaha.com/i/spaces/1zqKVqwrVzlxB?s=20.mp3')).toBe(LINK)
})

it('should assert we can check for generic url regex', async () => {
expect(getInputType('https://idkwhat.com/routeing/tou')).toBe(WEB_PAGE)
})
})
23 changes: 2 additions & 21 deletions src/components/AddContentModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ import { ToastMessage } from '../common/Toast/toastMessage'
import { BudgetStep } from './BudgetStep'
import { LocationStep } from './LocationStep'
import { SourceStep } from './SourceStep'

const youtubeRegex = /(https?:\/\/)?(www\.)?youtube\.com\/watch\?v=([A-Za-z0-9_-]+)/
const twitterSpaceRegex = /https:\/\/twitter\.com\/i\/spaces\/([A-Za-z0-9_-]+)/
const tweetUrlRegex = /https:\/\/twitter\.com\/[^/]+\/status\/(\d+)/
const mp3Regex = /(https?:\/\/)?([A-Za-z0-9_-]+)\.mp3/
const genericUrlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/
import { getInputType } from './utils'

export type FormData = {
input: string
Expand Down Expand Up @@ -160,21 +155,7 @@ export const AddContentModal = () => {
const source = watch('source')

useEffect(() => {
let inputType = DOCUMENT

if (youtubeRegex.test(source)) {
inputType = LINK
} else if (twitterSpaceRegex.test(source)) {
inputType = LINK
} else if (tweetUrlRegex.test(source)) {
inputType = TWITTER_SOURCE
} else if (mp3Regex.test(source)) {
inputType = LINK
} else if (genericUrlRegex.test(source)) {
inputType = WEB_PAGE
}

setValue('inputType', inputType)
setValue('inputType', getInputType(source))
}, [source, setValue])

const handleClose = () => {
Expand Down
25 changes: 25 additions & 0 deletions src/components/AddContentModal/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DOCUMENT, LINK, TWITTER_SOURCE, WEB_PAGE } from '~/constants'

const youtubeRegex = /(https?:\/\/)?(www\.)?youtube\.com\/watch\?v=([A-Za-z0-9_-]+)/
const twitterSpaceRegex = /https:\/\/twitter\.com\/i\/spaces\/([A-Za-z0-9_-]+)/
const tweetUrlRegex = /https:\/\/twitter\.com\/[^/]+\/status\/(\d+)/
const mp3Regex = /(https?:\/\/)?([A-Za-z0-9_-]+)\.mp3/
const genericUrlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/

export function getInputType(source: string) {
let inputType = DOCUMENT

if (youtubeRegex.test(source)) {
inputType = LINK
} else if (twitterSpaceRegex.test(source)) {
inputType = LINK
} else if (tweetUrlRegex.test(source)) {
inputType = TWITTER_SOURCE
} else if (mp3Regex.test(source)) {
inputType = LINK
} else if (genericUrlRegex.test(source)) {
inputType = WEB_PAGE
}

return inputType
}

0 comments on commit e6e1200

Please sign in to comment.