UNPKG

3.58 kBJavaScriptView Raw
1/* global describe, it, expect */
2
3const { prepareESLint, lint } = require("../../test_utils");
4
5const engine = prepareESLint("recommended");
6
7it("Warns on console.log", async () => {
8 const result = await lint(engine, `console.log("Yeah");\n`);
9
10 expect(result.messages).toMatchSnapshot();
11 expect(result.warningCount).toBe(1);
12 expect(result.errorCount).toBe(0);
13});
14
15it("Uses sonar plugin", async () => {
16 const result = await lint(
17 engine,
18 `
19/* global openWindow, closeWindow, moveWindowToTheBackground */
20
21function changeWindow(param) {
22 if (param === 1) {
23 openWindow();
24 } else if (param === 2) {
25 closeWindow();
26 } else if (param === 1) {
27 // Noncompliant ^
28 moveWindowToTheBackground();
29 }
30}
31`
32 );
33
34 expect(result.messages).toMatchSnapshot();
35 expect(result.warningCount).toBe(0);
36 expect(result.errorCount).toBe(2);
37});
38
39it("Works fine with ES6 code", async () => {
40 const result = await lint(
41 engine,
42 `
43class SkinnedMesh extends THREE.Mesh {
44 constructor(geometry, materials) {
45 super(geometry, materials);
46
47 this.idMatrix = SkinnedMesh.defaultMatrix();
48 this.bones = [];
49 this.boneMatrices = [];
50 //...
51 }
52 update(camera) {
53 //...
54 super.update();
55 }
56 get boneCount() {
57 return this.bones.length;
58 }
59 set matrixType(matrixType) {
60 this.idMatrix = SkinnedMesh[matrixType]();
61 }
62 static defaultMatrix() {
63 return new THREE.Matrix4();
64 }
65}
66
67const { op: a, lhs: { op: b }, rhs: c } = getASTNode();
68
69const someString = \`In JavaScript this is
70 not legal.\`;
71
72const obj = {
73 // __proto__
74 __proto__: theProtoObj,
75 // Shorthand for ‘someString: someString’
76 someString,
77 // Methods
78 toString() {
79 // Super calls
80 return \`d \${ super.toString()}\`;
81 },
82 // Computed (dynamic) property names
83 [ \`prop_\${ (() => 42)()}\` ]: 42
84};
85
86`
87 );
88
89 expect(result.messages).toMatchSnapshot();
90 expect(result.warningCount).toBe(0);
91 expect(result.errorCount).toBe(22);
92});
93
94describe("jsx-no-duplicate-props", () => {
95 it("works with different props", async () => {
96 const result = await lint(
97 engine,
98 `
99import * as React from "react";
100
101export default function SomeComponent() {
102 return <div id="" className="" />;
103}
104`
105 );
106
107 expect(result.messages).toEqual([], "no messages expected");
108 expect(result.warningCount).toBe(0, "no warnings expected");
109 expect(result.errorCount).toBe(0, "no errors expected");
110 });
111
112 it("works fails with the same prop", async () => {
113 const result = await lint(
114 engine,
115 `
116import * as React from "react";
117
118export default function SomeComponent() {
119 return <div id="" id="" />;
120}
121`
122 );
123
124 expect(result.messages).toMatchSnapshot();
125 expect(result.warningCount).toBe(0, "no warnings expectec");
126 expect(result.errorCount).toBe(1, "one error expected");
127 });
128});
129
130describe("no-did-mount-set-state", () => {
131 it("fails with setState in componentDidMount", async () => {
132 const result = await lint(
133 engine,
134 `
135import React from "react";
136import PropTypes from "prop-types";
137
138export default class MyComponent extends React.Component {
139 componentDidMount() {
140 this.setState({
141 name: this.props.name.toUpperCase()
142 });
143 }
144 render() {
145 return <div>Hello {this.state.name}</div>;
146 }
147}
148
149MyComponent.propTypes = {
150 name: PropTypes.string.isRequired
151};
152`
153 );
154
155 expect(result.messages).toMatchSnapshot();
156 expect(result.warningCount).toBe(0, "no warnings expected");
157 expect(result.errorCount).toBe(1, "one errir expected");
158 });
159});