:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { expectLogic } from 'kea-test-utils'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { InsightModel, InsightShortId, SubscriptionType } from '~/types'
import { subscriptionsLogic } from './subscriptionsLogic'
const Insight1 = '1' as InsightShortId
const Insight2 = '2' as InsightShortId
export const fixtureSubscriptionResponse = (id: number, args: Partial<SubscriptionType> = {}): SubscriptionType =>
({
id,
title: 'My example subscription',
target_type: 'email',
target_value: '[email protected],[email protected]',
frequency: 'monthly',
interval: 2,
start_date: '2022-01-01T00:09:00',
byweekday: ['wednesday'],
bysetpos: 1,
...args,
}) as SubscriptionType
function fixtureInsightResponse(id: number, data?: Partial<InsightModel>): Partial<InsightModel> {
return {
id: id,
short_id: id.toString() as InsightShortId,
name: 'insight',
result: [`result ${id}`],
...data,
}
}
describe('subscriptionsLogic', () => {
let logic: ReturnType<typeof subscriptionsLogic.build>
let subscriptions: SubscriptionType[] = []
let tileSubscriptions: SubscriptionType[] = []
beforeEach(async () => {
subscriptions = [fixtureSubscriptionResponse(1), fixtureSubscriptionResponse(2)]
tileSubscriptions = [fixtureSubscriptionResponse(20)]
useMocks({
get: {
'/api/environments/:team_id/insights/1': fixtureInsightResponse(1),
'/api/environments/:team_id/insights/2': fixtureInsightResponse(2),
'/api/environments/:team_id/insights': ({ request }) => {
const insightShortId = new URL(request.url).searchParams.get('short_id')
const res = insightShortId ? [fixtureInsightResponse(parseInt(insightShortId, 10))] : []
return [200, { results: res }]
},
'/api/environments/:team_id/subscriptions': ({ request }) => {
const url = new URL(request.url)
const insightIds = url.searchParams.get('insights')?.split(',') ?? []
const dashboardTiles = url.searchParams.get('dashboard_tiles')
let results: SubscriptionType[] = []
if (dashboardTiles === '9') {
results = tileSubscriptions
} else if (insightIds.includes(Insight2)) {
results = subscriptions
}
return [
200,
{
results,
count: results.length,
},
]
},
},
})
initKeaTests()
logic = subscriptionsLogic({
insightShortId: Insight1,
})
logic.mount()
})
it('loads subscriptions', async () => {
await expectLogic(logic).toFinishListeners().toMatchValues({
subscriptions: [],
subscriptionsLoading: false,
})
logic = subscriptionsLogic({
insightShortId: Insight2,
})
logic.mount()
await expectLogic(logic).toFinishListeners().toMatchValues({
subscriptions: subscriptions,
subscriptionsLoading: false,
})
})
it('loads subscriptions on dashboard tiles via the server-side dashboard_tiles filter', async () => {
logic = subscriptionsLogic({ dashboardId: 9 })
logic.mount()
await expectLogic(logic).toFinishListeners().toMatchValues({
insightSubscriptions: tileSubscriptions,
insightSubscriptionsLoading: false,
})
})
it('does not load tile subscriptions outside a dashboard context', async () => {
await expectLogic(logic).toFinishListeners().toMatchValues({
insightSubscriptions: [],
insightSubscriptionsLoading: false,
})
})
})