Chore: Fix playlist flaky test (#114241)

This commit is contained in:
Ezequiel Victorero
2025-11-20 14:17:53 -03:00
committed by GitHub
parent 4d25381716
commit 5ca3c27e4f
@@ -24,7 +24,21 @@ jest.mock('app/core/components/TagFilter/TagFilter', () => ({
function getTestContext() {
jest.clearAllMocks();
const backendSrvMock = jest.spyOn(backendSrv, 'fetch').mockImplementation(() => of(createFetchResponse({})));
// Create separate spies for different HTTP methods
const postSpy = jest.fn();
const otherSpy = jest.fn();
const backendSrvMock = jest.spyOn(backendSrv, 'fetch').mockImplementation((options) => {
if (options.method === 'POST') {
postSpy(options);
return of(createFetchResponse({}));
}
// Handle GET and other methods
otherSpy(options);
return of(createFetchResponse({ items: [] }));
});
jest.spyOn(backendSrv, 'search').mockResolvedValue([]);
const { rerender } = render(
@@ -33,7 +47,7 @@ function getTestContext() {
</TestProvider>
);
return { rerender, backendSrvMock };
return { rerender, backendSrvMock, postSpy, otherSpy };
}
describe('PlaylistNewPage', () => {
@@ -47,15 +61,17 @@ describe('PlaylistNewPage', () => {
describe('when submitted', () => {
it('then correct api should be called', async () => {
const { backendSrvMock } = getTestContext();
const { postSpy } = getTestContext();
expect(locationService.getLocation().pathname).toEqual('/');
await userEvent.type(screen.getByRole('textbox', { name: selectors.pages.PlaylistForm.name }), 'A new name');
fireEvent.submit(screen.getByRole('button', { name: /save/i }));
await waitFor(() => expect(backendSrvMock).toHaveBeenCalledTimes(1));
expect(backendSrvMock).toHaveBeenCalledWith(
await waitFor(() => expect(postSpy).toHaveBeenCalledTimes(1));
expect(postSpy).toHaveBeenCalledWith(
expect.objectContaining({
method: 'POST',
body: expect.objectContaining({
spec: {
title: 'A new name',