Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: moved some regex and made a new function that we test #567

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading