2021-08-05 18:52:15 +05:30
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {IntlProvider} from 'react-intl'
|
|
|
|
import React from 'react'
|
2021-10-04 12:27:37 -06:00
|
|
|
import {DndProvider} from 'react-dnd'
|
|
|
|
import {HTML5Backend} from 'react-dnd-html5-backend'
|
2021-10-04 06:22:30 +02:00
|
|
|
import configureStore, {MockStoreEnhanced} from 'redux-mock-store'
|
|
|
|
import {Middleware} from 'redux'
|
2021-08-05 18:52:15 +05:30
|
|
|
|
2022-01-13 23:26:27 +07:00
|
|
|
import {Block} from './blocks/block'
|
|
|
|
|
2021-09-15 04:35:41 +07:00
|
|
|
export const wrapIntl = (children?: React.ReactNode): JSX.Element => <IntlProvider locale='en'>{children}</IntlProvider>
|
2021-10-04 12:27:37 -06:00
|
|
|
export const wrapDNDIntl = (children?: React.ReactNode): JSX.Element => {
|
|
|
|
return (
|
|
|
|
<DndProvider backend={HTML5Backend}>
|
|
|
|
{wrapIntl(children)}
|
|
|
|
</DndProvider>
|
|
|
|
)
|
|
|
|
}
|
2021-08-05 18:52:15 +05:30
|
|
|
|
|
|
|
export function mockDOM(): void {
|
|
|
|
window.focus = jest.fn()
|
|
|
|
document.createRange = () => {
|
|
|
|
const range = new Range()
|
|
|
|
range.getBoundingClientRect = jest.fn()
|
|
|
|
range.getClientRects = () => {
|
|
|
|
return {
|
|
|
|
item: () => null,
|
|
|
|
length: 0,
|
|
|
|
[Symbol.iterator]: jest.fn(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return range
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 17:56:32 -03:00
|
|
|
export function mockMatchMedia(result: {matches: boolean}): void {
|
2021-09-20 20:06:05 +05:30
|
|
|
// We check if system preference is dark or light theme.
|
|
|
|
// This is required to provide it's definition since
|
|
|
|
// window.matchMedia doesn't exist in Jest.
|
|
|
|
Object.defineProperty(window, 'matchMedia', {
|
|
|
|
writable: true,
|
|
|
|
value: jest.fn().mockImplementation(() => {
|
|
|
|
return result
|
|
|
|
|
|
|
|
// return ({
|
|
|
|
// matches: true,
|
|
|
|
// })
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
}
|
2021-10-04 06:22:30 +02:00
|
|
|
|
2022-09-14 15:11:50 +02:00
|
|
|
export function mockStateStore(middleware: Middleware[], state: unknown): MockStoreEnhanced<unknown, unknown> {
|
2021-10-04 06:22:30 +02:00
|
|
|
const mockStore = configureStore(middleware)
|
|
|
|
return mockStore(state)
|
|
|
|
}
|
2022-01-13 23:26:27 +07:00
|
|
|
|
|
|
|
export type BlocksById<BlockType> = {[key: string]: BlockType}
|
|
|
|
|
2022-09-14 15:11:50 +02:00
|
|
|
export function blocksById<BlockType extends Block>(blocks: BlockType[]): BlocksById<BlockType> {
|
2022-01-13 23:26:27 +07:00
|
|
|
return blocks.reduce((res, block) => {
|
|
|
|
res[block.id] = block
|
|
|
|
return res
|
|
|
|
}, {} as BlocksById<BlockType>)
|
|
|
|
}
|