Skip to content

Commit

Permalink
fix(NewFeedsManagerScreen): no longer redirect
Browse files Browse the repository at this point in the history
Stop redirecting to `/job_distributor` when there is atleast one registered manager since we now want to support adding more than 1 manager.

JIRA: https://smartcontract-it.atlassian.net/browse/OPCORE-858
  • Loading branch information
graham-chainlink committed Sep 3, 2024
1 parent d74af46 commit 6335276
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 151 deletions.
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
121 changes: 9 additions & 112 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)
renderComponent([])

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

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,8 +77,6 @@ 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: 'URI *' }), 'localhost:8080')
userEvent.type(getByRole('textbox', { name: 'Public Key *' }), '1111')
Expand All @@ -147,18 +89,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,8 +131,6 @@ 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: 'URI *' }), 'localhost:8080')
userEvent.type(getByRole('textbox', { name: 'Public Key *' }), '1111')
Expand All @@ -215,37 +143,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 +164,6 @@ describe('NewFeedsManagerScreen', () => {

renderComponent(mocks)

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

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} />
}

0 comments on commit 6335276

Please sign in to comment.