Skip to content

Commit

Permalink
fix(labels): fix broken label file parsing in archive upload modal (#…
Browse files Browse the repository at this point in the history
…1323)

Signed-off-by: Thuan Vo <[email protected]>
  • Loading branch information
tthvo authored Aug 13, 2024
1 parent fec8a88 commit 73a7e9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/app/RecordingMetadata/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export const parseLabelsFromFile = (file: File): Observable<KeyValue[]> => {
.then((obj) => {
const labels: KeyValue[] = [];
const labelObj = obj['labels'];
if (labelObj) {
Object.keys(labelObj).forEach((key) => {
if (labelObj && Array.isArray(labelObj)) {
Object.values(labelObj).forEach((keyValue) => {
labels.push({
key: key,
value: labelObj[key],
key: keyValue.key,
value: keyValue.value,
});
});
return labels;
Expand All @@ -47,7 +47,6 @@ export const parseLabelsFromFile = (file: File): Observable<KeyValue[]> => {
}),
);
};

export const LabelPattern = /^\S+$/;

export const getValidatedOption = (isValid: boolean) => {
Expand Down
15 changes: 6 additions & 9 deletions src/test/RecordingMetadata/RecordingLabelFields.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ import * as tlr from '@testing-library/react';
import { cleanup, screen } from '@testing-library/react';
import { render, renderSnapshot } from '../utils';

const mockUploadedRecordingLabels = {
someUploaded: 'someUploadedValue',
const mockUploadedRecordingLabels: KeyValue = {
key: 'someUploaded',
value: 'someUploadedValue',
};
const mockMetadataFileName = 'mock.metadata.json';
const mockMetadataFile = new File(
[JSON.stringify({ labels: { ...mockUploadedRecordingLabels } })],
[JSON.stringify({ labels: [{ ...mockUploadedRecordingLabels }] })],
mockMetadataFileName,
{ type: 'json' },
);
mockMetadataFile.text = jest.fn(
() => new Promise((resolve, _) => resolve(JSON.stringify({ labels: { ...mockUploadedRecordingLabels } }))),
() => new Promise((resolve, _) => resolve(JSON.stringify({ labels: [{ ...mockUploadedRecordingLabels }] }))),
);

describe('<RecordingLabelFields />', () => {
Expand Down Expand Up @@ -387,10 +388,6 @@ describe('<RecordingLabelFields />', () => {
expect(labelUploadInput.files?.item(0)).toStrictEqual(mockMetadataFile);

expect(mockProps.setLabels).toHaveBeenCalledTimes(1);
expect(mockProps.setLabels).toHaveBeenCalledWith([
mockLabel1,
mockLabel2,
{ key: 'someUploaded', value: 'someUploadedValue' },
]);
expect(mockProps.setLabels).toHaveBeenCalledWith([mockLabel1, mockLabel2, mockUploadedRecordingLabels]);
});
});
16 changes: 6 additions & 10 deletions src/test/Recordings/ArchivedRecordingsTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ const mockUploadsTarget = {
labels: [],
annotations: { cryostat: [], platform: [] },
};
const mockRecordingLabels = [
const mockRecordingLabels: KeyValue[] = [
{
key: 'someLabel',
value: 'someValue',
},
];
const mockUploadedRecordingLabels = [
const mockUploadedRecordingLabels: KeyValue[] = [
{
key: 'someUploaded',
value: 'someUpdatedValue',
Expand All @@ -71,14 +71,10 @@ export const convertLabels = (kv: KeyValue[]): object => {
return out;
};
const mockMetadataFileName = 'mock.metadata.json';
const mockMetadataFile = new File(
[JSON.stringify({ labels: convertLabels(mockUploadedRecordingLabels) })],
mockMetadataFileName,
{ type: 'json' },
);
mockMetadataFile.text = jest.fn(() =>
Promise.resolve(JSON.stringify({ labels: convertLabels(mockUploadedRecordingLabels) })),
);
const mockMetadataFile = new File([JSON.stringify({ labels: mockUploadedRecordingLabels })], mockMetadataFileName, {
type: 'json',
});
mockMetadataFile.text = jest.fn(() => Promise.resolve(JSON.stringify({ labels: mockUploadedRecordingLabels })));

const mockRecording: ArchivedRecording = {
name: 'someRecording',
Expand Down

0 comments on commit 73a7e9e

Please sign in to comment.