|
| 1 | +const RuleTester = require('eslint').RuleTester; |
| 2 | +const rule = require('../../../lib/rules/no-restricted-service-injections'); |
| 3 | + |
| 4 | +const { DEFAULT_ERROR_MESSAGE } = rule; |
| 5 | + |
| 6 | +const ruleTester = new RuleTester({ |
| 7 | + parser: require.resolve('babel-eslint'), |
| 8 | + parserOptions: { |
| 9 | + ecmaVersion: 2015, |
| 10 | + sourceType: 'module', |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +ruleTester.run('no-restricted-service-injections', rule, { |
| 15 | + valid: [ |
| 16 | + { |
| 17 | + // Service name doesn't match (with property name): |
| 18 | + code: 'Component.extend({ myService: service() })', |
| 19 | + options: [{ paths: ['app/components'], services: ['abc'] }], |
| 20 | + filename: 'app/components/path.js', |
| 21 | + }, |
| 22 | + { |
| 23 | + // Service name doesn't match (with string argument): |
| 24 | + code: "Component.extend({ randomName: service('myService') })", |
| 25 | + options: [{ paths: ['app/components'], services: ['abc'] }], |
| 26 | + filename: 'app/components/path.js', |
| 27 | + }, |
| 28 | + { |
| 29 | + // Service name doesn't match (with decorator) |
| 30 | + code: "class MyComponent extends Component { @service('myService') randomName }", |
| 31 | + options: [{ paths: ['app/components'], services: ['abc'] }], |
| 32 | + filename: 'app/components/path.js', |
| 33 | + }, |
| 34 | + { |
| 35 | + // Service scope doesn't match: |
| 36 | + code: "Component.extend({ randomName: service('scope/myService') })", |
| 37 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 38 | + filename: 'app/components/path.js', |
| 39 | + }, |
| 40 | + { |
| 41 | + // File path doesn't match: |
| 42 | + code: 'Component.extend({ myService: service() })', |
| 43 | + options: [{ paths: ['other/path'], services: ['my-service'] }], |
| 44 | + filename: 'app/components/path.js', |
| 45 | + }, |
| 46 | + { |
| 47 | + // Not the service decorator: |
| 48 | + code: 'Component.extend({ myService: otherDecorator() })', |
| 49 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 50 | + filename: 'app/components/path.js', |
| 51 | + }, |
| 52 | + { |
| 53 | + // Ignores injection due to dynamic variable usage: |
| 54 | + code: 'Component.extend({ myService: service(SOME_VARIABLE) })', |
| 55 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 56 | + filename: 'app/components/path.js', |
| 57 | + }, |
| 58 | + ], |
| 59 | + invalid: [ |
| 60 | + { |
| 61 | + // Without service name argument: |
| 62 | + code: 'Component.extend({ myService: service() })', |
| 63 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 64 | + output: null, |
| 65 | + filename: 'app/components/path.js', |
| 66 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'Property' }], |
| 67 | + }, |
| 68 | + { |
| 69 | + // With camelized service name argument: |
| 70 | + code: "Component.extend({ randomName: service('myService') })", |
| 71 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 72 | + output: null, |
| 73 | + filename: 'app/components/path.js', |
| 74 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'Property' }], |
| 75 | + }, |
| 76 | + { |
| 77 | + // With dasherized service name argument: |
| 78 | + code: "Component.extend({ randomName: service('my-service') })", |
| 79 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 80 | + output: null, |
| 81 | + filename: 'app/components/path.js', |
| 82 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'Property' }], |
| 83 | + }, |
| 84 | + { |
| 85 | + // With nested, camelized service name: |
| 86 | + code: "Component.extend({ randomName: service('scope/myService') })", |
| 87 | + options: [{ paths: ['app/components'], services: ['scope/my-service'] }], |
| 88 | + output: null, |
| 89 | + filename: 'app/components/path.js', |
| 90 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'Property' }], |
| 91 | + }, |
| 92 | + { |
| 93 | + // With nested, dasherized service name: |
| 94 | + code: "Component.extend({ randomName: service('scope/my-service') })", |
| 95 | + options: [{ paths: ['app/components'], services: ['scope/my-service'] }], |
| 96 | + output: null, |
| 97 | + filename: 'app/components/path.js', |
| 98 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'Property' }], |
| 99 | + }, |
| 100 | + { |
| 101 | + // With decorator with camelized service name argument: |
| 102 | + code: "class MyComponent extends Component { @service('myService') randomName }", |
| 103 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 104 | + output: null, |
| 105 | + filename: 'app/components/path.js', |
| 106 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'ClassProperty' }], |
| 107 | + }, |
| 108 | + { |
| 109 | + // With decorator with dasherized service name argument: |
| 110 | + code: "class MyComponent extends Component { @service('my-service') randomName }", |
| 111 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 112 | + output: null, |
| 113 | + filename: 'app/components/path.js', |
| 114 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'ClassProperty' }], |
| 115 | + }, |
| 116 | + { |
| 117 | + // With decorator without service name argument (without parentheses): |
| 118 | + code: 'class MyComponent extends Component { @service myService }', |
| 119 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 120 | + output: null, |
| 121 | + filename: 'app/components/path.js', |
| 122 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'ClassProperty' }], |
| 123 | + }, |
| 124 | + { |
| 125 | + // With decorator without service name argument (with parentheses): |
| 126 | + code: 'class MyComponent extends Component { @service() myService }', |
| 127 | + options: [{ paths: ['app/components'], services: ['my-service'] }], |
| 128 | + output: null, |
| 129 | + filename: 'app/components/path.js', |
| 130 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'ClassProperty' }], |
| 131 | + }, |
| 132 | + { |
| 133 | + // With custom error message: |
| 134 | + code: 'Component.extend({ myService: service() })', |
| 135 | + options: [ |
| 136 | + { |
| 137 | + paths: ['app/components'], |
| 138 | + services: ['my-service'], |
| 139 | + error: 'my-service is deprecated, please do not use it.', |
| 140 | + }, |
| 141 | + ], |
| 142 | + output: null, |
| 143 | + filename: 'app/components/path.js', |
| 144 | + errors: [{ message: 'my-service is deprecated, please do not use it.', type: 'Property' }], |
| 145 | + }, |
| 146 | + { |
| 147 | + // With multiple violations: |
| 148 | + code: 'Component.extend({ myService: service() })', |
| 149 | + options: [ |
| 150 | + { paths: ['app/components'], services: ['my-service'], error: 'Error 1' }, |
| 151 | + { paths: ['app/components'], services: ['my-service'], error: 'Error 2' }, |
| 152 | + ], |
| 153 | + output: null, |
| 154 | + filename: 'app/components/path.js', |
| 155 | + errors: [ |
| 156 | + { message: 'Error 1', type: 'Property' }, |
| 157 | + { message: 'Error 2', type: 'Property' }, |
| 158 | + ], |
| 159 | + }, |
| 160 | + { |
| 161 | + // Without specifying any paths (should match any path): |
| 162 | + code: 'Component.extend({ myService: service() })', |
| 163 | + options: [{ services: ['my-service'] }], |
| 164 | + output: null, |
| 165 | + filename: 'app/components/path.js', |
| 166 | + errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'Property' }], |
| 167 | + }, |
| 168 | + ], |
| 169 | +}); |
0 commit comments