Browse Source

fix: add tests for parseMeta

Gerald 8 years ago
parent
commit
05796ea125
3 changed files with 62 additions and 11 deletions
  1. 46 10
      test/background/script.js
  2. 11 0
      test/common/index.js
  3. 5 1
      test/polyfill.js

+ 46 - 10
test/background/script.js

@@ -1,14 +1,5 @@
 import test from 'tape';
-import { isRemote, compareVersion } from 'src/background/utils/script';
-
-test('isRemote', t => {
-  t.notOk(isRemote());
-  t.notOk(isRemote('file:///tmp/file'));
-  t.notOk(isRemote('data:text/plain,hello,world'));
-  t.ok(isRemote('http://www.google.com'));
-  t.ok(isRemote('https://www.google.com'));
-  t.end();
-});
+import { compareVersion, parseMeta } from 'src/background/utils/script';
 
 test('compareVersion', t => {
   t.equal(compareVersion('1.2.3', '1.2.3'), 0);
@@ -20,3 +11,48 @@ test('compareVersion', t => {
   t.equal(compareVersion('1.10', '1.9'), 1);
   t.end();
 });
+
+const baseMeta = {
+  include: [],
+  exclude: [],
+  match: [],
+  excludeMatch: [],
+  require: [],
+  grant: [],
+  resources: {},
+  noframes: false,
+};
+
+test('parseMeta', t => {
+  t.deepEqual(parseMeta(`\
+// ==UserScript==
+// @name New Script
+// @namespace Violentmonkey Scripts
+// @description This is a script
+// @version 1.0
+// @match *://*/*
+// @grant none
+// ==/UserScript==
+`), Object.assign({}, baseMeta, {
+    name: 'New Script',
+    namespace: 'Violentmonkey Scripts',
+    description: 'This is a script',
+    version: '1.0',
+    match: ['*://*/*'],
+    grant: ['none'],
+  }));
+  t.deepEqual(parseMeta(`\
+// ==UserScript==
+// @name New Script
+// @namespace Violentmonkey Scripts
+// @match *://*/*
+// @noframes
+// ==/UserScript==
+`), Object.assign({}, baseMeta, {
+    name: 'New Script',
+    namespace: 'Violentmonkey Scripts',
+    match: ['*://*/*'],
+    noframes: true,
+  }));
+  t.end();
+});

+ 11 - 0
test/common/index.js

@@ -0,0 +1,11 @@
+import test from 'tape';
+import { isRemote } from 'src/common';
+
+test('isRemote', t => {
+  t.notOk(isRemote());
+  t.notOk(isRemote('file:///tmp/file'));
+  t.notOk(isRemote('data:text/plain,hello,world'));
+  t.ok(isRemote('http://www.google.com'));
+  t.ok(isRemote('https://www.google.com'));
+  t.end();
+});

+ 5 - 1
test/polyfill.js

@@ -1,8 +1,12 @@
+import tldRules from 'tldjs/rules.json';
+
 global.browser = {
   storage: {
     local: {
       get() {
-        return Promise.resolve({});
+        return Promise.resolve({
+          'dat:tldRules': tldRules,
+        });
       },
       set() {
         return Promise.resolve();