|
| 1 | +import { haveText, html, test } from '../utils' |
| 2 | + |
| 3 | +export function setupConsoleInterceptor( ...targetIds ) { |
| 4 | + const mappedTargetIds = targetIds.map( tid => `'${tid}'` ).join( ',' ) |
| 5 | + return ` |
| 6 | + let errorContainer = document.createElement('div'); |
| 7 | + errorContainer.id = 'errors' |
| 8 | + errorContainer.textContent = 'false' |
| 9 | + document.querySelector('#root').after(errorContainer) |
| 10 | + console.warnlog = console.warn.bind(console) |
| 11 | + console.warn = function () { |
| 12 | + document.getElementById( 'errors' ).textContent = [${mappedTargetIds}].some( target => arguments[1] === document.getElementById( target ) ) |
| 13 | + console.warnlog.apply(console, arguments) |
| 14 | + } |
| 15 | + ` |
| 16 | +} |
| 17 | + |
| 18 | +export function assertConsoleInterceptorHadErrorWithCorrectElement() { |
| 19 | + return ({get}) => { |
| 20 | + get('#errors').should(haveText('true')) |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +test('x-for identifier issue', |
| 25 | + [html` |
| 26 | + <div x-data="{ items: ['foo'] }"> |
| 27 | + <template id="xfor" x-for="item in itemzzzz"> |
| 28 | + <span x-text="item"></span> |
| 29 | + </template> |
| 30 | + </div> |
| 31 | + `, |
| 32 | + setupConsoleInterceptor( "xfor" ) |
| 33 | + ], |
| 34 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 35 | + true |
| 36 | +) |
| 37 | + |
| 38 | +test('x-text identifier issue', |
| 39 | + [html` |
| 40 | + <div x-data="{ items: ['foo'] }"> |
| 41 | + <template x-for="item in items"> |
| 42 | + <span id="xtext" x-text="itemzzz"></span> |
| 43 | + </template> |
| 44 | + </div> |
| 45 | + `, |
| 46 | + setupConsoleInterceptor( "xtext" ) |
| 47 | + ], |
| 48 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 49 | + true |
| 50 | +) |
| 51 | + |
| 52 | +test('x-init identifier issue', |
| 53 | + [html` |
| 54 | + <div id="xinit" x-data x-init="doesNotExist()"> |
| 55 | + </div> |
| 56 | + `, |
| 57 | + setupConsoleInterceptor( "xinit" ) |
| 58 | + ], |
| 59 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 60 | + true |
| 61 | +) |
| 62 | + |
| 63 | +test('x-show identifier issue', |
| 64 | + [html` |
| 65 | + <div id="xshow" x-data="{isOpen: true}" x-show="isVisible"> |
| 66 | + </div> |
| 67 | + `, |
| 68 | + setupConsoleInterceptor( "xshow" ) |
| 69 | + ], |
| 70 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 71 | + true |
| 72 | +) |
| 73 | + |
| 74 | +test('x-bind class object syntax identifier issue', |
| 75 | + [html` |
| 76 | + <div x-data="{isOpen: true}"> |
| 77 | + <div id="xbind" :class="{ 'block' : isVisible, 'hidden' : !isVisible }"></div> |
| 78 | + </div> |
| 79 | + `, |
| 80 | + setupConsoleInterceptor( "xbind" ) |
| 81 | + ], |
| 82 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 83 | + true |
| 84 | +) |
| 85 | + |
| 86 | +test('x-model identifier issue', |
| 87 | + [html` |
| 88 | + <div x-data="{value: ''}"> |
| 89 | + <input id="xmodel" x-model="thething"/> |
| 90 | + </div> |
| 91 | + `, |
| 92 | + setupConsoleInterceptor( "xmodel" ) |
| 93 | + ], |
| 94 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 95 | + true |
| 96 | +) |
| 97 | + |
| 98 | +test('x-if identifier issue', |
| 99 | + [html` |
| 100 | + <div x-data="{value: ''}"> |
| 101 | + <template id="xif" x-if="valuez === ''"> |
| 102 | + <span>Words</span> |
| 103 | + </template> |
| 104 | + </div> |
| 105 | + `, |
| 106 | + setupConsoleInterceptor( "xif" ) |
| 107 | + ], |
| 108 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 109 | + true |
| 110 | +) |
| 111 | + |
| 112 | +test('x-if identifier issue ( function )', |
| 113 | + [html` |
| 114 | + <div x-data="{shouldOpen: function(){}}"> |
| 115 | + <template id="xif" x-if="isOpen()"> |
| 116 | + <span>Words</span> |
| 117 | + </template> |
| 118 | + </div> |
| 119 | + `, |
| 120 | + setupConsoleInterceptor( "xif" ) |
| 121 | + ], |
| 122 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 123 | + true |
| 124 | +) |
| 125 | + |
| 126 | +test('x-effect identifier issue', |
| 127 | + [html` |
| 128 | + <div id="xeffect" x-data="{ label: 'Hello' }" x-effect="System.out.println(label)"> |
| 129 | + </div> |
| 130 | + `, |
| 131 | + setupConsoleInterceptor( "xeffect" ) |
| 132 | + ], |
| 133 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 134 | + true |
| 135 | +) |
| 136 | + |
| 137 | +test('x-on identifier issue', |
| 138 | + [html` |
| 139 | + <div x-data="{ label: 'Hello' }"> |
| 140 | + <div x-text="label"></div> |
| 141 | + <button id="xon" x-on:click="labelz += ' World!'">Change Message</button> |
| 142 | + </div> |
| 143 | + `, |
| 144 | + setupConsoleInterceptor( "xon" ) |
| 145 | + ], |
| 146 | + ({ get }) => { |
| 147 | + get( "#xon" ).click() |
| 148 | + get( "#errors" ).should(haveText('true')) |
| 149 | + }, |
| 150 | + true |
| 151 | +) |
| 152 | + |
| 153 | +test('x-data syntax error', |
| 154 | + [html` |
| 155 | + <div id="xdata" x-data="{ label: 'Hello' }aaa"> |
| 156 | + </div> |
| 157 | + `, |
| 158 | + setupConsoleInterceptor( "xdata" ) |
| 159 | + ], |
| 160 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 161 | + true |
| 162 | +) |
| 163 | + |
| 164 | +test('if statement syntax error', |
| 165 | + [html` |
| 166 | + <div x-data="{ label: 'Hello' }"> |
| 167 | + <div id="xtext" x-text="if( false { label } else { 'bye' }"></div> |
| 168 | + </div> |
| 169 | + `, |
| 170 | + setupConsoleInterceptor( "xtext" ) |
| 171 | + ], |
| 172 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 173 | + true |
| 174 | +) |
| 175 | + |
| 176 | +test('x-data with reference error and multiple errors', |
| 177 | + [html` |
| 178 | + <div id="xdata" x-data="{ items : [ {v:'one'},{v:'two'}], replaceItems }"> |
| 179 | + <template id="xtext" x-for="item in items"> |
| 180 | + <span x-text="item.v"></span> |
| 181 | + </template> |
| 182 | + </div> |
| 183 | + `, |
| 184 | + setupConsoleInterceptor( "xdata", "xtext" ) |
| 185 | + ], |
| 186 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 187 | + true |
| 188 | +) |
| 189 | + |
| 190 | +test('evaluation with syntax error', |
| 191 | + [html` |
| 192 | + <div x-data="{value: ''}"> |
| 193 | + <template id="xif" x-if="value ==== ''"> |
| 194 | + <span>Words</span> |
| 195 | + </template> |
| 196 | + </div> |
| 197 | + `, |
| 198 | + setupConsoleInterceptor( "xif" ) |
| 199 | + ], |
| 200 | + assertConsoleInterceptorHadErrorWithCorrectElement(), |
| 201 | + true |
| 202 | +) |
0 commit comments