From 1535d4cec0c256eee6f7c5def3d986b163b094c6 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Wed, 22 Jul 2020 10:43:05 +0400 Subject: [PATCH] added entity --- whatsapp-web.js/src/models/entity.js | 46 +++++++++++++++++++++++ whatsapp-web.js/test/test_entity.js | 55 ++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 whatsapp-web.js/src/models/entity.js create mode 100644 whatsapp-web.js/test/test_entity.js diff --git a/whatsapp-web.js/src/models/entity.js b/whatsapp-web.js/src/models/entity.js new file mode 100644 index 0000000..f6ac84c --- /dev/null +++ b/whatsapp-web.js/src/models/entity.js @@ -0,0 +1,46 @@ + + +class Entity { + + constructor(){ + this.data = new Map(); + this.count = 0; + } + + addData(key ,value){ + if(!this.isData(key)){ + this.data[key] = value; + return true; + } + return false; + } + + /** + * check if key exists + * @param {string} key + */ + isData(key){ + return this.data[key] != undefined ? true : false; + } + + deleteData(key){ + if(this.isData(key)){ + return delete this.data[key]; + } + return false; + } + + clearData(){ + this.data = new Map(); + this.count = 0; + } + + getData(key){ + if(this.isData(key)){ + return this.data[key]; + } + return null; + } +} + +module.exports.Entity = Entity; \ No newline at end of file diff --git a/whatsapp-web.js/test/test_entity.js b/whatsapp-web.js/test/test_entity.js new file mode 100644 index 0000000..f42eb2e --- /dev/null +++ b/whatsapp-web.js/test/test_entity.js @@ -0,0 +1,55 @@ +const expect = require('chai').expect; +const {data} = require('../src/models/data') +const {Entity} = require('../src/models/entity'); +const { Website } = require('../src/models/website'); + +describe('Entity', () => { + + it('Test : addData', () => { + let a = new Entity(); + a.addData('hello', new Website('hello')); + + expect(a.isData('hello')).to.true; + expect(a.count).to.equal(0); + + + }); + + it('Test : clearData()', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + + a.clearData(); + + expect(a.count).to.equal(0); + expect(a.isData("kruta")).to.false; + expect(a.isData("hello")).to.false; + expect(a.isData('pagol')).to.false; + expect(a.isData('hello world')).to.false; + }); + + it('Test: getData', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + + expect(a.getData('hello world').companyName).to.equal('hello world'); + expect(a.getData('gada').companyName).to.equal('gada'); + expect(a.getData('chu chu')).to.null; + + }) + + it('Test: deleteData', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + + a.deleteData('gada'); + expect(a.getData('gada')).to.null; + }) + +}) \ No newline at end of file