|
| 1 | +import { compile } from '../../src' |
| 2 | + |
| 3 | +describe('Transition multi children warnings', () => { |
| 4 | + function checkWarning( |
| 5 | + template: string, |
| 6 | + shouldWarn: boolean, |
| 7 | + message = `<Transition> expects exactly one child element or component.` |
| 8 | + ) { |
| 9 | + const spy = jest.fn() |
| 10 | + compile(template.trim(), { |
| 11 | + hoistStatic: true, |
| 12 | + transformHoist: null, |
| 13 | + onError: err => { |
| 14 | + spy(err.message) |
| 15 | + } |
| 16 | + }) |
| 17 | + |
| 18 | + if (shouldWarn) expect(spy).toHaveBeenCalledWith(message) |
| 19 | + else expect(spy).not.toHaveBeenCalled() |
| 20 | + } |
| 21 | + |
| 22 | + test('warns if multiple children', () => { |
| 23 | + checkWarning( |
| 24 | + ` |
| 25 | + <transition> |
| 26 | + <div>hey</div> |
| 27 | + <div>hey</div> |
| 28 | + </transition> |
| 29 | + `, |
| 30 | + true |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | + test('warns with v-for', () => { |
| 35 | + checkWarning( |
| 36 | + ` |
| 37 | + <transition> |
| 38 | + <div v-for="i in items">hey</div> |
| 39 | + </transition> |
| 40 | + `, |
| 41 | + true |
| 42 | + ) |
| 43 | + }) |
| 44 | + |
| 45 | + test('warns with multiple v-if + v-for', () => { |
| 46 | + checkWarning( |
| 47 | + ` |
| 48 | + <transition> |
| 49 | + <div v-if="a" v-for="i in items">hey</div> |
| 50 | + <div v-else v-for="i in items">hey</div> |
| 51 | + </transition> |
| 52 | + `, |
| 53 | + true |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + test('warns with template v-if', () => { |
| 58 | + checkWarning( |
| 59 | + ` |
| 60 | + <transition> |
| 61 | + <template v-if="ok"></template> |
| 62 | + </transition> |
| 63 | + `, |
| 64 | + true |
| 65 | + ) |
| 66 | + }) |
| 67 | + |
| 68 | + test('warns with multiple templates', () => { |
| 69 | + checkWarning( |
| 70 | + ` |
| 71 | + <transition> |
| 72 | + <template v-if="a"></template> |
| 73 | + <template v-else></template> |
| 74 | + </transition> |
| 75 | + `, |
| 76 | + true |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + test('warns if multiple children with v-if', () => { |
| 81 | + checkWarning( |
| 82 | + ` |
| 83 | + <transition> |
| 84 | + <div v-if="one">hey</div> |
| 85 | + <div v-if="other">hey</div> |
| 86 | + </transition> |
| 87 | + `, |
| 88 | + true |
| 89 | + ) |
| 90 | + }) |
| 91 | + |
| 92 | + test('does not warn with regular element', () => { |
| 93 | + checkWarning( |
| 94 | + ` |
| 95 | + <transition> |
| 96 | + <div>hey</div> |
| 97 | + </transition> |
| 98 | + `, |
| 99 | + false |
| 100 | + ) |
| 101 | + }) |
| 102 | + |
| 103 | + test('does not warn with one single v-if', () => { |
| 104 | + checkWarning( |
| 105 | + ` |
| 106 | + <transition> |
| 107 | + <div v-if="a">hey</div> |
| 108 | + </transition> |
| 109 | + `, |
| 110 | + false |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + test('does not warn with v-if v-else-if v-else', () => { |
| 115 | + checkWarning( |
| 116 | + ` |
| 117 | + <transition> |
| 118 | + <div v-if="a">hey</div> |
| 119 | + <div v-else-if="b">hey</div> |
| 120 | + <div v-else>hey</div> |
| 121 | + </transition> |
| 122 | + `, |
| 123 | + false |
| 124 | + ) |
| 125 | + }) |
| 126 | + |
| 127 | + test('does not warn with v-if v-else', () => { |
| 128 | + checkWarning( |
| 129 | + ` |
| 130 | + <transition> |
| 131 | + <div v-if="a">hey</div> |
| 132 | + <div v-else>hey</div> |
| 133 | + </transition> |
| 134 | + `, |
| 135 | + false |
| 136 | + ) |
| 137 | + }) |
| 138 | +}) |
| 139 | + |
| 140 | +test('inject persisted when child has v-show', () => { |
| 141 | + expect( |
| 142 | + compile(` |
| 143 | + <transition> |
| 144 | + <div v-show="ok" /> |
| 145 | + </transition> |
| 146 | + `).code |
| 147 | + ).toMatchSnapshot() |
| 148 | +}) |
| 149 | + |
| 150 | +test('the v-if/else-if/else branches in Transition should ignore comments', () => { |
| 151 | + expect( |
| 152 | + compile(` |
| 153 | + <transition> |
| 154 | + <div v-if="a">hey</div> |
| 155 | + <!-- this should be ignored --> |
| 156 | + <div v-else-if="b">hey</div> |
| 157 | + <!-- this should be ignored --> |
| 158 | + <div v-else> |
| 159 | + <p v-if="c"/> |
| 160 | + <!-- this should not be ignored --> |
| 161 | + <p v-else/> |
| 162 | + </div> |
| 163 | + </transition> |
| 164 | + `).code |
| 165 | + ).toMatchSnapshot() |
| 166 | +}) |
0 commit comments