mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-29 06:15:29 -04:00
refactor(tests): add test utils for mocking common things
Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
parent
b3fb1bbf30
commit
ad80b444ff
16 changed files with 186 additions and 117 deletions
56
frontend/src/test-utils/mock-app-state.ts
Normal file
56
frontend/src/test-utils/mock-app-state.ts
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import * as useApplicationStateModule from '../hooks/common/use-application-state'
|
||||
import type { ApplicationState } from '../redux/application-state'
|
||||
import type { DeepPartial } from 'redux'
|
||||
import { initialState as initialStateDarkMode } from '../redux/dark-mode/reducers'
|
||||
import { initialState as initialStateEditorConfig } from '../redux/editor/reducers'
|
||||
import { initialState as initialStateNoteDetails } from '../redux/note-details/initial-state'
|
||||
import { initialState as initialStateRealtimeStatus } from '../redux/realtime/reducers'
|
||||
import { initialState as initialStateRendererStatus } from '../redux/renderer-status/reducers'
|
||||
import type { NoteDetails } from '../redux/note-details/types/note-details'
|
||||
import type { RealtimeStatus } from '../redux/realtime/types'
|
||||
import type { HistoryEntryWithOrigin } from '../api/history/types'
|
||||
|
||||
jest.mock('../redux/editor/methods', () => ({
|
||||
loadFromLocalStorage: jest.fn().mockReturnValue(undefined)
|
||||
}))
|
||||
jest.mock('../hooks/common/use-application-state')
|
||||
|
||||
/**
|
||||
* Mocks the {@link ApplicationState} for a test.
|
||||
* When not overriden, it uses the initial state of the reducers.
|
||||
*
|
||||
* @param state Overrides for the mocked state
|
||||
*/
|
||||
export const mockAppState = (state?: DeepPartial<ApplicationState>) => {
|
||||
jest.spyOn(useApplicationStateModule, 'useApplicationState').mockImplementation((fn) => {
|
||||
return fn({
|
||||
darkMode: {
|
||||
...initialStateDarkMode,
|
||||
...state?.darkMode
|
||||
},
|
||||
editorConfig: {
|
||||
...initialStateEditorConfig,
|
||||
...state?.editorConfig
|
||||
},
|
||||
history: (state?.history ?? []) as HistoryEntryWithOrigin[],
|
||||
noteDetails: {
|
||||
...initialStateNoteDetails,
|
||||
...state?.noteDetails
|
||||
} as NoteDetails,
|
||||
realtimeStatus: {
|
||||
...initialStateRealtimeStatus,
|
||||
...state?.realtimeStatus
|
||||
} as RealtimeStatus,
|
||||
rendererStatus: {
|
||||
...initialStateRendererStatus,
|
||||
...state?.rendererStatus
|
||||
},
|
||||
user: state?.user ?? null
|
||||
})
|
||||
})
|
||||
}
|
43
frontend/src/test-utils/mock-note-permissions.ts
Normal file
43
frontend/src/test-utils/mock-note-permissions.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import type { ApplicationState } from '../redux/application-state'
|
||||
import type { DeepPartial } from 'redux'
|
||||
import { mockAppState } from './mock-app-state'
|
||||
import type { LoginUserInfo } from '../api/me/types'
|
||||
import type { NotePermissions } from '@hedgedoc/commons'
|
||||
|
||||
/**
|
||||
* Mocks the {@link NotePermissions} field of a note in the {@link ApplicationState }for a test.
|
||||
* This test-util should not be used alongside {@link mockAppState} to avoid overwriting the mocked state.
|
||||
*
|
||||
* @param ownUsername The name of the own user to set in the state (for comparing note ownership)
|
||||
* @param noteOwner The owner's username of the mocked note
|
||||
* @param permissions Overrides for the mocked permissions
|
||||
* @param additionalState Overrides for the overall mocked application state
|
||||
*/
|
||||
export const mockNotePermissions = (
|
||||
ownUsername: string,
|
||||
noteOwner: string,
|
||||
permissions?: DeepPartial<NotePermissions>,
|
||||
additionalState?: DeepPartial<ApplicationState>
|
||||
) => {
|
||||
mockAppState({
|
||||
...additionalState,
|
||||
noteDetails: {
|
||||
...additionalState?.noteDetails,
|
||||
permissions: {
|
||||
sharedToGroups: [],
|
||||
sharedToUsers: [],
|
||||
...permissions,
|
||||
owner: noteOwner
|
||||
}
|
||||
},
|
||||
user: {
|
||||
...additionalState?.user,
|
||||
username: ownUsername
|
||||
} as LoginUserInfo
|
||||
})
|
||||
}
|
19
frontend/src/test-utils/mock-ui-notifications.ts
Normal file
19
frontend/src/test-utils/mock-ui-notifications.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import * as useUiNotificationsModule from '../components/notifications/ui-notification-boundary'
|
||||
|
||||
jest.mock('../components/notifications/ui-notification-boundary')
|
||||
|
||||
/**
|
||||
* Mocks the {@link useUiNotifications} hook with stub functions for tests.
|
||||
*/
|
||||
export const mockUiNotifications = () => {
|
||||
jest.spyOn(useUiNotificationsModule, 'useUiNotifications').mockReturnValue({
|
||||
showErrorNotification: jest.fn(),
|
||||
dismissNotification: jest.fn(),
|
||||
dispatchUiNotification: jest.fn()
|
||||
})
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import * as useApplicationStateModule from '../hooks/common/use-application-state'
|
||||
import type { ApplicationState } from '../redux/application-state'
|
||||
|
||||
jest.mock('../hooks/common/use-application-state')
|
||||
export const mockNoteOwnership = (
|
||||
ownUsername: string,
|
||||
noteOwner: string,
|
||||
additionalState?: Partial<ApplicationState>
|
||||
) => {
|
||||
jest.spyOn(useApplicationStateModule, 'useApplicationState').mockImplementation((fn) => {
|
||||
return fn({
|
||||
...additionalState,
|
||||
noteDetails: {
|
||||
...additionalState?.noteDetails,
|
||||
permissions: {
|
||||
owner: noteOwner
|
||||
}
|
||||
},
|
||||
user: {
|
||||
...additionalState?.user,
|
||||
username: ownUsername
|
||||
}
|
||||
} as ApplicationState)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue