|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (C) 2023-2025 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + ***********************************************************************/ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | + |
| 21 | +import { render } from '@testing-library/svelte'; |
| 22 | +import { expect, test } from 'vitest'; |
| 23 | + |
| 24 | +import IconImage from './IconImage.svelte'; |
| 25 | + |
| 26 | +test('Expect valid source and alt text with dark mode', async () => { |
| 27 | + const image = render(IconImage, { image: { light: 'light.png', dark: 'dark.png' }, alt: 'this is alt text' }); |
| 28 | + |
| 29 | + const imageElement = image.getByRole('img'); |
| 30 | + |
| 31 | + expect(imageElement).toHaveAttribute('src', 'dark.png'); |
| 32 | + expect(imageElement).toHaveAttribute('alt', 'this is alt text'); |
| 33 | + |
| 34 | + await image.rerender({ image: { light: 'light2.png', dark: 'dark2.png' }, alt: 'this is another alt text' }); |
| 35 | + expect(imageElement).toHaveAttribute('src', 'dark2.png'); |
| 36 | +}); |
| 37 | + |
| 38 | +test('Expect no alt attribute if missing and default image', async () => { |
| 39 | + const image = render(IconImage, { image: 'image.png' }); |
| 40 | + |
| 41 | + const imageElement = image.getByRole('img'); |
| 42 | + expect(imageElement).toHaveAttribute('src', 'image.png'); |
| 43 | + expect(imageElement).not.toHaveAttribute('alt'); |
| 44 | +}); |
| 45 | + |
| 46 | +test('Expect string as image', async () => { |
| 47 | + const image = render(IconImage, { image: 'image1', alt: 'this is alt text' }); |
| 48 | + |
| 49 | + const imageElement = image.getByRole('img'); |
| 50 | + |
| 51 | + expect(imageElement).toHaveAttribute('src', 'image1'); |
| 52 | + expect(imageElement).toHaveAttribute('alt', 'this is alt text'); |
| 53 | + |
| 54 | + await image.rerender({ image: 'image2', alt: 'this is another alt text' }); |
| 55 | + expect(imageElement).toHaveAttribute('src', 'image2'); |
| 56 | +}); |
0 commit comments