Commit a8707afa20e0aa88caeb5d98637297630eb3c373
1 parent
364aac02
Add tests for 1807
Showing
2 changed files
with
87 additions
and
1 deletions
Show diff stats
test/unit/specs/select.spec.js
| 1 | -import {createVue, destroyVM, waitForIt} from '../util'; | |
| 1 | +import {createVue, destroyVM, waitForIt, promissedTick} from '../util'; | |
| 2 | 2 | |
| 3 | 3 | describe('Select.vue', () => { |
| 4 | 4 | let vm; |
| ... | ... | @@ -155,6 +155,82 @@ describe('Select.vue', () => { |
| 155 | 155 | }); |
| 156 | 156 | }); |
| 157 | 157 | |
| 158 | + describe('Behavior tests', () => { | |
| 159 | + it('should create different and independent instances', done => { | |
| 160 | + const options = [ | |
| 161 | + {value: 'beijing', label: 'Beijing'}, | |
| 162 | + {value: 'stockholm', label: 'Stockholm'}, | |
| 163 | + {value: 'lisboa', label: 'Lisboa'} | |
| 164 | + ]; | |
| 165 | + | |
| 166 | + vm = createVue({ | |
| 167 | + template: ` | |
| 168 | + <div> | |
| 169 | + <i-select v-model="modelA" multiple style="width:260px"> | |
| 170 | + <i-option v-for="item in cityList" :value="item.value" :key="item.value">{{ item.label }}</i-option> | |
| 171 | + </i-select> | |
| 172 | + <i-select v-model="modelB" multiple style="width:260px"> | |
| 173 | + <i-option v-for="item in cityList" :value="item.value" :key="item.value">{{ item.label }}</i-option> | |
| 174 | + </i-select> | |
| 175 | + </div> | |
| 176 | + `, | |
| 177 | + data() { | |
| 178 | + return { | |
| 179 | + cityList: [], | |
| 180 | + modelA: [], | |
| 181 | + modelB: [] | |
| 182 | + }; | |
| 183 | + }, | |
| 184 | + mounted() { | |
| 185 | + setTimeout(() => (this.cityList = options), 200); | |
| 186 | + } | |
| 187 | + }); | |
| 188 | + const [SelectA, SelectB] = vm.$children; | |
| 189 | + SelectA.toggleMenu(); | |
| 190 | + SelectB.toggleMenu(); | |
| 191 | + | |
| 192 | + new Promise(resolve => { | |
| 193 | + const condition = function() { | |
| 194 | + const optionsA = SelectA.$el.querySelectorAll('.ivu-select-item'); | |
| 195 | + const optionsB = SelectB.$el.querySelectorAll('.ivu-select-item'); | |
| 196 | + return optionsA.length > 0 && optionsB.length > 0; | |
| 197 | + }; | |
| 198 | + waitForIt(condition, resolve); | |
| 199 | + }) | |
| 200 | + .then(() => { | |
| 201 | + // click in A options | |
| 202 | + const optionsA = SelectA.$el.querySelectorAll('.ivu-select-item'); | |
| 203 | + optionsA[0].click(); | |
| 204 | + return promissedTick(SelectA); | |
| 205 | + }) | |
| 206 | + .then(() => { | |
| 207 | + expect(SelectA.value[0]).to.equal(options[0].value); | |
| 208 | + expect(SelectA.value.length).to.equal(1); | |
| 209 | + expect(SelectB.value.length).to.equal(0); | |
| 210 | + | |
| 211 | + // click in B options | |
| 212 | + const optionsB = SelectB.$el.querySelectorAll('.ivu-select-item'); | |
| 213 | + optionsB[1].click(); | |
| 214 | + optionsB[2].click(); | |
| 215 | + return promissedTick(SelectB); | |
| 216 | + }) | |
| 217 | + .then(() => { | |
| 218 | + // lets check the values! | |
| 219 | + const getSelections = component => { | |
| 220 | + const tags = component.$el.querySelectorAll('.ivu-select-selection .ivu-tag'); | |
| 221 | + return [...tags].map(el => el.textContent.trim()).join(','); | |
| 222 | + }; | |
| 223 | + const selectAValue = getSelections(SelectA); | |
| 224 | + const selectBValue = getSelections(SelectB); | |
| 225 | + | |
| 226 | + expect(selectAValue).to.equal(options[0].label); | |
| 227 | + expect(selectBValue).to.equal(options.slice(1, 3).map(obj => obj.label.trim()).join(',')); | |
| 228 | + | |
| 229 | + done(); | |
| 230 | + }); | |
| 231 | + }); | |
| 232 | + }); | |
| 233 | + | |
| 158 | 234 | describe('Performance tests', () => { |
| 159 | 235 | it('should handle big numbers of options', done => { |
| 160 | 236 | const manyLaterOptions = Array.apply(null, Array(200)).map((_, i) => { | ... | ... |
test/unit/util.js
| ... | ... | @@ -103,3 +103,13 @@ exports.waitForIt = function waitForIt(condition, callback) { |
| 103 | 103 | if (condition()) callback(); |
| 104 | 104 | else setTimeout(() => waitForIt(condition, callback), 50); |
| 105 | 105 | }; |
| 106 | + | |
| 107 | +/** | |
| 108 | +* Call a components .$nextTick in a promissified way | |
| 109 | +* @param {Vue Component} the component to work with | |
| 110 | +*/ | |
| 111 | +exports.promissedTick = component => { | |
| 112 | + return new Promise((resolve, reject) => { | |
| 113 | + component.$nextTick(resolve); | |
| 114 | + }); | |
| 115 | +}; | ... | ... |