added hw3 tests

This commit is contained in:
Märt Kalmo
2017-04-11 14:39:11 +03:00
parent c014283ed4
commit b14bcba08a
4 changed files with 135 additions and 11 deletions

View File

@@ -10,11 +10,6 @@ if "%1" == "" (
exit /b 1
)
if "%TAG%" == "hw3" (
echo hw3 not implemented yet
exit /b 1
)
if "%TAG%" == "hw4" (
echo hw4 not implemented yet
exit /b 1
@@ -24,6 +19,10 @@ if "%TAG%" == "hw2" (
set SUB_DIR=ng1
)
if "%TAG%" == "hw3" (
set SUB_DIR=ng1
)
if "%SUB_DIR%" == "" (
echo unknown tag
exit /b 1

View File

@@ -12,12 +12,12 @@ if [ $# -eq 0 ];then
exit 1
fi
if [ $TAG == "hw3" ] || [ $TAG == "hw4" ]; then
if [ $TAG == "hw4" ]; then
echo 'not implemented yet'
exit 1
fi
if [ $TAG == "hw2" ]; then
if [ $TAG == "hw2" ] || [ $TAG == "hw3" ]; then
SUB_DIR="ng1"
fi

View File

@@ -1,9 +1,133 @@
'use strict';
describe('Application part 3', function () {
var BASE_URL = 'http://localhost:3000/';
it('should ...', function () {
fail('part 3 tests not implemented yet');
describe('Application part 3', function() {
it('should insert contact with name and phone number', done => {
var sampleData = getSampleData();
var data = { name : sampleData.name, phone : sampleData.phone };
post('api/contacts', data).then(() => {
return get('api/contacts');
}).then(contacts => {
expect(contacts.map(each => each.name)).toContain(sampleData.name);
expect(contacts.map(each => each.phone)).toContain(sampleData.phone);
done();
});
});
it('should modify contact', done => {
var name = getSampleData().name;
var nameChanged = name + 1;
post('api/contacts', { name : name }).then(() => {
return get('api/contacts');
}).then(contacts => {
var fetchedItem = contacts.filter(each => each.name === name).pop();
fetchedItem.name = nameChanged;
return put('api/contacts/' + fetchedItem.id, fetchedItem);
}).then(() => {
return get('api/contacts');
}).then(contacts => {
expect(contacts.map(each => each.name)).not.toContain(name);
expect(contacts.map(each => each.name)).toContain(nameChanged);
done();
});
});
it('should delete contact', done => {
var name = getSampleData().name;
post('api/contacts', { name : name }).then(() => {
return get('api/contacts');
}).then(contacts => {
var id = contacts.filter(each => each.name === name).pop()._id;
return del('api/contacts/' + id);
}).then(() => {
return get('api/contacts');
}).then(contacts => {
expect(contacts.map(each => each.name)).not.toContain(name);
done();
});
});
it('should delete multiple contacts', done => {
var name = 'Jack';
post('api/contacts', { name : name }).then(() => {
return post('api/contacts', { name : name });
}).then(() => {
return get('api/contacts');
}).then(contacts => {
var ids = contacts
.filter(each => each.name === name)
.map(each => each._id);
return post('api/contacts/delete', ids);
}).then(() => {
return get('api/contacts');
}).then(contacts => {
expect(contacts.map(each => each.name)).not.toContain(name);
done();
});
});
});
var request = require('request');
function get(url) {
return new Promise((resolve, reject) => {
request(getUrl(url), function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(body);
} else {
resolve(JSON.parse(body));
}
});
});
}
function del(url) {
return modifyCommon(url, 'DELETE', undefined);
}
function post(url, data) {
return modifyCommon(url, 'POST', data);
}
function put(url, data) {
return modifyCommon(url, 'PUT', data);
}
function modifyCommon(url, method, data) {
return new Promise((resolve, reject) => {
request({
headers: { 'Content-type': 'application/json' },
url : getUrl(url),
method: method,
body: JSON.stringify(data)
}, (error, response, body) => {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(body);
} else {
resolve();
}
});
});
}
function getSampleData() {
var time = new Date().getTime();
return {
name : time,
phone : time + 1
};
}
function getUrl(path) {
return BASE_URL.replace(/\/$/, '') + '/' + path;
}

View File

@@ -6,10 +6,11 @@
"postinstall": "npm run update",
"start": "webdriver-manager start",
"hw2tests": "protractor ./conf.js --suite=hw2",
"hw3tests": "protractor ./conf.js --suite=hw3",
"hw3tests": "jasmine-node --matchall hw3-spec.js",
"hw4tests": "protractor ./conf.js --suite=hw4"
},
"devDependencies": {
"jasmine-node": "^1.14.5",
"phantomjs": "^2.1.7",
"protractor": "^4.0.11",
"request": "^2.79.0"