Best JavaScript code snippet using pact-foundation-pact
iaptResultsPage.js
Source:iaptResultsPage.js
...25 before('make request', async () => {26 const query = '123456';27 const locals = { lat, lon, query };28 const body = createBody(constants.types.IAPT, locals);29 nockRequests.withResponseBody(path, body, 200, 'search/threeResults.json');30 response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&gpquery=${gpQuery}&gpname=${gpname}&lat=${lat}&lon=${lon}`);31 $ = cheerio.load(response.text);32 iExpect.htmlWithStatus(response, 200);33 });34 it('has a back link to the GP results page', () => {35 iExpect.backLinkContent($, `${constants.siteRoot}${routes.results.path}?type=gp&query=${gpQuery}`);36 });37 it('should have a title of \'Find IAPT services - NHS\'', () => {38 expect($('head title').text()).to.equal(`${constants.app.title} - Services you can refer yourself to - NHS`);39 });40 it('should have an H1 of \'Psychological therapies services\'', () => {41 expect($('h1').text()).to.equal('Contact a psychological therapies service');42 });43 it('should display all of the results that were returned', () => {44 expect($('.results__count').text()).to.equal(resultCount.toString());45 expect($('.results__item').length).to.equal(resultCount);46 });47 it('should report number of services plurally', () => {48 expect($('.nhsuk-body-l').text().trim()).to.equal(`${resultCount} services are available for '${gpname}'.`);49 });50 it('should display contact information for each result', () => {51 $('.results__item').each((i, item) => {52 const email = $(item).find('.results__email');53 expect(email.text()).to.equal(`Email: iapt.email@result.${i}`);54 const emailHref = getHrefFromA(email);55 expect(emailHref).to.equal(`mailto:iapt.email@result.${i}`);56 const tel = $(item).find('.results__telephone');57 expect(tel.text()).to.equal(`Telephone: result ${i} IAPT telephone`);58 const telHref = getHrefFromA(tel);59 expect(telHref).to.equal(`tel:result ${i} IAPT telephone`);60 const orgName = $(item).find('.results__name').text();61 const website = $(item).find('.results__website');62 expect(website.text()).to.equal(`Visit ${orgName}'s website`);63 const websiteHref = getHrefFromA(website);64 expect(websiteHref).to.equal(`https://website.result.${i}`);65 });66 });67 it('should display a button to \'Refer yourself online\' for results with that option', () => {68 const selfReferralElements = $('.results__self__referral');69 expect(selfReferralElements.length).to.equal(2);70 const selfReferralElement0Href = getHrefFromA(selfReferralElements.eq(0));71 const selfReferralElement2Href = getHrefFromA(selfReferralElements.eq(1));72 expect(selfReferralElement0Href).to.equal('https://iapt.self.referral.0');73 expect(selfReferralElement2Href).to.equal('https://iapt.self.referral.2');74 });75 it('should display no message about online referrals not being available when there is no available option', () => {76 const resultItems = $('.results__item');77 expect(resultItems.eq(1).find('p').length).to.equal(3);78 });79 });80 describe('request directly from typeahead', () => {81 const gpName = 'gpName';82 const query = 'zero results';83 const locals = { lat, lon, query };84 before('make request', async () => {85 const body = createBody(constants.types.IAPT, locals);86 nockRequests.withResponseBody(path, body, 200, 'search/zeroResults.json');87 response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&gpquery=${gpName}&gpname=${gpName}&origin=search&lat=${lat}&lon=${lon}`);88 $ = cheerio.load(response.text);89 iExpect.htmlWithStatus(response, 200);90 expect($('.results__item').length).to.equal(0);91 });92 it('has a back link to the search page', () => {93 iExpect.backLinkContent($, `${constants.siteRoot}${routes.search.path}?query=${gpName}`);94 });95 it(`should have the page title - '${constants.app.title} - There are no services available for '${gpName}'- NHS`, () => {96 expect($('head title').text()).to.equal(`${constants.app.title} - There are no services available for '${gpName}' - NHS`);97 });98 });99 describe('zero results', () => {100 const gpName = 'gpName';101 before('make request', async () => {102 const query = 'zero results';103 const locals = { lat, lon, query };104 const body = createBody(constants.types.IAPT, locals);105 nockRequests.withResponseBody(path, body, 200, 'search/zeroResults.json');106 response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&gpname=${gpName}&lat=${lat}&lon=${lon}`);107 $ = cheerio.load(response.text);108 iExpect.htmlWithStatus(response, 200);109 expect($('.results__item').length).to.equal(0);110 });111 it('should display a message for zero results', () => {112 expect($('.nhsuk-body-l').text().trim()).to.equal(`There are no services available for '${gpName}'.`);113 });114 });115 describe('one result', () => {116 const gpName = 'gpName';117 const resultCount = 1;118 before('make request', async () => {119 const query = 'one result';120 const locals = { lat, lon, query };121 const body = createBody(constants.types.IAPT, locals);122 nockRequests.withResponseBody(path, body, 200, 'search/oneResult.json');123 response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&gpname=${gpName}&lat=${lat}&lon=${lon}`);124 $ = cheerio.load(response.text);125 iExpect.htmlWithStatus(response, 200);126 expect($('.results__item').length).to.equal(resultCount);127 });128 it('should report number of services singularly', () => {129 expect($('.nhsuk-body-l').text().trim()).to.equal(`${resultCount} service is available for '${gpName}'.`);130 });131 });132 });133 describe('no results', () => {134 const lat = 50;135 const lon = -1;136 it('should display message when no results returned', async () => {137 const query = 'noresults';138 const locals = { lat, lon, query };139 const body = createBody(constants.types.IAPT, locals);140 nockRequests.withResponseBody(path, body, 200, 'search/zeroResults.json');141 const response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&lat=${lat}&lon=${lon}`);142 iExpect.htmlWithStatus(response, 200);143 const $ = cheerio.load(response.text);144 expect($('.no-results').text()).to.equal('No results');145 });146 });147 describe('bad api responses', () => {148 const lat = 50;149 const lon = -1;150 it('should display an error page for a 400 response', async () => {151 const query = '400response';152 const locals = { lat, lon, query };153 const body = createBody(constants.types.IAPT, locals);154 nockRequests.withResponseBody(path, body, 400, 'search/400.json');155 const response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&lat=${lat}&lon=${lon}`);156 iExpect.errorPageContent(response);157 });158 it('should display an error page for a 403 response', async () => {159 const query = '403response';160 const locals = { lat, lon, query };161 const body = createBody(constants.types.IAPT, locals);162 nockRequests.withNoResponseBody(path, body, 403);163 const response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&lat=${lat}&lon=${lon}`);164 iExpect.errorPageContent(response);165 });166 it('should display an error page for a 404 response', async () => {167 const query = '404response';168 const locals = { lat, lon, query };169 const body = createBody(constants.types.IAPT, locals);170 nockRequests.withResponseBody(path, body, 404, 'search/404.json');171 const response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&lat=${lat}&lon=${lon}`);172 iExpect.errorPageContent(response);173 });174 it('should display an error page for a 415 response', async () => {175 const query = '415response';176 const locals = { lat, lon, query };177 const body = createBody(constants.types.IAPT, locals);178 nockRequests.withResponseBody(path, body, 415, 'search/415.json');179 const response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&lat=${lat}&lon=${lon}`);180 iExpect.errorPageContent(response);181 });182 it('should display an error page when an error is returned from the API', async () => {183 const query = 'error';184 const locals = { lat, lon, query };185 const body = createBody(constants.types.IAPT, locals);186 const error = { message: 'something went wrong' };187 nockRequests.withError(path, body, error);188 const response = await chai.request(server).get(`${constants.siteRoot}${routes.results.path}?type=${type}&ccgid=${query}&lat=${lat}&lon=${lon}`);189 iExpect.errorPageContent(response);190 });191 });192});
api-service.spec.ts
Source:api-service.spec.ts
...95 it('should call the correct base url', done => {96 httpMock.expect(apiBaseUrl + 'conceptschemes/MATERIALEN/c')97 .withMethod('GET')98 .withResponseStatus(200)99 .withResponseBody(true);100 sut.getConcepts('MATERIALEN')101 .then(response => {102 expect(response).toBeTruthy();103 done();104 });105 });106 it('should parse the concepts response correctly', done => {107 httpMock.expect(apiBaseUrl + 'conceptschemes/MATERIALEN/c?label=aard')108 .withMethod('GET')109 .withResponseStatus(200)110 .withResponseBody(testConcepts);111 sut.getConcepts('MATERIALEN', { label: 'aard' })112 .then(response => {113 expect(response as IMember[]).toEqual(testConcepts as IMember[]);114 expect(response as IMember[]).toContain(jasmine.objectContaining({id: 19}));115 done();116 });117 });118 it('should parse the tree response correctly', done => {119 httpMock.expect(apiBaseUrl + 'conceptschemes/MATERIALEN/tree')120 .withMethod('GET')121 .withResponseStatus(200)122 .withResponseBody(testTree);123 sut.getTree('MATERIALEN')124 .then(response => {125 expect(response as ITree).toEqual(testTree as ITree);126 expect(response as ITree).toContain(jasmine.objectContaining({ concept_id: 1 }));127 done();128 });129 });130 it('should parse the conceptById response correctly', done => {131 httpMock.expect(apiBaseUrl + 'conceptschemes/MATERIALEN/c/19')132 .withMethod('GET')133 .withResponseStatus(200)134 .withResponseBody(testConceptById);135 sut.getConceptById('MATERIALEN', 19)136 .then(response => {137 if (response) {138 expect(response as IConcept).toEqual(testConceptById as IConcept);139 expect(response.id).toBe(19);140 }141 done();142 });143 });...
swagger2-response-builder.ts
Source:swagger2-response-builder.ts
...22 const copyOfHeaders: Swagger2ResponseHeadersBuilder = {...this.state.headers};23 copyOfHeaders[name] = definition;24 return new Swagger2ResponseBuilder({...this.state, headers: copyOfHeaders});25 }26 public withResponseBody(responseBody: Swagger2Schema): Swagger2ResponseBuilder {27 const copyOfResponseBody = _.cloneDeep(responseBody);28 return new Swagger2ResponseBuilder({...this.state, schema: copyOfResponseBody});29 }30 public withSchemaRef($ref: string): Swagger2ResponseBuilder {31 return this.withResponseBody({$ref});32 }33 public build(): Swagger2Response {34 const response: Swagger2Response = {35 description: this.state.description36 };37 setPropertyIfDefined(response, 'schema', this.state.schema);38 if (this.state.headers) {39 response.headers = buildMapFromBuilders(this.state.headers);40 }41 return response;42 }43}...
Using AI Code Generation
1const { Matchers } = require('@pact-foundation/pact');2const { somethingLike, like, term } = Matchers;3const { like: like2 } = require('@pact-foundation/pact/dsl/matchers');4const { like: like3 } = require('@pact-foundation/pact/dsl/matchers');5const { Matchers: Matchers2 } = require('@pact-foundation/pact/dsl/matchers');6const { somethingLike: somethingLike2 } = require('@pact-foundation/pact/dsl/matchers');7const { like: like4 } = require('@pact-foundation/pact/dsl/matchers');8const { somethingLike: somethingLike3 } = require('@pact-foundation/pact/dsl/matchers');9const { like: like5 } = require('@pact-foundation/pact/dsl/matchers');10const { somethingLike: somethingLike4 } = require('@pact-foundation/pact/dsl/matchers');11const { like: like6 } = require('@pact-foundation/pact/dsl/matchers');12const { somethingLike: somethingLike5 } = require('@pact-foundation/pact/dsl/matchers');13const { like: like7 } = require('@pact-foundation/pact/dsl/matchers');14const { somethingLike: somethingLike6 } = require('@pact-foundation/pact/dsl/matchers');15const { like: like8 } = require('@pact-foundation/pact/dsl/matchers');16const { somethingLike: somethingLike7 } = require('@pact-foundation/pact/dsl/matchers');17const { like: like9 } = require('@pact-foundation/pact/dsl/matchers');18const { somethingLike: somethingLike8 } = require('@pact-foundation/pact/dsl/matchers');19const { like: like10 } = require('@pact-foundation/pact/dsl/matchers');20const { somethingLike: somethingLike9 } = require('@pact-foundation/pact/dsl/matchers');21const { like: like11 } = require('@pact-foundation/pact/dsl/matchers');22const { somethingLike: somethingLike10 } = require('@pact-foundation/pact/dsl/matchers');23const { like: like12 } = require('@pact-f
Using AI Code Generation
1const { Matchers } = require('@pact-foundation/pact');2const { somethingLike } = Matchers;3const getResponse = {4 body: {5 "id": somethingLike(1),6 "name": somethingLike("test"),7 "description": somethingLike("test"),8 "price": somethingLike(1.0),9 "quantity": somethingLike(1),10 "isAvailable": somethingLike(true)11 }12};13module.exports = {14};15const { Matchers } = require('@pact-foundation/pact');16const { somethingLike } = Matchers;17const getResponse = {18 body: {19 "id": somethingLike(1),20 "name": somethingLike("test"),21 "description": somethingLike("test"),22 "price": somethingLike(1.0),23 "quantity": somethingLike(1),24 "isAvailable": somethingLike(true)25 }26};27module.exports = {28};29const { Matchers } = require('@pact-foundation/pact');30const { somethingLike } = Matchers;31const getResponse = {32 body: {33 "id": somethingLike(1),34 "name": somethingLike("test"),35 "description": somethingLike("test"),36 "price": somethingLike(1.0),37 "quantity": somethingLike(1),38 "isAvailable": somethingLike(true)39 }40};41module.exports = {42};43const { Matchers } = require('@pact-foundation/pact');44const { somethingLike } = Matchers;45const getResponse = {46 body: {47 "id": somethingLike(1),48 "name": somethingLike("test"),49 "description": somethingLike("test"),50 "price": somethingLike(1.0),51 "quantity": somethingLike(1),52 "isAvailable": somethingLike(true)53 }54};55module.exports = {
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!!