cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From filmaj <...@git.apache.org>
Subject [GitHub] cordova-lib pull request #567: CB-12361: Added unit tests for prepare.js
Date Wed, 21 Jun 2017 20:55:39 GMT
Github user filmaj commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/567#discussion_r123363271
  
    --- Diff: spec-cordova/prepare.spec.js ---
    @@ -17,175 +17,265 @@
         under the License.
     */
     
    -var shell = require('shelljs'),
    -    PlatformJson = require('cordova-common').PlatformJson,
    -    path = require('path'),
    -    util = require('../src/cordova/util'),
    -    prepare = require('../src/cordova/prepare'),
    -    lazy_load = require('../src/cordova/lazy_load'),
    -    ConfigParser = require('cordova-common').ConfigParser,
    -    platforms = require('../src/platforms/platforms'),
    -    HooksRunner = require('../src/hooks/HooksRunner'),
    -    xmlHelpers = require('cordova-common').xmlHelpers,
    -    et = require('elementtree'),
    -    Q = require('q');
    +/* eslint-env jasmine */
     
    -var project_dir = '/some/path';
    -var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www';
});
    -
    -var TEST_XML = '<?xml version="1.0" encoding="UTF-8"?>\n' +
    -    '<widget xmlns     = "http://www.w3.org/ns/widgets"\n' +
    -    '        xmlns:cdv = "http://cordova.apache.org/ns/1.0"\n' +
    -    '        id        = "io.cordova.hellocordova"\n' +
    -    '        version   = "0.0.1">\n' +
    -    '    <name>Hello Cordova</name>\n' +
    -    '    <description>\n' +
    -    '        A sample Apache Cordova application that responds to the deviceready event.\n'
+
    -    '    </description>\n' +
    -    '    <author href="http://cordova.io" email="dev@cordova.apache.org">\n' +
    -    '        Apache Cordova Team\n' +
    -    '    </author>\n' +
    -    '    <content src="index.html" />\n' +
    -    '    <access origin="*" />\n' +
    -    '    <preference name="fullscreen" value="true" />\n' +
    -    '    <preference name="webviewbounce" value="true" />\n' +
    -    '</widget>\n';
    +var path = require('path');
    +var rewire = require('rewire');
    +var util = require('../src/cordova/util');
    +var cordova_config = require('../src/cordova/config');
    +var prepare = rewire('../src/cordova/prepare');
    +var restore = require('../src/cordova/restore-util');
    +var platforms = require('../src/platforms/platforms');
    +var HooksRunner = require('../src/hooks/HooksRunner');
    +var Q = require('q');
    +var PlatformJson = require('cordova-common').PlatformJson;
    +var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
     
    -describe('prepare command', function() {
    -    var is_cordova,
    -        cd_project_root,
    -        list_platforms,
    -        fire,
    -        parsers = {},
    -        find_plugins,
    -        cp,
    -        mkdir,
    -        load, platformApi, getPlatformApi;
    +var project_dir = '/some/path';
     
    +describe('cordova/prepare', function () {
    +    let platform_api_prepare_mock;
    +    let platform_api_add_mock;
    +    let platform_api_remove_mock;
         beforeEach(function () {
    -        getPlatformApi = spyOn(platforms, 'getPlatformApi').and.callFake(function (platform,
rootDir) {
    +        platform_api_prepare_mock = jasmine.createSpy('prepare').and.returnValue(Q());
    +        platform_api_add_mock = jasmine.createSpy('add').and.returnValue(Q());
    +        platform_api_remove_mock = jasmine.createSpy('remove').and.returnValue(Q());
    +        spyOn(platforms, 'getPlatformApi').and.callFake(function (platform, rootDir)
{
                 return {
    -                prepare: jasmine.createSpy('prepare').and.returnValue(Q()),
    +                prepare: platform_api_prepare_mock,
                     getPlatformInfo: jasmine.createSpy('getPlatformInfo').and.returnValue({
                         locations: {
                             www: path.join(project_dir, 'platforms', platform, 'www')
                         }
                     }),
    +                removePlugin: platform_api_add_mock,
    +                addPlugin: platform_api_remove_mock
                 };
             });
    -        is_cordova = spyOn(util, 'isCordova').and.returnValue(project_dir);
    -        cd_project_root = spyOn(util, 'cdProjectRoot').and.returnValue(project_dir);
    -        list_platforms = spyOn(util, 'listPlatforms').and.returnValue(supported_platforms);
    -        fire = spyOn(HooksRunner.prototype, 'fire').and.returnValue(Q());
    -
    -        find_plugins = spyOn(util, 'findPlugins').and.returnValue([]);
    -        spyOn(PlatformJson, 'load').and.returnValue(new PlatformJson(null, null, {}));
    -        spyOn(PlatformJson.prototype, 'save');
    -        load = spyOn(lazy_load, 'based_on_config').and.returnValue(Q());
    -        cp = spyOn(shell, 'cp').and.returnValue(true);
    -        mkdir = spyOn(shell, 'mkdir');
    -        spyOn(ConfigParser.prototype, 'write');
    -        spyOn(xmlHelpers, 'parseElementtreeSync').and.callFake(function() {
    -            return new et.ElementTree(et.XML(TEST_XML));
    -        });
    +        spyOn(PlatformJson, 'load');
    +        spyOn(HooksRunner.prototype, 'fire').and.returnValue(Q());
    +        spyOn(util, 'isCordova').and.returnValue(true);
         });
     
    -    describe('failure', function() {
    -        it('Test 001 : should not run outside of a cordova-based project by calling util.isCordova',
function(done) {
    -            is_cordova.and.returnValue(false);
    -            cd_project_root.and.callThrough();  // undo spy here because prepare depends
on cdprojectRoot for isCordova check
    -            prepare().then(function() {
    -                expect('this call').toBe('fail');
    -            }, function(err) {
    -                expect('' + err).toContain('Current working directory is not a Cordova-based
project.');
    -            }).fin(done);
    +    describe('main method', function () {
    +        beforeEach(function () {
    +            spyOn(cordova_config, 'read').and.returnValue({});
    +            spyOn(restore, 'installPlatformsFromConfigXML').and.returnValue(Q());
    +            spyOn(restore, 'installPluginsFromConfigXML').and.returnValue(Q());
    +            
    +            spyOn(prepare, 'preparePlatforms').and.returnValue(Q);
    +        });
    +        describe('failure', function () {
    +            it('should invoke util.preProcessOptions as preflight task checker, which,
if fails, should trigger promise rejection and only fire the before_prepare hook', function(done)
{
    +                spyOn(util, 'cdProjectRoot').and.returnValue(project_dir);
    +                spyOn(util, 'preProcessOptions').and.throwError();
    +                prepare({}).fail(function(err) {
    +                    expect(err).toBeDefined();
    --- End diff --
    
    I actually didn't know you could use `throwError` that way! Looking at the jasmine docs,
it looks like you can pass a string argument to `throwError` to customize the error message
- maybe try that? If not, in the past I have used `and.callFake(function () { throw new Error('boom!');
});`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


Mime
View raw message