This is an automated email from the ASF dual-hosted git repository.

erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-electron.git


The following commit(s) were added to refs/heads/master by this push:
     new 0cc78c3  Add tests to Api.spec.js (#22)
0cc78c3 is described below

commit 0cc78c37f61d08f5e53a431aa3677c75c9c705b3
Author: Ken Naito <[email protected]>
AuthorDate: Wed Feb 20 18:13:17 2019 +0900

    Add tests to Api.spec.js (#22)
---
 bin/templates/cordova/Api.js                       |   2 +-
 bin/templates/cordova/handler.js                   |   1 +
 tests/spec/fixtures/testapp/config.xml             |  40 ++
 tests/spec/fixtures/testapp/cordova/.gitkeep       |   0
 .../fixtures/testplugin/src/electron/sample.json   |   3 +
 tests/spec/fixtures/testplugin/www/plugin.js       |  26 ++
 tests/spec/unit/Api.spec.js                        | 423 ++++++++++++++++++++-
 tests/spec/unit/handler.spec.js                    |   7 +
 8 files changed, 482 insertions(+), 20 deletions(-)

diff --git a/bin/templates/cordova/Api.js b/bin/templates/cordova/Api.js
index 3260259..c21409f 100644
--- a/bin/templates/cordova/Api.js
+++ b/bin/templates/cordova/Api.js
@@ -54,7 +54,7 @@ class Api {
         this.platform = 'electron';
 
         // MyApp/platforms/electron
-        this.root = path.resolve(__dirname, '..');
+        this.root = platformRootDir || path.resolve(__dirname, '..');
         this.events = setupEvents(events);
         this.parser = new Parser(this.root);
         this.handler = require('./handler');
diff --git a/bin/templates/cordova/handler.js b/bin/templates/cordova/handler.js
index d99a4b8..7a382df 100644
--- a/bin/templates/cordova/handler.js
+++ b/bin/templates/cordova/handler.js
@@ -67,6 +67,7 @@ module.exports = {
             fs.writeFileSync(moduleDestination, scriptContent, 'utf-8');
         },
         uninstall: (jsModule, www_dir, plugin_id) => {
+            fs.removeSync(path.join(www_dir, 'plugins', plugin_id));
             const pluginRelativePath = path.join('plugins', plugin_id, 
jsModule.src);
             // common.removeFileAndParents(www_dir, pluginRelativePath);
             events.emit('verbose', `js-module uninstall called : 
${pluginRelativePath}`);
diff --git a/tests/spec/fixtures/testapp/config.xml 
b/tests/spec/fixtures/testapp/config.xml
new file mode 100644
index 0000000..59b4b72
--- /dev/null
+++ b/tests/spec/fixtures/testapp/config.xml
@@ -0,0 +1,40 @@
+<?xml version='1.0' encoding='utf-8'?>
+<widget id="whatever" version="1.1.1" xmlns="http://www.w3.org/ns/widgets"; 
xmlns:cdv="http://cordova.apache.org/ns/1.0";>
+    <name>HelloWorld</name>
+    <description>
+        A sample Apache Cordova application.
+    </description>
+    <author email="[email protected]" href="http://cordova.io";>
+        Cordova Team
+    </author>
+    <license>Apache 2.0 License</license>
+    <preference name="Orientation" value="portrait" />
+    <content src="index.html" main="index.js" />
+    <plugin name="cordova-plugin-whitelist" spec="1" />
+    <access origin="*" />
+    <allow-intent href="http://*/*"; />
+    <allow-intent href="https://*/*"; />
+    <allow-intent href="tel:*" />
+    <allow-intent href="sms:*" />
+    <allow-intent href="mailto:*"; />
+    <allow-intent href="geo:*" />
+    <platform name="android">
+        <allow-intent href="market:*" />
+    </platform>
+    <platform name="ios">
+        <allow-intent href="itms:*" />
+        <allow-intent href="itms-apps:*" />
+    </platform>
+    <icon src="res/electron/cordova.png"  width="16" height="16"/>
+    <platform name="electron">
+        <icon src="res/electron/cordova.png"/>
+        <icon src="res/electron/[email protected]" />
+        <icon src="res/electron/[email protected]" />
+        <icon src="res/electron/[email protected]" />
+        <icon src="res/electron/[email protected]" />
+        <icon src="res/electron/[email protected]" />
+        <icon src="res/electron/[email protected]" />
+        <icon src="res/electron/cordova_512.png" target="installer" />
+    </platform>
+    <engine name="electron" spec="github:apache/cordova-electron" />
+</widget>
diff --git a/tests/spec/fixtures/testapp/cordova/.gitkeep 
b/tests/spec/fixtures/testapp/cordova/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/tests/spec/fixtures/testplugin/src/electron/sample.json 
b/tests/spec/fixtures/testplugin/src/electron/sample.json
new file mode 100644
index 0000000..618d8dc
--- /dev/null
+++ b/tests/spec/fixtures/testplugin/src/electron/sample.json
@@ -0,0 +1,3 @@
+{
+  "title" : "sample"
+}
diff --git a/tests/spec/fixtures/testplugin/www/plugin.js 
b/tests/spec/fixtures/testplugin/www/plugin.js
new file mode 100644
index 0000000..c2eda3e
--- /dev/null
+++ b/tests/spec/fixtures/testplugin/www/plugin.js
@@ -0,0 +1,26 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+class TestPlugin {
+
+}
+
+module.exports = new TestPlugin();
diff --git a/tests/spec/unit/Api.spec.js b/tests/spec/unit/Api.spec.js
index 06c5e91..cde68a1 100644
--- a/tests/spec/unit/Api.spec.js
+++ b/tests/spec/unit/Api.spec.js
@@ -22,25 +22,66 @@ const path = require('path');
 const rewire = require('rewire');
 const templateDir = path.resolve(__dirname, '..', '..', '..', 'bin', 
'templates');
 const Api = rewire(path.join(templateDir, 'cordova', 'Api'));
-const tmpDir = path.join(__dirname, '../temp');
+const tmpDir = path.join(__dirname, '../../../temp');
+const tmpWorkDir = path.join(tmpDir, 'work');
 const apiRequire = Api.__get__('require');
 
+const FIXTURES = path.join(__dirname, '..', 'fixtures');
+
+const pluginFixture = path.join(FIXTURES, 'testplugin');
+const testProjectDir = path.join(tmpDir, 'testapp');
+
+function copyTestProject () {
+    fs.ensureDirSync(tmpDir);
+    fs.copySync(path.resolve(FIXTURES, 'testapp'), path.resolve(tmpDir, 
'testapp'));
+}
+
+function dirExists (dir) {
+    return fs.existsSync(dir) && fs.statSync(dir).isDirectory();
+}
+
+function fileExists (file) {
+    return fs.existsSync(file) && fs.statSync(file).isFile();
+}
+
+function readJson (file) {
+    return JSON.parse(
+        fs.readFileSync(
+            file
+        )
+    );
+}
+
+function writeJson (file, json) {
+    fs.writeFileSync(
+        file,
+        JSON.stringify(json, null, '  '),
+        'utf-8'
+    );
+}
+
 describe('Api class', () => {
-    const api = new Api(null, tmpDir);
+
+    fs.removeSync(tmpDir);
+    fs.ensureDirSync(tmpWorkDir);
+    copyTestProject();
+
+    const api = new Api(null, testProjectDir);
     const apiEvents = Api.__get__('selfEvents');
     apiEvents.removeAllListeners();
 
+    const rootDir = testProjectDir;
     const mockExpectedLocations = {
-        platformRootDir: tmpDir,
-        root: templateDir,
-        www: path.join(templateDir, 'www'),
-        res: path.join(templateDir, 'res'),
-        platformWww: path.join(templateDir, 'platform_www'),
-        configXml: path.join(templateDir, 'config.xml'),
-        defaultConfigXml: path.join(templateDir, 'cordova/defaults.xml'),
-        build: path.join(templateDir, 'build'),
-        buildRes: path.join(templateDir, 'build-res'),
-        cache: path.join(templateDir, 'cache'),
+        platformRootDir: rootDir,
+        root: rootDir,
+        www: path.join(rootDir, 'www'),
+        res: path.join(rootDir, 'res'),
+        platformWww: path.join(rootDir, 'platform_www'),
+        configXml: path.join(rootDir, 'config.xml'),
+        defaultConfigXml: path.join(rootDir, 'cordova/defaults.xml'),
+        build: path.join(rootDir, 'build'),
+        buildRes: path.join(rootDir, 'build-res'),
+        cache: path.join(rootDir, 'cache'),
         cordovaJs: 'bin/templates/project/assets/www/cordova.js',
         cordovaJsSrc: 'cordova-js-src'
     };
@@ -68,7 +109,7 @@ describe('Api class', () => {
              *   The API file path is actually located in 
"<project_dir>/platforms/electron/cordova".
              *   The expected path is "<project_dir>/platforms/electron" which 
is the electron's platform root dir
              */
-            expect(api.root).toBe(templateDir);
+            expect(api.root).toBe(rootDir);
         });
 
         it('should configure proper locations.', () => {
@@ -92,7 +133,7 @@ describe('Api class', () => {
             const actual = api.getPlatformInfo();
             const expected = {
                 locations: mockExpectedLocations,
-                root: templateDir,
+                root: rootDir,
                 name: 'electron',
                 version: '1.0.0',
                 projectConfig: undefined
@@ -125,25 +166,369 @@ describe('Api class', () => {
     /**
      * @todo Add useful tests.
      */
+
     describe('addPlugin method', () => {
+        let logs = {};
+        beforeEach(() => {
+            fs.removeSync(path.resolve(testProjectDir, 'electron.json'));
+            fs.removeSync(path.resolve(testProjectDir, 'www'));
+            apiEvents.addListener('verbose', (data) => {
+                logs.verbose.push(data);
+            });
+            logs = {
+                verbose: []
+            };
+        });
+
+        afterEach(() => {
+            apiEvents.removeAllListeners();
+        });
+
         it('should reject when missing plugin information', () => {
             api.addPlugin().then(
-                () => {},
+                () => {
+                    fail('Unwanted code branch');
+                },
                 (error) => {
                     expect(error).toEqual(new Error('The parameter is 
incorrect. The first parameter should be valid PluginInfo instance'));
                 }
             );
         });
+
+        it('empty plugin', (done) => {
+            api.addPlugin({
+                id: 'empty_plugin',
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => { return []; },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    expect(dirExists(path.resolve(testProjectDir, 
'www'))).toBeTruthy();
+                    expect(fileExists(path.resolve(testProjectDir, 
'electron.json'))).toBeTruthy();
+                    expect(fileExists(path.resolve(testProjectDir, 'www', 
'cordova_plugins.js'))).toBeTruthy();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                }
+            );
+        });
+
+        it('asset plugin', (done) => {
+            api.addPlugin({
+                id: 'asset-plugin',
+                dir: pluginFixture,
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => {
+                    return [{
+                        itemType: 'asset',
+                        src: 'src/electron/sample.json',
+                        target: 'js/sample.json'
+                    }];
+                },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    expect(fileExists(path.resolve(testProjectDir, 'www', 
'js', 'sample.json'))).toBeTruthy();
+                    expect(readJson(path.resolve(testProjectDir, 'www', 'js', 
'sample.json')).title).toEqual('sample');
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                }
+            );
+        });
+
+        it('js-module plugin', (done) => {
+            api.addPlugin({
+                id: 'module-plugin',
+                dir: pluginFixture,
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => { return []; },
+                getJsModules: (platform) => {
+                    return [{
+                        itemType: 'js-module',
+                        name: 'testmodule',
+                        src: 'www/plugin.js',
+                        clobbers: [ 'ModulePlugin.clobbers' ],
+                        merges: [ 'ModulePlugin.merges' ],
+                        runs: true
+                    }];
+                },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    expect(fileExists(path.resolve(testProjectDir, 'www', 
'plugins', 'module-plugin', 'www', 'plugin.js'))).toBeTruthy();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                }
+            );
+        });
+
+        it('unrecognized type plugin', (done) => {
+            api.addPlugin({
+                id: 'unrecognized-plugin',
+                dir: pluginFixture,
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => {
+                    return [{
+                        itemType: 'unrecognized'
+                    }];
+                },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                    done();
+                }
+            );
+        });
+
+        it('source-file type plugin', (done) => {
+            api.addPlugin({
+                id: 'source-file-plugin',
+                dir: pluginFixture,
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => {
+                    return [{
+                        itemType: 'source-file'
+                    }];
+                },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    expect(logs.verbose.some((message) => { return message === 
'source-file.install is currently not supported for electron'; })).toBeTruthy();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                    done();
+                }
+            );
+        });
+
+        it('empty plugin with browser platform', (done) => {
+            api.addPlugin({
+                id: 'empty_plugin',
+                getPlatformsArray: () => { return ['browser']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => { return []; },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    expect(dirExists(path.resolve(testProjectDir, 
'www'))).toBeTruthy();
+                    expect(fileExists(path.resolve(testProjectDir, 
'electron.json'))).toBeTruthy();
+                    expect(fileExists(path.resolve(testProjectDir, 'www', 
'cordova_plugins.js'))).toBeTruthy();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                }
+            );
+        });
     });
 
     /**
      * @todo Add useful tests.
      */
     describe('removePlugin method', () => {
+        let logs = {};
+        beforeEach(() => {
+            fs.removeSync(path.resolve(testProjectDir, 'electron.json'));
+            fs.removeSync(path.resolve(testProjectDir, 'www'));
+            apiEvents.addListener('verbose', (data) => {
+                logs.verbose.push(data);
+            });
+            logs = {
+                verbose: []
+            };
+        });
+
+        afterEach(() => {
+            apiEvents.removeAllListeners();
+        });
+
         it('should exist', () => {
             expect(api.removePlugin).toBeDefined();
             expect(typeof api.removePlugin).toBe('function');
         });
+
+        it('remove empty plugin', (done) => {
+            api.removePlugin({
+                id: 'empty_plugin',
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => { return []; },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                    done();
+                }
+            );
+        });
+
+        it('asset plugin', (done) => {
+            fs.ensureDirSync(path.resolve(testProjectDir, 'www', 'js'));
+            writeJson(path.resolve(testProjectDir, 'www', 'js', 
'sample.json'), { 'title': 'sample' });
+            api.removePlugin({
+                id: 'empty_plugin',
+                dir: pluginFixture,
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => {
+                    return [{
+                        itemType: 'asset',
+                        src: 'src/electron/sample.json',
+                        target: 'js/sample.json'
+                    }];
+                },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    expect(fileExists(path.resolve(testProjectDir, 'www', 
'js', 'sample.json'))).toBeFalsy();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                    done();
+                }
+            );
+        });
+
+        it('js-module plugin', (done) => {
+            fs.ensureDirSync(path.resolve(testProjectDir, 'www', 'plugins', 
'module-plugin', 'www'));
+            fs.copySync(path.resolve(pluginFixture, 'www', 'plugin.js'), 
path.resolve(testProjectDir, 'www', 'plugins', 'module-plugin', 'www', 
'plugin.js'));
+            expect(fileExists(path.resolve(testProjectDir, 'www', 'plugins', 
'module-plugin', 'www', 'plugin.js'))).toBeTruthy();
+            api.removePlugin({
+                id: 'module-plugin',
+                dir: pluginFixture,
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => { return []; },
+                getJsModules: (platform) => {
+                    return [{
+                        itemType: 'js-module',
+                        name: 'testmodule',
+                        src: 'www/plugin.js',
+                        clobbers: [ 'ModulePlugin.clobbers' ],
+                        merges: [ 'ModulePlugin.merges' ],
+                        runs: true
+                    }];
+                },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    expect(fileExists(path.resolve(testProjectDir, 'www', 
'plugins', 'module-plugin', 'www', 'plugin.js'))).toBeFalsy();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                    done();
+                }
+            );
+        });
+
+        it('unrecognized type plugin', (done) => {
+            api.removePlugin({
+                id: 'unrecognized-plugin',
+                dir: pluginFixture,
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => {
+                    return [{
+                        itemType: 'unrecognized'
+                    }];
+                },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                    done();
+                }
+            );
+        });
+
+        it('source-file type plugin', (done) => {
+            api.removePlugin({
+                id: 'source-file-plugin',
+                dir: pluginFixture,
+                getPlatformsArray: () => { return ['electron']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => {
+                    return [{
+                        itemType: 'source-file'
+                    }];
+                },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    expect(logs.verbose.some((message) => { return message === 
'source-file.uninstall is currently not supported for electron'; 
})).toBeTruthy();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                    done();
+                }
+            );
+        });
+
+        it('remove empty plugin with browser platform', (done) => {
+            api.removePlugin({
+                id: 'empty_plugin',
+                getPlatformsArray: () => { return ['browser']; },
+                getFilesAndFrameworks: (platform) => { return []; },
+                getAssets: (platform) => { return []; },
+                getJsModules: (platform) => { return []; },
+                getConfigFiles: (platform) => { return []; }
+            }, { }).then(
+                (result) => {
+                    expect(result).not.toBeDefined();
+                    done();
+                },
+                (error) => {
+                    fail('Unwanted code branch: ' + error);
+                    done();
+                }
+            );
+        });
+
     });
 
     /**
@@ -204,18 +589,18 @@ describe('Api class', () => {
 
     describe('createPlatform method', () => {
         beforeEach(() => {
-            fs.removeSync(tmpDir);
+            fs.removeSync(tmpWorkDir);
         });
 
         afterEach(() => {
-            fs.removeSync(tmpDir);
+            fs.removeSync(tmpWorkDir);
         });
 
         /**
          * @todo improve createPlatform to test actual created platforms.
          */
         it('should export static createPlatform function', () => {
-            Api.createPlatform(tmpDir).then(
+            return Api.createPlatform(tmpWorkDir).then(
                 (results) => {
                     
expect(results.constructor.name).toBe(api.constructor.name);
                 }
@@ -229,7 +614,7 @@ describe('Api class', () => {
                 };
             });
 
-            expect(() => 
Api.createPlatform(tmpDir)).toThrowError(/createPlatform is not callable from 
the electron project API/);
+            expect(() => 
Api.createPlatform(tmpWorkDir)).toThrowError(/createPlatform is not callable 
from the electron project API/);
 
             Api.__set__('require', apiRequire);
         });
diff --git a/tests/spec/unit/handler.spec.js b/tests/spec/unit/handler.spec.js
index dac16e5..1babd1a 100644
--- a/tests/spec/unit/handler.spec.js
+++ b/tests/spec/unit/handler.spec.js
@@ -148,8 +148,15 @@ describe('Handler export', () => {
                 const www_dir = 'www';
                 const pluginRelativePath = path.join('plugins', plugin_id, 
jsModule.src);
 
+                const removeSyncSpy = jasmine.createSpy('writeFileSync');
+                handler.__set__('fs', {
+                    removeSync: removeSyncSpy
+                });
+
                 handler['js-module'].uninstall(jsModule, www_dir, plugin_id);
 
+                expect(removeSyncSpy).toHaveBeenCalled();
+
                 const actual = emitSpy.calls.argsFor(0)[1];
                 const expected = `js-module uninstall called : 
${pluginRelativePath}`;
                 expect(emitSpy).toHaveBeenCalled();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to