-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add eslint plugin tests (#3905)
* add tests for eslint rule missing-observer * add tests for eslint rule no-anonymous-observer
- Loading branch information
Showing
4 changed files
with
99 additions
and
30 deletions.
There are no files selected for viewing
15 changes: 0 additions & 15 deletions
15
packages/eslint-plugin-mobx/__tests__/TODO-missing-observer.js
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
packages/eslint-plugin-mobx/__tests__/TODO-no-anonymous-observer.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { RuleTester } from "eslint" | ||
|
||
import rule from "../src/missing-observer.js" | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve("@typescript-eslint/parser"), | ||
parserOptions: {} | ||
}) | ||
|
||
const valids = [ | ||
"observer(function Named() { });", | ||
"const foo = observer(function Named() { })", | ||
"const Anonym = observer(function () { });", | ||
"const Arrow = observer(() => { });", | ||
"function notCmp() { }", | ||
"const notCmp = function notCmp() { }", | ||
"const notCmp = function () { }", | ||
"const notCmp = () => { }", | ||
"class NotCmp { }", | ||
"class NotCmp extends Foo { }", | ||
"class NotCmp extends React.Foo { }", | ||
"const Cmp = observer(class Cmp extends React.Component { })", | ||
"const Cmp = observer(class Cmp extends Component { })", | ||
"const Cmp = observer(class extends React.Component { })", | ||
"const Cmp = observer(class extends Component { })" | ||
] | ||
|
||
const invalids = [ | ||
["function Named() { }", "const Named = observer(function Named() { })"], | ||
["const foo = function Named() { }", "const foo = observer(function Named() { })"], | ||
["const Anonym = function () { };", "const Anonym = observer(function () { });"], | ||
["const Arrow = () => { };", "const Arrow = observer(() => { });"], | ||
[ | ||
"class Cmp extends React.Component { }", | ||
"const Cmp = observer(class Cmp extends React.Component { })" | ||
], | ||
["class Cmp extends Component { }", "const Cmp = observer(class Cmp extends Component { })"], | ||
[ | ||
"const Cmp = class extends React.Component { }", | ||
"const Cmp = observer(class extends React.Component { })" | ||
], | ||
[ | ||
"const Cmp = class extends Component { }", | ||
"const Cmp = observer(class extends Component { })" | ||
], | ||
["class extends Component { }", "observer(class extends Component { })"], | ||
["class extends React.Component { }", "observer(class extends React.Component { })"] | ||
] | ||
|
||
tester.run("missing-observer", rule, { | ||
valid: valids.map(code => ({ code })), | ||
invalid: invalids.map(([code, output]) => ({ | ||
code, | ||
output, | ||
errors: [{ messageId: "missingObserver" }] | ||
})) | ||
}) |
42 changes: 42 additions & 0 deletions
42
packages/eslint-plugin-mobx/__tests__/no-anonymous-observer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { RuleTester } from "eslint" | ||
|
||
import rule from "../src/no-anonymous-observer.js" | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve("@typescript-eslint/parser"), | ||
parserOptions: {} | ||
}) | ||
|
||
const valids = ["observer(function Name() {})", "observer(class Name {})"] | ||
|
||
const invalidsNotFixed = ["observer(() => {})", "observer(function () {})", "observer(class {})"] | ||
|
||
const invalidsFixed = [ | ||
["const Cmp = observer(() => {})", "const Cmp = observer(function Cmp() {})"], | ||
['const Cmp = observer(() => "")', 'const Cmp = observer(function Cmp() { return "" })'], | ||
[ | ||
"const Cmp = observer(() => expr())", | ||
"const Cmp = observer(function Cmp() { return expr() })" | ||
], | ||
[ | ||
"const Cmp = observer(() => literal)", | ||
"const Cmp = observer(function Cmp() { return literal })" | ||
], | ||
["const Cmp = observer(function () {})", "const Cmp = observer(function Cmp () {})"], | ||
["const Cmp = observer(class {})", "const Cmp = observer(class Cmp {})"] | ||
] | ||
|
||
tester.run("no-anonymous-observer", rule, { | ||
valid: valids.map(code => ({ code })), | ||
invalid: [ | ||
...invalidsNotFixed.map(code => ({ | ||
code, | ||
errors: [{ messageId: "observerComponentMustHaveName" }] | ||
})), | ||
...invalidsFixed.map(([code, output]) => ({ | ||
code, | ||
output, | ||
errors: [{ messageId: "observerComponentMustHaveName" }] | ||
})) | ||
] | ||
}) |