Best JavaScript code snippet using wpt
ideas.test.js
Source:ideas.test.js
...6describe('ideas', () => {7 describe('.get()', () => {8 it('should return model', async () => {9 // setup10 const db = createDB();11 await db.put({12 _id: '123',13 createdOn: '2018-03-21T12:03:40.000',14 value: 'test',15 posRel: {x: 100, y: 200}16 });17 // target18 const result = await ideaDB.get(db, '123');19 // check20 expect(result).to.be.instanceOf(Idea);21 expect(result.createdOn).to.equal('2018-03-21T12:03:40.000');22 expect(result.value).to.equal('test');23 expect(result.posRel).to.be.instanceOf(Point);24 expect(result.posRel).to.containSubset({x: 100, y: 200});25 });26 it('should fail if item does not exist', async () => {27 // setup28 const db = createDB();29 // target30 const promise = ideaDB.get(db, '123');31 // check32 await expect(promise).to.be.rejectedWith('missing');33 });34 });35 describe('.getAll()', () => {36 it('should return array of models', async () => {37 // setup38 const db = createDB();39 await db.put({40 _id: '123',41 createdOn: '2018-03-21T12:03:40.000',42 mindsetId: 'test id',43 value: 'test value'44 });45 // target46 const result = await ideaDB.getAll(db, 'test id');47 // check48 expect(result).to.have.length(1);49 expect(result[0]).to.be.instanceOf(Idea);50 expect(result[0].id).to.equal('123');51 expect(result[0].createdOn).to.equal('2018-03-21T12:03:40.000');52 expect(result[0].value).to.equal('test value');53 });54 it('should return ideas of certain mindset', async () => {55 // setup56 const db = createDB();57 await db.put({58 _id: '1',59 mindsetId: 'test id'60 });61 await db.put({62 _id: '2',63 mindsetId: 'another id'64 });65 // target66 const result = await ideaDB.getAll(db, 'test id');67 // check68 expect(result).to.have.length(1);69 expect(result[0].id).to.equal('1');70 });71 });72 describe('.add()', () => {73 it('should add item to DB', async () => {74 // setup75 const db = createDB();76 const idea = new Idea({77 createdOn: '2018-03-21T12:03:40.000',78 mindsetId: 'mindset id',79 value: 'test value',80 posRel: {x: 10, y: 20}81 });82 // target83 await ideaDB.add(db, idea);84 // check85 const result = (await db.allDocs({include_docs: true})).rows.map(86 r => r.doc87 );88 expect(result).to.have.length(1);89 expect(result[0]._id).to.equal(idea.id);90 expect(result[0].createdOn).to.equal('2018-03-21T12:03:40.000');91 expect(result[0].mindsetId).to.equal('mindset id');92 expect(result[0].value).to.equal('test value');93 expect(result[0].posRel.constructor).to.equal(Object);94 expect(result[0].posRel).to.deep.equal({x: 10, y: 20});95 });96 it('should add/get same item', async () => {97 // setup98 const db = createDB();99 const idea = new Idea({100 mindsetId: 'mindset id',101 value: 'test',102 posRel: {x: 10, y: 20}103 });104 // target105 await ideaDB.add(db, idea);106 const result = await ideaDB.get(db, idea.id);107 // check108 expect(result).to.deep.equal(idea);109 });110 it('should fail on duplicates', async () => {111 // setup112 const db = createDB();113 db.put({_id: '123'});114 const idea = new Idea({115 id: '123',116 mindsetId: 'mindset id',117 posRel: {x: 10, y: 20}118 });119 // target120 const promise = ideaDB.add(db, idea);121 await expect(promise).to.be.rejectedWith('Document update conflict');122 });123 it('should fail if parent mindset ID is empty', async () => {124 // setup125 const db = createDB();126 const idea = new Idea({127 id: '123',128 posRel: {x: 10, y: 20}129 });130 // target131 const promise = ideaDB.add(db, idea);132 await expect(promise).to.be.rejectedWith(133 `Failed to add idea '123' with empty parent mindset ID`134 );135 });136 });137 describe('.update()', () => {138 it('should update item', async () => {139 // setup140 const db = createDB();141 db.post({142 _id: '123',143 value: 'test 1',144 posRel: {x: 0, y: 0}145 });146 const update = {147 id: '123',148 value: 'test 2',149 posRel: {x: 0, y: 10}150 };151 // target152 await ideaDB.update(db, update);153 // check154 const result = await db.get('123');155 expect(result.value).to.equal('test 2');156 expect(result.posRel).to.deep.equal({x: 0, y: 10});157 });158 it('should not store unknown props', async () => {159 // setup160 const db = createDB();161 db.post({_id: 'i'});162 const idea = {163 id: 'i',164 pos: {x: 0, y: 0},165 X: 'unknown'166 };167 // target168 await ideaDB.update(db, idea);169 // check170 const result = await db.get('i');171 expect(result.X).to.not.exist;172 });173 it('should NOT call db if update object is empty', async () => {174 // setup175 const db = createDB();176 const get = spy(db.get);177 const put = spy(db.put);178 db.post({_id: 'i'});179 const idea = {180 id: 'i'181 };182 // target183 await ideaDB.update(db, idea);184 // check185 expect(get.called).to.be.false;186 expect(put.called).to.be.false;187 });188 it('should fail if item does not exist', async () => {189 // setup190 const db = createDB();191 const idea = new Idea({value: 'val'});192 // target193 const promise = ideaDB.update(db, idea);194 // check195 await expect(promise).to.be.rejectedWith('missing');196 });197 it('should fail if parent mindset ID is empty', async () => {198 // setup199 const db = createDB();200 db.post({201 _id: 'i',202 mindsetId: '1'203 });204 const patch = {205 id: 'i',206 value: 'test value',207 mindsetId: null208 };209 // target210 const promise = ideaDB.update(db, patch);211 // check212 await expect(promise).to.be.rejectedWith(213 `Failed to update idea 'i' with empty parent mindset ID`214 );215 });216 });217 describe('.remove()', () => {218 it('should remove item', async () => {219 // setup220 const db = createDB();221 await db.put({_id: 'die'});222 await db.put({_id: 'live'});223 // target224 await ideaDB.remove(db, 'die');225 // check226 const result = await db.allDocs({include_docs: true});227 expect(result.rows).to.have.length(1);228 expect(result.rows[0].id).to.equal('live');229 });230 it('should fail if item does not exist', async () => {231 // setup232 const db = createDB();233 // target234 const promise = ideaDB.remove(db, 'die');235 // check236 await expect(promise).to.be.rejectedWith('missing');237 });238 });239 describe('.removeAll()', () => {240 it('should remove all items', async () => {241 // setup242 const db = createDB();243 await db.post({value: '1'});244 await db.post({value: '2'});245 await db.post({value: '3'});246 // target247 await ideaDB.removeAll(db);248 // check249 const result = await db.allDocs();250 expect(result.rows).to.be.empty;251 });252 });...
create-db.spec.js
Source:create-db.spec.js
...9describe('createDB', function (){10 describe('The arguments', function (){11 it('Should reject if no configuration object is passed', function (done){12 var spy = sinon.spy();13 createDB()14 .catch(spy)15 .then(function(){16 assert(spy.calledOnce, 'createDB promise was rejected');17 done();18 })19 .catch(console.error);20 });21 it('Should reject if no name is passed', function (done){22 var spy = sinon.spy();23 createDB({})24 .catch(spy)25 .then(function(){26 assert(spy.calledOnce, 'createDB promise was rejected');27 done();28 })29 .catch(console.error);30 });31 it('Should reject if no version is passed', function (done){32 var spy = sinon.spy();33 createDB({ name: getDBName() })34 .catch(spy)35 .then(function(){36 assert(spy.calledOnce, 'createDB promise was rejected');37 done();38 })39 .catch(console.error);40 });41 });42 describe('Database resolution', function (){43 it('Should resolve with a valid indexDB instance', function (done){44 var dbName = getDBName();45 createDB({ name: dbName, version: 1})46 .then( function (db){47 assert(db, 'DB is truthy');48 assert(db instanceof IDBDatabase, 'returned DB is an instanceof IDBDatabase');49 cleanDB(db);50 done();51 })52 .catch(console.error);53 });54 it('Should resolve with the same db if called twice', function (done){55 var dbName = getDBName();56 createDB({ name: dbName, version: 1})57 .then(function(db){58 return [db, createDB({ name: dbName, version: 1})];59 })60 .spread(function(db1, db2){61 assert(db1, 'first DB returned okay');62 assert(db2, 'second DB returned okay');63 assert.equal(db1.name, db2.name, 'both returned DBs are the same');64 cleanDB(db1, db2);65 done();66 })67 .catch(console.error);68 });69 it('Should resolve with different db\'s if called twice with different params', function (done){70 var dbName = getDBName();71 var dbName2 = getDBName();72 createDB({ name: dbName, version: 1})73 .then(function(db){74 return [db, createDB({ name: dbName2, version: 1})];75 })76 .spread(function(db1, db2){77 assert(db1, 'first DB returned okay');78 assert(db2, 'second DB returned okay');79 assert.notEqual(db1.name, db2.name, 'both DBs returned are different');80 cleanDB(db1, db2);81 done();82 })83 .catch(console.error);84 });85 });86 describe('Creating object stores', function (){87 it('Should create object stores when passed within the configuration', function (done){88 var dbName = getDBName();89 createDB({ name: dbName, version: 1, objects: [{ name: 'test'}] })90 .then(function(db){91 assert(db, 'DB returned okay');92 var resultObj = db.objectStoreNames.contains('test');93 var resultLength = db.objectStoreNames.length;94 assert.equal(resultLength, 1, 'only one object store created');95 assert(resultObj, 'returned DB has the correct object stores');96 cleanDB(db);97 done();98 })99 .catch(console.error);100 });101 it('Should not throw errors when creating the same object stores on different versions', function (done){102 var dbName = getDBName();103 var spy = sinon.spy();104 createDB({ name: dbName, version: 1, objects: [ { name: 'test' } ]})105 .then(function(db){106 db.close();107 return [db, createDB({ name: dbName, version: 2, objects: [ { name: 'test' } ]})];108 })109 .catch(spy)110 .spread(function(db1, db2){111 assert(db1, 'got old db back okay');112 assert(db2, 'got new db back okay');113 assert.equal(spy.callCount, 0, 'error was not thrown');114 cleanDB(db1, db2);115 done();116 })117 .catch(console.error);118 });119 });120 describe('Creating indexes', function (){121 it('Should create indexes on the DB if specified', function (done){122 var dbName = getDBName();123 createDB({ name: dbName, version: 1, objects: [{ name: 'obj', indexes: [ {name: 'id', unique: true} ] }] })124 .then(function(db){125 assert(db, 'got the db okay');126 var objStore = db.transaction(['obj'], 'readwrite').objectStore('obj');127 var hasIndex = objStore.indexNames.contains('id');128 assert(hasIndex, 'created index successfully');129 cleanDB(db);130 done();131 })132 .catch(console.error);133 });134 });...
index.js
Source:index.js
1'use strict'2const Datastore = require('nedb-promise')3const join = require('path').join4const userDirectory = join(process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'], '.mocker/v1/db')5function createDatabase (dir, name) {6 return new Datastore({ filename: join(dir, name), autoload: true })7}8module.exports = {9 init: function init (option) {10 let dir = (option.source || {}).location || userDirectory11 let createDb = createDatabase.bind(null, dir)12 // api åºç¡æ°æ®13 const apiBase = createDb('/apiBase')14 // mockç¨äºæ¾ç¤ºæ°æ®15 const apiModel = createDb('/apiModel')16 // åºç¡é
ç½®17 const appBase = createDb('/appBase')18 // 项ç®ä¿¡æ¯19 const project = createDb('/project')20 const Lib = createDb('/lib')21 this.dbs = {22 apiBase,23 apiModel,24 appBase,25 project,26 Lib,27 }28 return this29 },...
Using AI Code Generation
1var wpt = require('wpt');2wpt.createDB('testdb', function(err, data) {3 if (err) {4 console.log(err);5 }6 else {7 console.log(data);8 }9});10module.exports.createDB = function(db, callback) {11 var request = require('request');12 request.put(url, function(err, res, body) {13 if (err) {14 callback(err);15 }16 else {17 callback(null, body);18 }19 });20};21function foo(func) {22 func();23}24function bar() {25 console.log(this);26}27foo(bar);28I'm trying to load a JSON file in node.js, but I'm getting an error saying "SyntaxError: Unexpected token < in JSON at position 0". I've tried to use JSON.parse() but it doesn't work. How can I fix this?29I am trying to get a list of files in a directory. I am using fs.readdir() to get the list of files in the directory. But I am getting an error "Error: EISDIR: illegal operation on a directory, read". How can I fix this?30I am trying to get a list of files in a directory. I am using fs.readdir() to get the list of files in the directory. But I am getting an error "Error: EISDIR: illegal operation on a directory, read". How can I fix this?31I am trying to get a list of files in a directory. I am using fs.readdir() to get the list of files in the directory. But I am getting an error "Error: EISDIR: illegal operation on a directory, read". How can I fix this?
Using AI Code Generation
1var wpta = require('./wpta');2wpta.createDB();3wpta.createTable();4wpta.insertData();5exports.createDB = function() {6}7exports.createTable = function() {8}9exports.insertData = function() {10}11exports.createDB = function() {12}13exports.createTable = function() {14}15exports.insertData = function() {16}17module.exports.createDB = function() {18}19module.exports.createTable = function() {20}21module.exports.insertData = function() {22}23module.exports = function() {24}25exports = function() {26}
Using AI Code Generation
1var wpt = require('wpt');2wpt.createDB('test', function(err, result){3 if(err){4 console.log('Error: ' + err);5 } else {6 console.log('Result: ' + result);7 }8});9var wpt = require('wpt');10wpt.createDB('test', function(err, result){11 if(err){12 console.log('Error: ' + err);13 } else {14 console.log('Result: ' + result);15 }16});17var wpt = require('wpt');18wpt.getDBs(function(err, result){19 if(err){20 console.log('Error: ' + err);21 } else {22 console.log('Result: ' + result);23 }24});25var wpt = require('wpt');26wpt.getDBs(function(err, result){27 if(err){28 console.log('Error: ' + err);29 } else {30 console.log('Result: ' + result);31 }32});33var wpt = require('w
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.createDB('testDB');3exports.createDB = function(dbName){4 var sqlite3 = require('sqlite3').verbose();5 var db = new sqlite3.Database(dbName);6 db.close();7}8var db = new sqlite3.Database('test.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, function(err) {9 if (err) {10 console.error(err.message);11 }12 console.log('Connected to the test database.');13});14db.serialize(function() {15 db.run('PRAGMA journal_mode = WAL');16});17var db = new sqlite3.Database('test.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, function(err) {18 if (err) {19 console.error(err.message);20 }21 console.log('Connected to the test database.');22});23db.serialize(function() {24 db.run('PRAGMA journal_mode = WAL');25 db.run('PRAGMA synchronous = NORMAL');26});27var db = new sqlite3.Database('test.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, function(err) {28 if (err) {29 console.error(err.message);30 }31 console.log('Connected to the test database.');32});33db.serialize(function() {34 db.run('PRAGMA journal_mode = WAL');35 db.run('PRAGMA synchronous = NORMAL');36 db.run('PRAGMA busy_timeout = 5000');37});38var db = new sqlite3.Database('test.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, function(err) {39 if (err) {40 console.error(err.message);41 }42 console.log('Connected to the test database.');43});44db.serialize(function() {45 db.run('PRAGMA journal_mode = WAL');46 db.run('PRAGMA
Using AI Code Generation
1var wptoolkit = require('wptoolkit.js');2wptoolkit.createDB('test', 'test', 'test', 'test');3var wptoolkit = require('wptoolkit.js');4wptoolkit.createDB('test', 'test', 'test', 'test');5var wptoolkit = require('wptoolkit.js');6wptoolkit.createDB('test', 'test', 'test', 'test');7var wptoolkit = require('wptoolkit.js');8wptoolkit.createDB('test', 'test', 'test', 'test');9var wptoolkit = require('wptoolkit.js');10wptoolkit.createDB('test', 'test', 'test', 'test');11var wptoolkit = require('wptoolkit.js');12wptoolkit.createDB('test', 'test', 'test', 'test');13var wptoolkit = require('wptoolkit.js');14wptoolkit.createDB('test', 'test', 'test', 'test');15var wptoolkit = require('wptoolkit.js');16wptoolkit.createDB('test', 'test', 'test', 'test');17var wptoolkit = require('wptoolkit.js');18wptoolkit.createDB('test', 'test', 'test', 'test');19var wptoolkit = require('wptoolkit.js');20wptoolkit.createDB('test', 'test', '
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!