Skip to content

Commit

Permalink
test: added test for twitter youtube and mp3 regex (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevkevinpal authored Nov 3, 2023
1 parent 4dd7a6d commit 23bc80f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/components/AddContentModal/SourceStep/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { twitterOrYoutubeRegexOrMp3 } from '../utils'

describe('twitterOrYoutubeRegexOrMp3', () => {
const regex = new RegExp(twitterOrYoutubeRegexOrMp3)

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

it('should assert we can check for youtube regex', async () => {
expect(regex.test('https://www.youtube.com/watch?v=D900-udw9Dw&ab_channel=SwanBitcoin')).toBe(true)
expect(regex.test('www.youtube.com/watch?v=D900-udw9Dw&ab_channel=SwanBitcoin')).toBe(true)
expect(regex.test('youtube.com/watch?v=D900-udw9Dw&ab_channel=SwanBitcoin')).toBe(true)
expect(regex.test('youtube.com')).toBe(false)
expect(regex.test('https://youtube.com')).toBe(false)
expect(regex.test('www.youtube.com')).toBe(false)
expect(regex.test('youtube.org')).toBe(false)
expect(regex.test('facebook.com')).toBe(false)
})

it('should assert we can check for mp3 regex', async () => {
expect(regex.test('idkwhat.mp3')).toBe(true)
expect(regex.test('https://idkwhat.mp3')).toBe(true)
expect(regex.test('www.idkwhat.mp3')).toBe(true)
expect(regex.test('idkwhat.org')).toBe(false)
expect(regex.test('facebook.com')).toBe(false)
})
})
4 changes: 1 addition & 3 deletions src/components/AddContentModal/SourceStep/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { TextInput } from '~/components/common/TextInput'
import { LINK, requiredRule } from '~/constants'
import { twitterOrYoutubeRegexOrMp3 } from './utils'

type Props = {
type: string
onNextStep: () => void
value?: string
}

const twitterOrYoutubeRegexOrMp3 =
/^(?:(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu\.be\/)[\w-]{11}(?:\S*)?|(?:https?:\/\/)?(?:www\.)?twitter\.com\/i\/spaces\/\d+.*$|.+\.mp3)$/i

export const SourceStep: FC<Props> = ({ type, onNextStep, value }) => (
<Flex>
<Flex align="center" direction="row" justify="space-between" mb={18}>
Expand Down
3 changes: 3 additions & 0 deletions src/components/AddContentModal/SourceStep/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This regex is specifically for youtube videos, twitter spaces and mp3 links
export const twitterOrYoutubeRegexOrMp3 =
/^(?:(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu\.be\/)[\w-]{11}(?:\S*)?|(?:https?:\/\/)?(?:www\.)?twitter\.com\/i\/spaces\/\d+.*$|.+\.mp3)$/i

0 comments on commit 23bc80f

Please sign in to comment.