|
| 1 | +/* |
| 2 | + * creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs |
| 3 | + * Copyright © 2023 Green Code Initiative (https://green-code-initiative.org) |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +"use strict"; |
| 20 | + |
| 21 | +//------------------------------------------------------------------------------ |
| 22 | +// Requirements |
| 23 | +//------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +const rule = require("../../../lib/rules/avoid-autoplay"); |
| 26 | +const RuleTester = require("eslint").RuleTester; |
| 27 | + |
| 28 | +//------------------------------------------------------------------------------ |
| 29 | +// Tests |
| 30 | +//------------------------------------------------------------------------------ |
| 31 | + |
| 32 | +const ruleTester = new RuleTester({ |
| 33 | + parserOptions: { |
| 34 | + ecmaVersion: 2021, |
| 35 | + sourceType: "module", |
| 36 | + ecmaFeatures: { |
| 37 | + jsx: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +const noAutoplayError = { |
| 43 | + messageId: "NoAutoplay", |
| 44 | + type: "JSXAttribute", |
| 45 | +}; |
| 46 | +const enforcePreloadNoneError = { |
| 47 | + messageId: "EnforcePreloadNone", |
| 48 | + type: "JSXAttribute", |
| 49 | +}; |
| 50 | +const BothError = { |
| 51 | + messageId: "NoAutoplayAndEnforcePreloadNone", |
| 52 | + type: "JSXAttribute", |
| 53 | +}; |
| 54 | + |
| 55 | +ruleTester.run("autoplay-audio-video-attribute-not-present", rule, { |
| 56 | + valid: [ |
| 57 | + '<audio preload="none"></audio>', |
| 58 | + '<video preload="none"></video>', |
| 59 | + '<video preload="none" {...props}></video>', |
| 60 | + ], |
| 61 | + invalid: [ |
| 62 | + { |
| 63 | + code: "<audio autoplay></audio>", |
| 64 | + errors: [BothError], |
| 65 | + }, |
| 66 | + { |
| 67 | + code: "<audio autoPlay></audio>", |
| 68 | + errors: [BothError], |
| 69 | + }, |
| 70 | + { |
| 71 | + code: "<audio autoPlay={true}></audio>", |
| 72 | + errors: [BothError], |
| 73 | + }, |
| 74 | + { |
| 75 | + code: '<video autoplay preload="auto"></video>', |
| 76 | + errors: [BothError], |
| 77 | + }, |
| 78 | + { |
| 79 | + code: '<video autoplay preload="none"></video>', |
| 80 | + errors: [noAutoplayError], |
| 81 | + }, |
| 82 | + { |
| 83 | + code: '<audio preload="auto"></audio>', |
| 84 | + errors: [enforcePreloadNoneError], |
| 85 | + }, |
| 86 | + ], |
| 87 | +}); |
0 commit comments