Skip to content

Commit

Permalink
test: added new tests and moved notify (#579)
Browse files Browse the repository at this point in the history
* test: added new tests and moved notify

* test: updated thresholds
  • Loading branch information
kevkevinpal authored Nov 8, 2023
1 parent 39819e6 commit 5aefbdd
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 73 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@
"collectCoverageFrom": ["./src/**/*.js", "./src/**/*.ts"],
"coverageThreshold": {
"global": {
"lines": 0
"lines": 2
},
"./src/components/**/utils/**.ts": {
"lines": 100
},
"./src/utils/": {
"lines": 23,
"branches": 29,
"functions": 33
"lines": 27,
"branches": 37,
"functions": 36
}
},
"moduleNameMapper": {
Expand Down
19 changes: 2 additions & 17 deletions src/components/AddContentModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Lsat } from 'lsat-js'
import { useEffect, useState } from 'react'
import { FieldValues, FormProvider, useForm } from 'react-hook-form'
import { MdCheckCircle, MdWarning } from 'react-icons/md'
import { toast } from 'react-toastify'
import * as sphinx from 'sphinx-bridge-kevkevinpal'
import { BaseModal } from '~/components/Modal'
import { DOCUMENT, LINK, NODE_ADD_ERROR, NODE_ADD_SUCCESS, TWITTER_SOURCE, WEB_PAGE } from '~/constants'
import { api } from '~/network/api'
import { useModal } from '~/stores/useModalStore'
import { useUserStore } from '~/stores/useUserStore'
import { SubmitErrRes } from '~/types'
import { colors, getLSat, payLsat, updateBudget, executeIfProd } from '~/utils'
import { ToastMessage } from '../common/Toast/toastMessage'
import { getLSat, payLsat, updateBudget, executeIfProd } from '~/utils'
import { notify } from '~/components/common/Toast/toastMessage'
import { BudgetStep } from './BudgetStep'
import { LocationStep } from './LocationStep'
import { SourceStep } from './SourceStep'
Expand All @@ -25,19 +23,6 @@ export type FormData = {
latitude: string
}

const notify = (message: string) => {
toast(<ToastMessage message={message} />, {
icon:
message === NODE_ADD_SUCCESS ? (
<MdCheckCircle color={colors.primaryGreen} />
) : (
<MdWarning color={colors.primaryRed} />
),
position: toast.POSITION.BOTTOM_CENTER,
type: message === NODE_ADD_SUCCESS ? 'success' : 'error',
})
}

const handleSubmitForm = async (
data: FieldValues,
close: () => void,
Expand Down
56 changes: 5 additions & 51 deletions src/components/AddSourceModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
import { Lsat } from 'lsat-js'
import { useEffect, useState } from 'react'
import { FieldValues, FormProvider, useForm } from 'react-hook-form'
import { MdCheckCircle, MdWarning } from 'react-icons/md'
import { toast } from 'react-toastify'
import * as sphinx from 'sphinx-bridge-kevkevinpal'
import { BaseModal } from '~/components/Modal'
import {
DOCUMENT,
GITHUB_REPOSITORY,
NODE_ADD_ERROR,
NODE_ADD_SUCCESS,
RSS,
TOPIC,
TWITTER_HANDLE,
YOUTUBE_CHANNEL,
} from '~/constants'
import { NODE_ADD_ERROR, NODE_ADD_SUCCESS, TWITTER_HANDLE } from '~/constants'
import { api } from '~/network/api'
import { useModal } from '~/stores/useModalStore'
import { useUserStore } from '~/stores/useUserStore'
import { SubmitErrRes } from '~/types'
import { colors } from '~/utils/colors'
import { getLSat } from '~/utils/getLSat'
import { payLsat } from '~/utils/payLsat'
import { updateBudget } from '~/utils/setBudget'
import { executeIfProd } from '~/utils/tests'
import { ToastMessage } from '../common/Toast/toastMessage'
import { getLSat, payLsat, updateBudget, executeIfProd } from '~/utils'
import { notify } from '~/components/common/Toast/toastMessage'
import { BudgetStep } from './BudgetStep'
import { SourceStep } from './SourceStep'

const twitterHandlePattern = /@(\w+)/g
const youtubeChannelPattern = /https?:\/\/(www\.)?youtube\.com\/(@)?([\w-]+)/i
const githubRepoPattern = /https?:\/\/github\.com\/([\w-]+)\/([\w-]+)/i
const genericUrlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/
import { getInputType } from './utils'

export type FormData = {
input: string
inputType: string
source: string
}

const notify = (message: string) => {
toast(<ToastMessage message={message} />, {
icon:
message === NODE_ADD_SUCCESS ? (
<MdCheckCircle color={colors.primaryGreen} />
) : (
<MdWarning color={colors.primaryRed} />
),
position: toast.POSITION.BOTTOM_CENTER,
type: message === NODE_ADD_SUCCESS ? 'success' : 'error',
})
}

const handleSubmitForm = async (
data: FieldValues,
close: () => void,
Expand Down Expand Up @@ -135,21 +103,7 @@ export const AddSourceModal = () => {
const source = watch('source')

useEffect(() => {
let inputType = DOCUMENT

if (youtubeChannelPattern.test(source)) {
inputType = YOUTUBE_CHANNEL
} else if (twitterHandlePattern.test(source)) {
inputType = TWITTER_HANDLE
} else if (githubRepoPattern.test(source)) {
inputType = GITHUB_REPOSITORY
} else if (genericUrlRegex.test(source)) {
inputType = RSS
} else {
inputType = TOPIC
}

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

const handleClose = () => {
Expand Down
36 changes: 36 additions & 0 deletions src/components/AddSourceModal/utils/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getInputType } from '..'
import { YOUTUBE_CHANNEL, RSS, TWITTER_HANDLE, GITHUB_REPOSITORY, TOPIC } 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(YOUTUBE_CHANNEL)
})

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

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

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

it('should assert we can check for generic url regex', async () => {
expect(getInputType('https://idkwhat.com/routeing/tou')).toBe(RSS)
})

it('should assert we can check for twitter handle regex', async () => {
expect(getInputType('https://twitter.com/@KevKevPal')).toBe(TWITTER_HANDLE)
})

it('should assert we can check for github repo regex', async () => {
expect(getInputType('https://github.com/stakwork/sphinx-relay')).toBe(GITHUB_REPOSITORY)
})

it('should assert we can check for topic regex', async () => {
expect(getInputType('Bitcoin')).toBe(TOPIC)
})
})
24 changes: 24 additions & 0 deletions src/components/AddSourceModal/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DOCUMENT, YOUTUBE_CHANNEL, TWITTER_HANDLE, GITHUB_REPOSITORY, RSS, TOPIC } from '~/constants'

const twitterHandlePattern = /@(\w+)/g
const youtubeChannelPattern = /https?:\/\/(www\.)?youtube\.com\/(@)?([\w-]+)/i
const githubRepoPattern = /https?:\/\/github\.com\/([\w-]+)\/([\w-]+)/i
const genericUrlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/

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

if (youtubeChannelPattern.test(source)) {
inputType = YOUTUBE_CHANNEL
} else if (twitterHandlePattern.test(source)) {
inputType = TWITTER_HANDLE
} else if (githubRepoPattern.test(source)) {
inputType = GITHUB_REPOSITORY
} else if (genericUrlRegex.test(source)) {
inputType = RSS
} else {
inputType = TOPIC
}

return inputType
}
16 changes: 15 additions & 1 deletion src/components/common/Toast/toastMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { toast, ToastContentProps } from 'react-toastify'
import * as sphinx from 'sphinx-bridge-kevkevinpal'
import styled from 'styled-components'
import { Text } from '~/components/common/Text'
import { BOOST_ERROR_BUDGET } from '~/constants'
import { BOOST_ERROR_BUDGET, NODE_ADD_SUCCESS } from '~/constants'
import { MdCheckCircle, MdWarning } from 'react-icons/md'
import { colors } from '~/utils/colors'

type Props = ToastContentProps & {
Expand Down Expand Up @@ -56,6 +57,19 @@ export const ToastMessage = ({ message }: Partial<Props>) => {
return <div>{message}</div>
}

export const notify = (message: string) => {
toast(<ToastMessage message={message} />, {
icon:
message === NODE_ADD_SUCCESS ? (
<MdCheckCircle color={colors.primaryGreen} />
) : (
<MdWarning color={colors.primaryRed} />
),
position: toast.POSITION.BOTTOM_CENTER,
type: message === NODE_ADD_SUCCESS ? 'success' : 'error',
})
}

const ButtonWrapper = styled.button`
background: ${colors.gray200};
border: 1px solid ${colors.white};
Expand Down

0 comments on commit 5aefbdd

Please sign in to comment.