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-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 { mockI18n } from '../../../markdown-renderer/test-utils/mock-i18n'
import { CopyToClipboardButton } from './copy-to-clipboard-button' 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' import * as uuidModule from 'uuid'
jest.mock('uuid') jest.mock('uuid')
@ -46,7 +40,9 @@ describe('Copy to clipboard button', () => {
const view = render(copyToClipboardButton) const view = render(copyToClipboardButton)
expect(view.container).toMatchSnapshot() expect(view.container).toMatchSnapshot()
const button = await screen.findByTitle('renderer.highlightCode.copyCode') const button = await screen.findByTitle('renderer.highlightCode.copyCode')
act(() => {
button.click() button.click()
})
const tooltip = await screen.findByRole('tooltip') const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveTextContent(expectSuccess ? 'copyOverlay.success' : 'copyOverlay.error') expect(tooltip).toHaveTextContent(expectSuccess ? 'copyOverlay.success' : 'copyOverlay.error')
expect(tooltip).toHaveAttribute('id', overlayId) 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>
<div <div
class="fade mt-5 alert alert-info show" class="fade mt-5 alert alert-info show"
data-testid="failedMessage" data-testid="createNoteMessage"
role="alert" role="alert"
> >
<span> <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`] = ` exports[`create non existing note hint shows an error message if note couldn't be created 1`] = `
<div> <div>
<div <div
class="fade mt-5 alert alert-info show" class="fade mt-5 alert alert-danger show"
data-testid="loadingMessage" data-testid="failedMessage"
role="alert" role="alert"
> >
<i <i
class="fa fa-spinner fa-spin mr-2 " class="fa fa-exclamation-triangle mr-1 "
/> />
noteLoadingBoundary.createNote.creating noteLoadingBoundary.createNote.error
</div> </div>
</div> </div>
`; `;

View file

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

View file

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

View file

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