-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(state-names): add test for state-names rule
add a new rule "state-names" for linting according to given options, add a test, update docs fix #3
- Loading branch information
Showing
2 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
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
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,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) |