Fixes warnings in some tests (#2241)

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-07-31 17:29:00 +02:00 committed by GitHub
parent cf72077920
commit a802656478
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 25 deletions

View file

@ -4,15 +4,9 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { mockI18n } from '../../../markdown-renderer/test-utils/mock-i18n'
import { CopyToClipboardButton } from './copy-to-clipboard-button'
import { render, screen } from '@testing-library/react'
import { act, render, screen } from '@testing-library/react'
import * as uuidModule from 'uuid'
jest.mock('uuid')
@ -46,7 +40,9 @@ describe('Copy to clipboard button', () => {
const view = render(copyToClipboardButton)
expect(view.container).toMatchSnapshot()
const button = await screen.findByTitle('renderer.highlightCode.copyCode')
button.click()
act(() => {
button.click()
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveTextContent(expectSuccess ? 'copyOverlay.success' : 'copyOverlay.error')
expect(tooltip).toHaveAttribute('id', overlayId)

View file

@ -35,7 +35,7 @@ exports[`create non existing note hint renders an button as initial state 1`] =
<div>
<div
class="fade mt-5 alert alert-info show"
data-testid="failedMessage"
data-testid="createNoteMessage"
role="alert"
>
<span>
@ -59,14 +59,14 @@ exports[`create non existing note hint renders an button as initial state 1`] =
exports[`create non existing note hint shows an error message if note couldn't be created 1`] = `
<div>
<div
class="fade mt-5 alert alert-info show"
data-testid="loadingMessage"
class="fade mt-5 alert alert-danger show"
data-testid="failedMessage"
role="alert"
>
<i
class="fa fa-spinner fa-spin mr-2 "
class="fa fa-exclamation-triangle mr-1 "
/>
noteLoadingBoundary.createNote.creating
noteLoadingBoundary.createNote.error
</div>
</div>
`;

View file

@ -6,11 +6,12 @@
import * as useSingleStringUrlParameterModule from '../../../hooks/common/use-single-string-url-parameter'
import { mockI18n } from '../../markdown-renderer/test-utils/mock-i18n'
import { render, screen } from '@testing-library/react'
import { act, render, screen } from '@testing-library/react'
import { CreateNonExistingNoteHint } from './create-non-existing-note-hint'
import * as createNoteWithPrimaryAliasModule from '../../../api/notes'
import type { Note, NoteMetadata } from '../../../api/notes/types'
import { Mock } from 'ts-mockery'
import { waitForOtherPromisesToFinish } from '../../../utils/wait-for-other-promises-to-finish'
describe('create non existing note hint', () => {
const mockedNoteId = 'mockedNoteId'
@ -26,37 +27,43 @@ describe('create non existing note hint', () => {
const mockCreateNoteWithPrimaryAlias = () => {
jest
.spyOn(createNoteWithPrimaryAliasModule, 'createNoteWithPrimaryAlias')
.mockImplementation((markdown, primaryAlias): Promise<Note> => {
.mockImplementation(async (markdown, primaryAlias): Promise<Note> => {
expect(markdown).toBe('')
expect(primaryAlias).toBe(mockedNoteId)
const metadata: NoteMetadata = Mock.of<NoteMetadata>({ primaryAddress: 'mockedPrimaryAlias' })
return Promise.resolve(Mock.of<Note>({ metadata }))
await waitForOtherPromisesToFinish()
return Mock.of<Note>({ metadata })
})
}
const mockFailingCreateNoteWithPrimaryAlias = () => {
jest
.spyOn(createNoteWithPrimaryAliasModule, 'createNoteWithPrimaryAlias')
.mockImplementation((markdown, primaryAlias): Promise<Note> => {
.mockImplementation(async (markdown, primaryAlias): Promise<Note> => {
expect(markdown).toBe('')
expect(primaryAlias).toBe(mockedNoteId)
return Promise.reject("couldn't create note")
await waitForOtherPromisesToFinish()
throw new Error("couldn't create note")
})
}
beforeAll(async () => {
await mockI18n()
})
afterEach(() => {
jest.resetAllMocks()
jest.resetModules()
})
beforeEach(async () => {
await mockI18n()
beforeEach(() => {
mockGetNoteIdQueryParameter()
})
it('renders an button as initial state', () => {
it('renders an button as initial state', async () => {
mockCreateNoteWithPrimaryAlias()
const view = render(<CreateNonExistingNoteHint></CreateNonExistingNoteHint>)
await screen.findByTestId('createNoteMessage')
expect(view.container).toMatchSnapshot()
})
@ -64,7 +71,9 @@ describe('create non existing note hint', () => {
mockCreateNoteWithPrimaryAlias()
const view = render(<CreateNonExistingNoteHint></CreateNonExistingNoteHint>)
const button = await screen.findByTestId('createNoteButton')
button.click()
act(() => {
button.click()
})
await screen.findByTestId('loadingMessage')
expect(view.container).toMatchSnapshot()
})
@ -73,7 +82,9 @@ describe('create non existing note hint', () => {
mockCreateNoteWithPrimaryAlias()
const view = render(<CreateNonExistingNoteHint></CreateNonExistingNoteHint>)
const button = await screen.findByTestId('createNoteButton')
button.click()
act(() => {
button.click()
})
await screen.findByTestId('redirect')
expect(view.container).toMatchSnapshot()
})
@ -82,7 +93,9 @@ describe('create non existing note hint', () => {
mockFailingCreateNoteWithPrimaryAlias()
const view = render(<CreateNonExistingNoteHint></CreateNonExistingNoteHint>)
const button = await screen.findByTestId('createNoteButton')
button.click()
act(() => {
button.click()
})
await screen.findByTestId('failedMessage')
expect(view.container).toMatchSnapshot()
})

View file

@ -54,7 +54,7 @@ export const CreateNonExistingNoteHint: React.FC = () => {
)
} else {
return (
<Alert variant={'info'} {...testId('failedMessage')} className={'mt-5'}>
<Alert variant={'info'} {...testId('createNoteMessage')} className={'mt-5'}>
<span>
<Trans i18nKey={'noteLoadingBoundary.createNote.question'} values={{ aliasName: noteIdFromUrl }} />
</span>

View file

@ -7,6 +7,7 @@
import { UserAvatar } from './user-avatar'
import { render } from '@testing-library/react'
import type { UserInfo } from '../../../api/users/types'
import { mockI18n } from '../../markdown-renderer/test-utils/mock-i18n'
describe('UserAvatar', () => {
const user: UserInfo = {
@ -14,6 +15,11 @@ describe('UserAvatar', () => {
displayName: 'Boaty McBoatFace',
photo: 'https://example.com/test.png'
}
beforeEach(async () => {
await mockI18n()
})
it('renders the user avatar correctly', () => {
const view = render(<UserAvatar user={user} />)
expect(view.container).toMatchSnapshot()

View file

@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
/**
* Waits until all other pending promises are processed.
*
* NodeJS has a queue for async code that waits for being processed. This method adds a promise to the very end of this queue.
* If the promise is resolved then this means that all other promises before it have been processed as well.
*
* @return A promise which resolves when all other promises have been processed
*/
export function waitForOtherPromisesToFinish(): Promise<void> {
return new Promise((resolve) => process.nextTick(resolve))
}