Fixed JS unit tests (#73)
* Fixed chai tests * Removed result.html from git * Ignoring result.html from git * Added tests for form.js, along with type enumeration and type testing
This commit is contained in:
parent
db24238071
commit
5b99b3512e
5 changed files with 132 additions and 104 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,6 +4,7 @@
|
|||
/assets/photos/import/*
|
||||
/assets/photos/export/*
|
||||
/frontend/node_modules/*
|
||||
/frontend/tests/result.html
|
||||
/assets/testdata
|
||||
*.log
|
||||
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
class Form {
|
||||
export const FormPropertyType = Object.freeze({
|
||||
String: 'string',
|
||||
Number: 'number',
|
||||
Object: 'object',
|
||||
});
|
||||
|
||||
export default class Form {
|
||||
constructor(definition) {
|
||||
this.definition = definition;
|
||||
}
|
||||
|
@ -6,9 +12,9 @@ class Form {
|
|||
setValues(values) {
|
||||
const def = this.getDefinition();
|
||||
|
||||
for(let prop in def) {
|
||||
if(def.hasOwnProperty(prop) && values.hasOwnProperty(prop)) {
|
||||
def[prop].value = values[prop];
|
||||
for (let prop in def) {
|
||||
if (values.hasOwnProperty(prop)) {
|
||||
this.setValue(prop, values[prop]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,16 +24,38 @@ class Form {
|
|||
getValues() {
|
||||
const result = {};
|
||||
const def = this.getDefinition();
|
||||
|
||||
for(let prop in def) {
|
||||
if(def.hasOwnProperty(prop)) {
|
||||
result[prop] = def[prop].value;
|
||||
}
|
||||
|
||||
for (let prop in def) {
|
||||
result[prop] = this.getValue(prop);
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
setValue(name, value) {
|
||||
const def = this.getDefinition();
|
||||
|
||||
if (!def.hasOwnProperty(name)) {
|
||||
throw `Property ${name} not found`;
|
||||
} else if (typeof value != def[name].type) {
|
||||
throw `Property ${name} must be ${def[name].type}`;
|
||||
} else {
|
||||
def[name].value = value;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
getValue(name) {
|
||||
const def = this.getDefinition();
|
||||
|
||||
if (def.hasOwnProperty(name)) {
|
||||
return def[name].value;
|
||||
} else {
|
||||
throw `Property ${name} not found`;
|
||||
}
|
||||
}
|
||||
|
||||
setDefinition(definition) {
|
||||
this.definition = definition;
|
||||
}
|
||||
|
@ -37,12 +65,14 @@ class Form {
|
|||
}
|
||||
|
||||
getOptions(fieldName) {
|
||||
if(this.definition && this.definition.hasOwnProperty(fieldName) && this.definition[fieldName].hasOwnProperty('options')) {
|
||||
if (
|
||||
this.definition &&
|
||||
this.definition.hasOwnProperty(fieldName) &&
|
||||
this.definition[fieldName].hasOwnProperty('options')
|
||||
) {
|
||||
return this.definition[fieldName].options;
|
||||
}
|
||||
|
||||
return [{option: '', label: ''}];
|
||||
return [{ option: '', label: '' }];
|
||||
}
|
||||
}
|
||||
|
||||
export default Form;
|
|
@ -38,7 +38,7 @@ describe('common/api', () => {
|
|||
Api.get('foo').then(
|
||||
(response) => {
|
||||
assert.equal(200, response.status);
|
||||
assert.equal(getCollectionResponse, response.data);
|
||||
assert.deepEqual(getCollectionResponse, response.data);
|
||||
done();
|
||||
}
|
||||
).catch(
|
||||
|
@ -52,7 +52,7 @@ describe('common/api', () => {
|
|||
Api.get('foo/123').then(
|
||||
(response) => {
|
||||
assert.equal(200, response.status);
|
||||
assert.equal(getEntityResponse, response.data);
|
||||
assert.deepEqual(getEntityResponse, response.data);
|
||||
done();
|
||||
}
|
||||
).catch(
|
||||
|
@ -66,7 +66,7 @@ describe('common/api', () => {
|
|||
Api.post('foo', postEntityResponse).then(
|
||||
(response) => {
|
||||
assert.equal(201, response.status);
|
||||
assert.equal(postEntityResponse, response.data);
|
||||
assert.deepEqual(postEntityResponse, response.data);
|
||||
done();
|
||||
}
|
||||
).catch(
|
||||
|
@ -80,7 +80,7 @@ describe('common/api', () => {
|
|||
Api.put('foo/2', putEntityResponse).then(
|
||||
(response) => {
|
||||
assert.equal(200, response.status);
|
||||
assert.equal(putEntityResponse, response.data);
|
||||
assert.deepEqual(putEntityResponse, response.data);
|
||||
done();
|
||||
}
|
||||
).catch(
|
||||
|
@ -94,7 +94,7 @@ describe('common/api', () => {
|
|||
Api.delete('foo/2', deleteEntityResponse).then(
|
||||
(response) => {
|
||||
assert.equal(204, response.status);
|
||||
assert.equal(deleteEntityResponse, response.data);
|
||||
assert.deepEqual(deleteEntityResponse, response.data);
|
||||
done();
|
||||
}
|
||||
).catch(
|
||||
|
|
|
@ -1,15 +1,90 @@
|
|||
import assert from 'assert';
|
||||
import Form from 'common/form';
|
||||
import Form, { FormPropertyType } from 'common/form';
|
||||
|
||||
describe('common/form', () => {
|
||||
it('setDefinition', () => {
|
||||
let def = {'foo': {'type': 'string', 'caption': 'Foo'}};
|
||||
it('setting and getting definition', () => {
|
||||
const def = { foo: { type: FormPropertyType.String, caption: 'Foo' } };
|
||||
const form = new Form();
|
||||
|
||||
form.setDefinition(def);
|
||||
|
||||
let result = form.getDefinition();
|
||||
|
||||
const result = form.getDefinition();
|
||||
assert.equal(result, def);
|
||||
});
|
||||
});
|
||||
|
||||
it('setting and getting a value according to type', () => {
|
||||
const def = {
|
||||
foo: { type: FormPropertyType.String, caption: 'Foo' },
|
||||
};
|
||||
const form = new Form();
|
||||
|
||||
form.setDefinition(def);
|
||||
form.setValue('foo', 'test');
|
||||
|
||||
const result = form.getValue('foo');
|
||||
assert.equal(result, 'test');
|
||||
});
|
||||
|
||||
it('setting a value not according to type', () => {
|
||||
const def = {
|
||||
foo: { type: FormPropertyType.String, caption: 'Foo' },
|
||||
};
|
||||
const form = new Form();
|
||||
|
||||
form.setDefinition(def);
|
||||
|
||||
assert.throws(() => {
|
||||
form.setValue('foo', 3);
|
||||
});
|
||||
});
|
||||
|
||||
it('setting and getting a value for missing property throws exception', () => {
|
||||
const def = {
|
||||
foo: { type: FormPropertyType.String, caption: 'Foo' },
|
||||
};
|
||||
const form = new Form();
|
||||
|
||||
form.setDefinition(def);
|
||||
|
||||
assert.throws(() => {
|
||||
form.setValue('bar', 3);
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
form.getValue('bar');
|
||||
});
|
||||
});
|
||||
|
||||
it('setting and getting a complex value', () => {
|
||||
const complexValue = {
|
||||
something: 'abc',
|
||||
another: 'def',
|
||||
};
|
||||
const def = {
|
||||
foo: {
|
||||
type: FormPropertyType.Object,
|
||||
caption: 'Foo',
|
||||
},
|
||||
};
|
||||
const form = new Form();
|
||||
|
||||
form.setDefinition(def);
|
||||
form.setValue('foo', complexValue);
|
||||
|
||||
const result = form.getValue('foo');
|
||||
assert.deepEqual(result, complexValue);
|
||||
});
|
||||
|
||||
it('setting and getting more values at once', () => {
|
||||
const def = {
|
||||
foo: { type: FormPropertyType.String, caption: 'Foo' },
|
||||
};
|
||||
const form = new Form();
|
||||
|
||||
form.setDefinition(def);
|
||||
form.setValues({ foo: 'test' });
|
||||
|
||||
const result = form.getValues();
|
||||
assert.equal(result.foo, 'test');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,78 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Unit Test Results</title>
|
||||
<style type="text/css">html,body{font-family:Arial,sans-serif;font-size:1rem;margin:0;padding:0;}body{padding:10px 40px;}h1{margin-bottom:0;}h2{margin-top:0;margin-bottom:0;color:#999;}table{width:100%;margin-top:20px;margin-bottom:20px;table-layout:fixed;}tr.header{background:#ddd;font-weight:bold;border-bottom:none;}td{padding:7px;border-top:none;border-left:1px black solid;border-bottom:1px black solid;border-right:none;word-break:break-all;word-wrap:break-word;}tr.pass td{color:#003b07;background:#86e191;}tr.skip td{color:#7d3a00;background:#ffd24a;}tr.fail td{color:#5e0e00;background:#ff9c8a;}tr:first-child td{border-top:1px black solid;}td:last-child{border-right:1px black solid;}tr.overview{font-weight:bold;color:#777;}tr.overview td{padding-bottom:0px;border-bottom:none;}tr.system-out td{color:#777;}tr.system-errors td{color:#f00;}hr{height:2px;margin:30px 0;background:#000;border:none;}section{margin-top:40px;}h3{margin:6px 0;}.overview{color:#333;font-weight:bold;}.system-out{margin:0.4rem 0;}.system-errors{color:#a94442}.spec{padding:0.8rem;margin:0.3rem 0;}.spec--pass{color:#3c763d;background-color:#dff0d8;border:1px solid #d6e9c6;}.spec--skip{color:#8a6d3b;background-color:#fcf8e3;border:1px solid #faebcc;}.spec--fail{color:#a94442;background-color:#f2dede;border:1px solid #ebccd1;}.spec--group{color:#636363;background-color:#f0f0f0;border:1px solid #e6e6e6;margin:0;}.spec--group:not(:first-of-type){margin:20px 0 0 0;}.spec__title{display:inline;}.spec__suite{display:inline;}.spec__descrip{font-weight:normal;}.spec__status{float:right;}.spec__log{padding-left: 2.3rem;}.hidden{display:none;}body.compact .spec p{margin-top:0;margin-bottom:0.5rem;}body.compact .spec,body.compact tr,body.compact .overview,body.compact .system-out,body.compact .system-errors{font-size:0.85rem;}body.compact .spec{padding:0.3rem 0.5rem;}body.compact section{margin-top:30px;}</style>
|
||||
</head>
|
||||
<body class="">
|
||||
<h1>Unit Test Results</h1>
|
||||
<section>
|
||||
<header class="overview">
|
||||
<div class="browser">Browser: PhantomJS 2.1.1 (Mac OS X 0.0.0)</div>
|
||||
<div class="timestamp">Timestamp: 5/31/2017, 2:52:07 PM</div>
|
||||
<p class="results">
|
||||
6 tests /
|
||||
0 errors /
|
||||
0 failures /
|
||||
0 skipped /
|
||||
runtime: 0.013s
|
||||
</p>
|
||||
</header>
|
||||
<div class="spec spec--pass" style="">
|
||||
<h3 class="spec__header">
|
||||
<div class="spec__title">
|
||||
<p class="spec__suite">common/api</p>
|
||||
<em class="spec__descrip">get("foo") should return a list of items and return with HTTP code 200</em>
|
||||
</div>
|
||||
<div class="spec__status">Passed in 0.006s</div>
|
||||
</h3>
|
||||
</div>
|
||||
<div class="spec spec--pass" style="">
|
||||
<h3 class="spec__header">
|
||||
<div class="spec__title">
|
||||
<p class="spec__suite">common/api</p>
|
||||
<em class="spec__descrip">get("foo/123") should return one item and return with HTTP code 200</em>
|
||||
</div>
|
||||
<div class="spec__status">Passed in 0.001s</div>
|
||||
</h3>
|
||||
</div>
|
||||
<div class="spec spec--pass" style="">
|
||||
<h3 class="spec__header">
|
||||
<div class="spec__title">
|
||||
<p class="spec__suite">common/api</p>
|
||||
<em class="spec__descrip">post("foo") should create one item and return with HTTP code 201</em>
|
||||
</div>
|
||||
<div class="spec__status">Passed in 0.002s</div>
|
||||
</h3>
|
||||
</div>
|
||||
<div class="spec spec--pass" style="">
|
||||
<h3 class="spec__header">
|
||||
<div class="spec__title">
|
||||
<p class="spec__suite">common/api</p>
|
||||
<em class="spec__descrip">put("foo/2") should update one item and return with HTTP code 200</em>
|
||||
</div>
|
||||
<div class="spec__status">Passed in 0.001s</div>
|
||||
</h3>
|
||||
</div>
|
||||
<div class="spec spec--pass" style="">
|
||||
<h3 class="spec__header">
|
||||
<div class="spec__title">
|
||||
<p class="spec__suite">common/api</p>
|
||||
<em class="spec__descrip">delete("foo/2") should delete one item and return with HTTP code 204</em>
|
||||
</div>
|
||||
<div class="spec__status">Passed in 0.002s</div>
|
||||
</h3>
|
||||
</div>
|
||||
<div class="spec spec--pass" style="">
|
||||
<h3 class="spec__header">
|
||||
<div class="spec__title">
|
||||
<p class="spec__suite">common/form</p>
|
||||
<em class="spec__descrip">setDefinition</em>
|
||||
</div>
|
||||
<div class="spec__status">Passed in 0.001s</div>
|
||||
</h3>
|
||||
</div>
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue