Skip to content

Commit

Permalink
Update toggle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit88 committed Mar 28, 2024
1 parent b829614 commit 268ca10
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
3 changes: 2 additions & 1 deletion packages/eslint-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
dist
dist
*.log
7 changes: 3 additions & 4 deletions packages/eslint-plugin/lib/rules/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Detects stale code from expired Opticks experiments',
description:
'Detects stale code from expired Opticks experiments, and other common mistakes',
recommended: false,
url: null // URL to the documentation page for this rule
},
Expand Down Expand Up @@ -101,12 +102,10 @@ module.exports = {
messageId: 'ExperimentConcluded',
data: {winningVariant},
node,
suggests: [
suggest: [
{
messageId: 'AddWinningVariant',
fix: (fixer) => {
// TODO: Add tests
console.log(winningVariantContent)
return fixer.replaceText(node, winningVariantContent)
}
}
Expand Down
65 changes: 37 additions & 28 deletions packages/eslint-plugin/tests/lib/rules/toggle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* @fileoverview Opticks
* @author Jop
* @author Gerrit
* @warning These test do not work with the isToggleImportedFromOpticks logic in the rules.
* To run the test, set isToggleImportedFromOpticks to "true" in the rule.
*/
'use strict'

Expand All @@ -17,73 +19,80 @@ const rule = require('../../../lib/rules/toggle'),

RuleTester.setDefaultConfig({
settings: {
opticks: {experiments: {foo: 'a', bar: undefined, baz: 'b'}}
opticks: {experiments: {foo: 'a', bar: null, baz: 'b'}}
}
})
const ruleTester = new RuleTester()

ruleTester.run('toggle', rule, {
valid: [
{code: "toggle('bar', 'a', 'b')"},
{code: "toggle('nonexistent', 'a', 'b')"}
],
valid: [{code: "toggle('bar', 'a', 'b')"}],
invalid: [
{
code: "toggle('foo', 'a', 'b')",
code: "toggle('bar', 'a')",
errors: [
{
message:
'Looks like this experiment concluded, and can be cleaned. The winning variant is a.',
type: 'CallExpression'
messageId: 'InvalidNrOfVariants',
type: 'CallExpression',
suggestions: [
{
messageId: 'AddNullBVariant',
output: "toggle('bar', 'a', null)"
}
]
}
],
output: "'a'"
output: null
},
{
code: "toggle('baz', 'a', 'b')",
code: "toggle('nonexistent', 'a', 'b')",
errors: [
{
messageId: 'ExperimentConcluded',
messageId: 'ExperimentNotConfigured',
type: 'CallExpression'
}
],
output: "'b'"
output: null
},
{
// TODO: make tests work with const too
code: "var intermediateVariable = toggle('bar', 'a', 'b')",
code: "toggle('foo', 'a', 'b')",
errors: [
{
messageId: 'VariableAssignment',
type: 'CallExpression'
message:
'Looks like this experiment concluded, and can be cleaned up. The winning variant is a.',
type: 'CallExpression',
suggestions: [
{
messageId: 'AddWinningVariant',
output: "'a'"
}
]
}
],
output: null
},
{
code: "toggle('bar', 'a')",
code: "toggle('baz', 'a', 'b')",
errors: [
{
messageId: 'InvalidNrOfVariants',
messageId: 'ExperimentConcluded',
type: 'CallExpression',
suggestions: [
{
messageId: 'AddNullBVariant',
output: "toggle('bar', 'a', null)"
messageId: 'AddWinningVariant',
output: "'b'"
}
]
}
],
output: null
},
{
code: `var Foo = styled('div')\`
display: flex;
\${toggle("foo", "a", "b")}
\`
`,
code: "var intermediateVariable = toggle('bar', 'a', 'b')",
errors: [
'This toggle is not called from a function, this might not be what you want to do because it might execute before Opticks received the user id. Is this intended?'
{
messageId: 'VariableAssignment',
type: 'CallExpression'
}
],
output: null
}
Expand Down

0 comments on commit 268ca10

Please sign in to comment.