|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | +// Requirements |
| 5 | +//------------------------------------------------------------------------------ |
| 6 | + |
| 7 | +const rule = require('../../../lib/rules/require-async-inverse-relationship'); |
| 8 | +const RuleTester = require('eslint').RuleTester; |
| 9 | + |
| 10 | +const parserOptions = { ecmaVersion: 2022, sourceType: 'module' }; |
| 11 | + |
| 12 | +const ruleTester = new RuleTester({ |
| 13 | + parserOptions, |
| 14 | + parser: require.resolve('@babel/eslint-parser'), |
| 15 | +}); |
| 16 | + |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | +// Tests |
| 19 | +//------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +ruleTester.run('require-async-inverse-relationship', rule, { |
| 22 | + valid: [ |
| 23 | + `import Model, { belongsTo } from '@ember-data/model'; |
| 24 | +
|
| 25 | + export default class extends Model { |
| 26 | + @belongsTo('post', { async: true, inverse: 'comments' }) post; |
| 27 | + }`, |
| 28 | + `import Model, { hasMany } from '@ember-data/model'; |
| 29 | +
|
| 30 | + export default class extends Model { |
| 31 | + @hasMany('comment', { async: true, inverse: 'post' }) comments; |
| 32 | + }`, |
| 33 | + `import Model, { belongsTo, hasMany } from '@ember-data/model'; |
| 34 | +
|
| 35 | + export default class extends Model { |
| 36 | + @belongsTo('post', { async: false, inverse: 'comments' }) post; |
| 37 | + @hasMany('user', { async: true, inverse: null }) owner; |
| 38 | + }`, |
| 39 | + ], |
| 40 | + |
| 41 | + invalid: [ |
| 42 | + { |
| 43 | + code: `import Model, { belongsTo } from '@ember-data/model'; |
| 44 | +
|
| 45 | + export default class extends Model { |
| 46 | + @belongsTo('post') post; |
| 47 | + }`, |
| 48 | + output: null, |
| 49 | + errors: [ |
| 50 | + { |
| 51 | + message: 'The @belongsTo decorator requires an `async` property to be specified.', |
| 52 | + type: 'CallExpression', |
| 53 | + }, |
| 54 | + { |
| 55 | + message: 'The @belongsTo decorator requires an `inverse` property to be specified.', |
| 56 | + type: 'CallExpression', |
| 57 | + }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + { |
| 61 | + code: `import Model, { belongsTo } from '@ember-data/model'; |
| 62 | +
|
| 63 | + export default class extends Model { |
| 64 | + @belongsTo('post', {}) post; |
| 65 | + }`, |
| 66 | + output: null, |
| 67 | + errors: [ |
| 68 | + { |
| 69 | + message: 'The @belongsTo decorator requires an `async` property to be specified.', |
| 70 | + type: 'CallExpression', |
| 71 | + }, |
| 72 | + { |
| 73 | + message: 'The @belongsTo decorator requires an `inverse` property to be specified.', |
| 74 | + type: 'CallExpression', |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + { |
| 79 | + code: `import Model, { belongsTo } from '@ember-data/model'; |
| 80 | +
|
| 81 | + export default class extends Model { |
| 82 | + @belongsTo('post', { async: 'comments'}) post; |
| 83 | + }`, |
| 84 | + output: null, |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + message: |
| 88 | + 'The @belongsTo decorator requires an `async` property to be specified as a boolean.', |
| 89 | + type: 'CallExpression', |
| 90 | + }, |
| 91 | + { |
| 92 | + message: 'The @belongsTo decorator requires an `inverse` property to be specified.', |
| 93 | + type: 'CallExpression', |
| 94 | + }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + { |
| 98 | + code: `import Model, { belongsTo } from '@ember-data/model'; |
| 99 | +
|
| 100 | + export default class extends Model { |
| 101 | + @belongsTo('post', { async: true }) post; |
| 102 | + }`, |
| 103 | + output: null, |
| 104 | + errors: [ |
| 105 | + { |
| 106 | + message: 'The @belongsTo decorator requires an `inverse` property to be specified.', |
| 107 | + type: 'CallExpression', |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + { |
| 112 | + code: `import Model, { belongsTo } from '@ember-data/model'; |
| 113 | +
|
| 114 | + export default class extends Model { |
| 115 | + @belongsTo('post', { inverse: 'comments' }) post; |
| 116 | + }`, |
| 117 | + output: null, |
| 118 | + errors: [ |
| 119 | + { |
| 120 | + message: 'The @belongsTo decorator requires an `async` property to be specified.', |
| 121 | + type: 'CallExpression', |
| 122 | + }, |
| 123 | + ], |
| 124 | + }, |
| 125 | + { |
| 126 | + code: `import Model, { hasMany } from '@ember-data/model'; |
| 127 | +
|
| 128 | + export default class extends Model { |
| 129 | + @hasMany('comment') comments; |
| 130 | + }`, |
| 131 | + output: null, |
| 132 | + errors: [ |
| 133 | + { |
| 134 | + message: 'The @hasMany decorator requires an `async` property to be specified.', |
| 135 | + type: 'CallExpression', |
| 136 | + }, |
| 137 | + { |
| 138 | + message: 'The @hasMany decorator requires an `inverse` property to be specified.', |
| 139 | + type: 'CallExpression', |
| 140 | + }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + { |
| 144 | + code: `import Model, { hasMany } from '@ember-data/model'; |
| 145 | +
|
| 146 | + export default class extends Model { |
| 147 | + @hasMany('comment', {}) comments; |
| 148 | + }`, |
| 149 | + output: null, |
| 150 | + errors: [ |
| 151 | + { |
| 152 | + message: 'The @hasMany decorator requires an `async` property to be specified.', |
| 153 | + type: 'CallExpression', |
| 154 | + }, |
| 155 | + { |
| 156 | + message: 'The @hasMany decorator requires an `inverse` property to be specified.', |
| 157 | + type: 'CallExpression', |
| 158 | + }, |
| 159 | + ], |
| 160 | + }, |
| 161 | + { |
| 162 | + code: `import Model, { hasMany } from '@ember-data/model'; |
| 163 | +
|
| 164 | + export default class extends Model { |
| 165 | + @hasMany('comment', { async: 'comments'}) comments; |
| 166 | + }`, |
| 167 | + output: null, |
| 168 | + errors: [ |
| 169 | + { |
| 170 | + message: |
| 171 | + 'The @hasMany decorator requires an `async` property to be specified as a boolean.', |
| 172 | + type: 'CallExpression', |
| 173 | + }, |
| 174 | + { |
| 175 | + message: 'The @hasMany decorator requires an `inverse` property to be specified.', |
| 176 | + type: 'CallExpression', |
| 177 | + }, |
| 178 | + ], |
| 179 | + }, |
| 180 | + { |
| 181 | + code: `import Model, { hasMany } from '@ember-data/model'; |
| 182 | +
|
| 183 | + export default class extends Model { |
| 184 | + @hasMany('comment', { async: true }) comments; |
| 185 | + }`, |
| 186 | + output: null, |
| 187 | + errors: [ |
| 188 | + { |
| 189 | + message: 'The @hasMany decorator requires an `inverse` property to be specified.', |
| 190 | + type: 'CallExpression', |
| 191 | + }, |
| 192 | + ], |
| 193 | + }, |
| 194 | + { |
| 195 | + code: `import Model, { hasMany } from '@ember-data/model'; |
| 196 | +
|
| 197 | + export default class extends Model { |
| 198 | + @hasMany('comment', { inverse: 'post' }) comments; |
| 199 | + }`, |
| 200 | + output: null, |
| 201 | + errors: [ |
| 202 | + { |
| 203 | + message: 'The @hasMany decorator requires an `async` property to be specified.', |
| 204 | + type: 'CallExpression', |
| 205 | + }, |
| 206 | + ], |
| 207 | + }, |
| 208 | + ], |
| 209 | +}); |
0 commit comments