diff --git a/checkout.bat b/checkout.bat index 504cef6..2e36e94 100644 --- a/checkout.bat +++ b/checkout.bat @@ -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 diff --git a/checkout.sh b/checkout.sh index 8524c3f..c8e703c 100755 --- a/checkout.sh +++ b/checkout.sh @@ -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 diff --git a/hw3-spec.js b/hw3-spec.js index 4a6a7b4..7b7199c 100644 --- a/hw3-spec.js +++ b/hw3-spec.js @@ -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; +} diff --git a/package.json b/package.json index 1c6bfe9..d230bac 100644 --- a/package.json +++ b/package.json @@ -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"