Skip to content
Permalink
Browse files
Refactor tests (#16)
* Factor out common checks
* Check more artifacts
* Simplify Checks


Co-authored-by: Christopher J. Brody <brodybits@litehelpers.net>
Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>
  • Loading branch information
raphinesse and Christopher J. Brody committed Jun 17, 2018
1 parent 0145473 commit 3d21f05e4195e4b5363b419306e75f2268c362e6
Showing 7 changed files with 69 additions and 82 deletions.
@@ -29,9 +29,10 @@ var CordovaError = require('cordova-common').CordovaError;
var ConfigParser = require('cordova-common').ConfigParser;
const {tmpDir, createWith, createWithMockFetch, expectRejection} = require('./helpers');

var appName = 'TestBase';
var appId = 'org.testing';
var project = path.join(tmpDir, appName);
const appName = 'TestBase';
const appId = 'org.testing';
const appVersion = '1.0.0';
const project = path.join(tmpDir, appName);

// Setup and teardown test dirs
beforeEach(function () {
@@ -57,93 +58,82 @@ describe('cordova create checks for valid-identifier', function () {

describe('create end-to-end', function () {

function checkProject () {
// Check if top level dirs exist.
function checkCommonArtifacts () {
// Check that top level dirs exist
var dirs = ['hooks', 'platforms', 'plugins', 'www'];
dirs.forEach(function (d) {
expect(path.join(project, d)).toExist();
});

// Check that README.md exists inside of hooks
expect(path.join(project, 'hooks', 'README.md')).toExist();

// Check if www files exist.
// Check that index.html exists inside of www
expect(path.join(project, 'www', 'index.html')).toExist();

// Check that config.xml was updated.
var configXml = new ConfigParser(path.join(project, 'config.xml'));
expect(configXml.packageName()).toEqual(appId);

// TODO (kamrik): check somehow that we got the right config.xml from the fixture and not some place else.
// expect(configXml.name()).toEqual('TestBase');
}

function checkConfigXml () {
// Check if top level dirs exist.
var dirs = ['hooks', 'platforms', 'plugins', 'www'];
dirs.forEach(function (d) {
expect(path.join(project, d)).toExist();
});
expect(path.join(project, 'hooks', 'README.md')).toExist();
// Check that www files don't get copied to the top level
expect(path.join(project, 'index.html')).not.toExist();

// index.js and template subdir folder should not exist (inner files should be copied to the project folder)
// index.js and template subdir folder should not exist in top level
// (inner files should be copied to the project top level folder)
expect(path.join(project, 'index.js')).not.toExist();
expect(path.join(project, 'template')).not.toExist();

// Check if www files exist.
expect(path.join(project, 'www', 'index.html')).toExist();
// Check that config.xml does not exist inside of www
expect(path.join(project, 'www', 'config.xml')).not.toExist();

// Check that config.xml was updated correctly
var configXml = new ConfigParser(path.join(project, 'config.xml'));
expect(configXml.packageName()).toEqual(appId);
expect(configXml.version()).toEqual('1.0.0');
expect(configXml.name()).toEqual(appName);
expect(configXml.version()).toEqual(appVersion);
}

// Check that config.xml does not exist inside of www
expect(path.join(project, 'www', 'config.xml')).not.toExist();
// Check that we got package.json and it was updated correctly
function checkPackageJson () {
const pkg = requireFresh(path.join(project, 'package.json'));
expect(pkg.name).toEqual(appId);
expect(pkg.displayName).toEqual(appName);
expect(pkg.version).toEqual(appVersion);
}

// Check that we got no package.json
// Check that we got no package.json
function checkNoPackageJson () {
expect(path.join(project, 'package.json')).not.toExist();

// Check that we got the right config.xml from the template and not stock
expect(configXml.description()).toEqual('this is the correct config.xml');
}

function checkSubDir () {
// Check if top level dirs exist.
var dirs = ['hooks', 'platforms', 'plugins', 'www'];
dirs.forEach(function (d) {
expect(path.join(project, d)).toExist();
});
expect(path.join(project, 'hooks', 'README.md')).toExist();
// Check that we did use the default template
function checkDefaultTemplate () {
const pkg = requireFresh(path.join(project, 'package.json'));
expect(pkg.author).toEqual('Apache Cordova Team');

// index.js and template subdir folder should not exist (inner files should be copied to the project folder)
expect(path.join(project, 'index.js')).not.toExist();
expect(path.join(project, 'template')).not.toExist();
const configXml = new ConfigParser(path.join(project, 'config.xml'));
expect(configXml.author()).toEqual('Apache Cordova Team');
}

// Check if config files exist.
expect(path.join(project, 'www', 'index.html')).toExist();
// Check that we did not use the default template
function checkNotDefaultTemplate () {
const configXml = new ConfigParser(path.join(project, 'config.xml'));
expect(configXml.author()).not.toEqual('Apache Cordova Team');
}

// Check that config.xml was updated.
var configXml = new ConfigParser(path.join(project, 'config.xml'));
expect(configXml.packageName()).toEqual(appId);
expect(configXml.version()).toEqual('1.0.0');
// Check that we got package.json (the correct one)
var pkjson = requireFresh(path.join(project, 'package.json'));
// Pkjson.displayName should equal config's name.
expect(pkjson.displayName).toEqual(appName);
expect(pkjson.valid).toEqual('true');

// Check that we got the right config.xml
expect(configXml.description()).toEqual('this is the correct config.xml');
function checkProjectCreatedWithFixtureTemplate () {
checkCommonArtifacts();
checkNoPackageJson();
checkNotDefaultTemplate();
}

function checkProjectCreatedWithDefaultTemplate () {
checkCommonArtifacts();
checkPackageJson();
checkDefaultTemplate();
}

it('should successfully run without template and use default hello-world app', function () {
// Create a real project with no template
// use default cordova-app-hello-world app
return create(project, appId, appName, {}, events)
.then(checkProject)
.then(function () {
var pkgJson = requireFresh(path.join(project, 'package.json'));
// confirm default hello world app copies over package.json and it matched appId
expect(pkgJson.name).toEqual(appId);
});
.then(checkProjectCreatedWithDefaultTemplate);
});

it('should successfully run with Git URL', function () {
@@ -161,7 +151,7 @@ describe('create end-to-end', function () {
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy.calls.argsFor(0)[0]).toBe(config.lib.www.url);
})
.then(checkProject);
.then(checkProjectCreatedWithDefaultTemplate);
});

it('should successfully run with NPM package', function () {
@@ -179,7 +169,7 @@ describe('create end-to-end', function () {
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy.calls.argsFor(0)[0]).toBe(config.lib.www.url);
})
.then(checkProject);
.then(checkProjectCreatedWithDefaultTemplate);
});

it('should successfully run with NPM package and explicitly fetch latest if no version is given', function () {
@@ -198,7 +188,7 @@ describe('create end-to-end', function () {
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy.calls.argsFor(0)[0]).toBe(config.lib.www.url + '@latest');
})
.then(checkProject);
.then(checkProjectCreatedWithDefaultTemplate);
});

it('should successfully run with template not having a package.json at toplevel', function () {
@@ -211,12 +201,7 @@ describe('create end-to-end', function () {
}
};
return create(project, appId, appName, config, events)
.then(checkProject)
.then(function () {
// Check that we got the right config.xml
var configXml = new ConfigParser(path.join(project, 'config.xml'));
expect(configXml.description()).toEqual('this is the very correct config.xml');
});
.then(checkProjectCreatedWithFixtureTemplate);
});

it('should successfully run with template having package.json and no sub directory', function () {
@@ -229,7 +214,7 @@ describe('create end-to-end', function () {
}
};
return create(project, appId, appName, config, events)
.then(checkProject);
.then(checkProjectCreatedWithFixtureTemplate);
});

it('should successfully run with template having package.json, and subdirectory, and no package.json in subdirectory', function () {
@@ -242,7 +227,7 @@ describe('create end-to-end', function () {
}
};
return create(project, appId, appName, config, events)
.then(checkProject);
.then(checkProjectCreatedWithFixtureTemplate);
});

it('should successfully run with template having package.json, and subdirectory, and package.json in subdirectory', function () {
@@ -255,7 +240,9 @@ describe('create end-to-end', function () {
}
};
return create(project, appId, appName, config, events)
.then(checkSubDir);
.then(checkCommonArtifacts)
.then(checkPackageJson)
.then(checkNotDefaultTemplate);
});

it('should successfully run config.xml in the www folder and move it outside', function () {
@@ -268,7 +255,7 @@ describe('create end-to-end', function () {
}
};
return create(project, appId, appName, config, events)
.then(checkConfigXml);
.then(checkProjectCreatedWithFixtureTemplate);
});

it('should successfully run with www folder as the template', function () {
@@ -281,13 +268,13 @@ describe('create end-to-end', function () {
}
};
return create(project, appId, appName, config, events)
.then(checkConfigXml);
.then(checkProjectCreatedWithFixtureTemplate);
});

it('should successfully run with existing, empty destination', function () {
shell.mkdir('-p', project);
return create(project, appId, appName, {}, events)
.then(checkProject);
.then(checkProjectCreatedWithDefaultTemplate);
});

describe('when --link-to is provided', function () {
@@ -21,7 +21,7 @@
<name>HelloCordova</name>
<description>this is the correct config.xml</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
Apache Cordova Test Team
</author>
<content src="index.html" />
<!-- Whitelist configuration. Refer to https://cordova.apache.org/docs/en/edge/guide_appdev_whitelist_index.md.html -->
@@ -10,7 +10,7 @@
</description>

<author href="http://cordova.io" email="dev@cordova.apache.org">
Apache Cordova Team
Apache Cordova Test Team
</author>

<access origin="*" />
@@ -21,7 +21,7 @@
<name>HelloCordova</name>
<description>this is the very correct config.xml</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
Apache Cordova Test Team
</author>
<content src="index.html" />
<!-- Whitelist configuration. Refer to https://cordova.apache.org/docs/en/edge/guide_appdev_whitelist_index.md.html -->
@@ -23,7 +23,7 @@
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
Apache Cordova Test Team
</author>
<content src="index.html" />
<!-- Whitelist configuration. Refer to https://cordova.apache.org/docs/en/edge/guide_appdev_whitelist_index.md.html -->
@@ -23,7 +23,7 @@
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
Apache Cordova Test Team
</author>
<content src="index.html" />
<!-- Whitelist configuration. Refer to https://cordova.apache.org/docs/en/edge/guide_appdev_whitelist_index.md.html -->
@@ -21,7 +21,7 @@
<name>HelloCordova</name>
<description>this is the correct config.xml</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
Apache Cordova Test Team
</author>
<content src="index.html" />
<!-- Whitelist configuration. Refer to https://cordova.apache.org/docs/en/edge/guide_appdev_whitelist_index.md.html -->

0 comments on commit 3d21f05

Please sign in to comment.