Skip to content

Commit

Permalink
chore(tests): split index and setVersion tests into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
gligoran committed Apr 9, 2017
1 parent e3e4669 commit 630f318
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 136 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

import setVersion from './set-version';

export default {
Expand Down
136 changes: 0 additions & 136 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
'use strict';

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import fs from 'fs-promise';

import cordovaSetVersion from '../src/index';

const fileParams = { encoding: 'UTF-8' };
const configPaths = {
COPY: './config.xml',
GOOD: './configs/config.good.xml',
MALFORMED: './configs/config.malformed.xml',
MISSING: './configs/config.missing.xml',
EXPECTED: './configs/config.expected.xml',
EXPECTED_JSON: './configs/config.expected-json.xml'
};
const packagePaths = {
COPY: './package.json',
GOOD: './packages/package.good.json',
MALFORMED: './packages/package.malformed.json',
NO_VERSION: './packages/package.no-version.json',
MISSING: './packages/package.missing.json'
};

chai.use(chaiAsPromised);
chai.should();

describe('cordova-set-version', () => {
Expand All @@ -34,120 +14,4 @@ describe('cordova-set-version', () => {
it('should exist', () => {
cordovaSetVersion.should.exist;
});

describe('setVersion', () => {
it('should exist', () => {
cordovaSetVersion.should.have.property('setVersion');
});

it('should be a function', () => {
let type = typeof cordovaSetVersion.setVersion;
type.should.equal('function');
});

it('should produce same content as in `config.expected.xml` when passed the argument `1.0.0`', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY, '1.0.0')
.then(() => fs.readFile(configPaths.COPY, fileParams));

let expectedConfig = fs.readFileSync(configPaths.EXPECTED, fileParams);

return promise.should.be.fulfilled
.then((result) => {
result.should.equal(expectedConfig);
});
});

it('should return an error about missing config.xml file', () => {
let promise = cordovaSetVersion.setVersion(configPaths.MISSING, '1.0.0')
.then(() => fs.readFile(configPaths.COPY, fileParams));

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.should.have.property('message');
error.message.should.contain('no such file or directory');
error.message.should.contain(configPaths.MISSING);
});
});

it('should return an error about bad config.xml file', () => {
let promise = cordovaSetVersion.setVersion(configPaths.MALFORMED, '1.0.0')

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.message.should.not.contain('no such file or directory');
});
});

it('should use version from package.json', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.GOOD, packagePaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY)
.then(() => fs.readFile(configPaths.COPY, fileParams));

let config = fs.readFileSync(configPaths.COPY, fileParams);
let expectedJsonConfig = fs.readFileSync(configPaths.EXPECTED_JSON, fileParams);

return promise.should.be.fulfilled
.then((result) => {
result.should.equal(expectedJsonConfig);
});
});

it('should return an error about missing package file', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY);

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.message.should.contain('no such file or directory');
error.message.should.contain('package.json');
});
});

it('should return an error about bad package file', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.MALFORMED, packagePaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY);

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.name.should.equal('JSONError');
error.message.should.contain('Unexpected end of input');
error.message.should.contain('package.json');
});
});

it('should return an error about no version', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.NO_VERSION, packagePaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY);

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.message.should.contain('no version');
error.message.should.contain('package.json');
});
});

afterEach(function () {
if (fs.existsSync(configPaths.COPY)) {
fs.removeSync(configPaths.COPY);
}

if (fs.existsSync(packagePaths.COPY)) {
fs.removeSync(packagePaths.COPY);
}
});
});
});
147 changes: 147 additions & 0 deletions test/set-version.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
'use strict';

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import fs from 'fs-promise';

import cordovaSetVersion from '../src/index';

const fileParams = { encoding: 'UTF-8' };
const configPaths = {
COPY: './config.xml',
GOOD: './configs/config.good.xml',
MALFORMED: './configs/config.malformed.xml',
MISSING: './configs/config.missing.xml',
EXPECTED: './configs/config.expected.xml',
EXPECTED_JSON: './configs/config.expected-json.xml'
};
const packagePaths = {
COPY: './package.json',
GOOD: './packages/package.good.json',
MALFORMED: './packages/package.malformed.json',
NO_VERSION: './packages/package.no-version.json',
MISSING: './packages/package.missing.json'
};

chai.use(chaiAsPromised);
chai.should();

describe('setVersion', () => {
before(() => {
process.chdir(__dirname);
});

it('should exist', () => {
cordovaSetVersion.should.have.property('setVersion');
});

it('should be a function', () => {
let type = typeof cordovaSetVersion.setVersion;
type.should.equal('function');
});

it('should produce same content as in `config.expected.xml` when passed the argument `1.0.0`', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY, '1.0.0')
.then(() => fs.readFile(configPaths.COPY, fileParams));

let expectedConfig = fs.readFileSync(configPaths.EXPECTED, fileParams);

return promise.should.be.fulfilled
.then((result) => {
result.should.equal(expectedConfig);
});
});

it('should return an error about missing config.xml file', () => {
let promise = cordovaSetVersion.setVersion(configPaths.MISSING, '1.0.0')
.then(() => fs.readFile(configPaths.COPY, fileParams));

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.should.have.property('message');
error.message.should.contain('no such file or directory');
error.message.should.contain(configPaths.MISSING);
});
});

it('should return an error about bad config.xml file', () => {
let promise = cordovaSetVersion.setVersion(configPaths.MALFORMED, '1.0.0')

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.message.should.not.contain('no such file or directory');
});
});

it('should use version from package.json', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.GOOD, packagePaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY)
.then(() => fs.readFile(configPaths.COPY, fileParams));

let config = fs.readFileSync(configPaths.COPY, fileParams);
let expectedJsonConfig = fs.readFileSync(configPaths.EXPECTED_JSON, fileParams);

return promise.should.be.fulfilled
.then((result) => {
result.should.equal(expectedJsonConfig);
});
});

it('should return an error about missing package file', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY);

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.message.should.contain('no such file or directory');
error.message.should.contain('package.json');
});
});

it('should return an error about bad package file', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.MALFORMED, packagePaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY);

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.name.should.equal('JSONError');
error.message.should.contain('Unexpected end of input');
error.message.should.contain('package.json');
});
});

it('should return an error about no version', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);
fs.copySync(packagePaths.NO_VERSION, packagePaths.COPY);

let promise = cordovaSetVersion.setVersion(configPaths.COPY);

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.message.should.contain('no version');
error.message.should.contain('package.json');
});
});

afterEach(function () {
if (fs.existsSync(configPaths.COPY)) {
fs.removeSync(configPaths.COPY);
}

if (fs.existsSync(packagePaths.COPY)) {
fs.removeSync(packagePaths.COPY);
}
});
});

0 comments on commit 630f318

Please sign in to comment.