142
St
startsInvasion / files
startsInvasion / libs / node_modules / qwery / tests / tests.js
8.2 KB
1sink('no conflict', function (test, ok) { 2 test('should return old qwery back to context', 1, function () { 3 ok(qwery() == 'success', 'old qwery called'); 4 }); 5}); 6 7sink('Contexts', function (test, ok) { 8 test('should be able to pass optional context', 2, function () { 9 ok(Q('.a').length === 3, 'no context found 3 elements (.a)'); 10 ok(Q('.a', Q('#boosh')[0]).length === 2, 'context found 2 elements (#boosh .a)'); 11 }); 12 test('should be able to pass string as context', 5, function() { 13 ok(Q('.a', '#boosh').length == 2, 'context found 2 elements(.a, #boosh)'); 14 ok(Q('.a', '.a').length == 0, 'context found 0 elements(.a, .a)'); 15 ok(Q('.a', '.b').length == 1, 'context found 1 elements(.a, .b)'); 16 ok(Q('.a', '#boosh .b').length == 1, 'context found 1 elements(.a, #boosh .b)'); 17 ok(Q('.b', '#boosh .b').length == 0, 'context found 0 elements(.b, #boosh .b)'); 18 }); 19 test('should not return duplicates from combinators', 1, function () { 20 ok(Q('#boosh,#boosh').length == 1, 'two booshes dont make a thing go right'); 21 }); 22}); 23 24sink('CSS 1', function (test, ok) { 25 test('get element by id', 2, function () { 26 var result = Q('#boosh'); 27 ok(!!result[0], 'found element with id=boosh'); 28 ok(!!Q('h1')[0], 'found 1 h1'); 29 }); 30 31 test('get elements by class', 5, function () { 32 ok(Q('#boosh .a').length == 2, 'found two elements'); 33 ok(!!Q('#boosh div.a')[0], 'found one element'); 34 ok(Q('#boosh div').length == 2, 'found two {div} elements'); 35 ok(!!Q('#boosh span')[0], 'found one {span} element'); 36 ok(!!Q('#boosh div div')[0], 'found a single div'); 37 }); 38 39 test('combos', 1, function () { 40 ok(Q('#boosh div,#boosh span').length == 3, 'found 2 divs and 1 span'); 41 }); 42 43 test('class with dashes', 1, function() { 44 ok(Q('.class-with-dashes').length == 1, 'found something'); 45 }); 46}); 47 48sink('CSS 2', function (test, ok) { 49 50 test('get elements by attribute', 4, function () { 51 var wanted = Q('#boosh div[test]')[0]; 52 var expected = document.getElementById('booshTest'); 53 ok(wanted == expected, 'found attribute'); 54 ok(Q('#boosh div[test=fg]')[0] == expected, 'found attribute with value'); 55 ok(Q('em[rel~="copyright"]').length == 1, 'found em[rel~="copyright"]'); 56 ok(Q('em[nopass~="copyright"]').length == 0, 'found em[nopass~="copyright"]'); 57 }); 58 59 test('should not throw error by attribute selector', 1, function () { 60 ok(Q('[foo^="bar"]').length === 1, 'found 1 element'); 61 }); 62 63 test('crazy town', 1, function () { 64 var el = document.getElementById('attr-test3'); 65 ok(Q('div#attr-test3.found.you[title="whatup duders"]')[0] == el, 'found the right element'); 66 }); 67 68}); 69 70sink('attribute selectors', function (test, ok) { 71 72 /* CSS 2 SPEC */ 73 74 test('[attr]', 1, function () { 75 var expected = document.getElementById('attr-test-1'); 76 ok(Q('#attributes div[unique-test]')[0] == expected, 'found attribute with [attr]'); 77 }); 78 79 test('[attr=val]', 3, function () { 80 var expected = document.getElementById('attr-test-2'); 81 ok(Q('#attributes div[test="two-foo"]')[0] == expected, 'found attribute with ='); 82 ok(Q("#attributes div[test='two-foo']")[0] == expected, 'found attribute with ='); 83 ok(Q('#attributes div[test=two-foo]')[0] == expected, 'found attribute with ='); 84 }); 85 86 test('[attr~=val]', 1, function () { 87 var expected = document.getElementById('attr-test-3'); 88 ok(Q('#attributes div[test~=three]')[0] == expected, 'found attribute with ~='); 89 }); 90 91 test('[attr|=val]', 2, function () { 92 var expected = document.getElementById('attr-test-2'); 93 ok(Q('#attributes div[test|="two-foo"]')[0] == expected, 'found attribute with |='); 94 ok(Q('#attributes div[test|=two]')[0] == expected, 'found attribute with |='); 95 }); 96 97 /* CSS 3 SPEC */ 98 99 test('[attr^=val]', 1, function () { 100 var expected = document.getElementById('attr-test-2'); 101 ok(Q('#attributes div[test^=two]')[0] == expected, 'found attribute with ^='); 102 }); 103 104 test('[attr$=val]', 1, function () { 105 var expected = document.getElementById('attr-test-2'); 106 ok(Q('#attributes div[test$=foo]')[0] == expected, 'found attribute with $='); 107 }); 108 109 test('[attr*=val]', 1, function () { 110 var expected = document.getElementById('attr-test-3'); 111 ok(Q('#attributes div[test*=hree]')[0] == expected, 'found attribute with *='); 112 }); 113 114 test('direct descendants', 1, function () { 115 ok(Q('#direct-descend > .direct-descend').length == 2, 'found two direct descendents'); 116 }) 117 118 test('sibling elements', 4, function () { 119 ok(Q('#sibling-selector ~ .sibling-selector').length == 2, 'found two siblings') 120 ok(Q('#sibling-selector ~ div.sibling-selector').length == 2, 'found two siblings') 121 ok(Q('#sibling-selector + div.sibling-selector').length == 1, 'found two siblings') 122 ok(Q('#sibling-selector + .sibling-selector').length == 1, 'found two siblings') 123 }) 124 125}); 126 127 128sink('tokenizer', function (test, ok) { 129 130 test('should not get weird tokens', 5, function () { 131 ok(Q('div .tokens[title="one"]')[0] == document.getElementById('token-one'), 'found div .tokens[title="one"]'); 132 ok(Q('div .tokens[title="one two"]')[0] == document.getElementById('token-two'), 'found div .tokens[title="one two"]'); 133 ok(Q('div .tokens[title="one two three #%"]')[0] == document.getElementById('token-three'), 'found div .tokens[title="one two three #%"]'); 134 ok(Q("div .tokens[title='one two three #%'] a")[0] == document.getElementById('token-four'), 'found div .tokens[title=\'one two three #%\'] a'); 135 ok(Q('div .tokens[title="one two three #%"] a[href$=foo] div')[0] == document.getElementById('token-five'), 'found div .tokens[title="one two three #%"] a[href=foo] div'); 136 }); 137 138}); 139 140sink('interesting syntaxes', function (test, ok) { 141 test('should parse bad selectors', 1, function () { 142 ok(Q('#spaced-tokens p em a').length, 'found element with funny tokens') 143 }) 144}) 145 146sink('order matters', function (test, ok) { 147 148 function tag(el) { 149 return el.tagName.toLowerCase(); 150 } 151 152 // <div id="order-matters"> 153 // <p class="order-matters"></p> 154 // <a class="order-matters"> 155 // <em class="order-matters"></em><b class="order-matters"></b> 156 // </a> 157 // </div> 158 159 test('the order of elements return matters', 4, function () { 160 var els = Q('#order-matters .order-matters'); 161 ok(tag(els[0]) == 'p', 'first element matched is a {p} tag'); 162 ok(tag(els[1]) == 'a', 'first element matched is a {a} tag'); 163 ok(tag(els[2]) == 'em', 'first element matched is a {em} tag'); 164 ok(tag(els[3]) == 'b', 'first element matched is a {b} tag'); 165 }); 166 167}); 168 169sink('pseudo selectors', function (test, ok) { 170 171 test(':first-child', 2, function () { 172 ok(Q('#pseudos div:first-child')[0] == document.getElementById('pseudos').getElementsByTagName('*')[0], 'found first child') 173 ok(Q('#pseudos div:first-child').length == 1, 'found only 1') 174 }) 175 176 test(':last-child', 2, function () { 177 var all = document.getElementById('pseudos').getElementsByTagName('div'); 178 ok(Q('#pseudos div:last-child')[0] == all[all.length - 1], 'found last child') 179 ok(Q('#pseudos div:last-child').length == 1, 'found only 1') 180 }) 181 182 test(':nth-child(odd|even|n)', 4, function () { 183 var second = document.getElementById('pseudos').getElementsByTagName('div')[1]; 184 ok(Q('#pseudos :nth-child(odd)').length == 4, 'found 4 odd elements'); 185 ok(Q('#pseudos div:nth-child(odd)').length == 3, 'found 3 odd elements with div tag'); 186 ok(Q('#pseudos div:nth-child(even)').length == 3, 'found 4 even elements with div tag'); 187 ok(Q('#pseudos div:nth-child(2)')[0] == second, 'found 2nd nth-child of pseudos'); 188 }) 189 190 test('ol > li[attr="boosh"]:last-child', 2, function () { 191 var expected = document.getElementById('attr-child-boosh'); 192 ok(Q('ol > li[attr="boosh"]:last-child').length == 1, 'only 1 element found'); 193 ok(Q('ol > li[attr="boosh"]:last-child')[0] == expected, 'found correct element'); 194 }) 195 196}) 197 198sink('argument types', function (test, ok) { 199 test('should be able to pass in nodes as arguments', 5, function () { 200 var el = document.getElementById('boosh'); 201 ok(Q(el)[0] == el, 'Q(el)[0] == el'); 202 ok(Q(el, 'body')[0] == el, "Q(el, 'body')[0] == el"); 203 ok(Q(el, document)[0] == el, "Q(el, document)[0] == el"); 204 ok(Q(window)[0] == window, 'Q(window)[0] == window'); 205 ok(Q(document)[0] == document, 'Q(document)[0] == document'); 206 }); 207}); 208 209start();