|
| 1 | +import Vue from 'vue' |
| 2 | +import expect from 'expect' |
| 3 | + |
| 4 | +import { resetStyled, expectCSSMatches } from './utils' |
| 5 | + |
| 6 | +let styled |
| 7 | + |
| 8 | +describe('"attrs" feature', () => { |
| 9 | + beforeEach(() => { |
| 10 | + styled = resetStyled() |
| 11 | + }) |
| 12 | + |
| 13 | + it('should add html attributes to an element', () => { |
| 14 | + const Component = styled('img', {}).attrs({ src: 'image.jpg' })` |
| 15 | + width: 50; |
| 16 | + ` |
| 17 | + const vm = new Vue(Component).$mount() |
| 18 | + expect(vm._vnode.data.domProps).toEqual({ src: 'image.jpg' }) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should add several html attributes to an element', () => { |
| 22 | + const Component = styled('img', {}).attrs({ src: 'image.jpg', alt: 'Test image' })` |
| 23 | + width: 50; |
| 24 | + ` |
| 25 | + const vm = new Vue(Component).$mount() |
| 26 | + expect(vm._vnode.data.domProps).toEqual({ src: 'image.jpg', alt: 'Test image' }) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should work as expected with empty attributes object provided', () => { |
| 30 | + const Component = styled('img', {}).attrs({})` |
| 31 | + width: 50; |
| 32 | + ` |
| 33 | + const vm = new Vue(Component).$mount() |
| 34 | + expectCSSMatches('.a {width: 50;}') |
| 35 | + }) |
| 36 | + |
| 37 | + it('should work as expected with null attributes object provided', () => { |
| 38 | + const Component = styled('img', {}).attrs(null)` |
| 39 | + width: 50; |
| 40 | + ` |
| 41 | + const vm = new Vue(Component).$mount() |
| 42 | + expectCSSMatches('.a {width: 50;}') |
| 43 | + expect(vm._vnode.data.domProps).toEqual({}) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should work as expected without attributes provided', () => { |
| 47 | + const Component = styled('img')` |
| 48 | + width: 50; |
| 49 | + ` |
| 50 | + const vm = new Vue(Component).$mount() |
| 51 | + expectCSSMatches('.a {width: 50;}') |
| 52 | + expect(vm._vnode.data.domProps).toEqual({}) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should work with a function as a parameter of of the method', () => { |
| 56 | + const Component = styled('img', {}).attrs(() => ({ |
| 57 | + src: 'image.jpg', |
| 58 | + alt: 'Test image', |
| 59 | + height: '50' |
| 60 | + }))` |
| 61 | + width: 50; |
| 62 | + ` |
| 63 | + const vm = new Vue(Component).$mount() |
| 64 | + expect(vm._vnode.data.domProps).toEqual({ src: 'image.jpg', alt: 'Test image', height: '50' }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should work with multiple attrs method call', () => { |
| 68 | + const Component = styled('img', {}) |
| 69 | + .attrs(() => ({ |
| 70 | + src: 'image.jpg', |
| 71 | + alt: 'Test image' |
| 72 | + })) |
| 73 | + .attrs({ |
| 74 | + height: '50' |
| 75 | + })` |
| 76 | + width: 50; |
| 77 | + ` |
| 78 | + const vm = new Vue(Component).$mount() |
| 79 | + expect(vm._vnode.data.domProps).toEqual({ src: 'image.jpg', alt: 'Test image', height: '50' }) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should access to all previous attribute properties', () => { |
| 83 | + const Component = styled('img', {}) |
| 84 | + .attrs(() => ({ |
| 85 | + src: 'image', |
| 86 | + alt: 'My test image' |
| 87 | + })) |
| 88 | + .attrs((props) => ({ |
| 89 | + src: props.src + '.jpg', |
| 90 | + height: 5 * 10 |
| 91 | + }))` |
| 92 | + width: 50; |
| 93 | + ` |
| 94 | + const vm = new Vue(Component).$mount() |
| 95 | + expect(vm._vnode.data.domProps).toEqual({ src: 'image.jpg', alt: 'My test image', height: 50 }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('should override attribute properties', () => { |
| 99 | + const Component = styled('img', {}) |
| 100 | + .attrs(() => ({ |
| 101 | + src: 'image.jpg', |
| 102 | + alt: 'Test image', |
| 103 | + height: '20' |
| 104 | + })) |
| 105 | + .attrs({ |
| 106 | + height: '50' |
| 107 | + })` |
| 108 | + width: 50; |
| 109 | + ` |
| 110 | + const vm = new Vue(Component).$mount() |
| 111 | + expect(vm._vnode.data.domProps).toEqual({ src: 'image.jpg', alt: 'Test image', height: '50' }) |
| 112 | + }) |
| 113 | + |
| 114 | + it('should access to component props', () => { |
| 115 | + const Component = styled('img', { propsHeight: Number }) |
| 116 | + .attrs((props) => ({ |
| 117 | + src: 'image.jpg', |
| 118 | + alt: 'Test image', |
| 119 | + height: props.propsHeight * 2 |
| 120 | + }))` |
| 121 | + width: 50; |
| 122 | + ` |
| 123 | + |
| 124 | + const vm = new Vue({ |
| 125 | + render: function (h) { |
| 126 | + return h(Component, { |
| 127 | + props: { |
| 128 | + propsHeight: 20 |
| 129 | + }, |
| 130 | + }) |
| 131 | + } |
| 132 | + }).$mount() |
| 133 | + |
| 134 | + expect(vm.$children[0]._vnode.data.domProps).toEqual({ src: 'image.jpg', alt: 'Test image', height: 40 }) |
| 135 | + }) |
| 136 | + |
| 137 | + it('attributes should be reactive', () => { |
| 138 | + const Component = styled('img', { propsHeight: Number }) |
| 139 | + .attrs((props) => ({ |
| 140 | + src: 'image.jpg', |
| 141 | + alt: 'Test image', |
| 142 | + height: props.propsHeight * 2 |
| 143 | + }))` |
| 144 | + width: 50; |
| 145 | + ` |
| 146 | + |
| 147 | + const vm = new Vue({ |
| 148 | + render: function (h) { |
| 149 | + const self = this |
| 150 | + return h(Component, { |
| 151 | + props: { |
| 152 | + propsHeight: self.dataHeight |
| 153 | + }, |
| 154 | + }) |
| 155 | + }, |
| 156 | + data: () => ({ |
| 157 | + dataHeight: 20 |
| 158 | + }) |
| 159 | + }).$mount() |
| 160 | + |
| 161 | + expect(vm.$children[0]._vnode.data.domProps).toEqual({ src: 'image.jpg', alt: 'Test image', height: 40 }) |
| 162 | + |
| 163 | + vm.dataHeight = 90 |
| 164 | + setTimeout(() => { // $nextTick |
| 165 | + expect(vm.$children[0]._vnode.data.domProps).toEqual({ src: 'image.jpg', alt: 'Test image', height: 180 }) |
| 166 | + }, 0) |
| 167 | + }) |
| 168 | +}) |
0 commit comments