|
| 1 | +/* global require */ |
| 2 | + |
| 3 | +"use strict"; |
| 4 | + |
| 5 | +const rule = require("../../../lib/rules/restrict-dom-node-retrieval"); |
| 6 | +const RuleTester = require("eslint").RuleTester; |
| 7 | + |
| 8 | +const ruleTester = new RuleTester({ |
| 9 | + languageOptions: { |
| 10 | + parserOptions: { |
| 11 | + ecmaVersion: 6, |
| 12 | + ecmaFeatures: { |
| 13 | + jsx: true, |
| 14 | + }, |
| 15 | + }, |
| 16 | + }, |
| 17 | +}); |
| 18 | + |
| 19 | +const ERROR_MSG = "Don't retrieve dom nodes directly, use react-refs to read/manipulate the dom"; |
| 20 | + |
| 21 | +ruleTester.run("restrict-dom-node-retrieval", rule, { |
| 22 | + valid: [ |
| 23 | + { |
| 24 | + code: `const MyComponent = () => { |
| 25 | + return <div>valid</div> |
| 26 | + }`, |
| 27 | + }, |
| 28 | + { |
| 29 | + code: `function MyComponent() { |
| 30 | + return <div>valid</div> |
| 31 | + }`, |
| 32 | + }, |
| 33 | + { |
| 34 | + code: `it("should be allowed in a test", () => { |
| 35 | + const element = document.querySelector(".class"); |
| 36 | + });`, |
| 37 | + }, |
| 38 | + { |
| 39 | + code: `it("should be allowed in a test", () => { |
| 40 | + const element = window.document.querySelector(".class"); |
| 41 | + });`, |
| 42 | + }, |
| 43 | + { |
| 44 | + code: `it("should be allowed in a test", () => { |
| 45 | + const element = document.querySelectorAll(".class"); |
| 46 | + });`, |
| 47 | + }, |
| 48 | + { |
| 49 | + code: `it("should be allowed in a test", () => { |
| 50 | + const element = window.document.querySelectorAll(".class"); |
| 51 | + });`, |
| 52 | + }, |
| 53 | + { |
| 54 | + code: `it("should be allowed in a test", () => { |
| 55 | + const element = document.getElementsByTagName("div"); |
| 56 | + });`, |
| 57 | + }, |
| 58 | + { |
| 59 | + code: `it("should be allowed in a test", () => { |
| 60 | + const element = window.document.getElementsByTagName("div"); |
| 61 | + });`, |
| 62 | + }, |
| 63 | + { |
| 64 | + code: `it("should be allowed in a test", () => { |
| 65 | + const element = document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "div"); |
| 66 | + });`, |
| 67 | + }, |
| 68 | + { |
| 69 | + code: `it("should be allowed in a test", () => { |
| 70 | + const element = window.document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "div"); |
| 71 | + });`, |
| 72 | + }, |
| 73 | + { |
| 74 | + code: `it("should be allowed in a test", () => { |
| 75 | + const element = document.getElementsByClassName("class"); |
| 76 | + });`, |
| 77 | + }, |
| 78 | + { |
| 79 | + code: `it("should be allowed in a test", () => { |
| 80 | + const element = window.document.getElementsByClassName("class"); |
| 81 | + });`, |
| 82 | + }, |
| 83 | + { |
| 84 | + code: `it("should be allowed in a test", () => { |
| 85 | + const element = document.getElementById("id"); |
| 86 | + });`, |
| 87 | + }, |
| 88 | + { |
| 89 | + code: `it("should be allowed in a test", () => { |
| 90 | + const element = window.document.getElementById("id"); |
| 91 | + });`, |
| 92 | + }, |
| 93 | + ], |
| 94 | + invalid: [ |
| 95 | + { |
| 96 | + code: `element = document.querySelector(".class")`, |
| 97 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 98 | + }, |
| 99 | + { |
| 100 | + code: `element = window.document.querySelector(".class")`, |
| 101 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 102 | + }, |
| 103 | + { |
| 104 | + code: `element = document.querySelectorAll(".class")`, |
| 105 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 106 | + }, |
| 107 | + { |
| 108 | + code: `element = window.document.querySelectorAll(".class")`, |
| 109 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 110 | + }, |
| 111 | + { |
| 112 | + code: `element = document.getElementsByTagName("div")`, |
| 113 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 114 | + }, |
| 115 | + { |
| 116 | + code: `element = window.document.getElementsByTagName("div")`, |
| 117 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 118 | + }, |
| 119 | + { |
| 120 | + code: `element = document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "div")`, |
| 121 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 122 | + }, |
| 123 | + { |
| 124 | + code: `element = window.document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "div")`, |
| 125 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 126 | + }, |
| 127 | + { |
| 128 | + code: `element = document.getElementsByClassName("class")`, |
| 129 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 130 | + }, |
| 131 | + { |
| 132 | + code: `element = window.document.getElementsByClassName("class")`, |
| 133 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 134 | + }, |
| 135 | + { |
| 136 | + code: `element = document.getElementById("id")`, |
| 137 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 138 | + }, |
| 139 | + { |
| 140 | + code: `element = window.document.getElementById("id")`, |
| 141 | + errors: [ { message: ERROR_MSG, type: "CallExpression" } ], |
| 142 | + }, |
| 143 | + ], |
| 144 | +}); |
0 commit comments