|
| 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-keep-awake"); |
| 26 | +const RuleTester = require("eslint").RuleTester; |
| 27 | + |
| 28 | +//------------------------------------------------------------------------------ |
| 29 | +// Tests |
| 30 | +//------------------------------------------------------------------------------ |
| 31 | + |
| 32 | +const ruleTester = new RuleTester({ |
| 33 | + parserOptions: { |
| 34 | + ecmaVersion: 2022, |
| 35 | + sourceType: "module", |
| 36 | + ecmaFeatures: { |
| 37 | + jsx: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +const expectedErrorHook = { |
| 43 | + messageId: "AvoidKeepAwake", |
| 44 | + type: "CallExpression", |
| 45 | +}; |
| 46 | + |
| 47 | +const expectedErrorFunction = { |
| 48 | + messageId: "AvoidKeepAwake", |
| 49 | + type: "CallExpression", |
| 50 | +}; |
| 51 | + |
| 52 | +ruleTester.run("avoid-keep-awake", rule, { |
| 53 | + valid: [ |
| 54 | + { |
| 55 | + code: ` |
| 56 | + import React from 'react'; |
| 57 | + import { Text, View } from 'react-native'; |
| 58 | + |
| 59 | + export default function ValidExample() { |
| 60 | + return ( |
| 61 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 62 | + <Text>This screen will sleep!</Text> |
| 63 | + </View> |
| 64 | + ); |
| 65 | + } |
| 66 | + `, |
| 67 | + }, |
| 68 | + { |
| 69 | + code: ` |
| 70 | + import React from 'react'; |
| 71 | + import { useKeepAwake } from 'other-library'; |
| 72 | + import { Button, View } from 'react-native'; |
| 73 | + |
| 74 | + export default class ValidExample extends React.Component { |
| 75 | + render() { |
| 76 | + useKeepAwake(); |
| 77 | + return ( |
| 78 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 79 | + </View> |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + `, |
| 84 | + }, |
| 85 | + ], |
| 86 | + invalid: [ |
| 87 | + { |
| 88 | + code: ` |
| 89 | + import { useKeepAwake } from 'expo-keep-awake'; |
| 90 | + import React from 'react'; |
| 91 | + import { Text, View } from 'react-native'; |
| 92 | + |
| 93 | + export default function KeepAwakeExample() { |
| 94 | + useKeepAwake(); |
| 95 | + return ( |
| 96 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 97 | + <Text>This screen will never sleep!</Text> |
| 98 | + </View> |
| 99 | + ); |
| 100 | + } |
| 101 | + `, |
| 102 | + errors: [expectedErrorHook], |
| 103 | + }, |
| 104 | + { |
| 105 | + code: ` |
| 106 | + import { activateKeepAwake } from 'expo-keep-awake'; |
| 107 | + import React from 'react'; |
| 108 | + import { Button, View } from 'react-native'; |
| 109 | +
|
| 110 | + export default class KeepAwakeExample extends React.Component { |
| 111 | + render() { |
| 112 | + return ( |
| 113 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 114 | + <Button onPress={this._activate} title="Activate" /> |
| 115 | + </View> |
| 116 | + ); |
| 117 | + } |
| 118 | +
|
| 119 | + _activate = () => { |
| 120 | + activateKeepAwake(); |
| 121 | + alert('Activated!'); |
| 122 | + }; |
| 123 | + }`, |
| 124 | + errors: [expectedErrorFunction], |
| 125 | + }, |
| 126 | + ], |
| 127 | +}); |
0 commit comments