Best JavaScript code snippet using wpt
misc.test.js
Source:misc.test.js
1'use strict'2const tap = require('tap')3const nock = require('nock')4const sinon = require('sinon')5const { createMockRequest, connectTestDb, disconnectTestDb } = require('../setup')6const { sortById, cleanDbData, compareValidationErrorBodies } = require('../utils')7const { version } = require('../../lib/loadConfigurations')8const { seed: seedLinks, data: mockLinks } = require('./__mocks__/links.mock')9const { seed: seedContacts, data: mockContacts } = require('./__mocks__/authorityContacts.mock')10const service = require('../../modules/misc/misc.service')11tap.test('/misc', async t => {12 await connectTestDb('simile-test-misc')13 await seedLinks()14 await seedContacts()15 nock.disableNetConnect()16 nock.enableNetConnect('127.0.0.1')17 const request = await createMockRequest()18 const baseUrl = `/${version}/misc`19 t.tearDown(async() => {20 await disconnectTestDb()21 nock.cleanAll()22 nock.enableNetConnect()23 })24 t.test('GET - /weather', async t => {25 t.test('returns 422 if query lat is defined and lon is not', async t => {26 const query = { lat: 0.0 }27 const { status, body } = await request28 .get(`${baseUrl}/weather`)29 .query(query)30 const expectedErrors = [31 {32 msg: 'Must have a value',33 param: 'lon',34 location: 'query',35 },36 {37 msg: 'Must be a float',38 param: 'lon',39 location: 'query',40 },41 ]42 t.strictSame(status, 422)43 compareValidationErrorBodies(body, expectedErrors, t)44 t.end()45 })46 t.test('returns 422 if query lon is defined and lat is not', async t => {47 const query = { lon: 0.0 }48 const { status, body } = await request49 .get(`${baseUrl}/weather`)50 .query(query)51 const expectedErrors = [52 {53 msg: 'Must have a value',54 param: 'lat',55 location: 'query',56 },57 {58 msg: 'Must be a float',59 param: 'lat',60 location: 'query',61 },62 ]63 t.strictSame(status, 422)64 compareValidationErrorBodies(body, expectedErrors, t)65 t.end()66 })67 t.test('returns 422 if query lat is wrong format', async t => {68 const query = { lat: 'foo', lon: 0.0 }69 const { status, body } = await request70 .get(`${baseUrl}/weather`)71 .query(query)72 const expectedErrors = [73 {74 value: 'foo',75 msg: 'Must be a float',76 param: 'lat',77 location: 'query',78 },79 ]80 t.strictSame(status, 422)81 compareValidationErrorBodies(body, expectedErrors, t)82 t.end()83 })84 t.test('returns 422 if query lon is wrong format', async t => {85 const query = { lat: 0.0, lon: 'foo' }86 const { status, body } = await request87 .get(`${baseUrl}/weather`)88 .query(query)89 const expectedErrors = [90 {91 value: 'foo',92 msg: 'Must be a float',93 param: 'lon',94 location: 'query',95 },96 ]97 t.strictSame(status, 422)98 compareValidationErrorBodies(body, expectedErrors, t)99 t.end()100 })101 t.test('returns 500 if OWM call fails', async t => {102 const query = { lat: 12.5, lon: 12.5 }103 const owmScope = nock('https://api.openweathermap.org')104 .get('/data/2.5/weather?lat=12.5&lon=12.5&appid=undefined&lang=en&units=metric')105 .replyWithError('Something wrong')106 const { status, body } = await request107 .get(`${baseUrl}/weather`)108 .query(query)109 t.strictSame(status, 500)110 t.strictSame(body, { meta: { code: 500, errorMessage: 'Error calling OpenWeatherMap', errorType: 'ServerException' } })111 t.ok(owmScope.isDone())112 t.end()113 })114 t.test('returns 500 if OWM call is not ok', async t => {115 const query = { lat: 12.5, lon: 12.5 }116 const owmScope = nock('https://api.openweathermap.org')117 .get('/data/2.5/weather?lat=12.5&lon=12.5&appid=undefined&lang=en&units=metric')118 .reply(400, { error: 'error' })119 const { status, body } = await request120 .get(`${baseUrl}/weather`)121 .query(query)122 t.strictSame(status, 500)123 t.strictSame(body, { meta: { code: 500, errorMessage: 'Error calling OpenWeatherMap', errorType: 'ServerException' } })124 t.ok(owmScope.isDone())125 t.end()126 })127 t.test('returns 200', async t => {128 const query = { lat: 12.5, lon: 12.5 }129 const owmData = {130 weather: [{ id: 200 }],131 main: { temp: 10 },132 wind: { speed: 10 },133 }134 const owmScope = nock('https://api.openweathermap.org')135 .get('/data/2.5/weather?lat=12.5&lon=12.5&appid=undefined&lang=en&units=metric')136 .reply(200, owmData)137 const { status, body } = await request138 .get(`${baseUrl}/weather`)139 .query(query)140 t.strictSame(status, 200)141 t.strictSame(body.data, { sky: 4, temperature: 10, wind: 10 })142 t.ok(owmScope.isDone())143 t.end()144 })145 t.end()146 })147 t.test('GET - /links', async t => {148 t.test('returns 500 if db query fails', async t => {149 const serviceStub = sinon.stub(service, 'getAllLinks').throws(new Error('Something wrong'))150 const { status, body } = await request.get(`${baseUrl}/links`)151 t.strictSame(status, 500)152 t.strictSame(body, { meta: { code: 500, errorMessage: 'Something wrong', errorType: 'ServerException' } })153 serviceStub.restore()154 t.end()155 })156 t.test('returns 200', async t => {157 const expectedData = [158 {159 link: mockLinks[1].link,160 name: mockLinks[1].name,161 order: mockLinks[1].order,162 },163 {164 link: mockLinks[0].link,165 name: mockLinks[0].name,166 order: mockLinks[0].order,167 },168 {169 link: mockLinks[2].link,170 name: mockLinks[2].name,171 order: mockLinks[2].order,172 },173 ]174 const { status, body } = await request.get(`${baseUrl}/links`)175 t.strictSame(status, 200)176 t.strictSame(cleanDbData(body.data), expectedData)177 t.end()178 })179 t.end()180 })181 t.test('GET - /authority-contacts', async t => {182 t.test('returns 422 if query area is wrong format', async t => {183 const query = { area: 'foo' }184 const { status, body } = await request185 .get(`${baseUrl}/authority-contacts`)186 .query(query)187 const expectedErrors = [188 {189 value: 'foo',190 msg: 'Must be an integer between 1 and 3',191 param: 'area',192 location: 'query',193 },194 ]195 t.strictSame(status, 422)196 compareValidationErrorBodies(body, expectedErrors, t)197 t.end()198 })199 t.test('returns 422 if query area is forbidden value', async t => {200 const query = { area: 10 }201 const { status, body } = await request202 .get(`${baseUrl}/authority-contacts`)203 .query(query)204 const expectedErrors = [205 {206 value: '10',207 msg: 'Must be an integer between 1 and 3',208 param: 'area',209 location: 'query',210 },211 ]212 t.strictSame(status, 422)213 compareValidationErrorBodies(body, expectedErrors, t)214 t.end()215 })216 t.test('returns 500 if db query fails', async t => {217 const serviceStub = sinon.stub(service, 'getAllContacts').throws(new Error('Something wrong'))218 const { status, body } = await request.get(`${baseUrl}/authority-contacts`)219 t.strictSame(status, 500)220 t.strictSame(body, { meta: { code: 500, errorMessage: 'Something wrong', errorType: 'ServerException' } })221 serviceStub.restore()222 t.end()223 })224 t.test('returns 200 without query', async t => {225 const expectedData = [226 {227 contact: mockContacts[0].contact,228 type: mockContacts[0].type,229 area: mockContacts[0].area,230 instructions: mockContacts[0].instructions,231 },232 {233 contact: mockContacts[1].contact,234 type: mockContacts[1].type,235 area: mockContacts[1].area,236 },237 {238 contact: mockContacts[2].contact,239 type: mockContacts[2].type,240 area: mockContacts[2].area,241 instructions: mockContacts[2].instructions,242 },243 ]244 const { status, body } = await request.get(`${baseUrl}/authority-contacts`)245 t.strictSame(status, 200)246 t.strictSame(cleanDbData(sortById(body.data)), expectedData)247 t.end()248 })249 t.test('returns 200 with query', async t => {250 const expectedData = [251 {252 contact: mockContacts[0].contact,253 type: mockContacts[0].type,254 area: mockContacts[0].area,255 instructions: mockContacts[0].instructions,256 },257 ]258 const { status, body } = await request259 .get(`${baseUrl}/authority-contacts`)260 .query({ area: mockContacts[0].area })261 t.strictSame(status, 200)262 t.strictSame(cleanDbData(sortById(body.data)), expectedData)263 t.end()264 })265 t.end()266 })267 t.end()...
mock.data.ts
Source:mock.data.ts
1// Mock Contact[]2export const mockContacts = [3 {4 id: 1234,5 url: 'http://contact/1',6 title: 'Mr.',7 first_name: 'Bob',8 middle_name: 'Marley',9 last_name: 'Doh',10 addresses: [],11 emails: [{ email: 'test@gmail.com' }],12 phones: [{ number: '356729910101' }],13 uuid: '3782101jdji3038'14 },15 {16 id: 1564,17 url: 'http://contact/145',18 title: 'Mrs.',19 first_name: 'Test',20 middle_name: 'User',21 last_name: 'LastName',22 addresses: [],23 emails: [{ email: 'testuser@gmail.com' }],24 phones: [{ number: '35672349910101' }],25 uuid: '3782101234jdji3038'26 }27];28export const mockMappedContacts = [29 {30 firstLetter: mockContacts[0].last_name.charAt(0).toUpperCase(),31 full_name: `${mockContacts[0].last_name}, ${mockContacts[0].first_name}`,32 firstEmail: mockContacts[0].emails[0].email,33 firstPhone: mockContacts[0].phones[0].number,34 firstAddress: '',35 firstCity: '',36 firstDistrict: '',37 firstPostal: ''38 },39 {40 firstLetter: mockContacts[1].last_name.charAt(0).toUpperCase(),41 full_name: `${mockContacts[1].last_name}, ${mockContacts[1].first_name}`,42 firstEmail: mockContacts[1].emails[0].email,43 firstPhone: mockContacts[1].phones[0].number,44 firstAddress: '',45 firstCity: '',46 firstDistrict: '',47 firstPostal: ''48 }49];50export const mockInstallers = [51 {52 id: 1,53 value: 'https://dev-midgard.walhall.io/api/tolauser/8/',54 label: 'Test User',55 key: 36726,56 uuid: 'abcd12345'57 },58 {59 id: 3,60 value: 'https://dev-midgard.walhall.io/api/tolauser/6/',61 label: 'John Doe',62 key: 367262,63 uuid: 'abcdef12345'64 }65];66// Mock date for CoreUser67export const mockCoreUsers = [68 {69 id: 1234,70 url: 'http://coreuser/124',71 title: 'Mr.',72 name: 'John Doe',73 core_user_uuid: '7389002244'74 },75 {76 id: 12,77 url: 'http://coreuser/12',78 title: 'Mr.',79 name: 'Bob Marley',80 core_user_uuid: '5637890-2'81 }82];83// Mock data for Coregroups84export const mockCoreGroups = [85 {86 name: 'Admin',87 permissions: {88 create: true,89 read: true,90 update: true,91 delete: true92 }93 },94 {95 name: 'ReadOnly',96 permissions: {97 create: false,98 read: false,99 update: false,100 delete: false101 }102 }103];104// Mock data for Documents105export const mockDocuments = [106 {107 id: 4,108 file_type: 'png',109 file_name: 'Test File.png',110 file: null,111 upload_date: new Date(),112 user_uuid: '36789101123',113 workflowlevel2_uuids: ['356891027823439']114 },115 {116 id: 5,117 file_type: 'pdf',118 file_name: 'Test File.pdf',119 file: 'http://file/test1',120 upload_date: new Date(),121 user_uuid: '367891011234',122 workflowlevel2_uuids: ['356891027839']123 }124];125export const documentsMock = [126 {127 'id': 142,128 'uuid': 'f7fece23-30a0-4c42-8daa-98a33b5bfa83',129 'upload_date': '2018-08-01T14:24:16.874316Z',130 'file': 'https://dev-kupfer-documents.k8s.mitgard.io/file/142',131 'file_description': 'test.pdf',132 'file_name': 'speisenplan 1307 18 Sommers (2).pdf',133 'file_type': 'pdf',134 // create_date: '2018-08-01T02:23:34Z',135 'organization_uuid': null,136 'user_uuid': '1be34f80-16a4-48c9-8192-4e9d184f0486',137 'contact_uuid': 'c7772ec3-0da8-4317-8046-dda975d098e5',138 'workflowlevel1_uuids': null,139 'workflowlevel2_uuids': ['63276ee5-93dd-41c1-9278-cd7eefdc28d2'],140 'blobLocalUrl': 'blob:http://localhost:8100/7fb942bc-5d04-4da6-84bd-c11064c3d460'141 },142 {143 'id': 144,144 'uuid': '9afa3435-57a2-4cef-bf8a-bc9ea57b9198',145 'upload_date': '2018-08-03T09:20:40.511372Z',146 'file': 'https://dev-kupfer-documents.k8s.mitgard.io/file/144',147 'file_description': 'ReparaturWrmepumpe_03082018_ScreenSho.png',148 'file_name': 'ReparaturWrmepumpe_03082018_ScreenSho.png',149 'file_type': 'png',150 // create_date: '2018-08-03T09:20:34.007000Z',151 'organization_uuid': null,152 'user_uuid': '1be34f80-16a4-48c9-8192-4e9d184f0486',153 'contact_uuid': '73fb8f84-2357-4a36-83fc-580f8c06d6f1',154 'workflowlevel1_uuids': null,155 'workflowlevel2_uuids': [156 '63276ee5-93dd-41c1-9278-cd7eefdc28d2'157 ],158 'blobLocalUrl': 'blob:http://localhost:8100/e36e5ec4-1018-4c4f-8ef2-e1151c1a1a4b'159 },160 {161 'id': 120,162 'uuid': '9afa3435-57a2-4cef-bf8a-bc9ea57b9198',163 'file': 'https://dev-kupfer-documents.k8s.mitgard.io/file/144',164 'file_description': 'ReparaturWrmepumpe_03082018_ScreenSho.png',165 'file_name': 'ReparaturWrmepumpe_03082018_ScreenSho.png',166 'file_type': 'png',167 'user_uuid': '1be34f80-16a4-48c9-8192-4e9d184f0486',168 'workflowlevel2_uuids': [169 '63276ee5-93dd-41c1-9278-cd7eefdc28d2'170 ],171 }172];173export const documentCreateMock = {174 'upload_date': '2018-08-03T09:20:34.007000Z',175 'file': 'https://dev-kupfer-documents.k8s.mitgard.io/file/144',176 'file_description': 'ReparaturWrmepumpe_03082018_ScreenSho.png',177 'file_name': 'ReparaturWrmepumpe_03082018_ScreenSho.png',178 'create_date': '2018-08-03T09:20:34.007000Z',179 'organization_uuid': 'fasd4214ewrfsd',180 'user_uuid': '1be34f80-16a4-48c9-8192-4e9d184f0486',181 'contact_uuid': '73fb8f84-2357-4a36-83fc-580f8c06d6f1',182 'workflowlevel1_uuids': null,183 'workflowlevel2_uuids': [184 '63276ee5-93dd-41c1-9278-cd7eefdc28d2'185 ]186};187// Mock data for Projects188export const mockProjects = [189 {190 id: 123,191 name: 'Test Project',192 level2_uuid: '356891027839',193 contact: { uuid: 'teyuwioqonsbbdb' }194 },195 {196 id: 124,197 name: 'Test Project 2',198 level2_uuid: '356891027823439',199 contact: { uuid: 'teyuwioqonsbbdb' }200 }...
api-actions.test.ts
Source:api-actions.test.ts
1import {Action} from 'redux';2import thunk, {ThunkDispatch} from 'redux-thunk';3import MockAdapter from 'axios-mock-adapter';4import {configureMockStore} from '@jedmao/redux-mock-store';5import {createAPI} from '../services/api';6import {APIRoute} from '../const';7import {State} from '../types/state';8import {makeFakeContact} from '../utils/mocks';9import {fetchContactsAction, postContactAction} from './api-actions';10import {loadContacts} from './action';11describe('Async actions', () => {12 const fakeRequireAuthorization = jest.fn();13 const api = createAPI(fakeRequireAuthorization());14 const mockAPI = new MockAdapter(api);15 const middlewares = [thunk.withExtraArgument(api)];16 const mockStore = configureMockStore<17 State,18 Action,19 ThunkDispatch<State, typeof api, Action>20 >(middlewares);21 it('should dispatch Load_Contacts when GET /contacts', async () => {22 const mockContacts = [...new Array(10)].map((_, idx) => makeFakeContact(idx + 1));23 mockAPI24 .onGet(APIRoute.Contacts)25 .reply(200, mockContacts);26 const store = mockStore();27 await store.dispatch(fetchContactsAction());28 expect(store.getActions()).toEqual([29 loadContacts(mockContacts),30 ]);31 });32 it('should dispatch Load_Contacts when POST /contacts and GET /contacts', async () => {33 const mockContact = makeFakeContact(1);34 const mockContacts = [...new Array(10)].map((_, idx) => makeFakeContact(idx + 1));35 const onSuccess = jest.fn();36 mockAPI37 .onPost(APIRoute.Contacts)38 .reply(200, mockContacts);39 const store = mockStore();40 await store.dispatch(postContactAction(mockContact, onSuccess));41 mockContacts.push(mockContact);42 expect(store.getActions()).toEqual(mockContacts);43 mockAPI44 .onGet(APIRoute.Contacts)45 .reply(200, mockContacts);46 await store.dispatch(fetchContactsAction());47 expect(store.getActions()).toEqual([48 loadContacts(mockContacts),49 ]);50 });...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2wptoolkit.mockContacts();3var wptoolkit = require('wptoolkit');4wptoolkit.mockContacts();5var wptoolkit = require('wptoolkit');6wptoolkit.mockContacts();7var wptoolkit = require('wptoolkit');8wptoolkit.mockContacts();9var wptoolkit = require('wptoolkit');10wptoolkit.mockContacts();11var wptoolkit = require('wptoolkit');12wptoolkit.mockContacts();13var wptoolkit = require('wptoolkit');14wptoolkit.mockContacts();15var wptoolkit = require('wptoolkit');16wptoolkit.mockContacts();17var wptoolkit = require('wptoolkit');18wptoolkit.mockContacts();19var wptoolkit = require('wptoolkit');20wptoolkit.mockContacts();
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2wptoolkit.mockContacts(10, function(err, data) {3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wptoolkit = require('wptoolkit');10wptoolkit.mockContacts(10, function(err, data) {11 if (err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var wptoolkit = require('wptoolkit');18wptoolkit.mockContacts(10, function(err, data) {19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25var wptoolkit = require('wptoolkit');26wptoolkit.mockContacts(10, function(err, data) {27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33var wptoolkit = require('wptoolkit');34wptoolkit.mockContacts(10, function(err, data) {35 if (err) {36 console.log(err);37 } else {38 console.log(data);39 }40});41var wptoolkit = require('wptoolkit');42wptoolkit.mockContacts(10, function(err, data) {43 if (err) {44 console.log(err);45 } else {46 console.log(data);47 }48});49var wptoolkit = require('wptoolkit');50wptoolkit.mockContacts(10, function(err, data) {51 if (err) {52 console.log(err);53 } else {54 console.log(data);55 }56});
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2wptoolkit.mockContacts(5, function(err, contacts) {3 console.log(contacts);4});5var wptoolkit = require('wptoolkit');6wptoolkit.mockContacts(10, function(err, contacts) {7 console.log(contacts);8});9var wptoolkit = require('wptoolkit');10wptoolkit.mockContacts(15, function(err, contacts) {11 console.log(contacts);12});13var wptoolkit = require('wptoolkit');14wptoolkit.mockContacts(20, function(err, contacts) {15 console.log(contacts);16});17var wptoolkit = require('wptoolkit');18wptoolkit.mockContacts(25, function(err, contacts) {19 console.log(contacts);20});21var wptoolkit = require('wptoolkit');22wptoolkit.mockContacts(30, function(err, contacts) {23 console.log(contacts);24});25var wptoolkit = require('wptoolkit');26wptoolkit.mockContacts(35, function(err, contacts) {27 console.log(contacts);28});29var wptoolkit = require('wptoolkit');30wptoolkit.mockContacts(40, function(err, contacts) {31 console.log(contacts);32});33var wptoolkit = require('wptoolkit');34wptoolkit.mockContacts(45, function(err, contacts) {35 console.log(contacts);36});
Using AI Code Generation
1var wptool = require('wptool');2var contacts = wptool.mockContacts();3console.log(contacts);4var mockContacts = function() {5 return {6 {
Using AI Code Generation
1var wp = require('wptoolkit');2var contacts = wp.contacts;3var mockContacts = contacts.mockContacts;4var c = mockContacts(5);5console.log(c);6var wp = require('wptoolkit');7var contacts = wp.contacts;8var mockContacts = contacts.mockContacts;9var c = mockContacts(5);10console.log(c);
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2wptoolkit.mockContacts(function(contacts) {3 console.log('contacts', contacts);4});5var wptoolkit = require('wptoolkit');6wptoolkit.mockContacts(function(contacts) {7 console.log('contacts', contacts);8});9var wptoolkit = require('wptoolkit');10wptoolkit.mockContacts(function(contacts) {11 console.log('contacts', contacts);12});13var wptoolkit = require('wptoolkit');14wptoolkit.mockContacts(function(contacts) {15 console.log('contacts', contacts);16});17var wptoolkit = require('wptoolkit');18wptoolkit.mockContacts(function(contacts) {19 console.log('contacts', contacts);20});21var wptoolkit = require('wptoolkit');22wptoolkit.mockContacts(function(contacts) {23 console.log('contacts', contacts);24});25var wptoolkit = require('wptoolkit');26wptoolkit.mockContacts(function(contacts) {27 console.log('contacts', contacts);28});29var wptoolkit = require('wptoolkit');30wptoolkit.mockContacts(function(contacts) {31 console.log('contacts', contacts);32});
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!!