|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +import React from 'react' |
| 25 | +import { render } from '@testing-library/react' |
| 26 | +import type { MockInstance } from 'vitest' |
| 27 | +import { vi } from 'vitest' |
| 28 | + |
| 29 | +import '@testing-library/jest-dom' |
| 30 | +import { runAxeCheck } from '@instructure/ui-axe-check' |
| 31 | +import { Img } from '../index' |
| 32 | + |
| 33 | +describe('<Img />', () => { |
| 34 | + const image = |
| 35 | + 'data:image/gif;base64,R0lGODlhFAAUAJEAAP/9/fYQEPytrflWViH5BAAAAAAALAAAAAAUABQAQAJKhI+pGe09lnhBnEETfodatVHNh1BR+ZzH9LAOCYrVYpiAfWWJOxrC/5MASbyZT4d6AUIBlUYGoR1FsAXUuTN5YhxAEYbrpKRkQwEAOw==' |
| 36 | + let consoleWarningMock: ReturnType<typeof vi.spyOn> |
| 37 | + let consoleErrorMock: ReturnType<typeof vi.spyOn> |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + // Mocking console to prevent test output pollution and expect for messages |
| 41 | + consoleWarningMock = vi |
| 42 | + .spyOn(console, 'warn') |
| 43 | + .mockImplementation(() => {}) as MockInstance |
| 44 | + consoleErrorMock = vi |
| 45 | + .spyOn(console, 'error') |
| 46 | + .mockImplementation(() => {}) as MockInstance |
| 47 | + }) |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + consoleWarningMock.mockRestore() |
| 51 | + consoleErrorMock.mockRestore() |
| 52 | + }) |
| 53 | + |
| 54 | + describe('for a11y', () => { |
| 55 | + it('should meet a11y standards', async () => { |
| 56 | + const { container } = render(<Img src={image} />) |
| 57 | + const axeCheck = await runAxeCheck(container) |
| 58 | + |
| 59 | + expect(axeCheck).toBe(true) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should render an empty alt attribute by default', async () => { |
| 63 | + const { container } = render(<Img src={image} />) |
| 64 | + const img = container.querySelector('img') |
| 65 | + |
| 66 | + expect(img).toHaveAttribute('alt', '') |
| 67 | + }) |
| 68 | + |
| 69 | + it('should render the provided alt attribute', async () => { |
| 70 | + const altValue = 'Foo' |
| 71 | + const { container } = render(<Img src={image} alt={altValue} />) |
| 72 | + const img = container.querySelector('img') |
| 73 | + |
| 74 | + expect(img).toHaveAttribute('alt', altValue) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should render an overlay color', async () => { |
| 79 | + const { container } = render( |
| 80 | + <Img src={image} overlay={{ color: '#ff0000', opacity: 7 }} /> |
| 81 | + ) |
| 82 | + const overlay = container.querySelector('[class*="-img__overlay"]') |
| 83 | + |
| 84 | + expect(overlay).toBeInTheDocument() |
| 85 | + }) |
| 86 | + |
| 87 | + it('should render a blur filter', async () => { |
| 88 | + const { container } = render(<Img src={image} withBlur={true} />) |
| 89 | + |
| 90 | + const img = container.querySelector('img')! |
| 91 | + const style = getComputedStyle(img) |
| 92 | + |
| 93 | + expect(style.filter).toContain('blur') |
| 94 | + }) |
| 95 | + |
| 96 | + it('should render a grayscale filter', async () => { |
| 97 | + const { container } = render(<Img src={image} withGrayscale={true} />) |
| 98 | + |
| 99 | + const img = container.querySelector('img')! |
| 100 | + const style = getComputedStyle(img) |
| 101 | + |
| 102 | + expect(style.filter).toContain('grayscale') |
| 103 | + }) |
| 104 | + |
| 105 | + // // If component renders as simple image |
| 106 | + it('should display block-level when display="block"', async () => { |
| 107 | + const { container } = render(<Img src={image} display="block" />) |
| 108 | + const img = container.querySelector('img')! |
| 109 | + const style = getComputedStyle(img) |
| 110 | + |
| 111 | + expect(style.display).toBe('block') |
| 112 | + }) |
| 113 | + |
| 114 | + // // If component has an overlay and renders as image inside containing element |
| 115 | + it('should display block-level with overlay when display="block"', async () => { |
| 116 | + const { container } = render( |
| 117 | + <Img |
| 118 | + src={image} |
| 119 | + display="block" |
| 120 | + overlay={{ color: 'tomato', opacity: 7 }} |
| 121 | + /> |
| 122 | + ) |
| 123 | + const img = container.querySelector('img')! |
| 124 | + const imgContainer = container.querySelector('[class*="-img__container"]')! |
| 125 | + const imgStyle = getComputedStyle(img) |
| 126 | + const imgContainerStyle = getComputedStyle(imgContainer) |
| 127 | + |
| 128 | + expect(imgStyle.display).toBe('block') |
| 129 | + expect(imgContainerStyle.display).toBe('block') |
| 130 | + }) |
| 131 | + |
| 132 | + it('should apply CSS object-fit: cover when constrain="cover"', async () => { |
| 133 | + const { container } = render( |
| 134 | + <div style={{ width: 16, height: 16 }}> |
| 135 | + <Img src={image} constrain="cover" /> |
| 136 | + </div> |
| 137 | + ) |
| 138 | + const img = container.querySelector('img')! |
| 139 | + const imgStyle = getComputedStyle(img) |
| 140 | + |
| 141 | + expect(imgStyle.objectFit).toBe('cover') |
| 142 | + }) |
| 143 | + |
| 144 | + it('should apply CSS object-fit: contain when constrain="contain"', async () => { |
| 145 | + const { container } = render( |
| 146 | + <div style={{ width: 16, height: 16 }}> |
| 147 | + <Img src={image} constrain="contain" /> |
| 148 | + </div> |
| 149 | + ) |
| 150 | + const img = container.querySelector('img')! |
| 151 | + const imgStyle = getComputedStyle(img) |
| 152 | + |
| 153 | + expect(imgStyle.objectFit).toBe('contain') |
| 154 | + }) |
| 155 | +}) |
0 commit comments