Skip to content

Commit

Permalink
test: ensuring the showMessageBox is called when an error occurs
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com>
  • Loading branch information
axel7083 committed Jan 4, 2024
1 parent 0f8962a commit ffac09d
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions packages/renderer/src/lib/image/ImageActions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import '@testing-library/jest-dom/vitest';
import { test, expect, beforeAll, vi } from 'vitest';
import { fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import ImageActions from '/@/lib/image/ImageActions.svelte';
import type { ImageInfoUI } from '/@/lib/image/ImageInfoUI';

const showMessageBoxMock = vi.fn();

vi.mock('./image-utils', () => {
return {
ImageUtils: vi.fn().mockImplementation(() => ({
deleteImage: vi.fn().mockImplementation(() => Promise.reject(new Error('Cannot delete image in test'))),
})),
};
});

beforeAll(() => {
(window as any).showMessageBox = showMessageBoxMock;
(window as any).getContributedMenus = vi.fn().mockImplementation(() => Promise.resolve([]));
(window as any).hasAuthconfigForImage = vi.fn().mockImplementation(() => Promise.resolve(true));
});

test('Expect showMessageBox to be called when error occurs', async () => {
render(ImageActions, {
onPushImage: vi.fn(),
onRenameImage: vi.fn(),
image: {
name: 'dummy',
} as unknown as ImageInfoUI,
});
const button = screen.getByTitle('Delete Image');
expect(button).toBeDefined();
await fireEvent.click(button);

await waitFor(() => {
expect(showMessageBoxMock).toHaveBeenCalledOnce();
});
});

0 comments on commit ffac09d

Please sign in to comment.