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

[OPCORE-858]: fix(NewFeedsManagerScreen): no longer redirect #86

Merged
merged 3 commits into from
Sep 3, 2024
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
5 changes: 5 additions & 0 deletions .changeset/chilly-garlics-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@smartcontractkit/operator-ui': minor
---

stop redirect to /job_distributors/ when there are registered job distributors
135 changes: 21 additions & 114 deletions src/screens/NewFeedsManager/NewFeedsManagerScreen.test.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import * as React from 'react'

import { MockedProvider, MockedResponse } from '@apollo/client/testing'
import userEvent from '@testing-library/user-event'
import { GraphQLError } from 'graphql'
import { Route } from 'react-router-dom'
import {
renderWithRouter,
screen,
waitForElementToBeRemoved,
} from 'support/test-utils'
import userEvent from '@testing-library/user-event'
import { MockedProvider, MockedResponse } from '@apollo/client/testing'
import { renderWithRouter, screen } from 'support/test-utils'

import { buildFeedsManager } from 'support/factories/gql/fetchFeedsManagers'
import Notifications from 'pages/Notifications'
import { FEEDS_MANAGERS_QUERY } from 'src/hooks/queries/useFeedsManagersQuery'
import { buildFeedsManager } from 'support/factories/gql/fetchFeedsManagers'
import {
CREATE_FEEDS_MANAGER_MUTATION,
NewFeedsManagerScreen,
} from './NewFeedsManagerScreen'
import Notifications from 'pages/Notifications'

const { findByTestId, findByText, getByRole } = screen
const { findByTestId, findByText, getByRole, getByText, getByTestId } = screen

function renderComponent(mocks: MockedResponse[]) {
renderWithRouter(
Expand All @@ -37,66 +33,14 @@ function renderComponent(mocks: MockedResponse[]) {

describe('NewFeedsManagerScreen', () => {
it('renders the page', async () => {
const mocks: MockedResponse[] = [
{
request: {
query: FEEDS_MANAGERS_QUERY,
},
result: {
data: {
feedsManagers: {
results: [],
},
},
},
},
]

renderComponent(mocks)

await waitForElementToBeRemoved(() => screen.queryByRole('progressbar'))
renderComponent([])

expect(await findByText('Register Job Distributor')).toBeInTheDocument()
expect(await findByTestId('feeds-manager-form')).toBeInTheDocument()
})

it('redirects when a manager exists', async () => {
const mocks: MockedResponse[] = [
{
request: {
query: FEEDS_MANAGERS_QUERY,
},
result: {
data: {
feedsManagers: {
results: [buildFeedsManager()],
},
},
},
},
]

renderComponent(mocks)

await waitForElementToBeRemoved(() => screen.queryByRole('progressbar'))

expect(await findByText('Redirect Success')).toBeInTheDocument()
expect(getByText('Register Job Distributor')).toBeInTheDocument()
expect(getByTestId('feeds-manager-form')).toBeInTheDocument()
})

it('submits the form', async () => {
const mocks: MockedResponse[] = [
{
request: {
query: FEEDS_MANAGERS_QUERY,
},
result: {
data: {
feedsManagers: {
results: [],
},
},
},
},
{
request: {
query: CREATE_FEEDS_MANAGER_MUTATION,
Expand Down Expand Up @@ -133,9 +77,10 @@ describe('NewFeedsManagerScreen', () => {

renderComponent(mocks)

await waitForElementToBeRemoved(() => screen.queryByRole('progressbar'))

// Note: The name input has a default value so we don't have to set it
userEvent.type(
getByRole('textbox', { name: 'Name *' }),
'Chainlink Feeds Manager',
)
userEvent.type(getByRole('textbox', { name: 'URI *' }), 'localhost:8080')
userEvent.type(getByRole('textbox', { name: 'Public Key *' }), '1111')

Expand All @@ -147,18 +92,6 @@ describe('NewFeedsManagerScreen', () => {

it('handles input errors', async () => {
const mocks: MockedResponse[] = [
{
request: {
query: FEEDS_MANAGERS_QUERY,
},
result: {
data: {
feedsManagers: {
results: [],
},
},
},
},
{
request: {
query: CREATE_FEEDS_MANAGER_MUTATION,
Expand Down Expand Up @@ -201,9 +134,10 @@ describe('NewFeedsManagerScreen', () => {

renderComponent(mocks)

await waitForElementToBeRemoved(() => screen.queryByRole('progressbar'))

// Note: The name input has a default value so we don't have to set it
userEvent.type(
getByRole('textbox', { name: 'Name *' }),
'Chainlink Feeds Manager',
)
userEvent.type(getByRole('textbox', { name: 'URI *' }), 'localhost:8080')
userEvent.type(getByRole('textbox', { name: 'Public Key *' }), '1111')

Expand All @@ -215,37 +149,8 @@ describe('NewFeedsManagerScreen', () => {
)
})

it('renders query GQL errors', async () => {
const mocks: MockedResponse[] = [
{
request: {
query: FEEDS_MANAGERS_QUERY,
},
result: {
errors: [new GraphQLError('Error!')],
},
},
]

renderComponent(mocks)

expect(await findByText('Error: Error!')).toBeInTheDocument()
})

it('renders mutation GQL errors', async () => {
const mocks: MockedResponse[] = [
{
request: {
query: FEEDS_MANAGERS_QUERY,
},
result: {
data: {
feedsManagers: {
results: [],
},
},
},
},
{
request: {
query: CREATE_FEEDS_MANAGER_MUTATION,
Expand All @@ -265,8 +170,10 @@ describe('NewFeedsManagerScreen', () => {

renderComponent(mocks)

await waitForElementToBeRemoved(() => screen.queryByRole('progressbar'))

userEvent.type(
getByRole('textbox', { name: 'Name *' }),
'Chainlink Feeds Manager',
)
userEvent.type(getByRole('textbox', { name: 'URI *' }), 'localhost:8080')
userEvent.type(getByRole('textbox', { name: 'Public Key *' }), '1111')

Expand Down
51 changes: 12 additions & 39 deletions src/screens/NewFeedsManager/NewFeedsManagerScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import React from 'react'

import { useMutation, gql } from '@apollo/client'
import { gql, useMutation } from '@apollo/client'
import { FormikHelpers } from 'formik'
import { useDispatch } from 'react-redux'
import { Redirect, useHistory, useLocation } from 'react-router-dom'
import { useHistory } from 'react-router-dom'

import { notifySuccessMsg, notifyErrorMsg } from 'actionCreators'
import { GraphqlErrorHandler } from 'src/components/ErrorHandler/GraphqlErrorHandler'
import { notifyErrorMsg, notifySuccessMsg } from 'actionCreators'
import { FormValues } from 'components/Form/FeedsManagerForm'
import { FEEDS_MANAGERS_QUERY } from 'src/hooks/queries/useFeedsManagersQuery'
import { useMutationErrorHandler } from 'src/hooks/useMutationErrorHandler'
import { parseInputErrors } from 'src/utils/inputErrors'
import { Loading } from 'src/components/Feedback/Loading'
import { NewFeedsManagerView } from './NewFeedsManagerView'
import {
FEEDS_MANAGERS_QUERY,
useFeedsManagersQuery,
} from 'src/hooks/queries/useFeedsManagersQuery'
import { useMutationErrorHandler } from 'src/hooks/useMutationErrorHandler'

export const CREATE_FEEDS_MANAGER_MUTATION = gql`
mutation CreateFeedsManager($input: CreateFeedsManagerInput!) {
Expand All @@ -34,6 +29,10 @@ export const CREATE_FEEDS_MANAGER_MUTATION = gql`
message
code
}
... on DuplicateFeedsManagerError {
message
code
}
... on NotFoundError {
message
code
Expand All @@ -51,32 +50,15 @@ export const CREATE_FEEDS_MANAGER_MUTATION = gql`

export const NewFeedsManagerScreen: React.FC = () => {
const history = useHistory()
const location = useLocation()
const dispatch = useDispatch()
const { handleMutationError } = useMutationErrorHandler()
const { data, loading, error } = useFeedsManagersQuery()
const [createFeedsManager] = useMutation<
CreateFeedsManager,
CreateFeedsManagerVariables
>(CREATE_FEEDS_MANAGER_MUTATION, {
refetchQueries: [FEEDS_MANAGERS_QUERY],
refetchQueries: [{ query: FEEDS_MANAGERS_QUERY }],
})

if (loading) {
return <Loading />
}

if (error) {
return <GraphqlErrorHandler error={error} />
}

// We currently only support a single feeds manager, but plan to support more
// in the future.
const manager =
data != undefined && data.feedsManagers.results[0]
? data.feedsManagers.results[0]
: undefined

const handleSubmit = async (
values: FormValues,
{ setErrors }: FormikHelpers<FormValues>,
Expand All @@ -94,7 +76,9 @@ export const NewFeedsManagerScreen: React.FC = () => {
dispatch(notifySuccessMsg('Job Distributor Created'))

break
// todo: remove SingleFeedsManagerError once multi feeds manager support is released
case 'SingleFeedsManagerError':
case 'DuplicateFeedsManagerError':
case 'NotFoundError':
dispatch(notifyErrorMsg(payload.message))

Expand All @@ -111,16 +95,5 @@ export const NewFeedsManagerScreen: React.FC = () => {
}
}

if (manager) {
return (
<Redirect
to={{
pathname: '/job_distributors',
state: { from: location },
}}
/>
)
}

return <NewFeedsManagerView onSubmit={handleSubmit} />
}
2 changes: 1 addition & 1 deletion src/screens/NewFeedsManager/NewFeedsManagerView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('NewFeedsManagerView', () => {

expect(getByText('Register Job Distributor')).toBeInTheDocument()
expect(getByTestId('feeds-manager-form')).toHaveFormValues({
name: 'Chainlink Feeds Manager',
name: '',
uri: '',
publicKey: '',
})
Expand Down
2 changes: 1 addition & 1 deletion src/screens/NewFeedsManager/NewFeedsManagerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'components/Form/FeedsManagerForm'

const initialValues = {
name: 'Chainlink Feeds Manager',
name: '',
uri: '',
publicKey: '',
}
Expand Down
Loading