Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): fix broken test related to GraphQL #1242

Merged
merged 14 commits into from
Apr 18, 2024
21 changes: 5 additions & 16 deletions src/app/Archives/AllArchivedRecordingsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ export const AllArchivedRecordingsTable: React.FC<AllArchivedRecordingsTableProp
}, [context.settings, refreshDirectoriesAndCounts]);

React.useEffect(() => {
const subscription = context.notificationChannel
.messages(NotificationCategory.RecordingMetadataUpdated)
.subscribe((event) => {
addSubscription(
context.notificationChannel.messages(NotificationCategory.RecordingMetadataUpdated).subscribe((event) => {
const updatedRecordingInfo = event.message;

setDirectories((currentDirectories) => {
Expand All @@ -180,28 +179,18 @@ export const AllArchivedRecordingsTable: React.FC<AllArchivedRecordingsTableProp
if (recording.name === updatedRecordingInfo.recording.name) {
return {
...recording,
metadata: { ...recording.metadata, labels: updatedRecordingInfo.recording.metadata.labels },
metadata: { ...recording.metadata, labels: updatedRecordingInfo?.recording?.metadata?.labels },
};
}
return recording;
}),
}));

return newDirectories;
});
});

return () => {
subscription.unsubscribe();
};
}, [context.notificationChannel]);

React.useEffect(() => {
addSubscription(
context.notificationChannel.messages(NotificationCategory.ActiveRecordingSaved).subscribe(() => {
refreshDirectoriesAndCounts();
}),
);
}, [addSubscription, context.notificationChannel, refreshDirectoriesAndCounts]);
}, [addSubscription, context.notificationChannel, setDirectories]);

React.useEffect(() => {
addSubscription(
Expand Down
59 changes: 16 additions & 43 deletions src/app/Archives/AllTargetsArchivedRecordingsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ export const AllTargetsArchivedRecordingsTable: React.FC<AllTargetsArchivedRecor
const handleNotification = React.useCallback(
(_: string, recording: ArchivedRecording, delta: number) => {
setArchivesForTargets((old) => {
const matchingTargets = old.filter(({ target }) => target.jvmId === recording.jvmId);
const matchingTargets = old.filter(({ target }) => {
return target.jvmId === recording.jvmId;
});
for (const matchedTarget of matchingTargets) {
const targetIdx = old.findIndex(({ target }) => target.connectUrl === matchedTarget.target.connectUrl);

const recordings = [...matchedTarget.recordings];
if (delta === 1) {
recordings.push(recording);
Expand Down Expand Up @@ -199,27 +200,10 @@ export const AllTargetsArchivedRecordingsTable: React.FC<AllTargetsArchivedRecor
.pipe(
map((v) => {
return v.data.targetNodes.map((node) => {
const target: Target = node.target;
return {
target: {
jvmId: node.target.jvmId,
connectUrl: node.target.connectUrl,
alias: node.target.alias,
labels: [],
annotations: {
cryostat: [],
platform: [],
},
},
targetAsObs: of({
jvmId: node.target.jvmId,
connectUrl: node.target.connectUrl,
alias: node.target.alias,
labels: [],
annotations: {
cryostat: [],
platform: [],
},
}),
target,
targetAsObs: of(target),
archiveCount: node.target.archivedRecordings.aggregate.count,
recordings: node.target.archivedRecordings.data as ArchivedRecording[],
};
Expand All @@ -244,6 +228,7 @@ export const AllTargetsArchivedRecordingsTable: React.FC<AllTargetsArchivedRecor
target {
archivedRecordings {
data {
jvmId
name
downloadUrl
reportUrl
Expand Down Expand Up @@ -294,32 +279,26 @@ export const AllTargetsArchivedRecordingsTable: React.FC<AllTargetsArchivedRecor

const handleTargetNotification = React.useCallback(
(evt: TargetDiscoveryEvent) => {
const target: Target = {
connectUrl: evt.serviceRef.connectUrl,
alias: evt.serviceRef.alias,
labels: [],
annotations: {
cryostat: [],
platform: [],
},
};
if (evt.kind === 'FOUND') {
getCountForNewTarget(target);
getCountForNewTarget(evt.serviceRef);
} else if (evt.kind === 'MODIFIED') {
setArchivesForTargets((old) => {
const idx = old.findIndex(({ target: t }) => isEqualTarget(t, target));
const idx = old.findIndex(({ target: t }) => isEqualTarget(t, evt.serviceRef));
if (idx >= 0) {
const matched = old[idx];
if (target.connectUrl === matched.target.connectUrl && target.alias === matched.target.alias) {
if (
evt.serviceRef.connectUrl === matched.target.connectUrl &&
evt.serviceRef.alias === matched.target.alias
) {
// If alias and connectUrl are not updated, ignore changes.
return old;
}
return old.splice(idx, 1, { ...matched, target: target, targetAsObs: of(target) });
return old.splice(idx, 1, { ...matched, target: evt.serviceRef, targetAsObs: of(evt.serviceRef) });
}
return old;
});
} else if (evt.kind === 'LOST') {
handleLostTarget(target);
handleLostTarget(evt.serviceRef);
}
},
[setArchivesForTargets, getCountForNewTarget, handleLostTarget],
Expand Down Expand Up @@ -403,13 +382,7 @@ export const AllTargetsArchivedRecordingsTable: React.FC<AllTargetsArchivedRecor
handleNotification(v.message.target, v.message.recording, -1);
}),
);
}, [
addSubscription,
context.notificationChannel,
handleNotification,
refreshArchivesForTargets,
getCountForNewTarget,
]);
}, [addSubscription, context.notificationChannel, handleNotification]);

const toggleExpanded = React.useCallback(
(target) => {
Expand Down
3 changes: 1 addition & 2 deletions src/app/Recordings/ArchivedRecordingsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,8 @@ export const ArchivedRecordingsTable: React.FC<ArchivedRecordingsTableProps> = (
const eventConnectUrlLabel = event.message.recording.metadata.labels.find(
(label) => label.key === 'connectUrl',
);

const matchesUploadsUrlAndJvmId =
currentTarget?.connectUrl === 'uploads' && event.message.recording.jvmId === 'uploads';

if (isUploadsTable && matchesUploadsUrlAndJvmId) {
refreshRecordingList();
}
Expand Down Expand Up @@ -381,6 +379,7 @@ export const ArchivedRecordingsTable: React.FC<ArchivedRecordingsTableProps> = (
) {
return;
}

setRecordings((oldRecordings) => {
return oldRecordings.map((recording) => {
if (recording.name === event.message.recording.name) {
Expand Down
1 change: 1 addition & 0 deletions src/app/Shared/Services/api.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export interface Recording {
}

export interface ArchivedRecording extends Recording {
jvmId?: string;
archivedTime: number;
size: number;
}
Expand Down
52 changes: 29 additions & 23 deletions src/test/Archives/AllArchivedRecordingsTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { AllArchivedRecordingsTable } from '@app/Archives/AllArchivedRecordingsT
import { NotificationMessage, ArchivedRecording, RecordingDirectory } from '@app/Shared/Services/api.types';
import { defaultServices } from '@app/Shared/Services/Services';
import '@testing-library/jest-dom';
import { cleanup, screen, within } from '@testing-library/react';
import { cleanup, screen, within, waitFor } from '@testing-library/react';
import { of } from 'rxjs';
import { render, renderSnapshot } from '../utils';

Expand All @@ -28,33 +28,44 @@ const mockConnectUrl2 = 'service:jmx:rmi://someUrl2';
const mockJvmId2 = 'fooJvmId2';
const mockConnectUrl3 = 'service:jmx:rmi://someUrl3';
const mockJvmId3 = 'fooJvmId3';
const mockName3 = 'someRecording3';

const mockCount1 = 1;

const mockRecordingSavedNotification = {
message: {
target: mockConnectUrl3,
recording: {
name: mockName3,
metadata: {
labels: {
key: 'someLabel',
value: 'someValue',
},
},
},
},
} as NotificationMessage;

const mockRecordingDeletedNotification = {
message: {
target: mockConnectUrl1,
recording: {
name: mockName3,
},
},
} as NotificationMessage;

const mockRecordingLabels = [
{
key: 'someLabel',
value: 'someValue',
},
];

const mockRecording: ArchivedRecording = {
name: 'someRecording',
downloadUrl: 'http://downloadUrl',
reportUrl: 'http://reportUrl',
metadata: { labels: mockRecordingLabels },
metadata: {
labels: [
{
key: 'someLabel',
value: 'someValue',
},
],
},
size: 2048,
archivedTime: 2048,
};
Expand Down Expand Up @@ -122,39 +133,32 @@ jest
jest
.spyOn(defaultServices.notificationChannel, 'messages')
.mockReturnValueOnce(of()) // renders correctly // NotificationCategory.RecordingMetadataUpdated
.mockReturnValueOnce(of()) // NotificationCategory.ActiveRecordingSaved
.mockReturnValueOnce(of()) // NotificationCategory.ArchivedRecordingCreated
.mockReturnValueOnce(of()) // NotificationCategory.ArchivedRecordingDeleted

.mockReturnValueOnce(of()) // shows no recordings when empty
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())

.mockReturnValueOnce(of()) // has the correct table elements
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())

.mockReturnValueOnce(of()) // correctly handles the search function
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())

.mockReturnValueOnce(of()) // expands targets to show their <ArchivedRecordingsTable />
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())

.mockReturnValueOnce(of(mockRecordingSavedNotification)) // increments the count when an archived recording is saved
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())
.mockReturnValueOnce(of()) // increments the count when an archived recording is saved
.mockReturnValueOnce(of(mockRecordingSavedNotification))
.mockReturnValueOnce(of())

.mockReturnValueOnce(of(mockRecordingDeletedNotification)) // decrements the count when an archived recording is deleted
.mockReturnValueOnce(of()) // decrements the count when an archived recording is deleted
.mockReturnValueOnce(of())
.mockReturnValueOnce(of())
.mockReturnValueOnce(of());
.mockReturnValueOnce(of(mockRecordingDeletedNotification));

describe('<AllArchivedRecordingsTable />', () => {
afterEach(cleanup);
Expand Down Expand Up @@ -249,7 +253,9 @@ describe('<AllArchivedRecordingsTable />', () => {

const thirdTarget = rows[2];
expect(within(thirdTarget).getByText(`${mockConnectUrl3}`)).toBeTruthy();
expect(within(thirdTarget).getByText(4)).toBeTruthy();
await waitFor(() => {
expect(within(thirdTarget).getByText('4')).toBeInTheDocument();
});
});

it('decrements the count when an archived recording is deleted', async () => {
Expand Down
Loading
Loading