Best JavaScript code snippet using qawolf
userRoute.js
Source:userRoute.js
1const express = require('express');2const Invite = require('../models/Invite');3const User = require('../models/User');4const { Roles } = require('../models/Roles');5const { Votes } = require('../models/Votes');6const AngelsProfile = require('../models/AngelsProfile');7const Meetings = require('../models/Meetings');8const StartupsProfile = require('../models/StartupsProfile');9const middleware = require('../middleware/middleware');10const StartupsInvite = require('../models/StartupsInvite');11const axios = require('axios');12const checkJwt = middleware.checkJwt;13const addRoleToUser = middleware.addRoleToUser;14const router = express.Router();15// Roles for Users16const UserRoles = {17 ADMIN: 'ADMIN',18 ANGEL: 'ANGEL',19 STARTUP: 'STARTUP',20};21/**22 * Get community members profile based on the id given23 */24router.get('/getAngel/profile/:id', checkJwt, async (req, res) => {25 try {26 let profile = await AngelsProfile.findOne({ authId: req.params.id });27 return res.json(profile);28 } catch (err) {29 console.error(err);30 return res.status(500).json({ msg: 'Server Error.' });31 }32});33/**34 * Get community member based on the id given35 */36router.get('/getAngel/:id', checkJwt, async (req, res) => {37 try {38 let profile = await User.findOne({ authId: req.params.id });39 return res.json(profile);40 } catch (err) {41 console.error(err);42 return res.status(500).json({ msg: 'Server Error.' });43 }44});45/**46 * get all community members available47 */48router.get('/getAngels', checkJwt, async (req, res) => {49 try {50 const users = await Roles.find({});51 const userRoles = [];52 users.forEach(user => {53 if (user.roles.ANGEL) {54 userRoles.push(user);55 }56 });57 const userIds = userRoles.map(({ authId }) => {58 return authId;59 });60 if (userIds.length < 1) {61 return res.status(200).json([]);62 }63 const usersQuery = await User.find({});64 const userArr = [];65 usersQuery.forEach(user => {66 userIds.map(id => {67 if (user.authId === id) {68 userArr.push(user);69 }70 });71 });72 return res.status(200).json(userArr);73 } catch (err) {74 console.error(err);75 return res.status(500).json({ msg: 'Server Error.' });76 }77});78/**79 * Consume invite as a user if code is valid add community member role to user80 */81router.post('/consumeInvite', checkJwt, async (req, res) => {82 try {83 const { invite } = req.body;84 let code = await Invite.findOne({ value: invite });85 if (!code) {86 throw new Error('Invite is not valid.');87 }88 if (code.consumed) {89 throw new Error('Code has been consumed.');90 }91 await addRoleToUser(req.user.sub, UserRoles.ANGEL);92 let updateInvite = {};93 updateInvite = {};94 updateInvite.consumed = true;95 updateInvite.consumedBy = req.user.sub;96 //update97 code = await Invite.findOneAndUpdate(98 { value: invite },99 { $set: updateInvite },100 { new: true },101 );102 return res.status(200).json(code);103 } catch (err) {104 console.error(err);105 return res.status(500).json({ msg: 'Server Error.' });106 }107});108/**109 * Can the user vote on the meeting in question110 */111router.post('/canvote', checkJwt, async (req, res) => {112 try {113 const meetingDate = new Date(req.body.date);114 const current = new Date();115 const bool = current.getTime() > meetingDate.getTime();116 return res.status(200).json(bool);117 } catch (err) {118 console.error(err);119 return res.status(500).json({ msg: 'Server Error.' });120 }121});122/**123 * Get all meetings created new and old.124 */125router.get('/getmeetings', checkJwt, async (req, res) => {126 try {127 const users = await Meetings.find({});128 const meetingsInfo = [];129 users.forEach(user => {130 meetingsInfo.push(user);131 });132 return res.status(200).json(meetingsInfo);133 } catch (err) {134 console.error(err);135 return res.status(500).json(err);136 }137});138/**139 * Get certain meeting based on that meeting id140 */141router.get('/getmeeting/:id', checkJwt, async (req, res) => {142 try {143 let meeting = await Meetings.findOne({ meetingId: req.params.id });144 return res.json(meeting);145 } catch (err) {146 console.error(err);147 return res.status(500).json(err);148 }149});150/**151 * Check if user has voted on the meeting in question152 */153router.get('/hasvoted/:id', checkJwt, async (req, res) => {154 try {155 let voted = await Votes.findOne({156 meetingId: req.params.id,157 authId: req.user.sub,158 });159 return res.status(200).json(voted);160 } catch (err) {161 console.error(err);162 return res.status(500).json({ msg: err });163 }164});165/**166 * Vote on meeting for the user logged in167 */168router.post('/voteonmeeting/:id', checkJwt, async (req, res) => {169 try {170 let vote = await Votes.findOne({171 meetingId: req.params.id,172 authId: req.user.sub,173 });174 if (vote) {175 throw new Error('Already voted.');176 }177 vote = new Votes({178 meetingId: req.params.id,179 authId: req.user.sub,180 votes: req.body.votes,181 });182 await vote.save();183 return res.status(200).json(vote);184 } catch (err) {185 console.error(err);186 return res.status(500).json({ msg: err });187 }188});189/**190 * Consume invite as a user if code is valid add community member role to user191 */192router.post('/consumeInviteAsAStartup', checkJwt, async (req, res) => {193 try {194 const { invite } = req.body;195 const userInfo = await axios('https://dev-5f25to47.auth0.com/userInfo', {196 headers: {197 Authorization: req.headers.authorization,198 },199 });200 const { name, sub, picture } = userInfo.data;201 let code = await StartupsInvite.findOne({ value: invite });202 if (!code) {203 throw new Error('Invite is not valid.');204 }205 if (code.consumed) {206 throw new Error('Code has been consumed.');207 }208 const { email, companyName, website } = code;209 let user = await User.findOne({ email });210 let allRoles = await Roles.findOne({ authId: sub });211 if (user && allRoles) {212 return res.status(200).json(user);213 }214 allRoles = new Roles({215 authId: sub,216 roles: {217 ADMIN: false,218 ANGEL: false,219 STARTUP: true,220 },221 });222 user = new User({223 authId: sub,224 profileImg: picture,225 email,226 name,227 });228 await allRoles.save();229 await user.save();230 // create startup profile231 //build profile object232 let profileFields = {};233 profileFields.user = sub;234 profileFields = {};235 if (sub) profileFields.authId = sub;236 if (companyName) profileFields.companyName = companyName;237 if (website) profileFields.website = website;238 profileFields.completed = true;239 let profile = await StartupsProfile.findOne({ authId: sub });240 if (profile) {241 //update242 profile = await StartupsProfile.findOneAndUpdate(243 { authId: sub },244 { $set: profileFields },245 { new: true },246 );247 } else {248 profile = new StartupsProfile(profileFields);249 await profile.save();250 }251 let updateInvite = {};252 updateInvite = {};253 updateInvite.consumed = true;254 updateInvite.consumedBy = req.user.sub;255 //update256 code = await Invite.findOneAndUpdate(257 { value: invite },258 { $set: updateInvite },259 { new: true },260 );261 return res.status(200).json(user);262 } catch (err) {263 console.error(err);264 return res.status(500).json({ msg: 'Server Error.' });265 }266});...
testExecutorsUpdateInvite.js
Source:testExecutorsUpdateInvite.js
1'use strict';2const initSteps = require('app/core/initSteps');3const {expect, assert} = require('chai');4const rewire = require('rewire');5const UpdateInvite = rewire('app/steps/ui/executors/updateinvite');6const co = require('co');7describe('Update-Invite', () => {8 let steps;9 let section;10 let templatePath;11 let i18next;12 let schema;13 let req;14 let ctx;15 let errors;16 beforeEach(() => {17 steps = initSteps([`${__dirname}/../../../app/steps/action/`, `${__dirname}/../../../app/steps/ui`]);18 section = 'executors';19 templatePath = 'executors/updateinvite';20 i18next = {};21 schema = {22 $schema: 'http://json-schema.org/draft-07/schema',23 properties: {}24 };25 req = {26 session: {27 form: {28 deceased: {29 firstName: 'Dee',30 lastName: 'Ceased'31 },32 executors: {33 executorsNumber: 3,34 invitesSent: true35 },36 ccdCase: {37 id: '1234-1234-1234-1234'38 }39 }40 }41 };42 });43 describe('getUrl()', () => {44 it('test correct url is returned from getUrl function', () => {45 const updateInvite = new UpdateInvite(steps, section, templatePath, i18next, schema);46 assert.equal(updateInvite.constructor.getUrl(), '/executors-update-invite');47 });48 });49 describe('getContextData()', () => {50 it('test ctx variables are correctly assigned when there are more than one executor with new email', (done) => {51 const updateInvite = new UpdateInvite(steps, section, templatePath, i18next, schema);52 req.session.form.executors.list = [53 {fullName: 'john', isApplying: true, isApplicant: true},54 {fullName: 'other applicant', isApplying: true, emailChanged: true},55 {fullName: 'harvey', isApplying: true, emailChanged: true}];56 ctx = updateInvite.getContextData(req);57 expect(ctx.executorsEmailChangedList).to.deep.equal([58 {59 emailChanged: true,60 fullName: 'other applicant',61 isApplying: true62 },63 {64 emailChanged: true,65 fullName: 'harvey',66 isApplying: true67 }68 ]);69 expect(ctx.inviteSuffix).to.deep.equal('-multiple');70 done();71 });72 it('test ctx variables are correctly assigned when there is only one executor with new email', (done) => {73 const updateInvite = new UpdateInvite(steps, section, templatePath, i18next, schema);74 req.session.form.executors.list = [75 {fullName: 'john', isApplying: true, isApplicant: true},76 {fullName: 'other applicant', isApplying: true, emailChanged: true},77 {fullName: 'harvey', isApplying: true}];78 ctx = updateInvite.getContextData(req);79 expect(ctx.executorsEmailChangedList).to.deep.equal([80 {81 emailChanged: true,82 fullName: 'other applicant',83 isApplying: true84 }85 ]);86 expect(ctx.inviteSuffix).to.deep.equal('');87 done();88 });89 });90 describe('handlePost()', () => {91 it('test that the emailChanged flag has been deleted', (done) => {92 const restoreInviteLink = UpdateInvite.__set__('InviteLink', class {93 post() {94 return Promise.resolve({95 invitations: [{96 inviteId: '5678',97 email: 'test@test.com',98 executorName: 'other applicant'99 }]100 });101 }102 });103 const updateInvite = new UpdateInvite(steps, section, templatePath, i18next, schema);104 ctx = {};105 errors = {};106 req.session.form.executors.list = [107 {fullName: 'john', isApplying: true, isApplicant: true},108 {fullName: 'other applicant', isApplying: true, emailChanged: true, inviteId: '5678', email: 'test@test.com'}109 ];110 const expectedExecutorList = [111 {fullName: 'john', isApplying: true, isApplicant: true, id: 0},112 {fullName: 'other applicant', isApplying: true, inviteId: '5678', email: 'test@test.com', id: 1}];113 co(function* () {114 [ctx, errors] = yield updateInvite.handlePost(ctx, errors, req.session.form);115 restoreInviteLink();116 console.log(req.session.form.executors.list);117 expect(req.session.form.executors.list).to.deep.equal(expectedExecutorList);118 done();119 }).catch(err => {120 done(err);121 });122 });123 });124 describe('action()', () => {125 it('test that context variables are removed and empty object returned', () => {126 const updateInvite = new UpdateInvite(steps, section, templatePath, i18next, schema);127 let formdata = {};128 let ctx = {129 executorsEmailChanged: true,130 executorsEmailChangedList: true,131 inviteSuffix: '-multiple'132 };133 [ctx, formdata] = updateInvite.action(ctx, formdata);134 expect(ctx).to.deep.equal({});135 });136 it('test that context variables are removed and object contains only appropriate variables', () => {137 const updateInvite = new UpdateInvite(steps, section, templatePath, i18next, schema);138 let formdata = {};139 let ctx = {140 executorsEmailChanged: true,141 executorsEmailChangedList: true,142 inviteSuffix: '-multiple',143 invitesSent: 'true'144 };145 [ctx, formdata] = updateInvite.action(ctx, formdata);146 expect(ctx).to.deep.equal({invitesSent: 'true'});147 });148 });...
Main.js
Source:Main.js
1import { Presence } from 'phoenix';2import { camelizeKeys } from 'humps';3import socket from '../../socket';4import { actions } from '../slices';5const channel = socket.channel('main');6const presence = new Presence(channel);7const listBy = (id, { metas: [first, ...rest] }) => {8 first.count = rest.length + 1;9 first.id = Number(id);10 return first;11};12const camelizeKeysAndDispatch = (dispatch, actionCreator) => data => (13 dispatch(actionCreator(camelizeKeys(data)))14);15export const init = () => dispatch => {16 presence.onSync(() => {17 const list = presence.list(listBy);18 camelizeKeysAndDispatch(dispatch, actions.syncPresenceList)(list);19 });20 const onJoinSuccess = () => {21 channel.on('invites:init', camelizeKeysAndDispatch(dispatch, actions.setInvites));22 channel.on('invites:created', camelizeKeysAndDispatch(dispatch, actions.addInvite));23 channel.on('invites:canceled', camelizeKeysAndDispatch(dispatch, actions.updateInvite));24 channel.on('invites:applied', data => {25 window.location.href = `/games/${data.invite.game_id}`;26 camelizeKeysAndDispatch(dispatch, actions.updateInvite)(data);27 });28 channel.on('invites:expired', camelizeKeysAndDispatch(dispatch, actions.updateInvite));29 channel.on('invites:dropped', camelizeKeysAndDispatch(dispatch, actions.updateInvite));30 };31 channel32 .join()33 .receive('ok', onJoinSuccess);34};35export const createInvite = params => dispatch => channel36 .push('invites:create', params)37 .receive('ok', camelizeKeysAndDispatch(dispatch, actions.addInvite));38export const acceptInvite = id => dispatch => channel39 .push('invites:accept', { id })40 .receive('ok', data => {41 window.location.href = `/games/${data.invite.game_id}`;42 camelizeKeysAndDispatch(dispatch, actions.updateInvite)(data);43 });44export const declineInvite = id => dispatch => channel45 .push('invites:cancel', { id })46 .receive('ok', camelizeKeysAndDispatch(dispatch, actions.updateInvite));47export const cancelInvite = id => dispatch => channel48 .push('invites:cancel', { id })...
Using AI Code Generation
1const { updateInvite } = require("qawolf");2const { chromium } = require("playwright");3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 await page.click("text=Sign in");8 await page.fill("input[name=\"identifier\"]", "test");9 await page.press("input[name=\"identifier\"]", "Enter");10 await page.fill("input[name=\"password\"]", "test");11 await page.press("input[name=\"password\"]", "Enter");12 await updateInvite({13 });14 await browser.close();15})();16const { updateInvite } = require("qawolf");17const { chromium } = require("playwright");18(async () => {19 const browser = await chromium.launch();20 const context = await browser.newContext();21 const page = await context.newPage();22 await page.click("text=Sign in");23 await page.fill("input[name=\"identifier\"]", "test");24 await page.press("input[name=\"identifier\"]", "Enter");25 await page.fill("input[name=\"password\"]", "test");26 await page.press("input[name=\"password\"]", "Enter");27 await updateInvite({28 });29 await browser.close();30})();31const { updateInvite } = require("qawolf");32const { chromium } = require("playwright");33(async () => {34 const browser = await chromium.launch();35 const context = await browser.newContext();36 const page = await context.newPage();37 await page.click("text=Sign in");38 await page.fill("input[name=\"identifier\"]", "test");39 await page.press("input[name=\"identifier\"]", "Enter");40 await page.fill("input[name=\"password\"]", "test");41 await page.press("input[name=\"password\"]", "Enter");42 await updateInvite({43 });44 await browser.close();45})();
Using AI Code Generation
1const qawolf = require("qawolf");2const { chromium } = require("playwright");3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 await qawolf.register(page, "test");8 await qawolf.updateInvite(page, "test", {9 });10 await browser.close();11})();12const qawolf = require("qawolf");13describe("My Test", () => {14 let browser;15 let page;16 beforeAll(async () => {17 browser = await qawolf.launch();18 page = await qawolf.createPage(browser);19 });20 afterAll(async () => {21 await qawolf.updateInvite(page, "test", {22 });23 await qawolf.stopVideos();24 await browser.close();25 });26 it("should pass", async () => {27 await qawolf.track(page, "test");28 });29});30const qawolf = require("qawolf");31describe("My Test", () => {32 let browser;33 let page;34 beforeAll(async () => {35 browser = await qawolf.launch();36 });37 afterAll(async () => {38 await qawolf.updateInvite(page, "test", {39 });40 await qawolf.stopVideos();41 await browser.close();42 });43 it("should pass", async () => {44 await qawolf.track(page, "test");45 });46});
Using AI Code Generation
1const { updateInvite } = require("qawolf");2const { create } = require("qawolf");3const { launch } = require("qawolf");4const { close } = require("qawolf");5const { type } = require("qawolf");6const { click } = require("qawolf");7const { select } = require("qawolf");8const { check } = require("qawolf");9const { uncheck } = require("qawolf");10const { hover } = require("qawolf");11const { scroll } = require("qawolf");12const { screenshot } = require("qawolf");13const { press } = require("qawolf");14const { drag } = require("qawolf");15const { drop } = require("qawolf");16const { upload } = require("qawolf");17const { waitFor } = require("qawolf");18const { waitForSelector } = require("qawolf");19const { waitForText } = require("qawolf");20const { waitForNotFound } = require("qawolf");21const { waitForFunction } = require("qawolf");22const { waitForNavigation } = require("qawolf");23const { waitForURL } = require("
Using AI Code Generation
1const { createInvite } = require("qawolf");2const test = async () => {3 const invite = await createInvite("test.js");4 console.log(invite.url);5};6test();7const { updateInvite } = require("qawolf");8const test = async () => {9 console.log(invite.url);10};11test();12const { deleteInvite } = require("qawolf");13const test = async () => {14 await deleteInvite("test.js");15};16test();17const { getInvite } = require("qawolf");18const test = async () => {19 const invite = await getInvite("test.js");20 console.log(invite.url);21};22test();23const { createInvite } = require("qawolf");24const test = async () => {25 const invite = await createInvite("test.js");26 console.log(invite.url);27};28test();29const { updateInvite } = require("qawolf");30const test = async () => {31 console.log(invite.url);32};33test();34const { deleteInvite } = require("qawolf");35const test = async () => {36 await deleteInvite("test.js");37};38test();39const { getInvite } = require("qawolf");40const test = async () => {41 const invite = await getInvite("test.js");42 console.log(invite.url);43};44test();45const { createInvite } = require("qawolf");46const test = async () => {47 const invite = await createInvite("test.js");
Using AI Code Generation
1const { updateInvite } = require("qawolf");2updateInvite("test", "test", "test", "test", "test", "test");3const { test } = require("qawolf");4test("test", "test", "test", "test", "test", "test", "test", "test", "test");5const { test } = require("qawolf");6test("test", "test", "test", "test", "test", "test", "test", "test", "test");7const { test } = require("qawolf");8test("test", "test", "test", "test", "test", "test", "test", "test", "test");9const { test } = require("qawolf");10test("test", "test", "test", "test", "test", "test", "test", "test", "test");11const { test } = require("qawolf");12test("test", "test", "test", "test", "test", "test", "test", "test", "test");13const { test } = require("qawolf");14test("test", "test", "test", "test", "test", "test", "test", "test", "test");15const { test } = require("qawolf");16test("test", "test", "test", "test", "test", "test", "test", "test", "test");17const { test } = require("qawolf");18test("test", "test", "test", "test", "test", "test", "test", "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!!