Skip to content

Commit

Permalink
test(state-names): add test for state-names rule
Browse files Browse the repository at this point in the history
add a new rule "state-names" for linting according to given options, add a test, update docs

fix #3
  • Loading branch information
rlaffers committed Apr 21, 2021
1 parent 5afeb7b commit 726d147
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docs/rules/state-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ createMachine({
})

// ❌ state names violates the given regex
/* eslint event-names: [ "warn", "regex", { "regex": "^\\w+:\\w+$" } ] */
/* eslint event-names: [ "warn", "regex", { "regex": "^\w+:\w+$" } ] */
createMachine({
states: {
PowerOn: {},
Expand All @@ -50,7 +50,7 @@ createMachine({
})

//
/* eslint event-names: [ "warn", "regex", { "regex": "^\\w+:\\w+$" } ] */
/* eslint event-names: [ "warn", "regex", { "regex": "^\w+:\w+$" } ] */
createMachine({
states: {
'power:on': {},
Expand All @@ -74,6 +74,6 @@ createMachine({
}

{
"xstate/state-names": ["warn", "regex", { "regex": "^[a-z]+$" }]
"xstate/state-names": ["warn", "regex", { "regex": "^\\w+:\\w+$" }]
}
```
147 changes: 147 additions & 0 deletions tests/lib/rules/state-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/state-names')

const tests = {
valid: [
`
createMachine({
states: {
powerOn: {},
powerOff: {},
},
})
`,
`
/* eslint state-names: [ "warn", "snakeCase" ] */
createMachine({
states: {
power_on: {},
active: {},
},
})
`,
`
/* eslint state-names: [ "warn", "regex", { "regex": "^[a-z]+:[a-z0-9]+$" } ] */
createMachine({
states: {
'power:on': {},
'mode:1': {},
},
})
`,
],

invalid: [
{
code: `
createMachine({
states: {
PowerOn: {},
power_on: {},
'power:on': {},
'power.on': {},
},
})
`,
errors: [
{
messageId: 'invalidStateName',
data: { name: 'PowerOn', fixedName: 'powerOn' },
},
{
messageId: 'invalidStateName',
data: { name: 'power_on', fixedName: 'powerOn' },
},
{
messageId: 'invalidStateName',
data: { name: 'power:on', fixedName: 'powerOn' },
},
{
messageId: 'invalidStateName',
data: { name: 'power.on', fixedName: 'powerOn' },
},
],
output: `
createMachine({
states: {
powerOn: {},
powerOn: {},
powerOn: {},
powerOn: {},
},
})
`,
},
// snake_case
{
code: `
/* eslint state-names: [ "warn", "snakeCase" ] */
createMachine({
states: {
PowerOn: {},
POWER___ON: {},
'power:on': {},
'power.on': {},
},
})
`,
errors: [
{
messageId: 'invalidStateName',
data: { name: 'PowerOn', fixedName: 'power_on' },
},
{
messageId: 'invalidStateName',
data: { name: 'POWER___ON', fixedName: 'power_on' },
},
{
messageId: 'invalidStateName',
data: { name: 'power:on', fixedName: 'power_on' },
},
{
messageId: 'invalidStateName',
data: { name: 'power.on', fixedName: 'power_on' },
},
],
output: `
/* eslint state-names: [ "warn", "snakeCase" ] */
createMachine({
states: {
power_on: {},
power_on: {},
power_on: {},
power_on: {},
},
})
`,
},
{
code: `
/* eslint state-names: [ "warn", "regex", { "regex": "^[a-z]+:[a-z0-9]+$" } ] */
createMachine({
states: {
PowerOn: {},
power_on: {},
},
})
`,
errors: [
{
messageId: 'stateNameViolatesRegex',
data: { name: 'PowerOn', regex: '^[a-z]+:[a-z0-9]+$' },
},
{
messageId: 'stateNameViolatesRegex',
data: { name: 'power_on', regex: '^[a-z]+:[a-z0-9]+$' },
},
],
},
],
}

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
},
})
ruleTester.run('state-names', rule, tests)

0 comments on commit 726d147

Please sign in to comment.