: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 posthog from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { AccessControlLevel, DashboardType } from '~/types'
import { textCardModalLogic } from './textCardModalLogic'
const makeDashboard = (body: string = 'existing text', agentContext: string = ''): DashboardType =>
({
id: 123,
name: 'Test dashboard',
description: '',
pinned: false,
created_at: '2024-01-01T00:00:00Z',
created_by: null,
last_accessed_at: null,
is_shared: false,
deleted: false,
creation_mode: 'default',
tiles: [
{
id: 1,
color: null,
layouts: {},
text: {
body,
agent_context: agentContext,
last_modified_at: '2024-01-01T00:00:00Z',
},
},
],
filters: {},
tags: [],
user_access_level: AccessControlLevel.Editor,
}) as DashboardType
describe('textCardModalLogic', () => {
beforeEach(() => {
initKeaTests()
jest.spyOn(lemonToast, 'error').mockImplementation(jest.fn())
jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)
})
afterEach(() => {
jest.restoreAllMocks()
})
it('does not show toast for expected form validation errors', async () => {
const logic = textCardModalLogic({
dashboard: makeDashboard('x'.repeat(4001)),
textTileId: 1,
onClose: jest.fn(),
tileType: 'text',
})
logic.mount()
await expectLogic(logic).toMatchValues({
textTileValidationErrors: {
body: 'Text is too long (4000 characters max)',
agent_context: null,
},
})
logic.actions.submitTextTileFailure({ error: 'Validation failed', errors: {} } as any, {})
expect(lemonToast.error).not.toHaveBeenCalled()
})
it('rejects empty text card body in form validation', async () => {
const logic = textCardModalLogic({
dashboard: makeDashboard(''),
textTileId: null,
onClose: jest.fn(),
tileType: 'text',
})
logic.mount()
await expectLogic(logic).toMatchValues({
textTileValidationErrors: {
body: 'This card would be empty! Type something first',
agent_context: null,
},
})
logic.actions.submitTextTileFailure({ error: 'Validation failed', errors: {} } as any, {})
expect(lemonToast.error).not.toHaveBeenCalled()
})
it('defaults new image tiles to a transparent background', () => {
const logic = textCardModalLogic({
dashboard: makeDashboard(),
textTileId: null,
onClose: jest.fn(),
tileType: 'image',
})
logic.mount()
expect(logic.values.textTile.transparent_background).toBe(true)
})
it('loads existing agent context', () => {
const logic = textCardModalLogic({
dashboard: makeDashboard('Dashboard summary', 'Data Catalog metric: activation_rate'),
textTileId: 1,
onClose: jest.fn(),
tileType: 'text',
})
logic.mount()
expect(logic.values.textTile.agent_context).toBe('Data Catalog metric: activation_rate')
})
it('does not mutate the dashboard tile before an update succeeds', () => {
useMocks({
patch: {
'/api/projects/:team_id/dashboards/:id/': () => new Promise(() => {}),
},
})
const dashboard = makeDashboard('Original summary', 'Original agent context')
const logic = textCardModalLogic({
dashboard,
textTileId: 1,
onClose: jest.fn(),
tileType: 'text',
})
logic.mount()
logic.actions.setTextTileValues({
body: 'Updated summary',
agent_context: 'Updated agent context',
})
logic.actions.submitTextTile()
expect(dashboard.tiles?.[0].text?.body).toBe('Original summary')
expect(dashboard.tiles?.[0].text?.agent_context).toBe('Original agent context')
})
it('does not show toast for expected api body validation errors', () => {
const logic = textCardModalLogic({
dashboard: makeDashboard('valid'),
textTileId: 1,
onClose: jest.fn(),
tileType: 'text',
})
logic.mount()
logic.actions.submitTextTileFailure(
{
error: 'Validation failed',
errors: {},
} as any,
{ body: ['Text is too long (4000 characters max)'] }
)
expect(lemonToast.error).not.toHaveBeenCalled()
})
it('shows toast for unexpected submit failures', () => {
const logic = textCardModalLogic({
dashboard: makeDashboard('valid'),
textTileId: 1,
onClose: jest.fn(),
tileType: 'text',
})
logic.mount()
logic.actions.submitTextTileFailure(
{
error: 'Network error',
errors: {},
} as any,
{}
)
expect(lemonToast.error).toHaveBeenCalledWith('Could not save text: Network error')
})
it('uses image wording for unexpected image tile save failures', () => {
const logic = textCardModalLogic({
dashboard: makeDashboard(''),
textTileId: 1,
onClose: jest.fn(),
tileType: 'image',
})
logic.mount()
logic.actions.submitTextTileFailure(
{
error: 'Network error',
errors: {},
} as any,
{}
)
expect(lemonToast.error).toHaveBeenCalledWith('Could not save image: Network error')
})
it.each([
['image', ''],
['text', 'Dashboard context'],
])('reports %s content type when a text tile saves', (contentType, body) => {
const agentContext = 'Data Catalog metric: activation_rate'
const onClose = jest.fn()
const logic = textCardModalLogic({
dashboard: makeDashboard(),
textTileId: null,
onClose,
tileType: 'text',
})
logic.mount()
logic.actions.submitTextTileSuccess({
body,
agent_context: agentContext,
transparent_background: false,
})
expect(onClose).toHaveBeenCalledTimes(1)
expect(posthog.capture).toHaveBeenCalledWith(
'dashboard text tile saved',
expect.objectContaining({
content_type: contentType,
has_agent_context: true,
agent_context_length: agentContext.length,
})
)
})
})