summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/tests/no-arbitrary-setTimeout.js
blob: ea194af6e39ffe697f465f2856427c262c6247d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

var rule = require("../lib/rules/no-arbitrary-setTimeout");
var RuleTester = require("eslint").RuleTester;

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: "latest" } });

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

function wrapCode(code, filename = "xpcshell/test_foo.js") {
  return { code, filename };
}

function invalidCode(code) {
  let obj = wrapCode(code);
  obj.errors = [{ messageId: "listenForEvents", type: "CallExpression" }];
  return obj;
}

ruleTester.run("no-arbitrary-setTimeout", rule, {
  valid: [
    wrapCode("setTimeout(function() {}, 0);"),
    wrapCode("setTimeout(function() {});"),
    wrapCode("setTimeout(function() {}, 10);", "test_foo.js"),
  ],
  invalid: [
    invalidCode("setTimeout(function() {}, 10);"),
    invalidCode("setTimeout(function() {}, timeout);"),
  ],
});