|
| 1 | +import * as React from 'react' |
| 2 | + |
| 3 | +import { GraphQLError } from 'graphql' |
| 4 | +import { |
| 5 | + renderWithRouter, |
| 6 | + screen, |
| 7 | + waitForElementToBeRemoved, |
| 8 | +} from 'support/test-utils' |
| 9 | +import { MockedProvider, MockedResponse } from '@apollo/client/testing' |
| 10 | +import userEvent from '@testing-library/user-event' |
| 11 | + |
| 12 | +import { |
| 13 | + OCR2Keys, |
| 14 | + DELETE_OCR2_KEY_BUNDLE_MUTATION, |
| 15 | +} from './OCR2Keys' |
| 16 | +import { |
| 17 | + buildOCR2KeyBundle, |
| 18 | + buildOCR2KeyBundles, |
| 19 | +} from 'support/factories/gql/fetchOCR2KeyBundles' |
| 20 | +import Notifications from 'pages/Notifications' |
| 21 | +import { OCR2_KEY_BUNDLES_QUERY } from 'src/hooks/queries/useOCR2KeysQuery' |
| 22 | +import { waitForLoading } from 'support/test-helpers/wait' |
| 23 | + |
| 24 | +const { findByText, getByRole, queryByText } = screen |
| 25 | + |
| 26 | +function renderComponent(mocks: MockedResponse[]) { |
| 27 | + renderWithRouter( |
| 28 | + <> |
| 29 | + <Notifications /> |
| 30 | + <MockedProvider mocks={mocks} addTypename={false}> |
| 31 | + <OCR2Keys /> |
| 32 | + </MockedProvider> |
| 33 | + </>, |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +function fetchOCR2KeyBundlesQuery( |
| 38 | + bundles: ReadonlyArray<Ocr2KeyBundlesPayload_ResultsFields>, |
| 39 | +) { |
| 40 | + return { |
| 41 | + request: { |
| 42 | + query: OCR2_KEY_BUNDLES_QUERY, |
| 43 | + }, |
| 44 | + result: { |
| 45 | + data: { |
| 46 | + ocr2KeyBundles: { |
| 47 | + results: bundles, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +describe('OCR2Keys', () => { |
| 55 | + it('renders the page', async () => { |
| 56 | + const payload = buildOCR2KeyBundles() |
| 57 | + const mocks: MockedResponse[] = [fetchOCR2KeyBundlesQuery(payload)] |
| 58 | + |
| 59 | + renderComponent(mocks) |
| 60 | + |
| 61 | + await waitForLoading() |
| 62 | + |
| 63 | + expect(await findByText(`Key ID: ${payload[0].id}`)).toBeInTheDocument() |
| 64 | + }) |
| 65 | + |
| 66 | + it('renders GQL query errors', async () => { |
| 67 | + const mocks: MockedResponse[] = [ |
| 68 | + { |
| 69 | + request: { |
| 70 | + query: OCR2_KEY_BUNDLES_QUERY, |
| 71 | + }, |
| 72 | + result: { |
| 73 | + errors: [new GraphQLError('Error!')], |
| 74 | + }, |
| 75 | + }, |
| 76 | + ] |
| 77 | + |
| 78 | + renderComponent(mocks) |
| 79 | + |
| 80 | + expect(await findByText('Error!')).toBeInTheDocument() |
| 81 | + }) |
| 82 | + |
| 83 | + it('deletes an OCR2 Key Bundle', async () => { |
| 84 | + const payload = buildOCR2KeyBundle() |
| 85 | + |
| 86 | + const mocks: MockedResponse[] = [ |
| 87 | + fetchOCR2KeyBundlesQuery([payload]), |
| 88 | + { |
| 89 | + request: { |
| 90 | + query: DELETE_OCR2_KEY_BUNDLE_MUTATION, |
| 91 | + variables: { id: payload.id }, |
| 92 | + }, |
| 93 | + result: { |
| 94 | + data: { |
| 95 | + deleteOCR2KeyBundle: { |
| 96 | + __typename: 'DeleteOCR2KeyBundleSuccess', |
| 97 | + bundle: payload, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + fetchOCR2KeyBundlesQuery([]), |
| 103 | + ] |
| 104 | + |
| 105 | + renderComponent(mocks) |
| 106 | + |
| 107 | + expect(await findByText(`Key ID: ${payload.id}`)).toBeInTheDocument() |
| 108 | + |
| 109 | + userEvent.click(getByRole('button', { name: /delete/i })) |
| 110 | + userEvent.click(getByRole('button', { name: /confirm/i })) |
| 111 | + |
| 112 | + await waitForElementToBeRemoved(getByRole('dialog')) |
| 113 | + |
| 114 | + expect( |
| 115 | + await findByText( |
| 116 | + 'Successfully deleted Off-ChainReporting Key Bundle Key', |
| 117 | + ), |
| 118 | + ).toBeInTheDocument() |
| 119 | + |
| 120 | + expect(queryByText(`Key ID: ${payload.id}`)).toBeNull() |
| 121 | + }) |
| 122 | +}) |
0 commit comments