|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var rule = require('../../lib/rules/use-count-method') |
| 4 | +var RuleTester = require('eslint').RuleTester |
| 5 | +var toCode = require('../lib/join-multiline-code') |
| 6 | + |
| 7 | +var eslintTester = new RuleTester() |
| 8 | + |
| 9 | +eslintTester.run('use-count-method', rule, { |
| 10 | + valid: [ |
| 11 | + toCode([ |
| 12 | + 'expect(element.all(by.repeater("product in products")).count()).toBeGreaterThan(1);' |
| 13 | + ]), |
| 14 | + toCode([ |
| 15 | + 'var products = [];', |
| 16 | + 'console.log(products.length);' |
| 17 | + ]), |
| 18 | + toCode([ |
| 19 | + 'element.all(by.repeater("product in products")).then(function (products) {', |
| 20 | + ' console.log("test");', |
| 21 | + '});' |
| 22 | + ]), |
| 23 | + toCode([ |
| 24 | + 'element.all(by.repeater("product in products")).then(function (products) {', |
| 25 | + ' var testArray = [1, 2, 3];', |
| 26 | + ' expect(testArray.length).toEqual(3)', |
| 27 | + '});' |
| 28 | + ]), |
| 29 | + toCode([ |
| 30 | + 'element.all(by.repeater("product in products")).then(function (products) {', |
| 31 | + ' var testArray = [1, 2, 3];', |
| 32 | + ' expect(3).toEqual(testArray.length)', |
| 33 | + '});' |
| 34 | + ]), |
| 35 | + toCode([ |
| 36 | + 'element.all(by.repeater("product in products")).then(function () {', |
| 37 | + '});' |
| 38 | + ]), |
| 39 | + toCode([ |
| 40 | + 'element.all(by.repeater("product in products")).then(function () {', |
| 41 | + ' var testArray = [1, 2, 3];', |
| 42 | + ' expect(3).toEqual(testArray.length)', |
| 43 | + '});' |
| 44 | + ]), |
| 45 | + toCode([ |
| 46 | + 'element.all(by.repeater("product in products")).filter(function (elm) {', |
| 47 | + ' return true;', |
| 48 | + '});' |
| 49 | + ]), |
| 50 | + toCode([ |
| 51 | + 'element.all(by.repeater("product in products")).then(console.log);' |
| 52 | + ]), |
| 53 | + toCode([ |
| 54 | + 'somePromise.then(function (products) {', |
| 55 | + ' expect(products.length).toEqual(1);', |
| 56 | + '});' |
| 57 | + ]), |
| 58 | + toCode([ |
| 59 | + 'then(function (products) {', |
| 60 | + ' expect(products.length).toEqual(1);', |
| 61 | + '});' |
| 62 | + ]), |
| 63 | + toCode([ |
| 64 | + 'var products = [1, 2, 3];', |
| 65 | + 'element.all(by.repeater("product in products")).then(function () {', |
| 66 | + ' expect(products.length).toEqual(1);', |
| 67 | + '});' |
| 68 | + ]), |
| 69 | + toCode([ |
| 70 | + 'promise.method().then(function (products) {', |
| 71 | + ' expect(products.length).toEqual(1);', |
| 72 | + '});' |
| 73 | + ]) |
| 74 | + // TODO: promise.all().then() - recognized as an ElementArrayFinder! |
| 75 | + // TODO: run against ap-ui-html |
| 76 | + ], |
| 77 | + |
| 78 | + invalid: [ |
| 79 | + { |
| 80 | + code: toCode([ |
| 81 | + 'element.all(by.repeater("product in products")).then(function (products) {', |
| 82 | + ' expect(products.length >= 1).toBeTruthy();', |
| 83 | + '});' |
| 84 | + ]), |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 88 | + } |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + code: toCode([ |
| 93 | + 'element.all(by.repeater("product in products")).then((products) => {', |
| 94 | + ' expect(products.length >= 1).toBeTruthy();', |
| 95 | + '});' |
| 96 | + ]), |
| 97 | + parserOptions: { |
| 98 | + ecmaVersion: 6 |
| 99 | + }, |
| 100 | + errors: [ |
| 101 | + { |
| 102 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 103 | + } |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + code: toCode([ |
| 108 | + 'element.all(by.repeater("product in products")).then(function (products) {', |
| 109 | + ' expect(1).toEqual(products.length);', |
| 110 | + '});' |
| 111 | + ]), |
| 112 | + errors: [ |
| 113 | + { |
| 114 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 115 | + } |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + code: toCode([ |
| 120 | + 'element.all(by.repeater("product in products")).then((products) => {', |
| 121 | + ' expect(1).toEqual(products.length);', |
| 122 | + '});' |
| 123 | + ]), |
| 124 | + parserOptions: { |
| 125 | + ecmaVersion: 6 |
| 126 | + }, |
| 127 | + errors: [ |
| 128 | + { |
| 129 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 130 | + } |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + code: toCode([ |
| 135 | + 'element.all(by.repeater("product in products")).then(function (products) {', |
| 136 | + ' expect(products.length).toEqual(products.length);', |
| 137 | + '});' |
| 138 | + ]), |
| 139 | + errors: [ |
| 140 | + { |
| 141 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 142 | + }, |
| 143 | + { |
| 144 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 145 | + } |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + code: toCode([ |
| 150 | + 'element.all(by.repeater("product in products")).then((products) => {', |
| 151 | + ' expect(products.length).toEqual(products.length);', |
| 152 | + '});' |
| 153 | + ]), |
| 154 | + parserOptions: { |
| 155 | + ecmaVersion: 6 |
| 156 | + }, |
| 157 | + errors: [ |
| 158 | + { |
| 159 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 160 | + }, |
| 161 | + { |
| 162 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 163 | + } |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + code: toCode([ |
| 168 | + '$$(".product").then(function (products) {', |
| 169 | + ' expect(products.length >= 1).toBeTruthy();', |
| 170 | + '});' |
| 171 | + ]), |
| 172 | + errors: [ |
| 173 | + { |
| 174 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 175 | + } |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + code: toCode([ |
| 180 | + '$$(".product").then(function (products) {', |
| 181 | + ' expect(1).toEqual(products.length);', |
| 182 | + '});' |
| 183 | + ]), |
| 184 | + errors: [ |
| 185 | + { |
| 186 | + message: 'Array.length inside promise resolution function detected. Use count() instead.' |
| 187 | + } |
| 188 | + ] |
| 189 | + } |
| 190 | + ] |
| 191 | +}) |
0 commit comments