| 
 | 1 | +import { mount } from '@vue/test-utils'  | 
 | 2 | +import ContentLoader from './ContentLoader'  | 
 | 3 | + | 
 | 4 | +describe('ContentLoader', () => {  | 
 | 5 | +  it('has default values for props', () => {  | 
 | 6 | +    const wrapper = mount(ContentLoader)  | 
 | 7 | + | 
 | 8 | +    expect(wrapper.vm.width).toBe(400)  | 
 | 9 | +    expect(wrapper.vm.height).toBe(130)  | 
 | 10 | +    expect(wrapper.vm.speed).toBe(2)  | 
 | 11 | +    expect(wrapper.vm.preserveAspectRatio).toBe('xMidYMid meet')  | 
 | 12 | +    expect(wrapper.vm.baseUrl).toBe('')  | 
 | 13 | +    expect(wrapper.vm.primaryColor).toBe('#f9f9f9')  | 
 | 14 | +    expect(wrapper.vm.secondaryColor).toBe('#ecebeb')  | 
 | 15 | +    expect(wrapper.vm.primaryOpacity).toBe(1)  | 
 | 16 | +    expect(wrapper.vm.secondaryOpacity).toBe(1)  | 
 | 17 | +    expect(wrapper.vm.animate).toBe(true)  | 
 | 18 | +  })  | 
 | 19 | + | 
 | 20 | +  it('has viewbox, version and aspect ratio attributes on svg element', () => {  | 
 | 21 | +    const wrapper = mount(ContentLoader, {  | 
 | 22 | +      props: {  | 
 | 23 | +        width: 300,  | 
 | 24 | +        height: 200,  | 
 | 25 | +        preserveAspectRatio: 'xMaxYMid slice'  | 
 | 26 | +      }  | 
 | 27 | +    })  | 
 | 28 | + | 
 | 29 | +    expect(wrapper.find('svg').attributes()).toEqual({  | 
 | 30 | +      preserveAspectRatio: 'xMaxYMid slice',  | 
 | 31 | +      version: '1.1',  | 
 | 32 | +      viewBox: '0 0 300 200'  | 
 | 33 | +    })  | 
 | 34 | +  })  | 
 | 35 | + | 
 | 36 | +  it('has viewbox, version and aspect ratio attributes on svg element', () => {  | 
 | 37 | +    const wrapper = mount(ContentLoader, {  | 
 | 38 | +      props: {  | 
 | 39 | +        width: 300,  | 
 | 40 | +        height: 200,  | 
 | 41 | +        preserveAspectRatio: 'xMaxYMid slice'  | 
 | 42 | +      }  | 
 | 43 | +    })  | 
 | 44 | + | 
 | 45 | +    expect(wrapper.find('svg').attributes()).toEqual({  | 
 | 46 | +      preserveAspectRatio: 'xMaxYMid slice',  | 
 | 47 | +      version: '1.1',  | 
 | 48 | +      viewBox: '0 0 300 200'  | 
 | 49 | +    })  | 
 | 50 | +  })  | 
 | 51 | + | 
 | 52 | +  it('draws a rect filled by the gradient and clipped by the shapes', () => {  | 
 | 53 | +    const wrapper = mount(ContentLoader)  | 
 | 54 | + | 
 | 55 | +    expect(wrapper.find('rect').attributes()).toEqual({  | 
 | 56 | +      style: `fill: url(#${wrapper.vm.idGradient});`,  | 
 | 57 | +      'clip-path': `url(#${wrapper.vm.idClip})`,  | 
 | 58 | +      x: '0',  | 
 | 59 | +      y: '0',  | 
 | 60 | +      width: '400',  | 
 | 61 | +      height: '130'  | 
 | 62 | +    })  | 
 | 63 | +  })  | 
 | 64 | + | 
 | 65 | +  it('draws a clipPath with a unique ID', () => {  | 
 | 66 | +    const wrapper = mount(ContentLoader)  | 
 | 67 | + | 
 | 68 | +    expect(wrapper.find('defs clipPath').attributes()).toEqual({  | 
 | 69 | +      id: wrapper.vm.idClip  | 
 | 70 | +    })  | 
 | 71 | +  })  | 
 | 72 | + | 
 | 73 | +  it('draws a linear gradient with a unique ID', () => {  | 
 | 74 | +    const wrapper = mount(ContentLoader)  | 
 | 75 | + | 
 | 76 | +    expect(wrapper.find('defs linearGradient').attributes()).toEqual({  | 
 | 77 | +      id: wrapper.vm.idGradient  | 
 | 78 | +    })  | 
 | 79 | +  })  | 
 | 80 | + | 
 | 81 | +  it('draws a linear gradient with 3 stops', () => {  | 
 | 82 | +    const wrapper = mount(ContentLoader)  | 
 | 83 | +    const stops = wrapper.findAll('defs linearGradient stop')  | 
 | 84 | + | 
 | 85 | +    expect(stops.length).toBe(3)  | 
 | 86 | +    expect(stops[0].attributes()).toEqual({  | 
 | 87 | +      offset: '0%',  | 
 | 88 | +      'stop-color': '#f9f9f9',  | 
 | 89 | +      'stop-opacity': '1'  | 
 | 90 | +    })  | 
 | 91 | +    expect(stops[1].attributes()).toEqual({  | 
 | 92 | +      offset: '50%',  | 
 | 93 | +      'stop-color': '#ecebeb',  | 
 | 94 | +      'stop-opacity': '1'  | 
 | 95 | +    })  | 
 | 96 | +    expect(stops[2].attributes()).toEqual({  | 
 | 97 | +      offset: '100%',  | 
 | 98 | +      'stop-color': '#f9f9f9',  | 
 | 99 | +      'stop-opacity': '1'  | 
 | 100 | +    })  | 
 | 101 | +  })  | 
 | 102 | + | 
 | 103 | +  it('animates the gradient by default using given speed', () => {  | 
 | 104 | +    const wrapper = mount(ContentLoader)  | 
 | 105 | +    const animations = wrapper.findAll('defs linearGradient animate')  | 
 | 106 | + | 
 | 107 | +    expect(animations.length).toBe(3)  | 
 | 108 | +    expect(animations[0].attributes()).toEqual({  | 
 | 109 | +      attributeName: 'offset',  | 
 | 110 | +      values: '-2; 1',  | 
 | 111 | +      dur: '2s',  | 
 | 112 | +      repeatCount: 'indefinite'  | 
 | 113 | +    })  | 
 | 114 | +    expect(animations[1].attributes()).toEqual({  | 
 | 115 | +      attributeName: 'offset',  | 
 | 116 | +      values: '-1.5; 1.5',  | 
 | 117 | +      dur: '2s',  | 
 | 118 | +      repeatCount: 'indefinite'  | 
 | 119 | +    })  | 
 | 120 | +    expect(animations[2].attributes()).toEqual({  | 
 | 121 | +      attributeName: 'offset',  | 
 | 122 | +      values: '-1; 2',  | 
 | 123 | +      dur: '2s',  | 
 | 124 | +      repeatCount: 'indefinite'  | 
 | 125 | +    })  | 
 | 126 | +  })  | 
 | 127 | + | 
 | 128 | +  it('does not animate if animate prop is false', () => {  | 
 | 129 | +    const wrapper = mount(ContentLoader, {  | 
 | 130 | +      props: {  | 
 | 131 | +        animate: false  | 
 | 132 | +      }  | 
 | 133 | +    })  | 
 | 134 | + | 
 | 135 | +    expect(wrapper.findAll('defs linearGradient animate').length).toBe(0)  | 
 | 136 | +  })  | 
 | 137 | + | 
 | 138 | +  it('has a default element to clip with', () => {  | 
 | 139 | +    const wrapper = mount(ContentLoader)  | 
 | 140 | + | 
 | 141 | +    expect(wrapper.find('defs clipPath rect').attributes()).toEqual({  | 
 | 142 | +      x: '0',  | 
 | 143 | +      y: '0',  | 
 | 144 | +      rx: '5',  | 
 | 145 | +      ry: '5',  | 
 | 146 | +      width: '400',  | 
 | 147 | +      height: '130'  | 
 | 148 | +    })  | 
 | 149 | +  })  | 
 | 150 | + | 
 | 151 | +  it('outputs the default slot within the clipPath', () => {  | 
 | 152 | +    const wrapper = mount(ContentLoader, {  | 
 | 153 | +      slots: {  | 
 | 154 | +        default: '<circle cx="30" cy="30" r="30" />'  | 
 | 155 | +      }  | 
 | 156 | +    })  | 
 | 157 | + | 
 | 158 | +    expect(wrapper.find('defs clipPath circle').html()).toEqual(  | 
 | 159 | +      '<circle cx="30" cy="30" r="30"></circle>'  | 
 | 160 | +    )  | 
 | 161 | +  })  | 
 | 162 | + | 
 | 163 | +  it('use the baseUrl to link the gradient & clip path', () => {  | 
 | 164 | +    const wrapper = mount(ContentLoader, {  | 
 | 165 | +      props: {  | 
 | 166 | +        baseUrl: '/path'  | 
 | 167 | +      }  | 
 | 168 | +    })  | 
 | 169 | + | 
 | 170 | +    expect(wrapper.find('rect').attributes()).toMatchObject({  | 
 | 171 | +      style: `fill: url(/path#${wrapper.vm.idGradient});`,  | 
 | 172 | +      'clip-path': `url(/path#${wrapper.vm.idClip})`  | 
 | 173 | +    })  | 
 | 174 | +  })  | 
 | 175 | + | 
 | 176 | +  it('use the uniqueKey to generate static IDs', () => {  | 
 | 177 | +    const wrapper = mount(ContentLoader, {  | 
 | 178 | +      props: {  | 
 | 179 | +        uniqueKey: 'unique'  | 
 | 180 | +      }  | 
 | 181 | +    })  | 
 | 182 | + | 
 | 183 | +    expect(wrapper.vm.idClip).toEqual('unique-idClip')  | 
 | 184 | +    expect(wrapper.vm.idGradient).toEqual('unique-idGradient')  | 
 | 185 | +  })  | 
 | 186 | + | 
 | 187 | +  it('apply extra attributes on the root element', () => {  | 
 | 188 | +    const wrapper = mount(ContentLoader, {  | 
 | 189 | +      props: {  | 
 | 190 | +        class: 'loader',  | 
 | 191 | +        id: 'loader'  | 
 | 192 | +      }  | 
 | 193 | +    })  | 
 | 194 | + | 
 | 195 | +    expect(wrapper.find('svg').classes()).toEqual(['loader'])  | 
 | 196 | +    expect(wrapper.find('svg').attributes()).toMatchObject({ id: 'loader' })  | 
 | 197 | +  })  | 
 | 198 | +})  | 
0 commit comments