Best JavaScript code snippet using argos
sql-user-service.test.ts
Source:sql-user-service.test.ts
...45 statusCalculator,46 new NullMessageBus(),47 );48 // ACT49 const me = await service.getOrCreateUser({50 user: {51 name: 'me',52 email: 'me@example.com',53 provider: 'test',54 providerUserId: '2893674528967345',55 },56 });57 const alsoMe = await service.getOrCreateUser({58 user: {59 name: 'me',60 email: 'me@example.com',61 provider: 'test',62 providerUserId: '2893674528967345',63 },64 });65 const them = await service.getOrCreateUser({66 user: {67 name: 'them',68 email: 'them@example.com',69 provider: 'test',70 providerUserId: 'kjlafsdhkljadlhjfkflhjak',71 },72 });73 // ASSERT74 expect(me).toEqual(alsoMe);75 expect(me).not.toEqual(them);76 });77 });78 describe('getUsers', () => {79 it('gets list of users by id', async () => {80 // ARRANGE81 const service = new SqlUserService(82 pool,83 schema,84 statusCalculator,85 new NullMessageBus(),86 );87 const me = await service.getOrCreateUser({88 user: {89 name: 'me',90 email: 'me@example.com',91 provider: 'test',92 providerUserId: '2893674528967345',93 },94 });95 const them = await service.getOrCreateUser({96 user: {97 name: 'them',98 email: 'them@example.com',99 provider: 'test',100 providerUserId: 'kjlafsdhkljadlhjfkflhjak',101 },102 });103 expect(me).toBeTruthy();104 expect(them).toBeTruthy();105 // ACT106 const users = await service.getUsers({ userIds: [me.id, them.id] });107 // ASSERT108 expect(users.items.sort((a, b) => a.id.localeCompare(b.id))).toEqual<109 User[]110 >([me, them].sort((a, b) => a.id.localeCompare(b.id)));111 });112 });113 describe('updateUser', () => {114 it('updates the user', async () => {115 // ARRANGE116 const { id } = await new SqlUserService(117 pool,118 schema,119 statusCalculator,120 new NullMessageBus(),121 ).getOrCreateUser({122 user: {123 name: 'me',124 email: 'me@example.com',125 provider: 'test',126 providerUserId: '2893674528967345',127 },128 });129 const service = new SqlUserService(130 pool,131 schema,132 statusCalculator,133 new NullMessageBus(),134 id,135 );136 const me = await service.updateUser({137 userId: id,138 userUpdate: { status: 'away' },139 });140 // ACT141 const updatedMe = await service.updateUser({142 userId: me.id,143 userUpdate: { status: 'flow' },144 });145 // ASSERT146 expect(me).not.toEqual(updatedMe);147 });148 });149 describe('addFriendToUser', () => {150 it('works', async () => {151 // ARRANGE152 const me = await new SqlUserService(153 pool,154 schema,155 statusCalculator,156 new NullMessageBus(),157 ).getOrCreateUser({158 user: {159 name: 'me',160 email: 'me@example.com',161 provider: 'test',162 providerUserId: '2893674528967345',163 },164 });165 const them = await new SqlUserService(166 pool,167 schema,168 statusCalculator,169 new NullMessageBus(),170 ).getOrCreateUser({171 user: {172 name: 'them',173 email: 'them@example.com',174 provider: 'test',175 providerUserId: 'kjlafsdhkljadlhjfkflhjak',176 },177 });178 const service = new SqlUserService(179 pool,180 schema,181 statusCalculator,182 new NullMessageBus(),183 me.id,184 );185 // ACT186 const result = await service.addFriendToUser({187 userId: me.id,188 friendId: them.id,189 });190 // ASSERT191 expect(result).toEqual(them);192 });193 describe('pagination', () => {194 let users: User[];195 let me: User;196 beforeEach(async () => {197 me = await new SqlUserService(198 pool,199 schema,200 statusCalculator,201 new NullMessageBus(),202 ).getOrCreateUser({203 user: {204 name: 'me',205 email: 'me@example.com',206 provider: 'test',207 providerUserId: '2893674528967345',208 },209 });210 const svc = new SqlUserService(211 pool,212 schema,213 statusCalculator,214 new NullMessageBus(),215 );216 users = [217 await svc.getOrCreateUser({218 user: {219 name: 'a',220 email: 'a@example.com',221 provider: 'test',222 providerUserId: 't4d38ny778do3wbt',223 },224 }),225 await svc.getOrCreateUser({226 user: {227 name: 'b',228 email: 'b@example.com',229 provider: 'test',230 providerUserId: 'n78got4dt5w8no7g',231 },232 }),233 await svc.getOrCreateUser({234 user: {235 name: 'c',236 email: 'c@example.com',237 provider: 'test',238 providerUserId: 'f4de8goqf4degi8o',239 },240 }),241 await svc.getOrCreateUser({242 user: {243 name: 'd',244 email: 'd@example.com',245 provider: 'test',246 providerUserId: '8odtnoq78dgt34',247 },248 }),249 await svc.getOrCreateUser({250 user: {251 name: 'e',252 email: 'e@example.com',253 provider: 'test',254 providerUserId: 'o89gm73x8oq3gn',255 },256 }),257 ];258 await Promise.all(259 users.map((user) =>260 new SqlUserService(261 pool,262 schema,263 statusCalculator,264 new NullMessageBus(),265 me.id,266 ).addFriendToUser({267 userId: me.id,268 friendId: user.id,269 }),270 ),271 );272 });273 it('works without count or cursors', async () => {274 // ARRANGE275 const service = new SqlUserService(276 pool,277 schema,278 statusCalculator,279 new NullMessageBus(),280 me.id,281 );282 // ACT283 const result = await service.getFriendsByUserId({ userId: me.id });284 // ASSERT285 expect(result).toEqual<UserPage>({286 hasPreviousPage: false,287 startCursor: users[0].id,288 items: users,289 endCursor: users[4].id,290 hasNextPage: false,291 });292 });293 it('takes from start without cursor', async () => {294 // ARRANGE295 const service = new SqlUserService(296 pool,297 schema,298 statusCalculator,299 new NullMessageBus(),300 me.id,301 );302 const [a, b, c] = users;303 // ACT304 const result = await service.getFriendsByUserId({305 userId: me.id,306 first: 3,307 });308 // ASSERT309 expect(result).toEqual<UserPage>({310 hasPreviousPage: false,311 startCursor: a.id,312 items: [a, b, c],313 endCursor: c.id,314 hasNextPage: true,315 });316 });317 it('takes after cursor with some remaining', async () => {318 // ARRANGE319 const service = new SqlUserService(320 pool,321 schema,322 statusCalculator,323 new NullMessageBus(),324 me.id,325 );326 const [a, b, c, d, e] = users;327 // ACT328 const result = await service.getFriendsByUserId({329 userId: me.id,330 after: b.id,331 first: 2,332 });333 // ASSERT334 expect(result).toEqual<UserPage>({335 hasPreviousPage: true,336 startCursor: c.id,337 items: [c, d],338 endCursor: d.id,339 hasNextPage: true,340 });341 });342 it('takes after cursor without any remaining', async () => {343 // ARRANGE344 const service = new SqlUserService(345 pool,346 schema,347 statusCalculator,348 new NullMessageBus(),349 me.id,350 );351 const [a, b, c, d, e] = users;352 // ACT353 const result = await service.getFriendsByUserId({354 userId: me.id,355 after: b.id,356 first: 25,357 });358 // ASSERT359 expect(result).toEqual<UserPage>({360 hasPreviousPage: true,361 startCursor: c.id,362 items: [c, d, e],363 endCursor: e.id,364 hasNextPage: false,365 });366 });367 });368 });369 describe('getFriendsByUserId', () => {370 it('works', async () => {371 // ARRANGE372 const me = await new SqlUserService(373 pool,374 schema,375 statusCalculator,376 new NullMessageBus(),377 ).getOrCreateUser({378 user: {379 name: 'me',380 email: 'me@example.com',381 provider: 'test',382 providerUserId: '2893674528967345',383 },384 });385 const them = await new SqlUserService(386 pool,387 schema,388 statusCalculator,389 new NullMessageBus(),390 ).getOrCreateUser({391 user: {392 name: 'them',393 email: 'them@example.com',394 provider: 'test',395 providerUserId: 'kjlafsdhkljadlhjfkflhjak',396 },397 });398 const other = await new SqlUserService(399 pool,400 schema,401 statusCalculator,402 new NullMessageBus(),403 ).getOrCreateUser({404 user: {405 name: 'other',406 email: 'other@example.com',407 provider: 'test',408 providerUserId: 'akfjl973t4ny7t4donygtd',409 },410 });411 const service = new SqlUserService(412 pool,413 schema,414 statusCalculator,415 new NullMessageBus(),416 me.id,417 );418 await service.addFriendToUser({419 userId: me.id,420 friendId: them.id,421 });422 // ACT423 const mine = await new SqlUserService(424 pool,425 schema,426 statusCalculator,427 new NullMessageBus(),428 me.id,429 ).getFriendsByUserId({430 userId: me.id,431 });432 const theirs = await new SqlUserService(433 pool,434 schema,435 statusCalculator,436 new NullMessageBus(),437 them.id,438 ).getFriendsByUserId({439 userId: them.id,440 });441 const others = await new SqlUserService(442 pool,443 schema,444 statusCalculator,445 new NullMessageBus(),446 other.id,447 ).getFriendsByUserId({448 userId: other.id,449 });450 // ASSERT451 expect(mine.items).toEqual([them]);452 expect(theirs.items).toEqual([me]);453 expect(others.items).toEqual([]);454 });455 });456 describe('removeFriendFromUser', () => {457 it('works', async () => {458 // ARRANGE459 const me = await new SqlUserService(460 pool,461 schema,462 statusCalculator,463 new NullMessageBus(),464 ).getOrCreateUser({465 user: {466 name: 'me',467 email: 'me@example.com',468 provider: 'test',469 providerUserId: '2893674528967345',470 },471 });472 const them = await new SqlUserService(473 pool,474 schema,475 statusCalculator,476 new NullMessageBus(),477 ).getOrCreateUser({478 user: {479 name: 'them',480 email: 'them@example.com',481 provider: 'test',482 providerUserId: 'kjlafsdhkljadlhjfkflhjak',483 },484 });485 const service = new SqlUserService(486 pool,487 schema,488 statusCalculator,489 new NullMessageBus(),490 me.id,491 );...
getOrCreateUserTest.js
Source:getOrCreateUserTest.js
1import tester from 'lambda-tester';2import {expect} from 'chai';3import UserModel from '../libs/UserModel';4import {getOrCreateUser} from '../functions/getOrCreateUser';5async function expectQueryCount(fbId, count) {6 const res = await UserModel7 .query(fbId)8 .usingIndex('FbIdIndex')9 .execAsync();10 expect(res.Count).to.equal(count);11}12async function deleteUser(userId) {13 return await UserModel.destroyAsync(userId);14}15describe('getOrCreateUser', () => {16 const fbId = '1234567890abcd';17 const payload = {18 fbId,19 name: 'Full Name Here',20 email: 'test@email.com'21 };22 it('should error when userId is not provided', () => {23 return tester(getOrCreateUser)24 .event({})25 .expectError(err => {26 expect(err.message).to.equal('Missing fbId parameter');27 });28 });29 it('should return error when name not provided', () => {30 return tester(getOrCreateUser)31 .event({fbId: '1'})32 .expectError(err => {33 expect(err.message).to.equal('Missing name parameter');34 });35 });36 it('should return error when email not provided', () => {37 return tester(getOrCreateUser)38 .event({fbId: '1', name: 'ao'})39 .expectError(err => {40 expect(err.message).to.equal('Missing email parameter');41 });42 });43 it('should create user if user does not exists', async() => {44 let userId = null;45 try {46 await expectQueryCount(payload.fbId, 0);47 await tester(getOrCreateUser)48 .event(payload)49 .expectResult(res => {50 expect(res.fbId).to.equal(payload.fbId);51 expect(res.name).to.equal(payload.name);52 expect(res.email).to.equal(payload.email);53 expect(res.userId).to.be.ok;54 userId = res.userId;55 });56 await expectQueryCount(payload.fbId, 1);57 } finally {58 await deleteUser(userId);59 }60 });61 it('should retrieve existing user when user already exist', async() => {62 let userId = null;63 try {64 const newUser = {65 fbId,66 name: 'SecondUser',67 email: 'second@user.com'68 };69 await tester(getOrCreateUser)70 .event(payload)71 .expectResult();72 await tester(getOrCreateUser)73 .event(newUser)74 .expectResult(res => {75 expect(res.fbId).to.equal(payload.fbId);76 expect(res.name).to.equal(payload.name);77 expect(res.email).to.equal(payload.email);78 expect(res.userId).to.be.ok;79 userId = res.userId;80 });81 } finally {82 await deleteUser(userId);83 }84 });85 it('should error when trying to save with extra parameters provided', async () => {86 const payloadWithExtras = Object.assign({}, payload, {fbId: 'extras', a: 'a', b: 'b', c: 'c'});87 await tester(getOrCreateUser)88 .event(payloadWithExtras)89 .expectError(err => {90 expect(err.cause.name).to.equal('ValidationError');91 });92 })...
getOrCreateUser.ts
Source:getOrCreateUser.ts
1import { CreateUserRequest } from "./models/createUserRequest"2import { User } from "./models/user";3import {userRepo} from './userRepo';4import { v4 as uuidv4 } from 'uuid';5class GetOrCreateUser {6 public async run(request: CreateUserRequest): Promise<User> {7 console.log('Get or create User', request);8 const existingUser = await userRepo.getByEmail(request.email);9 if (existingUser) {10 return existingUser;11 }12 13 return userRepo.create({14 id: uuidv4(),15 displayName: request.displayName,16 givenName: request.givenName,17 lastName: request.lastName,18 email: request.email,19 profileImage: request.profileImage,20 totalGames: 0,21 average: 0,22 totalThrownDarts: 0,23 totalWon: 0 24 });25 }26}...
Using AI Code Generation
1const argosyServiceUsers = require('argosy-service-users');2const argosy = require('argosy');3const seneca = require('seneca')();4const service = argosy();5const users = argosyServiceUsers();6service.pipe(users).pipe(service);7service.accept({ role: 'users', cmd: 'getOrCreateUser', email: '
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!!