From 73a7e9e96cdcb17771a79f337255d7100dc5ec9e Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Tue, 13 Aug 2024 13:02:51 -0700 Subject: [PATCH] fix(labels): fix broken label file parsing in archive upload modal (#1323) Signed-off-by: Thuan Vo --- src/app/RecordingMetadata/utils.ts | 9 ++++----- .../RecordingLabelFields.test.tsx | 15 ++++++--------- .../Recordings/ArchivedRecordingsTable.test.tsx | 16 ++++++---------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/app/RecordingMetadata/utils.ts b/src/app/RecordingMetadata/utils.ts index 1911800e6..bd361cc27 100644 --- a/src/app/RecordingMetadata/utils.ts +++ b/src/app/RecordingMetadata/utils.ts @@ -34,11 +34,11 @@ export const parseLabelsFromFile = (file: File): Observable => { .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; @@ -47,7 +47,6 @@ export const parseLabelsFromFile = (file: File): Observable => { }), ); }; - export const LabelPattern = /^\S+$/; export const getValidatedOption = (isValid: boolean) => { diff --git a/src/test/RecordingMetadata/RecordingLabelFields.test.tsx b/src/test/RecordingMetadata/RecordingLabelFields.test.tsx index 3fbed03da..7a0a58e2d 100644 --- a/src/test/RecordingMetadata/RecordingLabelFields.test.tsx +++ b/src/test/RecordingMetadata/RecordingLabelFields.test.tsx @@ -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('', () => { @@ -387,10 +388,6 @@ describe('', () => { 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]); }); }); diff --git a/src/test/Recordings/ArchivedRecordingsTable.test.tsx b/src/test/Recordings/ArchivedRecordingsTable.test.tsx index 80b7e0537..48da506eb 100644 --- a/src/test/Recordings/ArchivedRecordingsTable.test.tsx +++ b/src/test/Recordings/ArchivedRecordingsTable.test.tsx @@ -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', @@ -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',