Skip to content

Commit

Permalink
much cleaner tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin committed Sep 5, 2024
1 parent 635ef6f commit dd6baf2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8846,7 +8846,7 @@
"$ref": "#/definitions/DurationType"
},
{
"const": "latest",
"const": "start_time",
"type": "string"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,28 @@ import {

describe('sessionRecordingsPlaylistLogic', () => {
let logic: ReturnType<typeof sessionRecordingsPlaylistLogic.build>
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({
Expand Down Expand Up @@ -54,7 +73,7 @@ describe('sessionRecordingsPlaylistLogic', () => {
return [
200,
{
results: [`List of recordings offset by ${listOfSessionRecordings.length}`],
results: [offsetRecording],
},
]
} else if (
Expand Down Expand Up @@ -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')
Expand All @@ -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],
})
})
})
Expand Down Expand Up @@ -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, () => {
Expand Down Expand Up @@ -370,8 +396,9 @@ describe('sessionRecordingsPlaylistLogic', () => {
.toFinishAllListeners()
.toMatchValues({
sessionRecordingsResponse: {
results: listOfSessionRecordings,
order: 'start_time',
has_next: undefined,
results: listOfSessionRecordings,
},
sessionRecordings: listOfSessionRecordings,
})
Expand All @@ -382,6 +409,8 @@ describe('sessionRecordingsPlaylistLogic', () => {
.toFinishAllListeners()
.toMatchValues({
sessionRecordingsResponse: {
has_next: undefined,
order: 'start_time',
results: [
{
...aRecording,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -235,21 +235,15 @@ function combineLegacyRecordingFilters(
}

function sortRecordings(recordings: SessionRecordingType[], order: RecordingsQuery['order']): SessionRecordingType[] {
let orderKey:
const orderKey:
| 'recording_duration'
| 'active_seconds'
| 'inactive_seconds'
| 'console_error_count'
| '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]
Expand Down Expand Up @@ -358,7 +352,7 @@ export const sessionRecordingsPlaylistLogic = kea<sessionRecordingsPlaylistLogic
{
results: [],
has_next: false,
order: 'latest',
order: 'start_time',
} as RecordingsQueryResponse & { order: RecordingsQuery['order'] },
{
loadSessionRecordings: async ({ direction }, breakpoint) => {
Expand Down Expand Up @@ -417,7 +411,7 @@ export const sessionRecordingsPlaylistLogic = kea<sessionRecordingsPlaylistLogic
const fetchedRecordings = await api.recordings.list({
kind: NodeKind.RecordingsQuery,
session_ids: recordingIds,
order: 'latest',
order: 'start_time',
})

recordings = [...recordings, ...fetchedRecordings.results]
Expand All @@ -431,7 +425,7 @@ export const sessionRecordingsPlaylistLogic = kea<sessionRecordingsPlaylistLogic
})),
reducers(({ props }) => ({
orderBy: [
'latest' as RecordingsQuery['order'],
'start_time' as RecordingsQuery['order'],
{ persist: true },
{
setOrderBy: (_, { orderBy }) => orderBy,
Expand Down

0 comments on commit dd6baf2

Please sign in to comment.