analytics/assets/test-utils/app-context-providers.tsx
Artur Pata 51fba1c2cf
Add tests for FE date calculations, fix niggles with site context (#4471)
* Adds tests for FE date calculations during DST changeover times
* Fixes unparsed offset in SiteContext
* Fixes unparsed embedded in SiteContext
* Adds tests for SiteContext
2024-08-28 11:14:29 +03:00

76 lines
2.1 KiB
TypeScript

/** @format */
import React, { ReactNode } from 'react'
import SiteContextProvider, {
PlausibleSite
} from '../js/dashboard/site-context'
import UserContextProvider, { Role } from '../js/dashboard/user-context'
import { MemoryRouter, MemoryRouterProps } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import QueryContextProvider from '../js/dashboard/query-context'
import { getRouterBasepath } from '../js/dashboard/router'
type TestContextProvidersProps = {
children: ReactNode
routerProps?: Pick<MemoryRouterProps, 'initialEntries'>
siteOptions?: Partial<PlausibleSite>
}
export const TestContextProviders = ({
children,
routerProps,
siteOptions
}: TestContextProvidersProps) => {
const defaultSite: PlausibleSite = {
domain: 'plausible.io/unit',
offset: 0,
hasGoals: false,
hasProps: false,
funnelsAvailable: false,
propsAvailable: false,
conversionsOptedOut: false,
funnelsOptedOut: false,
propsOptedOut: false,
revenueGoals: [],
funnels: [],
statsBegin: '',
nativeStatsBegin: '',
embedded: false,
background: '',
isDbip: false,
flags: {},
validIntervalsByPeriod: {},
shared: false
}
const site = { ...defaultSite, ...siteOptions }
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false
}
}
})
const defaultInitialEntries = [getRouterBasepath(site)]
return (
// <ThemeContextProvider> not interactive component, default value is suitable
<SiteContextProvider site={site}>
<UserContextProvider role={Role.admin} loggedIn={true}>
<MemoryRouter
basename={getRouterBasepath(site)}
initialEntries={defaultInitialEntries}
{...routerProps}
>
<QueryClientProvider client={queryClient}>
<QueryContextProvider>{children}</QueryContextProvider>
</QueryClientProvider>
</MemoryRouter>
</UserContextProvider>
</SiteContextProvider>
// </ThemeContextProvider>
)
}