|
| 1 | +import { RuleTester } from "@typescript-eslint/rule-tester" |
| 2 | +import { afterAll, describe, it } from "vitest" |
| 3 | + |
| 4 | +import { consistentPluralFormat } from "./consistent-plural-format.js" |
| 5 | + |
| 6 | +RuleTester.afterAll = afterAll |
| 7 | +RuleTester.describe = describe |
| 8 | +RuleTester.it = it |
| 9 | + |
| 10 | +const ruleTester = new RuleTester({ |
| 11 | + languageOptions: { |
| 12 | + parserOptions: { |
| 13 | + ecmaVersion: 2022, |
| 14 | + sourceType: "module" |
| 15 | + } |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +ruleTester.run("consistent-plural-format", consistentPluralFormat, { |
| 20 | + valid: [ |
| 21 | + // All required keys present (default: one, other) |
| 22 | + "plural(count, { one: '# item', other: '# items' })", |
| 23 | + "plural(count, { one: 'One', other: 'Many', zero: 'None' })", |
| 24 | + |
| 25 | + // With i18n prefix |
| 26 | + "i18n.plural(count, { one: '# item', other: '# items' })", |
| 27 | + |
| 28 | + // Custom required keys |
| 29 | + { |
| 30 | + code: "plural(count, { other: 'items' })", |
| 31 | + options: [{ requiredKeys: ["other"] }] |
| 32 | + }, |
| 33 | + { |
| 34 | + code: "plural(count, { one: '#', other: '#', zero: 'none' })", |
| 35 | + options: [{ requiredKeys: ["one", "other", "zero"] }] |
| 36 | + }, |
| 37 | + |
| 38 | + // Non-plural calls should be ignored |
| 39 | + "select(value, { male: 'He', female: 'She', other: 'They' })", |
| 40 | + "someOtherFunction({ one: 'x' })", |
| 41 | + |
| 42 | + // No object argument (edge case) |
| 43 | + "plural(count)" |
| 44 | + ], |
| 45 | + invalid: [ |
| 46 | + // Missing 'other' (default required) |
| 47 | + { |
| 48 | + code: "plural(count, { one: '# item' })", |
| 49 | + errors: [{ messageId: "missingPluralKey", data: { key: "other" } }] |
| 50 | + }, |
| 51 | + |
| 52 | + // Missing 'one' (default required) |
| 53 | + { |
| 54 | + code: "plural(count, { other: '# items' })", |
| 55 | + errors: [{ messageId: "missingPluralKey", data: { key: "one" } }] |
| 56 | + }, |
| 57 | + |
| 58 | + // Missing both default required keys |
| 59 | + { |
| 60 | + code: "plural(count, { zero: 'None' })", |
| 61 | + errors: [ |
| 62 | + { messageId: "missingPluralKey", data: { key: "one" } }, |
| 63 | + { messageId: "missingPluralKey", data: { key: "other" } } |
| 64 | + ] |
| 65 | + }, |
| 66 | + |
| 67 | + // With i18n prefix |
| 68 | + { |
| 69 | + code: "i18n.plural(count, { one: '# item' })", |
| 70 | + errors: [{ messageId: "missingPluralKey", data: { key: "other" } }] |
| 71 | + }, |
| 72 | + |
| 73 | + // Custom required keys missing |
| 74 | + { |
| 75 | + code: "plural(count, { one: '#', other: '#' })", |
| 76 | + options: [{ requiredKeys: ["one", "other", "zero"] }], |
| 77 | + errors: [{ messageId: "missingPluralKey", data: { key: "zero" } }] |
| 78 | + }, |
| 79 | + |
| 80 | + // Empty object |
| 81 | + { |
| 82 | + code: "plural(count, {})", |
| 83 | + errors: [ |
| 84 | + { messageId: "missingPluralKey", data: { key: "one" } }, |
| 85 | + { messageId: "missingPluralKey", data: { key: "other" } } |
| 86 | + ] |
| 87 | + } |
| 88 | + ] |
| 89 | +}) |
| 90 | + |
0 commit comments