Skip to content

: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.

test(quill): add jest + RTL coverage for DatePicker and DateTimePicker (#65102)

Paul D'Ambraa2f2c29e3a33
diff --git a/frontend/jest.config.ts b/frontend/jest.config.ts
index 0811af581f0a..44fdb8c25ac8 100644
--- a/frontend/jest.config.ts
+++ b/frontend/jest.config.ts
@@ -76,7 +76,12 @@ const esmModules = [
'yaml/browser',
]
function rootDirectories(): string[] {
- return ['<rootDir>/src', '<rootDir>/../products', '<rootDir>/../packages/quill/packages/charts/src']
+ return [
+ '<rootDir>/src',
+ '<rootDir>/../products',
+ '<rootDir>/../packages/quill/packages/charts/src',
+ '<rootDir>/../packages/quill/packages/components/src',
+ ]
}
const config: Config = {
diff --git a/frontend/package.json b/frontend/package.json
index 6f18a413d0e2..7468a6014caf 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -35,7 +35,7 @@
"typegen:write": "cd .. && NODE_OPTIONS=--max-old-space-size=8192 kea-typegen write --delete --show-ts-errors --use-cache",
"typegen:write:no-cache": "cd .. && NODE_OPTIONS=--max-old-space-size=8192 kea-typegen write --delete --show-ts-errors",
"schema:build:json": "ts-node ./bin/build-schema-json.mjs && oxfmt ./src/queries/schema.json && ts-node ./bin/build-validators.mjs && oxfmt ./src/queries/validators.js",
- "test": "SHARD_IDX=${SHARD_INDEX:-1}; SHARD_TOTAL=${SHARD_COUNT:-1}; echo $SHARD_IDX/$SHARD_TOTAL; pnpm build:products && jest --testPathPattern='(frontend/|products/|common/)' --forceExit --shard=$SHARD_IDX/$SHARD_TOTAL",
+ "test": "SHARD_IDX=${SHARD_INDEX:-1}; SHARD_TOTAL=${SHARD_COUNT:-1}; echo $SHARD_IDX/$SHARD_TOTAL; pnpm build:products && jest --testPathPattern='(frontend/|products/|common/|packages/quill/packages/components/)' --forceExit --shard=$SHARD_IDX/$SHARD_TOTAL",
"jest": "pnpm build:products && jest",
"typescript:check": "tsgo --noEmit && echo \"No errors reported by tsgo.\"",
"typescript:version": "tsgo --version",
diff --git a/packages/quill/packages/components/src/date-picker.test.tsx b/packages/quill/packages/components/src/date-picker.test.tsx
new file mode 100644
index 000000000000..efddd6b1e98a
--- /dev/null
+++ b/packages/quill/packages/components/src/date-picker.test.tsx
@@ -0,0 +1,69 @@
+import { cleanup, render, screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+
+import { DatePicker } from './date-picker'
+
+// Base UI's Switch constructs PointerEvents, which jsdom doesn't implement.
+if (typeof window.PointerEvent === 'undefined') {
+ window.PointerEvent = class extends MouseEvent {} as typeof window.PointerEvent
+}
+
+const VALUE = new Date(2023, 0, 15, 10, 30) // 15 Jan 2023, 10:30
+const MAX = new Date(2030, 0, 1)
+
+describe('DatePicker', () => {
+ afterEach(cleanup)
+
+ it('applies a date-only value floored to the start of the day', async () => {
+ const onApply = jest.fn()
+ render(<DatePicker value={VALUE} maxDate={MAX} onApply={onApply} />)
+
+ await userEvent.click(screen.getByLabelText('Select Jan 20, 2023'))
+ await userEvent.click(screen.getByLabelText('Apply date'))
+
+ expect(onApply).toHaveBeenCalledTimes(1)
+ const applied: Date = onApply.mock.calls[0][0]
+ expect([applied.getFullYear(), applied.getMonth(), applied.getDate()]).toEqual([2023, 0, 20])
+ expect([applied.getHours(), applied.getMinutes()]).toEqual([0, 0])
+ })
+
+ it('keeps the time of day when showTime is on', async () => {
+ const onApply = jest.fn()
+ render(<DatePicker value={VALUE} maxDate={MAX} showTime onApply={onApply} />)
+
+ await userEvent.click(screen.getByLabelText('Select Jan 20, 2023'))
+ await userEvent.click(screen.getByLabelText('Apply date'))
+
+ const applied: Date = onApply.mock.calls[0][0]
+ expect([applied.getDate(), applied.getHours(), applied.getMinutes()]).toEqual([20, 10, 30])
+ })
+
+ it('re-floors to the start of the day when the time toggle is switched off', async () => {
+ const onApply = jest.fn()
+ render(<DatePicker value={VALUE} maxDate={MAX} showTime onApply={onApply} />)
+
+ await userEvent.click(screen.getByRole('switch', { name: 'Include time' })) // turn time off
+ await userEvent.click(screen.getByLabelText('Apply date'))
+
+ const applied: Date = onApply.mock.calls[0][0]
+ expect([applied.getDate(), applied.getHours(), applied.getMinutes()]).toEqual([15, 0, 0])
+ })
+
+ it('disables calendar days before minDate', () => {
+ render(<DatePicker value={VALUE} minDate={new Date(2023, 0, 10)} maxDate={MAX} onApply={jest.fn()} />)
+
+ expect(screen.getByLabelText('Select Jan 5, 2023').getAttribute('aria-disabled')).toBe('true')
+ expect(screen.getByLabelText('Select Jan 20, 2023').getAttribute('aria-disabled')).toBe('false')
+ })
+
+ it('calls onCancel without applying', async () => {
+ const onApply = jest.fn()
+ const onCancel = jest.fn()
+ render(<DatePicker value={VALUE} maxDate={MAX} onApply={onApply} onCancel={onCancel} />)
+
+ await userEvent.click(screen.getByLabelText('Cancel'))
+
+ expect(onCancel).toHaveBeenCalledTimes(1)
+ expect(onApply).not.toHaveBeenCalled()
+ })
+})
diff --git a/packages/quill/packages/components/src/date-time-picker.test.tsx b/packages/quill/packages/components/src/date-time-picker.test.tsx
new file mode 100644
index 000000000000..3af7fbbe4a69
--- /dev/null
+++ b/packages/quill/packages/components/src/date-time-picker.test.tsx
@@ -0,0 +1,47 @@
+import { cleanup, render, screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+
+import { CUSTOM_RANGE } from './date-time-ranges'
+import { DateTimePicker } from './date-time-picker'
+
+// The day grid wraps each Button in a div carrying the data-is-* range flags.
+const dayCell = (label: string): HTMLElement => {
+ const cell = screen.getByLabelText(label).closest('[data-is-start]')
+ if (!(cell instanceof HTMLElement)) {
+ throw new Error(`No range-flagged day cell found for "${label}"`)
+ }
+ return cell
+}
+
+// Range entirely inside value.end's month (January), which the always-rendered right calendar shows.
+const VALUE = { start: new Date(2023, 0, 10), end: new Date(2023, 0, 20), range: CUSTOM_RANGE }
+const MAX = new Date(2030, 0, 1)
+
+describe('DateTimePicker', () => {
+ afterEach(cleanup)
+
+ it('marks the range start, end, and in-between days from the shared grid', () => {
+ render(<DateTimePicker value={VALUE} maxDate={MAX} onApply={jest.fn()} onCancel={jest.fn()} />)
+
+ expect(dayCell('Select Jan 10, 2023').getAttribute('data-is-start')).toBe('true')
+ expect(dayCell('Select Jan 20, 2023').getAttribute('data-is-end')).toBe('true')
+ expect(dayCell('Select Jan 15, 2023').getAttribute('data-is-between')).toBe('true')
+
+ const outside = dayCell('Select Jan 25, 2023')
+ expect(outside.getAttribute('data-is-start')).toBe('false')
+ expect(outside.getAttribute('data-is-end')).toBe('false')
+ expect(outside.getAttribute('data-is-between')).toBe('false')
+ })
+
+ it('applies the current range unchanged', async () => {
+ const onApply = jest.fn()
+ render(<DateTimePicker value={VALUE} maxDate={MAX} onApply={onApply} onCancel={jest.fn()} />)
+
+ await userEvent.click(screen.getByLabelText('Apply date range'))
+
+ expect(onApply).toHaveBeenCalledTimes(1)
+ const applied = onApply.mock.calls[0][0]
+ expect([applied.start.getDate(), applied.end.getDate()]).toEqual([10, 20])
+ expect(applied.range).toBe(CUSTOM_RANGE)
+ })
+})
diff --git a/packages/quill/packages/components/tsconfig.build.json b/packages/quill/packages/components/tsconfig.build.json
index b994fd0e4019..052175207487 100644
--- a/packages/quill/packages/components/tsconfig.build.json
+++ b/packages/quill/packages/components/tsconfig.build.json
@@ -16,5 +16,5 @@
}
},
"include": ["src"],
- "exclude": ["src/**/*.stories.tsx", "src/**/*.stories.ts"]
+ "exclude": ["src/**/*.stories.tsx", "src/**/*.stories.ts", "src/**/*.test.tsx", "src/**/*.test.ts"]
}