Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Janelle Law committed Jul 29, 2022
1 parent e081104 commit 6aebe92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/app/RecordingMetadata/BulkEditLabels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const BulkEditLabels: React.FunctionComponent<BulkEditLabelsProps> = (pro
)
.subscribe((value) => setRecordings(value))
);
}, [addSubscription, context, context.target, context.api, setRecordings]);
}, [addSubscription, props.isTargetRecording, context, context.target, context.api, setRecordings]);

React.useEffect(() => {
addSubscription(context.target.target().subscribe(refreshRecordingList));
Expand Down
6 changes: 4 additions & 2 deletions src/app/Shared/Services/Api.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ export class ApiService {
);
}

postRecordingMetadata(recordingName: string, labels: RecordingLabel[]): Observable<string> {
postRecordingMetadata(recordingName: string, labels: RecordingLabel[]): Observable<ArchivedRecording[]> {
return this.target.target()
.pipe(
filter(target => target !== NO_TARGET),
Expand All @@ -554,10 +554,11 @@ export class ApiService {
}
}`)
),
map(v => v.data.targetNodes[0].recordings.archived as ArchivedRecording[]),
)
}

postTargetRecordingMetadata(recordingName: string, labels: RecordingLabel[]): Observable<string> {
postTargetRecordingMetadata(recordingName: string, labels: RecordingLabel[]): Observable<ActiveRecording[]> {
return this.target.target()
.pipe(
filter(target => target !== NO_TARGET),
Expand All @@ -577,6 +578,7 @@ export class ApiService {
}
}`)
),
map(v => v.data.targetNodes[0].recordings.active as ActiveRecording[]),
)
}

Expand Down
56 changes: 16 additions & 40 deletions src/test/RecordingMetadata.tsx/BulkEditLabels.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ import { of } from 'rxjs';
import '@testing-library/jest-dom';
import { BulkEditLabels } from '@app/RecordingMetadata/BulkEditLabels';
import { ServiceContext, defaultServices } from '@app/Shared/Services/Services';
import { ArchivedRecording } from '@app/Shared/Services/Api.service';
import { ActiveRecording, ArchivedRecording } from '@app/Shared/Services/Api.service';
import { NotificationMessage } from '@app/Shared/Services/NotificationChannel.service';
import { Button, TextInput } from '@patternfly/react-core';

jest.mock('@patternfly/react-core', () => ({
...jest.requireActual('@patternfly/react-core'),
Expand All @@ -70,11 +71,22 @@ const mockLabelsNotification = {
},
} as NotificationMessage;
const mockDeleteNotification = { message: { target: mockConnectUrl, recording: mockRecording } } as NotificationMessage;

const mockArchivedRecordingsResponse = {
data: {
targetNodes: [
{
recordings: {
archived: [mockRecording] as ArchivedRecording[],
},
},
],
},
};
jest.spyOn(defaultServices.target, 'target').mockReturnValue(of(mockTarget));
jest.spyOn(defaultServices.api, 'doGet').mockReturnValue(of([mockRecording]));
jest.spyOn(defaultServices.api, 'postTargetRecordingMetadata').mockReturnValue(of('updatedLabels'));
jest.spyOn(defaultServices.api, 'postRecordingMetadata').mockReturnValue(of('updatedLabels'));
jest.spyOn(defaultServices.api, 'postTargetRecordingMetadata').mockReturnValue(of());
jest.spyOn(defaultServices.api, 'postRecordingMetadata').mockReturnValue(of());
jest.spyOn(defaultServices.api, 'graphql').mockReturnValue(of(mockArchivedRecordingsResponse));
jest.spyOn(defaultServices.notificationChannel, 'messages').mockReturnValue(of());
jest
.spyOn(defaultServices.notificationChannel, 'messages')
Expand Down Expand Up @@ -221,40 +233,4 @@ describe('<BulkEditLabels />', () => {
const saveRequestSpy = jest.spyOn(defaultServices.api, 'postRecordingMetadata');
expect(saveRequestSpy).toHaveBeenCalledTimes(1);
});

it('adds a label when Add Label is clicked', () => {
render(
<ServiceContext.Provider value={defaultServices}>
<BulkEditLabels {...mockProps} isTargetRecording={false} />
</ServiceContext.Provider>
);

userEvent.click(screen.getByText('Edit'));

expect(screen.getAllByLabelText('label key').length).toBe(1);
expect(screen.getAllByLabelText('label value').length).toBe(1);

userEvent.click(screen.getByText('Add Label'));

expect(screen.getAllByLabelText('label key').length).toBe(2);
expect(screen.getAllByLabelText('label value').length).toBe(2);
});

it('removes a label when Delete button is clicked', () => {
render(
<ServiceContext.Provider value={defaultServices}>
<BulkEditLabels {...mockProps} isTargetRecording={false} />
</ServiceContext.Provider>
);

userEvent.click(screen.getByText('Edit'));

expect(screen.getAllByLabelText('label key').length).toBe(1);
expect(screen.getAllByLabelText('label value').length).toBe(1);

userEvent.click(screen.getAllByTestId('remove-label-button')[0]);

expect(screen.queryByLabelText('label key')).not.toBeInTheDocument();
expect(screen.queryByLabelText('label value')).not.toBeInTheDocument();
});
});

0 comments on commit 6aebe92

Please sign in to comment.