From dd6baf221b304e28e07fb5064f298ca14d1de0cd Mon Sep 17 00:00:00 2001 From: David Newell Date: Thu, 5 Sep 2024 11:02:29 +0100 Subject: [PATCH] much cleaner tests --- frontend/src/queries/schema.json | 2 +- .../sessionRecordingsPlaylistLogic.test.ts | 47 +++++++++++++++---- .../sessionRecordingsPlaylistLogic.ts | 18 +++---- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.json index b50f5627db5f0..755dbecfab0b3 100644 --- a/frontend/src/queries/schema.json +++ b/frontend/src/queries/schema.json @@ -8846,7 +8846,7 @@ "$ref": "#/definitions/DurationType" }, { - "const": "latest", + "const": "start_time", "type": "string" }, { diff --git a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.test.ts b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.test.ts index 695db20866757..b79c7f805a7cb 100644 --- a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.test.ts +++ b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.test.ts @@ -15,9 +15,28 @@ import { describe('sessionRecordingsPlaylistLogic', () => { let logic: ReturnType - const aRecording = { id: 'abc', viewed: false, recording_duration: 10, console_error_count: 50 } - const bRecording = { id: 'def', viewed: false, recording_duration: 10, console_error_count: 100 } + const aRecording = { + id: 'abc', + viewed: false, + recording_duration: 10, + start_time: '2023-10-12T16:55:36.404000Z', + console_error_count: 50, + } + const bRecording = { + id: 'def', + viewed: false, + recording_duration: 10, + start_time: '2023-05-12T16:55:36.404000Z', + console_error_count: 100, + } const listOfSessionRecordings = [aRecording, bRecording] + const offsetRecording = { + id: `recording_offset_by_${listOfSessionRecordings.length}`, + viewed: false, + recording_duration: 10, + start_time: '2023-08-12T16:55:36.404000Z', + console_error_count: 75, + } beforeEach(() => { useMocks({ @@ -54,7 +73,7 @@ describe('sessionRecordingsPlaylistLogic', () => { return [ 200, { - results: [`List of recordings offset by ${listOfSessionRecordings.length}`], + results: [offsetRecording], }, ] } else if ( @@ -167,6 +186,11 @@ describe('sessionRecordingsPlaylistLogic', () => { }) describe('ordering', () => { + afterEach(() => { + logic.actions.setOrderBy('start_time') + logic.actions.loadSessionRecordings() + }) + it('is set by setOrderBy, loads filtered results and orders the non pinned recordings', async () => { await expectLogic(logic, () => { logic.actions.setOrderBy('console_error_count') @@ -179,21 +203,22 @@ describe('sessionRecordingsPlaylistLogic', () => { expect(logic.values.otherRecordings.map((r) => r.console_error_count)).toEqual([100, 50]) }) - it('adds an offset when not using latest ordering', async () => { + it('adds an offset', async () => { await expectLogic(logic, () => { - logic.actions.setOrderBy('console_error_count') + logic.actions.loadSessionRecordings() }) - .toDispatchActionsInAnyOrder(['loadSessionRecordingsSuccess']) + .toDispatchActions(['loadSessionRecordingsSuccess']) .toMatchValues({ sessionRecordings: listOfSessionRecordings, }) await expectLogic(logic, () => { - logic.actions.maybeLoadSessionRecordings('newer') + logic.actions.loadSessionRecordings('older') }) .toDispatchActions(['loadSessionRecordingsSuccess']) .toMatchValues({ - sessionRecordings: [...listOfSessionRecordings, 'List of recordings offset by 2'], + // reorganises recordings based on start_time + sessionRecordings: [aRecording, offsetRecording, bRecording], }) }) }) @@ -306,6 +331,7 @@ describe('sessionRecordingsPlaylistLogic', () => { expect(router.values.searchParams.filters).toHaveProperty('date_to', '2021-10-20') }) }) + describe('duration filter', () => { it('is set by setFilters and fetches results from server and sets the url', async () => { await expectLogic(logic, () => { @@ -370,8 +396,9 @@ describe('sessionRecordingsPlaylistLogic', () => { .toFinishAllListeners() .toMatchValues({ sessionRecordingsResponse: { - results: listOfSessionRecordings, + order: 'start_time', has_next: undefined, + results: listOfSessionRecordings, }, sessionRecordings: listOfSessionRecordings, }) @@ -382,6 +409,8 @@ describe('sessionRecordingsPlaylistLogic', () => { .toFinishAllListeners() .toMatchValues({ sessionRecordingsResponse: { + has_next: undefined, + order: 'start_time', results: [ { ...aRecording, diff --git a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts index e93c61ff487b0..1c064ff9d2cf8 100644 --- a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts +++ b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts @@ -157,7 +157,7 @@ export function convertUniversalFiltersToRecordingsQuery(universalFilters: Recor return { kind: NodeKind.RecordingsQuery, - order: 'latest', + order: 'start_time', date_from: universalFilters.date_from, date_to: universalFilters.date_to, properties, @@ -235,7 +235,7 @@ function combineLegacyRecordingFilters( } function sortRecordings(recordings: SessionRecordingType[], order: RecordingsQuery['order']): SessionRecordingType[] { - let orderKey: + const orderKey: | 'recording_duration' | 'active_seconds' | 'inactive_seconds' @@ -243,13 +243,7 @@ function sortRecordings(recordings: SessionRecordingType[], order: RecordingsQue | 'click_count' | 'keypress_count' | 'mouse_activity_count' - | 'start_time' = 'start_time' - - if (order === 'duration') { - orderKey = 'recording_duration' - } else if (order != 'latest') { - orderKey = order - } + | 'start_time' = order === 'duration' ? 'recording_duration' : order === 'random_sample' ? 'start_time' : order return recordings.sort((a, b) => { const orderA = a[orderKey] @@ -358,7 +352,7 @@ export const sessionRecordingsPlaylistLogic = kea { @@ -417,7 +411,7 @@ export const sessionRecordingsPlaylistLogic = kea ({ orderBy: [ - 'latest' as RecordingsQuery['order'], + 'start_time' as RecordingsQuery['order'], { persist: true }, { setOrderBy: (_, { orderBy }) => orderBy,