Skip to content

Commit

Permalink
add simple smoke tests for FileEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Dec 11, 2024
1 parent 2c61c39 commit 3548c54
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
99 changes: 99 additions & 0 deletions catalog/app/components/FileEditor/FileEditor.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import * as React from 'react'
import renderer from 'react-test-renderer'
import { renderHook } from '@testing-library/react-hooks'

import AsyncResult from 'utils/AsyncResult'

import { useState } from './State'
import { Editor } from './FileEditor'

jest.mock('utils/AWS', () => ({ S3: { use: () => {} } }))

jest.mock('./Skeleton', () => () => <div>Skeleton</div>)

jest.mock('utils/NamedRoutes', () => ({
...jest.requireActual('utils/NamedRoutes'),
use: jest.fn(() => ({ urls: {} })),
}))

jest.mock(
'react-router-dom',
jest.fn(() => ({
...jest.requireActual('react-router-dom'),
useParams: jest.fn(() => ({ bucket: 'b', key: 'k' })),
useLocation: jest.fn(() => ({ search: '?edit=true' })),
})),
)

jest.mock(
'components/Preview/Display',
jest.fn(() => () => <h1>Display error</h1>),
)

jest.mock(
'components/Preview/loaders/utils',
jest.fn(() => ({
...jest.requireActual('components/Preview/loaders/utils'),
useObjectGetter: () => ({
case: (cases: any) => AsyncResult.case(cases, AsyncResult.Ok({ Body: 'body' })),
}),
})),
)

jest.mock(
'./TextEditor',
jest.fn(() => () => <h1>Text Editor</h1>),
)

jest.mock(
'constants/config',
jest.fn(() => ({})),
)

const loadMode = jest.fn((): 'fulfilled' => {
throw Promise.resolve(null)
})

jest.mock(
'./loader',
jest.fn(() => ({
loadMode: jest.fn(() => loadMode()),
detect: () => 'text',
useWriteData: () => {},
})),
)

describe('components/FileEditor/FileEditor', () => {
describe('Editor', () => {
const handle = { bucket: 'b', key: 'k' }
const { result } = renderHook(() => useState(handle))
it('Show skeleton', () => {
const tree = renderer
.create(
<Editor
{...result.current}
className="root"
editing={{ brace: 'json' }}
handle={handle}
/>,
)
.toJSON()
expect(tree).toMatchSnapshot()
})

it('Show TextEditor', () => {
loadMode.mockImplementation(() => 'fulfilled')
const tree = renderer
.create(
<Editor
{...result.current}
className="root"
editing={{ brace: 'json' }}
handle={handle}
/>,
)
.toJSON()
expect(tree).toMatchSnapshot()
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`components/FileEditor/FileEditor Editor Show TextEditor 1`] = `
<div
className="makeStyles-tab-1 makeStyles-active-2"
>
<h1>
Text Editor
</h1>
</div>
`;

exports[`components/FileEditor/FileEditor Editor Show skeleton 1`] = `
<div>
Skeleton
</div>
`;

0 comments on commit 3548c54

Please sign in to comment.