Skip to content

Commit

Permalink
chore: Add eslint plugin tests (#3905)
Browse files Browse the repository at this point in the history
* add tests for eslint rule missing-observer

* add tests for eslint rule no-anonymous-observer
  • Loading branch information
dartess authored Oct 15, 2024
1 parent 51354c2 commit 70762a0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 30 deletions.
15 changes: 0 additions & 15 deletions packages/eslint-plugin-mobx/__tests__/TODO-missing-observer.js

This file was deleted.

This file was deleted.

57 changes: 57 additions & 0 deletions packages/eslint-plugin-mobx/__tests__/missing-observer.js
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 packages/eslint-plugin-mobx/__tests__/no-anonymous-observer.js
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" }]
}))
]
})

0 comments on commit 70762a0

Please sign in to comment.