diff --git a/src/components/common/copyable/copy-to-clipboard-button/copy-to-clipboard-button.test.tsx b/src/components/common/copyable/copy-to-clipboard-button/copy-to-clipboard-button.test.tsx
index 2c45a9624..b66bab8e2 100644
--- a/src/components/common/copyable/copy-to-clipboard-button/copy-to-clipboard-button.test.tsx
+++ b/src/components/common/copyable/copy-to-clipboard-button/copy-to-clipboard-button.test.tsx
@@ -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)
diff --git a/src/components/common/note-loading-boundary/__snapshots__/create-non-existing-note-hint.test.tsx.snap b/src/components/common/note-loading-boundary/__snapshots__/create-non-existing-note-hint.test.tsx.snap
index a2eed506b..2159a9540 100644
--- a/src/components/common/note-loading-boundary/__snapshots__/create-non-existing-note-hint.test.tsx.snap
+++ b/src/components/common/note-loading-boundary/__snapshots__/create-non-existing-note-hint.test.tsx.snap
@@ -35,7 +35,7 @@ exports[`create non existing note hint renders an button as initial state 1`] =
@@ -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`] = `
- noteLoadingBoundary.createNote.creating
+ noteLoadingBoundary.createNote.error
`;
diff --git a/src/components/common/note-loading-boundary/create-non-existing-note-hint.test.tsx b/src/components/common/note-loading-boundary/create-non-existing-note-hint.test.tsx
index 5fd41bf69..49c0e7feb 100644
--- a/src/components/common/note-loading-boundary/create-non-existing-note-hint.test.tsx
+++ b/src/components/common/note-loading-boundary/create-non-existing-note-hint.test.tsx
@@ -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 => {
+ .mockImplementation(async (markdown, primaryAlias): Promise => {
expect(markdown).toBe('')
expect(primaryAlias).toBe(mockedNoteId)
const metadata: NoteMetadata = Mock.of({ primaryAddress: 'mockedPrimaryAlias' })
- return Promise.resolve(Mock.of({ metadata }))
+ await waitForOtherPromisesToFinish()
+ return Mock.of({ metadata })
})
}
const mockFailingCreateNoteWithPrimaryAlias = () => {
jest
.spyOn(createNoteWithPrimaryAliasModule, 'createNoteWithPrimaryAlias')
- .mockImplementation((markdown, primaryAlias): Promise => {
+ .mockImplementation(async (markdown, primaryAlias): Promise => {
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()
+ await screen.findByTestId('createNoteMessage')
expect(view.container).toMatchSnapshot()
})
@@ -64,7 +71,9 @@ describe('create non existing note hint', () => {
mockCreateNoteWithPrimaryAlias()
const view = render()
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()
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()
const button = await screen.findByTestId('createNoteButton')
- button.click()
+ act(() => {
+ button.click()
+ })
await screen.findByTestId('failedMessage')
expect(view.container).toMatchSnapshot()
})
diff --git a/src/components/common/note-loading-boundary/create-non-existing-note-hint.tsx b/src/components/common/note-loading-boundary/create-non-existing-note-hint.tsx
index daa11046a..ef33b96d0 100644
--- a/src/components/common/note-loading-boundary/create-non-existing-note-hint.tsx
+++ b/src/components/common/note-loading-boundary/create-non-existing-note-hint.tsx
@@ -54,7 +54,7 @@ export const CreateNonExistingNoteHint: React.FC = () => {
)
} else {
return (
-
+
diff --git a/src/components/common/user-avatar/user-avatar.test.tsx b/src/components/common/user-avatar/user-avatar.test.tsx
index a68492fc2..21e412ed8 100644
--- a/src/components/common/user-avatar/user-avatar.test.tsx
+++ b/src/components/common/user-avatar/user-avatar.test.tsx
@@ -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()
expect(view.container).toMatchSnapshot()
diff --git a/src/utils/wait-for-other-promises-to-finish.ts b/src/utils/wait-for-other-promises-to-finish.ts
new file mode 100644
index 000000000..1cda0349e
--- /dev/null
+++ b/src/utils/wait-for-other-promises-to-finish.ts
@@ -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 {
+ return new Promise((resolve) => process.nextTick(resolve))
+}