|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { |
| 4 | + getBackgroundClassesAndStyles, |
| 5 | + useBackgroundProps, |
| 6 | +} from '../use-background-props'; |
| 7 | + |
| 8 | +const backgroundImage = { |
| 9 | + url: 'https://example.com/image.jpg', |
| 10 | + id: 123, |
| 11 | + source: 'file', |
| 12 | +}; |
| 13 | + |
| 14 | +describe( 'getBackgroundClassesAndStyles', () => { |
| 15 | + it( 'should return the class and styles for a background image', () => { |
| 16 | + expect( |
| 17 | + getBackgroundClassesAndStyles( { |
| 18 | + style: { background: { backgroundImage } }, |
| 19 | + } ) |
| 20 | + ).toEqual( { |
| 21 | + className: 'has-background', |
| 22 | + style: { |
| 23 | + backgroundImage: "url( 'https://example.com/image.jpg' )", |
| 24 | + backgroundSize: 'cover', |
| 25 | + }, |
| 26 | + } ); |
| 27 | + } ); |
| 28 | + |
| 29 | + it( 'should return the class and styles for a gradient', () => { |
| 30 | + expect( |
| 31 | + getBackgroundClassesAndStyles( { |
| 32 | + style: { |
| 33 | + background: { |
| 34 | + gradient: 'linear-gradient(135deg, #000 0%, #fff 100%)', |
| 35 | + }, |
| 36 | + }, |
| 37 | + } ) |
| 38 | + ).toEqual( { |
| 39 | + className: 'has-background', |
| 40 | + style: { |
| 41 | + backgroundImage: 'linear-gradient(135deg, #000 0%, #fff 100%)', |
| 42 | + }, |
| 43 | + } ); |
| 44 | + } ); |
| 45 | + |
| 46 | + it( 'should layer the gradient over the background image', () => { |
| 47 | + expect( |
| 48 | + getBackgroundClassesAndStyles( { |
| 49 | + style: { |
| 50 | + background: { |
| 51 | + backgroundImage, |
| 52 | + backgroundSize: 'contain', |
| 53 | + gradient: 'linear-gradient(135deg, #000 0%, #fff 100%)', |
| 54 | + }, |
| 55 | + }, |
| 56 | + } ) |
| 57 | + ).toEqual( { |
| 58 | + className: 'has-background', |
| 59 | + style: { |
| 60 | + backgroundImage: |
| 61 | + "linear-gradient(135deg, #000 0%, #fff 100%), url( 'https://example.com/image.jpg' )", |
| 62 | + backgroundSize: 'contain', |
| 63 | + backgroundPosition: '50% 50%', |
| 64 | + }, |
| 65 | + } ); |
| 66 | + } ); |
| 67 | + |
| 68 | + it( 'should convert a preset gradient to a CSS custom property', () => { |
| 69 | + expect( |
| 70 | + getBackgroundClassesAndStyles( { |
| 71 | + style: { |
| 72 | + background: { gradient: 'var:preset|gradient|aurora' }, |
| 73 | + }, |
| 74 | + } ).style |
| 75 | + ).toEqual( { |
| 76 | + backgroundImage: 'var(--wp--preset--gradient--aurora)', |
| 77 | + } ); |
| 78 | + } ); |
| 79 | + |
| 80 | + it( 'should not add a class when the background is clipped to the text', () => { |
| 81 | + const { className, style } = getBackgroundClassesAndStyles( { |
| 82 | + style: { |
| 83 | + background: { |
| 84 | + gradient: 'var:preset|gradient|aurora', |
| 85 | + backgroundClip: 'text', |
| 86 | + }, |
| 87 | + }, |
| 88 | + } ); |
| 89 | + expect( className ).toBeUndefined(); |
| 90 | + expect( style.backgroundClip ).toBe( 'text' ); |
| 91 | + } ); |
| 92 | + |
| 93 | + it( 'should return nothing for empty attributes', () => { |
| 94 | + expect( getBackgroundClassesAndStyles( {} ) ).toEqual( { |
| 95 | + className: undefined, |
| 96 | + style: {}, |
| 97 | + } ); |
| 98 | + } ); |
| 99 | +} ); |
| 100 | + |
| 101 | +describe( 'useBackgroundProps', () => { |
| 102 | + it( 'should return the same props as getBackgroundClassesAndStyles', () => { |
| 103 | + const attributes = { |
| 104 | + style: { |
| 105 | + background: { |
| 106 | + backgroundImage, |
| 107 | + gradient: 'var:preset|gradient|aurora', |
| 108 | + }, |
| 109 | + }, |
| 110 | + }; |
| 111 | + const { result } = renderHook( () => useBackgroundProps( attributes ) ); |
| 112 | + expect( result.current ).toEqual( |
| 113 | + getBackgroundClassesAndStyles( attributes ) |
| 114 | + ); |
| 115 | + } ); |
| 116 | +} ); |
0 commit comments