Best JavaScript code snippet using cypress
ProjectAPI.test.js
Source:ProjectAPI.test.js
1import chai, { expect } from "chai"2import "@babel/polyfill"3import config from "../../../config/config"4import * as DBHelper from "../../../models/dbHelper"5import UserAPI from "../../../src/users/UserAPI"6import ProjectAPI from "../../../src/projects/ProjectAPI"7import MemoAPI from "../../../src/memos/MemoAPI"8let dbHelper;9let userDao;10let projectDao;11let projectUserDao;12let memoDao;13let shareKeyDao;14let user;15let userId;16describe("ProjectAPI Test", function () {17 before(async () => {18 dbHelper = new DBHelper.DbHelper();19 await dbHelper.createDB(config.test);20 await dbHelper.connect(config.test);21 dbHelper.init();22 await dbHelper.migrate();23 userDao = dbHelper.getUserDao();24 projectDao = dbHelper.getProjectDao();25 projectUserDao = dbHelper.getProjectUserDao();26 memoDao = dbHelper.getMemoDao();27 shareKeyDao = dbHelper.getShareKeyDao();28 userId = "bhw";29 });30 after(async () => {31 dbHelper.disconnect();32 });33 beforeEach(async () => {34 await dbHelper.migrate(true);35 user = await UserAPI.createUser(dbHelper,userId);36 });37 it("should create new project", async () => {38 //given39 const projectName = "testProject";40 //when41 const project = await ProjectAPI.createProject(dbHelper,user.userId,projectName);42 //then43 const selectedProject = await projectDao.findOne({44 raw: true,45 where: {46 name: projectName47 }48 });49 const selectedProjectUser = await projectUserDao.findOne({50 raw: true,51 where: {52 userId : user.userId,53 projectId : selectedProject.projectId54 }55 });56 expect(selectedProject.name).to.equal(projectName);57 expect(selectedProjectUser.projectId).to.equal(selectedProject.projectId);58 expect(selectedProjectUser.userId).to.equal(user.userId);59 });60 it("should add memo to project", async () => {61 //given62 const projectName = "testProject";63 const project = await ProjectAPI.createProject(dbHelper,user.userId,projectName);64 const memoList = [{ content: "memo1", url: "google.com" ,positionLeft : "10px", positionTop : "10px"}, { content: "memo2", url: "google.com" ,positionLeft : "10px", positionTop : "10px"}];65 await MemoAPI.saveMemoList(dbHelper,user.userId,memoList);66 let selectedMemoList = await memoDao.findAll({67 raw : true68 });69 const memoIdList = []70 for(let memo of selectedMemoList){71 memoIdList.push(memo.memoId);72 }73 //when74 await ProjectAPI.addMemoToProject(dbHelper,user.userId,project.projectId,memoIdList);75 //then76 const selectedProject = await projectDao.findOne({77 raw: true,78 where: {79 name: projectName80 }81 });82 selectedMemoList = await memoDao.findAll({83 raw : true84 });85 for(let memo of selectedMemoList){86 expect(memo.projectId).to.equal(selectedProject.projectId);87 }88 });89 it("should read memo group by project",async function(){90 //given 91 92 const url = "google.com";93 let memoList = [{ content: "this is memo", url: url ,positionLeft:"10px",positionTop:"10px"}];94 await MemoAPI.saveMemoList(dbHelper,user.userId, memoList);95 96 //when97 const allProjectList = await ProjectAPI.readProject(dbHelper,user.userId);98 const privateProject = allProjectList[0];99 100 //then101 expect(privateProject.name).to.equal("private project");102 expect(privateProject.Memos.length).to.equal(1);103 });104 it("should share project", async () => {105 //given106 const projectName = "testProject";107 const project = await ProjectAPI.createProject(dbHelper,user.userId,projectName);108 const memoList = [{ content: "memo1", url: "google.com" ,positionLeft : "10px", positionTop : "10px"}, { content: "memo2", url: "google.com" ,positionLeft : "10px", positionTop : "10px"}];109 await MemoAPI.saveMemoList(dbHelper,user.userId,memoList);110 let selectedMemoList = await memoDao.findAll({111 raw : true112 });113 const memoIdList = []114 for(let memo of selectedMemoList){115 memoIdList.push(memo.memoId);116 }117 await ProjectAPI.addMemoToProject(dbHelper,user.userId,project.projectId,memoIdList);118 let r = await ProjectAPI.createShareKey(dbHelper,user.userId,2);119 const shareKeyList = await shareKeyDao.findAll();120 const shareKey = shareKeyList[0];121 //when122 const otherUser = await UserAPI.createUser(dbHelper,"other");123 await ProjectAPI.addProjectToUser(dbHelper,otherUser.userId,shareKey.key);124 //then125 const selectedProjectUserList = await projectUserDao.findAll({126 raw : true,127 where : {128 userId : otherUser.userId129 }130 });131 expect(selectedProjectUserList.length).to.equal(2);132 });...
project.model.server.js
Source:project.model.server.js
1/*2* API for Project schema in mongoose db.3*4* */5module.exports = function (app, mongoose, logger) {6 var q = require('q');7 var ProjectSchema = require('./project.schema.server')(app, mongoose);8 var ProjectModel = mongoose.model('Project', ProjectSchema);9 var api = {10 createProject:createProject,11 findProjectById:findProjectById,12 findProjectForUser:findProjectForUser,13 updateProject:updateProject,14 deleteProject:deleteProject15 };16 return api;17 /*18 * createProject: Creates a new Project in mongo db.19 * params: userId, Project object created similar to ProjectSchema.20 * returns: promise.21 */22 function createProject(userId, project) {23 project.userId = userId;24 var deferred = q.defer();25 ProjectModel.create(project, function (err, dbProject) {26 if(err){27 logger.error('Unable to create project.' + err);28 deferred.reject(err);29 } else {30 deferred.resolve(dbProject);31 }32 });33 return deferred.promise;34 }35 /*36 * findProjectById : find project by project id.37 * params: projectId38 * returns: promise39 */40 function findProjectById(projectId) {41 var deferred = q.defer();42 ProjectModel.findById(projectId, function (err, dbProject) {43 if(err){44 logger.error('Unable to find project. Id: ' + projectId + "Error: " + err);45 deferred.reject(err);46 } else {47 deferred.resolve(dbProject);48 }49 });50 return deferred.promise;51 }52 /*53 * findProjectForUser: Finds list of projects for user.54 * params: userId55 * returns: promise56 */57 function findProjectForUser(userId) {58 var deferred = q.defer();59 ProjectModel.find({userId:userId}, function (err, dbProject) {60 if(err){61 logger.error("Can not find project for user " + userId + " Error: "+ err);62 deferred.reject(err);63 } else {64 deferred.resolve(dbProject);65 }66 });67 return deferred.promise;68 }69 /*70 * updateProject: updates the project.71 * params: projectId and project object with updated fields.72 * returns: promise.73 */74 function updateProject(projectId, project) {75 var deferred = q.defer();76 ProjectModel.update({_id:projectId},{$set:project}, function (err, dbProject) {77 if(err) {78 logger.error("Can not update project with id " + projectId + " Error: " + err);79 deferred.reject(err);80 }81 else {82 deferred.resolve(dbProject);83 }84 });85 return deferred.promise;86 }87 /*88 * deleteProject: deletes project from database.89 * params: projectId90 * returns: promise91 */92 function deleteProject(projectId) {93 var deferred = q.defer();94 ProjectModel.remove({_id:projectId}, function (err) {95 if(err) {96 logger.error("Can not delete project with id " + projectId + " Error: " + err);97 deferred.reject(err);98 }99 else {100 deferred.resolve(200);101 }102 });103 return deferred.promise;104 }...
test.js
Source:test.js
1const api = require('./api');2const test = async () => {3 try {4 const contract = await api.deploy();5 const projectId = await api.createProject(contract, api.getDeployerAccount(), 'testproject','description',400,'');6 console.log(projectId);7 console.log(api.getFundingStatus(contract, projectId));8 await Promise.all(api.getAccounts().map(9 account => api.invest(contract, account, projectId, 10002000)10 ));11 console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>");12 console.log(api.getFundingStatus(contract, projectId));13 console.log("<<<<<<<<<<<<<<<<<<<<");14 console.log(api.eth.getBalance(contract).toString());15 api.logBalances();16 await(api.refund(contract, projectId));17 console.log(api.getFundingStatus(contract, projectId));18 console.log(api.eth.getBalance(contract).toString());19 api.logBalances();20 console.log('done');21 } catch (err) {22 console.error(err);23 }24}25const test2 = async () => {26 try {27 const contract = await api.deploy();28 console.log(contract);29 const testCon = "0xa87c15cb158e0da1f71684f7af166d8791559040";30 const projectId = await api.createProject(contract, api.getDeployerAccount(), 'title','description',400,'2012.08.10');31 console.log(projectId," 1111");32 const projectId2 = await api.createProject(contract, api.getDeployerAccount(), 'title2','description2',2400,'2018.08.10');33 console.log(projectId2," 2222");34 console.log(api.getFundingStatus(contract, projectId));35 console.log(api.getFundingStatus(contract, projectId2));36 37 api.invest("0x61a92139c39ca9c54918ed188bc530d6ac00a75a", "0x3bc4fdbb79810818d1fa4eaf30af296b2c6d220f", 0, 102000);38 api.invest("0x61a92139c39ca9c54918ed188bc530d6ac00a75a", "0x3bc4fdbb79810818d1fa4eaf30af296b2c6d220f", 1, 220002000);39 // const output = await api.listAllProject(contract);40 //console.log(api.getCreatorAddress(contract,projectId)," hello");41 // const output2 = await api.creatorViewProject(contract,api.getCreatorAddress(contract,projectId));42 43 44 await api.viewInvestedProject("0x61a92139c39ca9c54918ed188bc530d6ac00a75a","0x3bc4fdbb79810818d1fa4eaf30af296b2c6d220f");45 //console.log(output3);46 } catch (err) {...
create.spec.js
Source:create.spec.js
1import createWrapper from '@/test/support/create_wrapper.js'2import form from '@/components/admin/projects/form'3describe('create', () => {4 const $api = {5 createProject: () => {}6 }7 const mocks = { $api }8 const successResponse = { data: { name: 'new-test-project', id: 1 } }9 context('successful creating', () => {10 let apiStub;11 beforeEach(() => {12 apiStub = sinon.stub(mocks.$api, 'createProject').returns(successResponse)13 });14 afterEach(() => {15 sinon.restore();16 });17 it('should call formSubmit method and pass callbacks', async () => {18 const wrapper = createWrapper(form, { mocks }, fakeStoreData())19 const successCallbackStub = sinon.stub(wrapper.vm, 'successCreatedCallback').returns("successCallback")20 const errorCallbackStub = sinon.stub(wrapper.vm, 'errorCallback').withArgs(wrapper.vm.$t("projects.was_not_created")).returns("errorCallback")21 const formSubmitStub = sinon.stub(wrapper.vm, "$formSubmit")22 await wrapper.vm.create()23 expect(formSubmitStub.calledOnce).to.be.true24 expect(formSubmitStub.args[0][1]).to.eq("successCallback")25 expect(formSubmitStub.args[0][2]).to.eq("errorCallback")26 });27 it('should call the api method', async () => {28 const wrapper = createWrapper(form, { mocks }, fakeStoreData())29 wrapper.setData({ form: {30 name: "test-example",31 regexp_of_grouping: "\ATT-\d+"32 } })33 await wrapper.vm.create()34 expect(apiStub.calledOnceWith({35 name: "test-example",36 regexp_of_grouping: "\ATT-\d+"37 })).to.be.true38 });39 });...
index.js
Source:index.js
1const express = require('express');2const app = express();3const PORT = 5000;4const bodyParser = require('body-parser');5const cors = require('cors');6const homepage = require('./routes/homePage');7const createproject = require('./routes/createProjectPage');8const issuePage = require('./routes/createIssue');9app.use(express.json());10app.use(cors());11app.use('/api/issuepage', issuePage);12app.use('/api/homepage', homepage);13app.use('/api/createproject', createproject);14app.listen(PORT, () => {15 console.log("server running");...
serve.test.js
Source:serve.test.js
1const axios = require('axios')2async function api(path, data) {3 try {4 return await axios.post('http://localhost:5070/', { path, data })5 } catch (e) {6 return e.response7 }8}9describe('serve', () => {10 it('should return success on empty app', async () => {11 try {12 const result = await api('createProject', {})13 expect(result.data).toBe('')14 expect(result.status).toBe(200)15 } catch (e) {16 console.log(`Serve tests needs server: node index.js`)17 }18 })...
createProject.js
Source:createProject.js
1import { CREATE_PROJECT } from 'CONSTANTS/routePaths';2const ROUTE = {3 exact: true,4 path: CREATE_PROJECT,5};6if( ON_SERVER ){7 ROUTE.handler = require('ROUTES/handlers/api/createProject').default;8}9export default {10 post: [ ROUTE ],...
service.js
Source:service.js
1import request from '@/utils/request';2export async function createproject(data) {3 return request('/server/api/createproject', {4 method: 'POST',5 data,6 });...
Using AI Code Generation
1const cypress = require('cypress')2const fs = require('fs')3 .createProject('cypress-test', {4 })5 .then((project) => {6 console.log(project)7 fs.writeFileSync('cypress.json', JSON.stringify(project.config))8 })9describe('My First Test', () => {10 it('Does not do much!', () => {11 expect(true).to.equal(true)12 })13})14describe('My First Test', () => {15 it('Does not do much!', () => {16 expect(true).to.equal(true)17 })18})
Using AI Code Generation
1const cypress = require('cypress');2const fs = require('fs');3const projectPath = '/path/to/project';4cypress.createProject(projectPath).then((project) => {5 fs.writeFileSync('cypress.json', JSON.stringify(project.config, null, 2));6});
Using AI Code Generation
1import { api } from 'cypress-api'2api.createProject('My Project').then((response) => {3})4api.createProject('My Project', 'my-project-id').then((response) => {5})6api.createProject('My Project', 'my-project-id').then((response) => {7})8api.createProject('My Project', 'my-project-id').then((response) => {9})10api.createProject('My Project', 'my-project-id').then((response) => {11})12api.createProject('My Project', 'my-project-id').then((response) => {13})14api.createProject('My Project', 'my-project-id').then((response) => {15})16api.createProject('My Project', 'my-project-id').then((response) => {17})18api.createProject('My Project', 'my-project-id').then((response) => {19})20api.createProject('My Project', 'my-project-id').then((response) => {21})22api.createProject('My Project', 'my-project-id').then((response) => {23})24api.createProject('My Project', 'my-project-id').then((response) => {25})26api.createProject('My Project', 'my-project-id').then((response) => {27})
Using AI Code Generation
1const { createProject } = require('@cypress/api')2const options = {3}4createProject(options).then((project) => {5 console.log('Created project', project)6})
Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.
You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.
Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.
Get 100 minutes of automation test minutes FREE!!